Add patch for positioning
Browse files- index.html +13 -0
- src/popover.ts +31 -4
index.html
CHANGED
|
@@ -166,6 +166,7 @@
|
|
| 166 |
<p>given below are the examples of simple `highlight`</p>
|
| 167 |
<div class="buttons">
|
| 168 |
<button id="highlight-btn">Animated Highlight</button>
|
|
|
|
| 169 |
<button id="simple-highlight-btn">Simple Highlight</button>
|
| 170 |
<button id="transition-highlight-btn">Transition Highlight</button>
|
| 171 |
<button id="disallow-close">Disallow Close</button>
|
|
@@ -910,6 +911,18 @@ npm install driver.js</pre
|
|
| 910 |
}, 2000);
|
| 911 |
});
|
| 912 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 913 |
document.getElementById("highlight-btn").addEventListener("click", () => {
|
| 914 |
driver({
|
| 915 |
animate: true,
|
|
|
|
| 166 |
<p>given below are the examples of simple `highlight`</p>
|
| 167 |
<div class="buttons">
|
| 168 |
<button id="highlight-btn">Animated Highlight</button>
|
| 169 |
+
<button id="buggy-highlight-btn">Buggy Highlight</button>
|
| 170 |
<button id="simple-highlight-btn">Simple Highlight</button>
|
| 171 |
<button id="transition-highlight-btn">Transition Highlight</button>
|
| 172 |
<button id="disallow-close">Disallow Close</button>
|
|
|
|
| 911 |
}, 2000);
|
| 912 |
});
|
| 913 |
|
| 914 |
+
document.getElementById("buggy-highlight-btn").addEventListener("click", () => {
|
| 915 |
+
driver().highlight({
|
| 916 |
+
element: ".page-header h1 sup",
|
| 917 |
+
popover: {
|
| 918 |
+
title: "Buggy Highlight",
|
| 919 |
+
description: "Unlike the older version, new version doesn't work with z-indexes so no more positional issues.",
|
| 920 |
+
side: "bottom",
|
| 921 |
+
align: "start",
|
| 922 |
+
},
|
| 923 |
+
});
|
| 924 |
+
});
|
| 925 |
+
|
| 926 |
document.getElementById("highlight-btn").addEventListener("click", () => {
|
| 927 |
driver({
|
| 928 |
animate: true,
|
src/popover.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
import { bringInView, getFocusableElements } from "./utils";
|
| 2 |
import { Config, DriverHook, getConfig, getCurrentDriver } from "./config";
|
| 3 |
-
import { getState, setState, State } from "./state";
|
| 4 |
import { Driver, DriveStep } from "./driver";
|
| 5 |
-
import { onDriverClick } from "./events";
|
| 6 |
import { emit } from "./emitter";
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
export type Side = "top" | "right" | "bottom" | "left" | "over";
|
| 9 |
export type Alignment = "start" | "center" | "end";
|
|
@@ -585,7 +585,6 @@ function renderPopoverArrow(alignment: Alignment, side: Side, element: Element)
|
|
| 585 |
arrowSide = "right";
|
| 586 |
arrowAlignment = "end";
|
| 587 |
}
|
| 588 |
-
} else {
|
| 589 |
}
|
| 590 |
|
| 591 |
if (!arrowSide) {
|
|
@@ -593,6 +592,34 @@ function renderPopoverArrow(alignment: Alignment, side: Side, element: Element)
|
|
| 593 |
} else {
|
| 594 |
popoverArrow.classList.add(`driver-popover-arrow-side-${arrowSide}`);
|
| 595 |
popoverArrow.classList.add(`driver-popover-arrow-align-${arrowAlignment}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 596 |
}
|
| 597 |
}
|
| 598 |
|
|
|
|
|
|
|
| 1 |
import { Config, DriverHook, getConfig, getCurrentDriver } from "./config";
|
|
|
|
| 2 |
import { Driver, DriveStep } from "./driver";
|
|
|
|
| 3 |
import { emit } from "./emitter";
|
| 4 |
+
import { onDriverClick } from "./events";
|
| 5 |
+
import { getState, setState, State } from "./state";
|
| 6 |
+
import { bringInView, getFocusableElements } from "./utils";
|
| 7 |
|
| 8 |
export type Side = "top" | "right" | "bottom" | "left" | "over";
|
| 9 |
export type Alignment = "start" | "center" | "end";
|
|
|
|
| 585 |
arrowSide = "right";
|
| 586 |
arrowAlignment = "end";
|
| 587 |
}
|
|
|
|
| 588 |
}
|
| 589 |
|
| 590 |
if (!arrowSide) {
|
|
|
|
| 592 |
} else {
|
| 593 |
popoverArrow.classList.add(`driver-popover-arrow-side-${arrowSide}`);
|
| 594 |
popoverArrow.classList.add(`driver-popover-arrow-align-${arrowAlignment}`);
|
| 595 |
+
|
| 596 |
+
const elementRect = element.getBoundingClientRect();
|
| 597 |
+
const arrowRect = popoverArrow.getBoundingClientRect();
|
| 598 |
+
const stagePadding = getConfig("stagePadding") || 0;
|
| 599 |
+
|
| 600 |
+
const isElementPartiallyInViewPort =
|
| 601 |
+
elementRect.left - stagePadding < window.innerWidth &&
|
| 602 |
+
elementRect.right + stagePadding > 0 &&
|
| 603 |
+
elementRect.top - stagePadding < window.innerHeight &&
|
| 604 |
+
elementRect.bottom + stagePadding > 0;
|
| 605 |
+
|
| 606 |
+
if (side === "bottom" && isElementPartiallyInViewPort) {
|
| 607 |
+
const isArrowWithinElementBounds =
|
| 608 |
+
arrowRect.x > elementRect.x && arrowRect.x + arrowRect.width < elementRect.x + elementRect.width;
|
| 609 |
+
|
| 610 |
+
if (!isArrowWithinElementBounds) {
|
| 611 |
+
popoverArrow.classList.remove(`driver-popover-arrow-align-${arrowAlignment}`);
|
| 612 |
+
popoverArrow.classList.add(`driver-popover-arrow-none`);
|
| 613 |
+
// reduce the top position by the padding
|
| 614 |
+
popover.wrapper.style.transform = `translateY(-${stagePadding / 2}px)`;
|
| 615 |
+
} else {
|
| 616 |
+
popover.wrapper.style.transform = `translateY(0)`;
|
| 617 |
+
}
|
| 618 |
+
|
| 619 |
+
// TODO: implement this using either of the following:
|
| 620 |
+
// 1 - move the arrow to the center of the element and point it towards the popover. This way, scrolling or resizing the window will move the arrow to the correct position.
|
| 621 |
+
// 2 - calculate the center position of the element and point the arrow towards the popover. However, we will have to re-calculate the position of the arrow when the window is resized or scrolled.
|
| 622 |
+
}
|
| 623 |
}
|
| 624 |
}
|
| 625 |
|