File size: 6,250 Bytes
aa3b624 e459aae 333d0df 0f198fd 82a88c5 9bde4cc aa3b624 edd7dca aa3b624 3c1c1f4 aa3b624 836d49f aa3b624 edd7dca aa3b624 70d8f15 aa3b624 0b494ac 9a5b24f aa3b624 e459aae 9a5b24f aa3b624 70d8f15 aa3b624 66a740c 0b494ac 836d49f 9db3a38 70d8f15 ef09b80 836d49f 34b7e3b a0a08e9 9bcba14 d656141 e5cb552 eb375ea e5cb552 333d0df e5cb552 d656141 ef09b80 eb375ea e5cb552 333d0df e5cb552 ef09b80 34b7e3b 3b225cb 9db3a38 0b494ac aa3b624 e5cb552 9bde4cc 4c98241 9bde4cc aa3b624 3b225cb 70d8f15 3b225cb aa3b624 66a740c 70d8f15 aa3b624 70d8f15 8480986 eb375ea e5cb552 333d0df e5cb552 ef09b80 e5cb552 0b494ac aa3b624 e5cb552 c2d9950 aa3b624 70d8f15 836d49f 70d8f15 9db3a38 82a88c5 4ccc492 6bf3ab4 e43c081 4ccc492 70d8f15 6bf3ab4 aa3b624 edd7dca aa3b624 4ccc492 6bf3ab4 aa3b624 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
import { DriveStep } from "./driver";
import { refreshOverlay, trackActiveElement, transitionStage } from "./overlay";
import { getConfig, getCurrentDriver } from "./config";
import { hidePopover, renderPopover, repositionPopover } from "./popover";
import { bringInView } from "./utils";
import { getState, setState } from "./state";
function mountDummyElement(): Element {
const existingDummy = document.getElementById("driver-dummy-element");
if (existingDummy) {
return existingDummy;
}
let element = document.createElement("div");
element.id = "driver-dummy-element";
element.style.width = "0";
element.style.height = "0";
element.style.pointerEvents = "none";
element.style.opacity = "0";
element.style.position = "fixed";
element.style.top = "50%";
element.style.left = "50%";
document.body.appendChild(element);
return element;
}
export function highlight(step: DriveStep) {
const { element } = step;
let elemObj: Element | null = null;
if (typeof element === "string") {
elemObj = document.querySelector(element);
if (! elemObj || elemObj.getBoundingClientRect().width === 0) {
elemObj = null;
document.querySelectorAll(element).forEach(function (el) {
var rect = el.getBoundingClientRect();
if (!elemObj && rect.width > 0 && rect.height > 0) {
elemObj = el;
return;
}
});
}
} else if (element instanceof Element) {
elemObj = element;
}
// If the element is not found, we mount a 1px div
// at the center of the screen to highlight and show
// the popover on top of that. This is to show a
// modal-like highlight.
if (!elemObj) {
elemObj = mountDummyElement();
}
transferHighlight(elemObj, step);
}
export function refreshActiveHighlight() {
const activeHighlight = getState("__activeElement");
const activeStep = getState("__activeStep")!;
if (!activeHighlight) {
return;
}
trackActiveElement(activeHighlight);
refreshOverlay();
repositionPopover(activeHighlight, activeStep);
}
function transferHighlight(toElement: Element, toStep: DriveStep) {
const duration = 400;
const start = Date.now();
const fromStep = getState("__activeStep");
const fromElement = getState("__activeElement") || toElement;
// If it's the first time we're highlighting an element, we show
// the popover immediately. Otherwise, we wait for the animation
// to finish before showing the popover.
const isFirstHighlight = !fromElement || fromElement === toElement;
const isToDummyElement = toElement.id === "driver-dummy-element";
const isFromDummyElement = fromElement.id === "driver-dummy-element";
const isAnimatedTour = getConfig("animate");
const highlightStartedHook = toStep.onHighlightStarted || getConfig("onHighlightStarted");
const highlightedHook = toStep?.onHighlighted || getConfig("onHighlighted");
const deselectedHook = fromStep?.onDeselected || getConfig("onDeselected");
const config = getConfig();
const state = getState();
if (!isFirstHighlight && deselectedHook) {
deselectedHook(isFromDummyElement ? undefined : fromElement, fromStep!, {
config,
state,
driver: getCurrentDriver(),
});
}
if (highlightStartedHook) {
highlightStartedHook(isToDummyElement ? undefined : toElement, toStep, {
config,
state,
driver: getCurrentDriver(),
});
}
const hasDelayedPopover = !isFirstHighlight && isAnimatedTour;
let isPopoverRendered = false;
hidePopover();
setState("previousStep", fromStep);
setState("previousElement", fromElement);
setState("activeStep", toStep);
setState("activeElement", toElement);
const animate = () => {
const transitionCallback = getState("__transitionCallback");
// This makes sure that the repeated calls to transferHighlight
// don't interfere with each other. Only the last call will be
// executed.
if (transitionCallback !== animate) {
return;
}
const elapsed = Date.now() - start;
const timeRemaining = duration - elapsed;
const isHalfwayThrough = timeRemaining <= duration / 2;
if (toStep.popover && isHalfwayThrough && !isPopoverRendered && hasDelayedPopover) {
renderPopover(toElement, toStep);
isPopoverRendered = true;
}
if (getConfig("animate") && elapsed < duration) {
transitionStage(elapsed, duration, fromElement, toElement);
} else {
trackActiveElement(toElement);
if (highlightedHook) {
highlightedHook(isToDummyElement ? undefined : toElement, toStep, {
config: getConfig(),
state: getState(),
driver: getCurrentDriver(),
});
}
setState("__transitionCallback", undefined);
setState("__previousStep", fromStep);
setState("__previousElement", fromElement);
setState("__activeStep", toStep);
setState("__activeElement", toElement);
}
window.requestAnimationFrame(animate);
};
setState("__transitionCallback", animate);
window.requestAnimationFrame(animate);
bringInView(toElement);
if (!hasDelayedPopover && toStep.popover) {
renderPopover(toElement, toStep);
}
fromElement.classList.remove("driver-active-element", "driver-no-interaction");
fromElement.removeAttribute("aria-haspopup");
fromElement.removeAttribute("aria-expanded");
fromElement.removeAttribute("aria-controls");
const disableActiveInteraction = toStep.disableActiveInteraction ?? getConfig("disableActiveInteraction");
if (disableActiveInteraction) {
toElement.classList.add("driver-no-interaction");
}
toElement.classList.add("driver-active-element");
toElement.setAttribute("aria-haspopup", "dialog");
toElement.setAttribute("aria-expanded", "true");
toElement.setAttribute("aria-controls", "driver-popover-content");
}
export function destroyHighlight() {
document.getElementById("driver-dummy-element")?.remove();
document.querySelectorAll(".driver-active-element").forEach(element => {
element.classList.remove("driver-active-element", "driver-no-interaction");
element.removeAttribute("aria-haspopup");
element.removeAttribute("aria-expanded");
element.removeAttribute("aria-controls");
});
}
|