Skip to content

Commit 04bbfaa

Browse files
committed
Add patch for positioning
1 parent 2dda34a commit 04bbfaa

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

index.html

+13
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ <h2>Highlight Feature</h2>
166166
<p>given below are the examples of simple `highlight`</p>
167167
<div class="buttons">
168168
<button id="highlight-btn">Animated Highlight</button>
169+
<button id="buggy-highlight-btn">Buggy Highlight</button>
169170
<button id="simple-highlight-btn">Simple Highlight</button>
170171
<button id="transition-highlight-btn">Transition Highlight</button>
171172
<button id="disallow-close">Disallow Close</button>
@@ -910,6 +911,18 @@ <h2>Usage and Demo</h2>
910911
}, 2000);
911912
});
912913

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+
913926
document.getElementById("highlight-btn").addEventListener("click", () => {
914927
driver({
915928
animate: true,

src/popover.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { bringInView, getFocusableElements } from "./utils";
21
import { Config, DriverHook, getConfig, getCurrentDriver } from "./config";
3-
import { getState, setState, State } from "./state";
42
import { Driver, DriveStep } from "./driver";
5-
import { onDriverClick } from "./events";
63
import { emit } from "./emitter";
4+
import { onDriverClick } from "./events";
5+
import { getState, setState, State } from "./state";
6+
import { bringInView, getFocusableElements } from "./utils";
77

88
export type Side = "top" | "right" | "bottom" | "left" | "over";
99
export type Alignment = "start" | "center" | "end";
@@ -585,14 +585,41 @@ function renderPopoverArrow(alignment: Alignment, side: Side, element: Element)
585585
arrowSide = "right";
586586
arrowAlignment = "end";
587587
}
588-
} else {
589588
}
590589

591590
if (!arrowSide) {
592591
popoverArrow.classList.add("driver-popover-arrow-none");
593592
} else {
594593
popoverArrow.classList.add(`driver-popover-arrow-side-${arrowSide}`);
595594
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+
}
596623
}
597624
}
598625

0 commit comments

Comments
 (0)