File size: 22,833 Bytes
333d0df d0e0b03 04bbfaa b8874fd 9bde4cc 9d9dd89 9a49612 9d9dd89 8480986 6aa2828 9d9dd89 d6df79d fe7906f 03f5109 6aa2828 d6df79d 1d5e713 d0e0b03 03f5109 d6df79d d0e0b03 28ba7df 333d0df 28ba7df d0e0b03 3223525 8480986 9bde4cc 9d9dd89 d6b7277 9d9dd89 03f5109 0f198fd 9d9dd89 b8874fd 9db3a38 9bde4cc 9db3a38 515af9d 9bde4cc 6aa2828 b8874fd 6aa2828 d6df79d 6aa2828 03f5109 6aa2828 93af367 03f5109 d6df79d 03f5109 515af9d c887729 515af9d 6aa2828 03f5109 0f198fd 86da344 03f5109 86da344 03f5109 fe7906f 86da344 03f5109 0f198fd 03f5109 6aa2828 03f5109 0f198fd 03f5109 6aa2828 03f5109 0f198fd 03f5109 6aa2828 f86a242 9d9dd89 f86a242 9d9dd89 b8874fd 6bf3ab4 f86a242 1d5e713 d0e0b03 7660c1f 333d0df 7660c1f d0e0b03 7660c1f 333d0df 7660c1f d0e0b03 7660c1f 333d0df 7660c1f d0e0b03 4b04f2c d0e0b03 0f198fd 333d0df 0f198fd d0e0b03 9bde4cc 42aa4be 425e02a 333d0df 425e02a 28ba7df 42aa4be 0f198fd 2d82059 0f198fd b8874fd fd57e7f edd7dca fd57e7f 9bde4cc f86a242 9d9dd89 f86a242 9bde4cc edd7dca 9bde4cc f86a242 9bde4cc 9d9dd89 f86a242 8480986 35ff9ac 3d651f8 fd57e7f 3d651f8 baff93a fd57e7f baff93a 9a5b24f 9bde4cc 68ab4bd 9a5b24f 4da4a2a 9a5b24f 515af9d 68ab4bd fd57e7f 68ab4bd f86a242 35ff9ac 68ab4bd f86a242 35ff9ac 68ab4bd f86a242 35ff9ac 68ab4bd f86a242 35ff9ac 8e19ba1 fd57e7f f86a242 35ff9ac 9bde4cc f86a242 8e19ba1 f86a242 3d651f8 35ff9ac 3d651f8 4da4a2a 3d651f8 35ff9ac 3d651f8 4da4a2a 353f356 3d651f8 baff93a f86a242 4da4a2a 353f356 baff93a 4da4a2a f86a242 353f356 4da4a2a 7fbff02 353f356 4da4a2a 9bde4cc 4da4a2a fd57e7f 4da4a2a fd57e7f 4da4a2a fd57e7f 4da4a2a 04bbfaa 4da4a2a 68ab4bd 9d9dd89 b8874fd d6b7277 b8874fd 6bf3ab4 9d9dd89 515af9d 9d9dd89 b8874fd 9d9dd89 6bf3ab4 9d9dd89 515af9d f86a242 b8874fd 9d9dd89 6bf3ab4 9d9dd89 6bf3ab4 8d0f3ea b8874fd 6bf3ab4 03f5109 9d9dd89 647be2c b8874fd 9d9dd89 6bf3ab4 9d9dd89 b8874fd 9d9dd89 6bf3ab4 9d9dd89 b8874fd 9d9dd89 03f5109 9d9dd89 b8874fd 8d0f3ea d6b7277 9d9dd89 b8874fd 9d9dd89 d6b7277 9d9dd89 03f5109 b8874fd 9bde4cc 9d9dd89 b8874fd 9d9dd89 b8874fd |
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
import { Config, DriverHook, getConfig, getCurrentDriver } from "./config";
import { Driver, DriveStep } from "./driver";
import { emit } from "./emitter";
import { onDriverClick } from "./events";
import { getState, setState, State } from "./state";
import { bringInView, getFocusableElements } from "./utils";
export type Side = "top" | "right" | "bottom" | "left" | "over";
export type Alignment = "start" | "center" | "end";
export type AllowedButtons = "next" | "previous" | "close";
export type Popover = {
title?: string;
description?: string;
side?: Side;
align?: Alignment;
showButtons?: AllowedButtons[];
showProgress?: boolean;
disableButtons?: AllowedButtons[];
popoverClass?: string;
// Button texts
progressText?: string;
doneBtnText?: string;
nextBtnText?: string;
prevBtnText?: string;
// Called after the popover is rendered
onPopoverRender?: (popover: PopoverDOM, opts: { config: Config; state: State; driver: Driver }) => void;
// Button callbacks
onNextClick?: DriverHook;
onPrevClick?: DriverHook;
onCloseClick?: DriverHook;
};
export type PopoverDOM = {
wrapper: HTMLElement;
arrow: HTMLElement;
title: HTMLElement;
description: HTMLElement;
footer: HTMLElement;
progress: HTMLElement;
previousButton: HTMLButtonElement;
nextButton: HTMLButtonElement;
closeButton: HTMLButtonElement;
footerButtons: HTMLElement;
};
export function hidePopover() {
const popover = getState("popover");
if (!popover) {
return;
}
popover.wrapper.style.display = "none";
}
export function renderPopover(element: Element, step: DriveStep) {
let popover = getState("popover");
if (popover) {
document.body.removeChild(popover.wrapper);
}
popover = createPopover();
document.body.appendChild(popover.wrapper);
const {
title,
description,
showButtons,
disableButtons,
showProgress,
nextBtnText = getConfig("nextBtnText") || "Next →",
prevBtnText = getConfig("prevBtnText") || "← Previous",
progressText = getConfig("progressText") || "{current} of {total}",
} = step.popover || {};
popover.nextButton.innerHTML = nextBtnText;
popover.previousButton.innerHTML = prevBtnText;
popover.progress.innerHTML = progressText;
if (title) {
popover.title.innerHTML = title;
popover.title.style.display = "block";
} else {
popover.title.style.display = "none";
}
if (description) {
popover.description.innerHTML = description;
popover.description.style.display = "block";
} else {
popover.description.style.display = "none";
}
const showButtonsConfig: AllowedButtons[] = showButtons || getConfig("showButtons")!;
const showProgressConfig = showProgress || getConfig("showProgress") || false;
const showFooter =
showButtonsConfig?.includes("next") || showButtonsConfig?.includes("previous") || showProgressConfig;
popover.closeButton.style.display = showButtonsConfig.includes("close") ? "block" : "none";
if (showFooter) {
popover.footer.style.display = "flex";
popover.progress.style.display = showProgressConfig ? "block" : "none";
popover.nextButton.style.display = showButtonsConfig.includes("next") ? "block" : "none";
popover.previousButton.style.display = showButtonsConfig.includes("previous") ? "block" : "none";
} else {
popover.footer.style.display = "none";
}
const disabledButtonsConfig: AllowedButtons[] = disableButtons || getConfig("disableButtons")! || [];
if (disabledButtonsConfig?.includes("next")) {
popover.nextButton.disabled = true;
popover.nextButton.classList.add("driver-popover-btn-disabled");
}
if (disabledButtonsConfig?.includes("previous")) {
popover.previousButton.disabled = true;
popover.previousButton.classList.add("driver-popover-btn-disabled");
}
if (disabledButtonsConfig?.includes("close")) {
popover.closeButton.disabled = true;
popover.closeButton.classList.add("driver-popover-btn-disabled");
}
// Reset the popover position
const popoverWrapper = popover.wrapper;
popoverWrapper.style.display = "block";
popoverWrapper.style.left = "";
popoverWrapper.style.top = "";
popoverWrapper.style.bottom = "";
popoverWrapper.style.right = "";
popoverWrapper.id = "driver-popover-content";
popoverWrapper.setAttribute("role", "dialog");
popoverWrapper.setAttribute("aria-labelledby", "driver-popover-title");
popoverWrapper.setAttribute("aria-describedby", "driver-popover-description");
// Reset the classes responsible for the arrow position
const popoverArrow = popover.arrow;
popoverArrow.className = "driver-popover-arrow";
// Reset any custom classes on the popover
const customPopoverClass = step.popover?.popoverClass || getConfig("popoverClass") || "";
popoverWrapper.className = `driver-popover ${customPopoverClass}`.trim();
// Handles the popover button clicks
onDriverClick(
popover.wrapper,
e => {
const target = e.target as HTMLElement;
const onNextClick = step.popover?.onNextClick || getConfig("onNextClick");
const onPrevClick = step.popover?.onPrevClick || getConfig("onPrevClick");
const onCloseClick = step.popover?.onCloseClick || getConfig("onCloseClick");
if (target.classList.contains("driver-popover-next-btn")) {
// If the user has provided a custom callback, call it
// otherwise, emit the event.
if (onNextClick) {
return onNextClick(element, step, {
config: getConfig(),
state: getState(),
driver: getCurrentDriver(),
});
} else {
return emit("nextClick");
}
}
if (target.classList.contains("driver-popover-prev-btn")) {
if (onPrevClick) {
return onPrevClick(element, step, {
config: getConfig(),
state: getState(),
driver: getCurrentDriver(),
});
} else {
return emit("prevClick");
}
}
if (target.classList.contains("driver-popover-close-btn")) {
if (onCloseClick) {
return onCloseClick(element, step, {
config: getConfig(),
state: getState(),
driver: getCurrentDriver(),
});
} else {
return emit("closeClick");
}
}
return undefined;
},
target => {
// Only prevent the default action if we're clicking on a driver button
// This allows us to have links inside the popover title and description
return (
!popover?.description.contains(target) &&
!popover?.title.contains(target) &&
typeof target.className === "string" &&
target.className.includes("driver-popover")
);
}
);
setState("popover", popover);
const onPopoverRender = step.popover?.onPopoverRender || getConfig("onPopoverRender");
if (onPopoverRender) {
onPopoverRender(popover, {
config: getConfig(),
state: getState(),
driver: getCurrentDriver(),
});
}
repositionPopover(element, step);
bringInView(popoverWrapper);
// Focus on the first focusable element in active element or popover
const isToDummyElement = element.classList.contains("driver-dummy-element");
const focusableElement = getFocusableElements([popoverWrapper, ...(isToDummyElement ? [] : [element])]);
if (focusableElement.length > 0) {
focusableElement[0].focus();
}
}
type PopoverDimensions = {
width: number;
height: number;
realWidth: number;
realHeight: number;
};
function getPopoverDimensions(): PopoverDimensions | undefined {
const popover = getState("popover");
if (!popover?.wrapper) {
return;
}
const boundingClientRect = popover.wrapper.getBoundingClientRect();
const stagePadding = getConfig("stagePadding") || 0;
const popoverOffset = getConfig("popoverOffset") || 0;
return {
width: boundingClientRect.width + stagePadding + popoverOffset,
height: boundingClientRect.height + stagePadding + popoverOffset,
realWidth: boundingClientRect.width,
realHeight: boundingClientRect.height,
};
}
function calculateTopForLeftRight(
alignment: Alignment,
config: {
elementDimensions: DOMRect;
popoverDimensions: PopoverDimensions;
popoverPadding: number;
popoverArrowDimensions: { width: number; height: number };
}
): number {
const { elementDimensions, popoverDimensions, popoverPadding, popoverArrowDimensions } = config;
if (alignment === "start") {
return Math.max(
Math.min(
elementDimensions.top - popoverPadding,
window.innerHeight - popoverDimensions!.realHeight - popoverArrowDimensions.width
),
popoverArrowDimensions.width
);
}
if (alignment === "end") {
return Math.max(
Math.min(
elementDimensions.top - popoverDimensions?.realHeight + elementDimensions.height + popoverPadding,
window.innerHeight - popoverDimensions?.realHeight - popoverArrowDimensions.width
),
popoverArrowDimensions.width
);
}
if (alignment === "center") {
return Math.max(
Math.min(
elementDimensions.top + elementDimensions.height / 2 - popoverDimensions?.realHeight / 2,
window.innerHeight - popoverDimensions?.realHeight - popoverArrowDimensions.width
),
popoverArrowDimensions.width
);
}
return 0;
}
// Calculate the left placement for top and bottom sides
function calculateLeftForTopBottom(
alignment: Alignment,
config: {
elementDimensions: DOMRect;
popoverDimensions: PopoverDimensions;
popoverPadding: number;
popoverArrowDimensions: { width: number; height: number };
}
): number {
const { elementDimensions, popoverDimensions, popoverPadding, popoverArrowDimensions } = config;
if (alignment === "start") {
return Math.max(
Math.min(
elementDimensions.left - popoverPadding,
window.innerWidth - popoverDimensions!.realWidth - popoverArrowDimensions.width
),
popoverArrowDimensions.width
);
}
if (alignment === "end") {
return Math.max(
Math.min(
elementDimensions.left - popoverDimensions?.realWidth + elementDimensions.width + popoverPadding,
window.innerWidth - popoverDimensions?.realWidth - popoverArrowDimensions.width
),
popoverArrowDimensions.width
);
}
if (alignment === "center") {
return Math.max(
Math.min(
elementDimensions.left + elementDimensions.width / 2 - popoverDimensions?.realWidth / 2,
window.innerWidth - popoverDimensions?.realWidth - popoverArrowDimensions.width
),
popoverArrowDimensions.width
);
}
return 0;
}
export function repositionPopover(element: Element, step: DriveStep) {
const popover = getState("popover");
if (!popover) {
return;
}
const { align = "start", side = "left" } = step?.popover || {};
// Configure the popover positioning
const requiredAlignment: Alignment = align;
const requiredSide: Side = element.id === "driver-dummy-element" ? "over" : side;
const popoverPadding = getConfig("stagePadding") || 0;
const popoverDimensions = getPopoverDimensions()!;
const popoverArrowDimensions = popover.arrow.getBoundingClientRect();
const elementDimensions = element.getBoundingClientRect();
const topValue = elementDimensions.top - popoverDimensions!.height;
let isTopOptimal = topValue >= 0;
const bottomValue = window.innerHeight - (elementDimensions.bottom + popoverDimensions!.height);
let isBottomOptimal = bottomValue >= 0;
const leftValue = elementDimensions.left - popoverDimensions!.width;
let isLeftOptimal = leftValue >= 0;
const rightValue = window.innerWidth - (elementDimensions.right + popoverDimensions!.width);
let isRightOptimal = rightValue >= 0;
const noneOptimal = !isTopOptimal && !isBottomOptimal && !isLeftOptimal && !isRightOptimal;
let popoverRenderedSide: Side = requiredSide;
if (requiredSide === "top" && isTopOptimal) {
isRightOptimal = isLeftOptimal = isBottomOptimal = false;
} else if (requiredSide === "bottom" && isBottomOptimal) {
isRightOptimal = isLeftOptimal = isTopOptimal = false;
} else if (requiredSide === "left" && isLeftOptimal) {
isRightOptimal = isTopOptimal = isBottomOptimal = false;
} else if (requiredSide === "right" && isRightOptimal) {
isLeftOptimal = isTopOptimal = isBottomOptimal = false;
}
if (requiredSide === "over") {
const leftToSet = window.innerWidth / 2 - popoverDimensions!.realWidth / 2;
const topToSet = window.innerHeight / 2 - popoverDimensions!.realHeight / 2;
popover.wrapper.style.left = `${leftToSet}px`;
popover.wrapper.style.right = `auto`;
popover.wrapper.style.top = `${topToSet}px`;
popover.wrapper.style.bottom = `auto`;
} else if (noneOptimal) {
const leftValue = window.innerWidth / 2 - popoverDimensions?.realWidth! / 2;
const bottomValue = 10;
popover.wrapper.style.left = `${leftValue}px`;
popover.wrapper.style.right = `auto`;
popover.wrapper.style.bottom = `${bottomValue}px`;
popover.wrapper.style.top = `auto`;
} else if (isLeftOptimal) {
const leftToSet = Math.min(
leftValue,
window.innerWidth - popoverDimensions?.realWidth - popoverArrowDimensions.width
);
const topToSet = calculateTopForLeftRight(requiredAlignment, {
elementDimensions,
popoverDimensions,
popoverPadding,
popoverArrowDimensions,
});
popover.wrapper.style.left = `${leftToSet}px`;
popover.wrapper.style.top = `${topToSet}px`;
popover.wrapper.style.bottom = `auto`;
popover.wrapper.style.right = "auto";
popoverRenderedSide = "left";
} else if (isRightOptimal) {
const rightToSet = Math.min(
rightValue,
window.innerWidth - popoverDimensions?.realWidth - popoverArrowDimensions.width
);
const topToSet = calculateTopForLeftRight(requiredAlignment, {
elementDimensions,
popoverDimensions,
popoverPadding,
popoverArrowDimensions,
});
popover.wrapper.style.right = `${rightToSet}px`;
popover.wrapper.style.top = `${topToSet}px`;
popover.wrapper.style.bottom = `auto`;
popover.wrapper.style.left = "auto";
popoverRenderedSide = "right";
} else if (isTopOptimal) {
const topToSet = Math.min(
topValue,
window.innerHeight - popoverDimensions!.realHeight - popoverArrowDimensions.width
);
let leftToSet = calculateLeftForTopBottom(requiredAlignment, {
elementDimensions,
popoverDimensions,
popoverPadding,
popoverArrowDimensions,
});
popover.wrapper.style.top = `${topToSet}px`;
popover.wrapper.style.left = `${leftToSet}px`;
popover.wrapper.style.bottom = `auto`;
popover.wrapper.style.right = "auto";
popoverRenderedSide = "top";
} else if (isBottomOptimal) {
const bottomToSet = Math.min(
bottomValue,
window.innerHeight - popoverDimensions?.realHeight - popoverArrowDimensions.width
);
let leftToSet = calculateLeftForTopBottom(requiredAlignment, {
elementDimensions,
popoverDimensions,
popoverPadding,
popoverArrowDimensions,
});
popover.wrapper.style.left = `${leftToSet}px`;
popover.wrapper.style.bottom = `${bottomToSet}px`;
popover.wrapper.style.top = `auto`;
popover.wrapper.style.right = "auto";
popoverRenderedSide = "bottom";
}
// Popover stays on the screen if the element scrolls out of the visible area.
// Render the arrow again to make sure it's in the correct position
// e.g. if element scrolled out of the screen to the top, the arrow should be rendered
// pointing to the top. If the element scrolled out of the screen to the bottom,
// the arrow should be rendered pointing to the bottom.
if (!noneOptimal) {
renderPopoverArrow(requiredAlignment, popoverRenderedSide, element);
} else {
popover.arrow.classList.add("driver-popover-arrow-none");
}
}
function renderPopoverArrow(alignment: Alignment, side: Side, element: Element) {
const popover = getState("popover");
if (!popover) {
return;
}
const elementDimensions = element.getBoundingClientRect();
const popoverDimensions = getPopoverDimensions()!;
const popoverArrow = popover.arrow;
const popoverWidth = popoverDimensions.width;
const windowWidth = window.innerWidth;
const elementWidth = elementDimensions.width;
const elementLeft = elementDimensions.left;
const popoverHeight = popoverDimensions.height;
const windowHeight = window.innerHeight;
const elementTop = elementDimensions.top;
const elementHeight = elementDimensions.height;
// Remove all arrow classes
popoverArrow.className = "driver-popover-arrow";
let arrowSide = side;
let arrowAlignment = alignment;
if (side === "top") {
if (elementLeft + elementWidth <= 0) {
arrowSide = "right";
arrowAlignment = "end";
} else if (elementLeft + elementWidth - popoverWidth <= 0) {
arrowSide = "top";
arrowAlignment = "start";
}
if (elementLeft >= windowWidth) {
arrowSide = "left";
arrowAlignment = "end";
} else if (elementLeft + popoverWidth >= windowWidth) {
arrowSide = "top";
arrowAlignment = "end";
}
} else if (side === "bottom") {
if (elementLeft + elementWidth <= 0) {
arrowSide = "right";
arrowAlignment = "start";
} else if (elementLeft + elementWidth - popoverWidth <= 0) {
arrowSide = "bottom";
arrowAlignment = "start";
}
if (elementLeft >= windowWidth) {
arrowSide = "left";
arrowAlignment = "start";
} else if (elementLeft + popoverWidth >= windowWidth) {
arrowSide = "bottom";
arrowAlignment = "end";
}
} else if (side === "left") {
if (elementTop + elementHeight <= 0) {
arrowSide = "bottom";
arrowAlignment = "end";
} else if (elementTop + elementHeight - popoverHeight <= 0) {
arrowSide = "left";
arrowAlignment = "start";
}
if (elementTop >= windowHeight) {
arrowSide = "top";
arrowAlignment = "end";
} else if (elementTop + popoverHeight >= windowHeight) {
arrowSide = "left";
arrowAlignment = "end";
}
} else if (side === "right") {
if (elementTop + elementHeight <= 0) {
arrowSide = "bottom";
arrowAlignment = "start";
} else if (elementTop + elementHeight - popoverHeight <= 0) {
arrowSide = "right";
arrowAlignment = "start";
}
if (elementTop >= windowHeight) {
arrowSide = "top";
arrowAlignment = "start";
} else if (elementTop + popoverHeight >= windowHeight) {
arrowSide = "right";
arrowAlignment = "end";
}
}
if (!arrowSide) {
popoverArrow.classList.add("driver-popover-arrow-none");
} else {
popoverArrow.classList.add(`driver-popover-arrow-side-${arrowSide}`);
popoverArrow.classList.add(`driver-popover-arrow-align-${arrowAlignment}`);
const elementRect = element.getBoundingClientRect();
const arrowRect = popoverArrow.getBoundingClientRect();
const stagePadding = getConfig("stagePadding") || 0;
const isElementPartiallyInViewPort =
elementRect.left - stagePadding < window.innerWidth &&
elementRect.right + stagePadding > 0 &&
elementRect.top - stagePadding < window.innerHeight &&
elementRect.bottom + stagePadding > 0;
if (side === "bottom" && isElementPartiallyInViewPort) {
const isArrowWithinElementBounds =
arrowRect.x > elementRect.x && arrowRect.x + arrowRect.width < elementRect.x + elementRect.width;
if (!isArrowWithinElementBounds) {
popoverArrow.classList.remove(`driver-popover-arrow-align-${arrowAlignment}`);
popoverArrow.classList.add(`driver-popover-arrow-none`);
// reduce the top position by the padding
popover.wrapper.style.transform = `translateY(-${stagePadding / 2}px)`;
} else {
popover.wrapper.style.transform = `translateY(0)`;
}
// TODO: implement this using either of the following:
// 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.
// 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.
}
}
}
function createPopover(): PopoverDOM {
const wrapper = document.createElement("div");
wrapper.classList.add("driver-popover");
const arrow = document.createElement("div");
arrow.classList.add("driver-popover-arrow");
const title = document.createElement("header");
title.id = "driver-popover-title";
title.classList.add("driver-popover-title");
title.style.display = "none";
title.innerText = "Popover Title";
const description = document.createElement("div");
description.id = "driver-popover-description";
description.classList.add("driver-popover-description");
description.style.display = "none";
description.innerText = "Popover description is here";
const closeButton = document.createElement("button");
closeButton.type = "button";
closeButton.classList.add("driver-popover-close-btn");
closeButton.setAttribute("aria-label", "Close");
closeButton.innerHTML = "×";
const footer = document.createElement("footer");
footer.classList.add("driver-popover-footer");
const progress = document.createElement("span");
progress.classList.add("driver-popover-progress-text");
progress.innerText = "";
const footerButtons = document.createElement("span");
footerButtons.classList.add("driver-popover-navigation-btns");
const previousButton = document.createElement("button");
previousButton.type = "button";
previousButton.classList.add("driver-popover-prev-btn");
previousButton.innerHTML = "← Previous";
const nextButton = document.createElement("button");
nextButton.type = "button";
nextButton.classList.add("driver-popover-next-btn");
nextButton.innerHTML = "Next →";
footerButtons.appendChild(previousButton);
footerButtons.appendChild(nextButton);
footer.appendChild(progress);
footer.appendChild(footerButtons);
wrapper.appendChild(closeButton);
wrapper.appendChild(arrow);
wrapper.appendChild(title);
wrapper.appendChild(description);
wrapper.appendChild(footer);
return {
wrapper,
arrow,
title,
description,
footer,
previousButton,
nextButton,
closeButton,
footerButtons,
progress,
};
}
export function destroyPopover() {
const popover = getState("popover");
if (!popover) {
return;
}
popover.wrapper.parentElement?.removeChild(popover.wrapper);
}
|