diff options
Diffstat (limited to 'alpinejs/packages/trap/dist/cdn.js')
-rw-r--r-- | alpinejs/packages/trap/dist/cdn.js | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/alpinejs/packages/trap/dist/cdn.js b/alpinejs/packages/trap/dist/cdn.js index b104cf6..8d2b40b 100644 --- a/alpinejs/packages/trap/dist/cdn.js +++ b/alpinejs/packages/trap/dist/cdn.js @@ -620,28 +620,78 @@ // packages/trap/src/index.js function src_default(Alpine) { - Alpine.directive("trap", (el, {expression}, {effect, evaluateLater}) => { + Alpine.directive("trap", Alpine.skipDuringClone((el, {expression, modifiers}, {effect, evaluateLater}) => { let evaluator = evaluateLater(expression); let oldValue = false; let trap = createFocusTrap(el, { escapeDeactivates: false, - allowOutsideClick: true + allowOutsideClick: true, + fallbackFocus: () => el }); + let undoInert = () => { + }; + let undoDisableScrolling = () => { + }; effect(() => evaluator((value) => { if (oldValue === value) return; if (value && !oldValue) { setTimeout(() => { + if (modifiers.includes("inert")) + undoInert = setInert(el); + if (modifiers.includes("noscroll")) + undoDisableScrolling = disableScrolling(); trap.activate(); }); } if (!value && oldValue) { + undoInert(); + undoInert = () => { + }; + undoDisableScrolling(); + undoDisableScrolling = () => { + }; trap.deactivate(); } oldValue = !!value; })); + }, (el, {expression, modifiers}, {evaluate}) => { + if (modifiers.includes("inert") && evaluate(expression)) + setInert(el); + })); + } + function setInert(el) { + let undos = []; + crawlSiblingsUp(el, (sibling) => { + let cache = sibling.hasAttribute("aria-hidden"); + sibling.setAttribute("aria-hidden", "true"); + undos.push(() => cache || sibling.removeAttribute("aria-hidden")); + }); + return () => { + while (undos.length) + undos.pop()(); + }; + } + function crawlSiblingsUp(el, callback) { + if (el.isSameNode(document.body) || !el.parentNode) + return; + Array.from(el.parentNode.children).forEach((sibling) => { + if (!sibling.isSameNode(el)) + callback(sibling); + crawlSiblingsUp(el.parentNode, callback); }); } + function disableScrolling() { + let overflow = document.documentElement.style.overflow; + let paddingRight = document.documentElement.style.paddingRight; + let scrollbarWidth = window.innerWidth - document.documentElement.clientWidth; + document.documentElement.style.overflow = "hidden"; + document.documentElement.style.paddingRight = `${scrollbarWidth}px`; + return () => { + document.documentElement.style.overflow = overflow; + document.documentElement.style.paddingRight = paddingRight; + }; + } // packages/trap/builds/cdn.js document.addEventListener("alpine:init", () => { |