diff --git a/shepherd.js/src/components/shepherd-modal.ts b/shepherd.js/src/components/shepherd-modal.ts
index 3e9b0f47b..7dd75eb5b 100644
--- a/shepherd.js/src/components/shepherd-modal.ts
+++ b/shepherd.js/src/components/shepherd-modal.ts
@@ -1,4 +1,5 @@
import { makeOverlayPath } from '../utils/overlay-path.ts';
+import { autoUpdate } from '../utils/position.ts';
import type { Step } from '../step.ts';
import './shepherd-modal.css';
@@ -78,6 +79,7 @@ const PATH_NUMBER = /-?[\d.]+(e-\d+)?/g;
export function createShepherdModal(container: HTMLElement): ShepherdModalAPI {
let rafId: number | undefined;
+ let stopTracking: (() => void)[] = [];
let currentPath = '';
// Memoizes the chain of scroll parents of a highlighted element for the
// lifetime of a single step. Reset in `_cleanupStepEventListeners`.
@@ -122,7 +124,7 @@ export function createShepherdModal(container: HTMLElement): ShepherdModalAPI {
: to;
// Writing `d` invalidates layout even when the value is unchanged, and this
- // runs every frame.
+ // runs every frame of a glide, a scroll or an animation.
if (d !== currentPath) {
currentPath = d;
pathEl.setAttribute('d', d);
@@ -300,6 +302,8 @@ export function createShepherdModal(container: HTMLElement): ShepherdModalAPI {
if (rafId) {
cancelAnimationFrame(rafId);
}
+ stopTracking.forEach((stop) => stop());
+ stopTracking = [];
// Scroll parents are only memoized for the duration of a single step.
_stepScrollParents = new WeakMap();
@@ -319,7 +323,7 @@ export function createShepherdModal(container: HTMLElement): ShepherdModalAPI {
const iframeOffset = _getIframeOffset(step.target);
const targetScrollParent = _cachedScrollParents(step.target)[0] ?? null;
- const rafLoop = () => {
+ const update = () =>
positionModal(
modalOverlayOpeningPadding,
modalOverlayOpeningRadius,
@@ -329,10 +333,32 @@ export function createShepherdModal(container: HTMLElement): ShepherdModalAPI {
step.target,
step._resolvedExtraHighlightElements
);
- rafId = requestAnimationFrame(rafLoop);
+ // A glide tweens the path in script, so it needs every frame until it lands.
+ const glide = () => {
+ update();
+ rafId = glideEnd > Date.now() ? requestAnimationFrame(glide) : undefined;
};
- rafLoop();
+ glide();
+
+ // Otherwise the opening only changes when a highlight moves or resizes, or a
+ // scroll parent does, which crops it without moving it. `autoUpdate` reports
+ // those without polling, so an open tour on a still page does no work.
+ // With nothing to highlight, the overlay itself is tracked, since its path
+ // still has to follow the viewport.
+ const highlights = [
+ ...new Set([step.target, ...(step._resolvedExtraHighlightElements ?? [])])
+ ].filter((el): el is HTMLElement => el?.nodeType === Node.ELEMENT_NODE);
+ const tracked = highlights.length
+ ? [
+ ...new Set([
+ ...highlights,
+ ...highlights.flatMap((el) => _cachedScrollParents(el))
+ ])
+ ]
+ : [element];
+ stopTracking = tracked.map((el) => autoUpdate(el, element, update));
+
touchStep = step;
window.addEventListener('touchmove', _preventModalBodyTouch, {
passive: false
@@ -445,10 +471,11 @@ export function createShepherdModal(container: HTMLElement): ShepherdModalAPI {
*
* Resolving inline would be prohibitively expensive: the containment check in
* `positionModal` is O(n^2) over the highlighted elements and runs on every
- * animation frame, while each walk costs one `window.getComputedStyle` call
- * per ancestor. The answer cannot change between frames of a step, so it is
- * resolved once per element instead — matching the once-per-step contract the
- * target already has in `_styleForStep`.
+ * frame of a glide or a scroll, while each walk costs one
+ * `window.getComputedStyle` call per ancestor. The answer cannot change
+ * between frames of a step, so it is resolved once per element instead —
+ * matching the once-per-step contract the target already has in
+ * `_styleForStep`.
*/
function _cachedScrollParents(el?: HTMLElement | null): HTMLElement[] {
// `nodeType`, not `instanceof`, so a target in a same-origin iframe counts.
diff --git a/shepherd.js/src/utils/position.ts b/shepherd.js/src/utils/position.ts
index 7ee4a3d74..ed0666bad 100644
--- a/shepherd.js/src/utils/position.ts
+++ b/shepherd.js/src/utils/position.ts
@@ -826,11 +826,34 @@ function observeMove(element: Element, onMove: () => void) {
const onResize = () => refresh(true);
const win = getWindow(element);
win.addEventListener('resize', onResize);
+ // The observer watches the box the element had when it was made, so an element
+ // that shrinks could then move within that old box without being noticed.
+ // `autoUpdate` reports the resize itself.
+ const resizes =
+ typeof ResizeObserver === 'function'
+ ? new ResizeObserver(onResize)
+ : undefined;
+ resizes?.observe(element, { box: 'border-box' });
+ // An inline element has no box for a ResizeObserver, so new text or attributes
+ // are the only sign that it changed size, and nothing else reports it.
+ const mutations =
+ typeof MutationObserver === 'function' &&
+ styleOf(element).display === 'inline'
+ ? new MutationObserver(() => refresh())
+ : undefined;
+ mutations?.observe(element, {
+ attributes: true,
+ characterData: true,
+ childList: true,
+ subtree: true
+ });
refresh(true);
return () => {
disposed = true;
win.removeEventListener('resize', onResize);
+ resizes?.disconnect();
+ mutations?.disconnect();
stop();
};
}
@@ -847,7 +870,7 @@ function observeMove(element: Element, onMove: () => void) {
*/
export function autoUpdate(
reference: Element,
- floating: HTMLElement,
+ floating: Element,
update: () => void
) {
const win = getWindow(floating);
@@ -1003,8 +1026,10 @@ export function autoUpdate(
typeof IntersectionObserver === 'function'
? chain.map((element) => observeMove(element, check))
: [];
+ // The border box, which is what `check` compares, so a border or padding change
+ // on a fixed-size element counts.
[reference, floating, ...frames].forEach((element) =>
- resizes?.observe(element)
+ resizes?.observe(element, { box: 'border-box' })
);
const stop = () => {
diff --git a/shepherd.js/test/cypress/examples/overlay-tracking.html b/shepherd.js/test/cypress/examples/overlay-tracking.html
new file mode 100644
index 000000000..f0b8bf192
--- /dev/null
+++ b/shepherd.js/test/cypress/examples/overlay-tracking.html
@@ -0,0 +1,63 @@
+
+
+