chunk.EAHZ6VJU.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {
  2. getTabbableElements
  3. } from "./chunk.DVN52LS5.js";
  4. // src/internal/support.ts
  5. function isPreventScrollSupported() {
  6. let supported = false;
  7. document.createElement("div").focus({
  8. get preventScroll() {
  9. supported = true;
  10. return false;
  11. }
  12. });
  13. return supported;
  14. }
  15. // src/internal/modal.ts
  16. var activeModals = [];
  17. var Modal = class {
  18. constructor(element) {
  19. this.tabDirection = "forward";
  20. this.element = element;
  21. this.handleFocusIn = this.handleFocusIn.bind(this);
  22. this.handleKeyDown = this.handleKeyDown.bind(this);
  23. }
  24. activate() {
  25. activeModals.push(this.element);
  26. document.addEventListener("focusin", this.handleFocusIn);
  27. document.addEventListener("keydown", this.handleKeyDown);
  28. }
  29. deactivate() {
  30. activeModals = activeModals.filter((modal) => modal !== this.element);
  31. document.removeEventListener("focusin", this.handleFocusIn);
  32. document.removeEventListener("keydown", this.handleKeyDown);
  33. }
  34. isActive() {
  35. return activeModals[activeModals.length - 1] === this.element;
  36. }
  37. handleFocusIn(event) {
  38. const path = event.composedPath();
  39. if (this.isActive() && !path.includes(this.element)) {
  40. const tabbableElements = getTabbableElements(this.element);
  41. const index = this.tabDirection === "backward" ? tabbableElements.length - 1 : 0;
  42. tabbableElements[index].focus({ preventScroll: true });
  43. }
  44. }
  45. handleKeyDown(event) {
  46. if (event.key === "Tab" && event.shiftKey) {
  47. this.tabDirection = "backward";
  48. setTimeout(() => this.tabDirection = "forward");
  49. }
  50. }
  51. };
  52. var modal_default = Modal;
  53. export {
  54. isPreventScrollSupported,
  55. modal_default
  56. };