UNPKG

@ariakit/react

Version:

Toolkit for building accessible web apps with React

1,122 lines (1,033 loc) 41.2 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _5FTT4HCAcjs = require('./5FTT4HCA.cjs'); var _DFLJFYEEcjs = require('./DFLJFYEE.cjs'); var _S64IPIO4cjs = require('./S64IPIO4.cjs'); var _DWSXHZYMcjs = require('./DWSXHZYM.cjs'); var _ITEGEFBHcjs = require('./ITEGEFBH.cjs'); var _5FQZLBJ7cjs = require('./5FQZLBJ7.cjs'); var _MTBSKKM4cjs = require('./MTBSKKM4.cjs'); var _LZ6L3ECGcjs = require('./LZ6L3ECG.cjs'); // ../ariakit-components/dist/dialog/dialog-store.js function createDialogStore(props = {}) { return _ITEGEFBHcjs.createDisclosureStore.call(void 0, props); } // ../ariakit-react-components/dist/dialog/dialog-store.js function useDialogStoreProps(store, update, props) { return _ITEGEFBHcjs.useDisclosureStoreProps.call(void 0, store, update, props); } function useDialogStore(props = {}) { const [store, update] = _5FQZLBJ7cjs.useStore.call(void 0, createDialogStore, props); return useDialogStoreProps(store, update, props); } // ../ariakit-react-components/dist/focusable/focusable-container.js var _jsxruntime = require('react/jsx-runtime'); var TagName = "div"; var useFocusableContainer = _LZ6L3ECGcjs.createHook.call(void 0, function useFocusableContainer2({ autoFocusOnShow = true, ...props }) { props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _MTBSKKM4cjs.FocusableContext.Provider, { value: autoFocusOnShow, children: element }), [autoFocusOnShow]); return props; }); var FocusableContainer = _LZ6L3ECGcjs.forwardRef.call(void 0, function FocusableContainer2(props) { return _LZ6L3ECGcjs.createElement.call(void 0, TagName, useFocusableContainer(props)); }); // ../ariakit-react-components/dist/dialog/utils/is-backdrop.js function isBackdrop(element, ...ids) { if (!element) return false; const backdrop = element.getAttribute("data-backdrop"); if (backdrop == null) return false; if (backdrop === "") return true; if (backdrop === "true") return true; if (!ids.length) return true; return ids.some((id) => backdrop === id); } // ../ariakit-react-components/dist/dialog/utils/orchestrate.js var cleanups = /* @__PURE__ */ new WeakMap(); function flushCleanupStack(elementCleanups, key, stack) { while (stack.length) { const entry = stack[stack.length - 1]; if (!entry) { elementCleanups.delete(key); return; } if (!entry.disposed) return; stack.pop(); entry.cleanup(); } elementCleanups.delete(key); } function orchestrate(element, key, setup) { var _a; let elementCleanups = cleanups.get(element); if (!elementCleanups) { elementCleanups = /* @__PURE__ */ new Map(); cleanups.set(element, elementCleanups); } const stack = (_a = elementCleanups.get(key)) != null ? _a : []; const entry = { cleanup: setup(), disposed: false }; if (!stack.length) elementCleanups.set(key, stack); stack.push(entry); return () => { if (!stack.includes(entry)) return; entry.disposed = true; flushCleanupStack(elementCleanups, key, stack); }; } function setAttribute(element, attr, value) { const setup = () => { const previousValue = element.getAttribute(attr); element.setAttribute(attr, value); return () => { if (previousValue == null) element.removeAttribute(attr); else element.setAttribute(attr, previousValue); }; }; return orchestrate(element, attr, setup); } function setProperty(element, property, value) { const setup = () => { const exists = property in element; const previousValue = element[property]; element[property] = value; return () => { if (!exists) delete element[property]; else element[property] = previousValue; }; }; return orchestrate(element, property, setup); } function assignStyle(element, style) { if (!element) return () => { }; const setup = () => { const prevStyle = element.style.cssText; Object.assign(element.style, style); return () => { element.style.cssText = prevStyle; }; }; return orchestrate(element, "style", setup); } function setCSSProperty(element, property, value) { if (!element) return () => { }; const setup = () => { const previousValue = element.style.getPropertyValue(property); element.style.setProperty(property, value); return () => { if (previousValue) element.style.setProperty(property, previousValue); else element.style.removeProperty(property); }; }; return orchestrate(element, property, setup); } // ../ariakit-react-components/dist/dialog/utils/tree-cleanup.js function getPropertyName(id = "", kind = "outside") { return `__ariakit-dialog-${kind}${id ? `-${id}` : ""}`; } function markElement(element, id = "") { return _LZ6L3ECGcjs.chain.call(void 0, setProperty(element, getPropertyName(), true), setProperty(element, getPropertyName(id), true)); } function markAncestor(element, id = "") { return _LZ6L3ECGcjs.chain.call(void 0, setProperty(element, getPropertyName("", "ancestor"), true), setProperty(element, getPropertyName(id, "ancestor"), true)); } function markTreeInside(id, elements) { return _LZ6L3ECGcjs.chain.call(void 0, ...elements.map((element) => { if (!element) return void 0; return setProperty(element, getPropertyName(id, "inside"), true); })); } function isElementInside(element, id) { const propertyName = getPropertyName(id, "inside"); do { if (element[propertyName]) return true; if (!element.parentElement) return false; element = element.parentElement; } while (true); } function isElementMarked(element, id) { const ancestorProperty = getPropertyName(id, "ancestor"); if (element[ancestorProperty]) return true; const elementProperty = getPropertyName(id); do { if (element[elementProperty]) return true; if (!element.parentElement) return false; element = element.parentElement; } while (true); } function addElementMarkCleanup({ cleanups: cleanups2, element, id, ids }) { if (isBackdrop(element, ...ids)) return; cleanups2.push(markElement(element, id)); } function addAncestorMarkCleanup({ cleanups: cleanups2, ancestor, element, id }) { if (element.hasAttribute("data-dialog") && element.id !== id) return; cleanups2.push(markAncestor(ancestor, id)); } function restoreCleanups(cleanups2) { var _a; for (let index = cleanups2.length - 1; index >= 0; index -= 1) (_a = cleanups2[index]) == null ? void 0 : _a.call(cleanups2); } // ../ariakit-react-components/dist/dialog/utils/walk-tree-outside.js var ignoreTags = ["SCRIPT", "STYLE"]; function getSnapshotPropertyName(id) { return `__ariakit-dialog-snapshot-${id}`; } function inSnapshot(id, element) { const doc = _LZ6L3ECGcjs.getDocument.call(void 0, element); const propertyName = getSnapshotPropertyName(id); if (!doc.body[propertyName]) return true; do { if (element === doc.body) return false; if (element[propertyName]) return true; if (!element.parentElement) return false; element = element.parentElement; } while (true); } function shouldWalkElement(id, element, ignoredElements) { if (ignoreTags.includes(element.tagName)) return false; if (!inSnapshot(id, element)) return false; return !ignoredElements.some((enabledElement) => enabledElement && _LZ6L3ECGcjs.contains.call(void 0, element, enabledElement)); } function walkTreeOutside(id, elements, callback, ancestorCallback) { for (let element of elements) { if (!(element == null ? void 0 : element.isConnected)) continue; const hasAncestorAlready = elements.some((maybeAncestor) => { if (!maybeAncestor) return false; if (maybeAncestor === element) return false; return maybeAncestor.contains(element); }); const doc = _LZ6L3ECGcjs.getDocument.call(void 0, element); const originalElement = element; while (element.parentElement && element !== doc.body) { ancestorCallback == null ? void 0 : ancestorCallback(element.parentElement, originalElement); if (!hasAncestorAlready) { for (const child of element.parentElement.children) if (shouldWalkElement(id, child, elements)) callback(child, originalElement); } element = element.parentElement; } } } function createWalkTreeSnapshot(id, elements) { const { body } = _LZ6L3ECGcjs.getDocument.call(void 0, elements[0]); const cleanups2 = []; const markElement2 = (element) => { cleanups2.push(setProperty(element, getSnapshotPropertyName(id), true)); }; walkTreeOutside(id, elements, markElement2); return _LZ6L3ECGcjs.chain.call(void 0, setProperty(body, getSnapshotPropertyName(id), true), () => { for (const cleanup of cleanups2) cleanup(); }); } // ../ariakit-react-components/dist/dialog/utils/mark-tree-outside.js function markTreeOutside(id, elements) { const cleanups2 = []; const ids = elements.map((el) => el == null ? void 0 : el.id); walkTreeOutside(id, elements, (element) => { addElementMarkCleanup({ cleanups: cleanups2, element, id, ids }); }, (ancestor, element) => { addAncestorMarkCleanup({ cleanups: cleanups2, ancestor, element, id }); }); const restoreAccessibilityTree = () => { restoreCleanups(cleanups2); }; return restoreAccessibilityTree; } // ../ariakit-react-components/dist/dialog/dialog-backdrop.js var _react = require('react'); function DialogBackdrop({ store, backdrop, backdropRef, alwaysVisible, hidden }) { const ref = _react.useRef.call(void 0, null); const disclosure = _ITEGEFBHcjs.useDisclosureStore.call(void 0, { disclosure: store }); const contentElement = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "contentElement"); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { const backdrop2 = ref.current; const dialog = contentElement; if (!backdrop2) return; if (!dialog) return; backdrop2.style.zIndex = getComputedStyle(dialog).zIndex; }, [contentElement]); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { const id = contentElement == null ? void 0 : contentElement.id; if (!id) return; const backdrop2 = ref.current; if (!backdrop2) return; return markAncestor(backdrop2, id); }, [contentElement]); const props = _ITEGEFBHcjs.useDisclosureContent.call(void 0, { ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, backdropRef), store: disclosure, role: "presentation", "data-backdrop": (contentElement == null ? void 0 : contentElement.id) || "", alwaysVisible, hidden: hidden != null ? hidden : void 0, style: { position: "fixed", top: 0, right: 0, bottom: 0, left: 0 } }); if (!backdrop) return null; if (_react.isValidElement.call(void 0, backdrop)) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _DFLJFYEEcjs.Role, { ...props, render: backdrop }); const Component = typeof backdrop !== "boolean" ? backdrop : "div"; return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _DFLJFYEEcjs.Role, { ...props, render: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Component, {}) }); } // ../ariakit-react-components/dist/dialog/utils/supports-inert.js function supportsInert() { return "inert" in HTMLElement.prototype; } // ../ariakit-react-components/dist/dialog/utils/disable-accessibility-tree-outside.js function hideElementFromAccessibilityTree(element) { return setAttribute(element, "aria-hidden", "true"); } // ../ariakit-react-components/dist/dialog/utils/is-focus-trap.js function isFocusTrap(element, ...ids) { if (!element) return false; const attr = element.getAttribute("data-focus-trap"); if (attr == null) return false; if (!ids.length) return true; if (attr === "") return false; return ids.some((id) => attr === id); } // ../ariakit-react-components/dist/dialog/utils/disable-tree.js function disableTree(element, ignoredElements) { if (!("style" in element)) return _LZ6L3ECGcjs.noop; if (supportsInert()) return setProperty(element, "inert", true); return _LZ6L3ECGcjs.chain.call(void 0, ..._LZ6L3ECGcjs.getAllTabbableIn.call(void 0, element, true).map((element2) => { if (ignoredElements == null ? void 0 : ignoredElements.some((el) => el && _LZ6L3ECGcjs.contains.call(void 0, el, element2))) return _LZ6L3ECGcjs.noop; const restoreFocusMethod = orchestrate(element2, "focus", () => { element2.focus = _LZ6L3ECGcjs.noop; return () => { delete element2.focus; }; }); return _LZ6L3ECGcjs.chain.call(void 0, setAttribute(element2, "tabindex", "-1"), restoreFocusMethod); }), hideElementFromAccessibilityTree(element), assignStyle(element, { pointerEvents: "none", userSelect: "none", cursor: "default" })); } function addDisabledElementCleanup({ cleanups: cleanups2, element, elements, ids }) { if (isBackdrop(element, ...ids)) return; if (isFocusTrap(element, ...ids)) return; cleanups2.push(disableTree(element, elements)); } function addRoleNoneCleanup(cleanups2, ancestor, elements) { if (!ancestor.hasAttribute("role")) return; if (elements.some((el) => el && _LZ6L3ECGcjs.contains.call(void 0, el, ancestor))) return; cleanups2.push(setAttribute(ancestor, "role", "none")); } function markAndDisableTreeOutside(id, elements) { const cleanups2 = []; const ids = elements.map((el) => el == null ? void 0 : el.id); walkTreeOutside(id, elements, (element) => { addElementMarkCleanup({ cleanups: cleanups2, element, id, ids }); addDisabledElementCleanup({ cleanups: cleanups2, element, elements, ids }); }, (ancestor, element) => { addAncestorMarkCleanup({ cleanups: cleanups2, ancestor, element, id }); addRoleNoneCleanup(cleanups2, ancestor, elements); }); const restoreTreeOutside = () => { restoreCleanups(cleanups2); }; return restoreTreeOutside; } // ../ariakit-react-components/dist/dialog/utils/prepend-hidden-dismiss.js function prependHiddenDismiss(container, onClick) { const button = _LZ6L3ECGcjs.getDocument.call(void 0, container).createElement("button"); button.type = "button"; button.tabIndex = -1; button.textContent = "Dismiss popup"; Object.assign(button.style, _S64IPIO4cjs.getVisuallyHiddenStyle.call(void 0, )); button.addEventListener("click", onClick); container.prepend(button); const removeHiddenDismiss = () => { button.removeEventListener("click", onClick); button.remove(); }; return removeHiddenDismiss; } // ../ariakit-react-components/dist/dialog/utils/use-previous-mouse-down-ref.js function usePreviousMouseDownRef(enabled, scope) { const previousMouseDownRef = _react.useRef.call(void 0, null); _react.useEffect.call(void 0, () => { if (!enabled) { previousMouseDownRef.current = null; return; } const onMouseDown = (event) => { previousMouseDownRef.current = event.target; }; return _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, "mousedown", onMouseDown, true, scope); }, [enabled, scope]); return previousMouseDownRef; } // ../ariakit-react-components/dist/dialog/utils/use-hide-on-interact-outside.js function isInDocument(target) { if (target.tagName === "HTML") return true; return _LZ6L3ECGcjs.contains.call(void 0, _LZ6L3ECGcjs.getDocument.call(void 0, target).body, target); } function isDisclosure(disclosure, target) { if (!disclosure) return false; if (_LZ6L3ECGcjs.contains.call(void 0, disclosure, target)) return true; const activeId = target.getAttribute("aria-activedescendant"); if (activeId) { const activeElement = _LZ6L3ECGcjs.getDocument.call(void 0, disclosure).getElementById(activeId); if (activeElement) return _LZ6L3ECGcjs.contains.call(void 0, disclosure, activeElement); } return false; } function isMouseEventOnDialog(event, dialog) { if (!("clientY" in event)) return false; const rect = dialog.getBoundingClientRect(); if (rect.width === 0 || rect.height === 0) return false; return rect.top <= event.clientY && event.clientY <= rect.top + rect.height && rect.left <= event.clientX && event.clientX <= rect.left + rect.width; } function useEventOutside({ store, type, listener, capture, open, contentElement, focusedRef }) { const callListener = _LZ6L3ECGcjs.useEvent.call(void 0, listener); _react.useEffect.call(void 0, () => { if (!open) return; const onEvent = (event) => { const { contentElement: contentElement2, disclosureElement } = store.getState(); const target = event.target; if (!contentElement2) return; if (!_LZ6L3ECGcjs.isElement.call(void 0, target)) return; if (!isInDocument(target)) return; if (_LZ6L3ECGcjs.contains.call(void 0, contentElement2, target)) return; if (isDisclosure(disclosureElement, target)) return; if (target.hasAttribute("data-focus-trap")) return; if (isMouseEventOnDialog(event, contentElement2)) return; if (isElementInside(target, contentElement2.id)) return; if (focusedRef.current && !isElementMarked(target, contentElement2.id)) return; callListener(event); }; return _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, type, onEvent, capture, contentElement ? _LZ6L3ECGcjs.getWindow.call(void 0, contentElement) : void 0); }, [ open, capture, store, type, callListener, contentElement, focusedRef ]); } function shouldHideOnInteractOutside(hideOnInteractOutside, event) { if (typeof hideOnInteractOutside === "function") return hideOnInteractOutside(event); return !!hideOnInteractOutside; } function useHideOnInteractOutside(store, hideOnInteractOutside, domReady, interactedOutsideRef) { const open = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "open"); const contentElement = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "contentElement"); const previousMouseDownRef = usePreviousMouseDownRef(open, contentElement ? _LZ6L3ECGcjs.getWindow.call(void 0, contentElement) : void 0); const focusedRef = _react.useRef.call(void 0, false); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { if (!open) return; if (!domReady) return; if (!contentElement) return; focusedRef.current = false; const onFocus = () => { focusedRef.current = true; }; contentElement.addEventListener("focusin", onFocus, true); return () => contentElement.removeEventListener("focusin", onFocus, true); }, [ open, domReady, contentElement ]); const props = { store, capture: true, open, contentElement, focusedRef }; useEventOutside({ ...props, type: "click", listener: (event) => { const { contentElement: contentElement2 } = store.getState(); const previousMouseDown = previousMouseDownRef.current; if (!previousMouseDown) return; if (!isElementMarked(previousMouseDown, contentElement2 == null ? void 0 : contentElement2.id)) return; if (!shouldHideOnInteractOutside(hideOnInteractOutside, event)) return; if (interactedOutsideRef) interactedOutsideRef.current = true; store.hide(); } }); useEventOutside({ ...props, type: "focusin", listener: (event) => { const { contentElement: contentElement2 } = store.getState(); if (!contentElement2) return; if (event.target === _LZ6L3ECGcjs.getDocument.call(void 0, contentElement2)) return; if (!shouldHideOnInteractOutside(hideOnInteractOutside, event)) return; store.hide(); } }); useEventOutside({ ...props, type: "contextmenu", listener: (event) => { if (!shouldHideOnInteractOutside(hideOnInteractOutside, event)) return; if (interactedOutsideRef) interactedOutsideRef.current = true; store.hide(); } }); } // ../ariakit-react-components/dist/dialog/utils/use-nested-dialogs.js var NestedDialogsContext = _react.createContext.call(void 0, {}); function useNestedDialogs(store) { const context = _react.useContext.call(void 0, NestedDialogsContext); const [dialogs, setDialogs] = _react.useState.call(void 0, []); const add = _react.useCallback.call(void 0, (dialog) => { var _a; setDialogs((dialogs2) => [...dialogs2, dialog]); return _LZ6L3ECGcjs.chain.call(void 0, (_a = context.add) == null ? void 0 : _a.call(context, dialog), () => { setDialogs((dialogs2) => dialogs2.filter((d) => d !== dialog)); }); }, [context]); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { return _5FQZLBJ7cjs.sync.call(void 0, store, ["open", "contentElement"], (state) => { var _a; if (!state.open) return; if (!state.contentElement) return; return (_a = context.add) == null ? void 0 : _a.call(context, store); }); }, [store, context]); const providerValue = _react.useMemo.call(void 0, () => ({ store, add }), [store, add]); return { wrapElement: _react.useCallback.call(void 0, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, NestedDialogsContext.Provider, { value: providerValue, children: element }), [providerValue]), nestedDialogs: dialogs }; } // ../ariakit-react-components/dist/dialog/utils/use-root-dialog.js var _reactdom = require('react-dom'); function useRootDialog({ attribute, contentId, contentElement, enabled }) { const [updated, retry] = _LZ6L3ECGcjs.useForceUpdate.call(void 0, ); const isRootDialog = _react.useCallback.call(void 0, () => { if (!enabled) return false; if (!contentElement) return false; const { body } = _LZ6L3ECGcjs.getDocument.call(void 0, contentElement); const id = body.getAttribute(attribute); return !id || id === contentId; }, [ updated, enabled, contentElement, attribute, contentId ]); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { if (!enabled) return; if (!contentId) return; if (!contentElement) return; const { body } = _LZ6L3ECGcjs.getDocument.call(void 0, contentElement); if (isRootDialog()) { body.setAttribute(attribute, contentId); return () => body.removeAttribute(attribute); } const observer = new MutationObserver(() => _reactdom.flushSync.call(void 0, retry)); observer.observe(body, { attributeFilter: [attribute] }); return () => observer.disconnect(); }, [ updated, enabled, contentId, contentElement, isRootDialog, attribute ]); return isRootDialog; } // ../ariakit-react-components/dist/dialog/utils/use-prevent-body-scroll.js var isIOS = _LZ6L3ECGcjs.isApple.call(void 0, ) && !_LZ6L3ECGcjs.isMac.call(void 0, ); function getPaddingProperty(documentElement) { const documentLeft = documentElement.getBoundingClientRect().left; return Math.round(documentLeft) + documentElement.scrollLeft ? "paddingLeft" : "paddingRight"; } function usePreventBodyScroll(contentElement, contentId, enabled) { const isRootDialog = useRootDialog({ attribute: "data-dialog-prevent-body-scroll", contentElement, contentId, enabled }); (isIOS ? _react.useEffect : (0, _LZ6L3ECGcjs.useSafeLayoutEffect))(() => { if (!isRootDialog()) return; if (!contentElement) return; const doc = _LZ6L3ECGcjs.getDocument.call(void 0, contentElement); const win = _LZ6L3ECGcjs.getWindow.call(void 0, contentElement); const { documentElement, body } = doc; const cssScrollbarWidth = documentElement.style.getPropertyValue("--scrollbar-width"); const scrollbarWidth = cssScrollbarWidth ? Number.parseInt(cssScrollbarWidth, 10) : win.innerWidth - documentElement.clientWidth; const setScrollbarWidthProperty = () => setCSSProperty(documentElement, "--scrollbar-width", `${scrollbarWidth}px`); const paddingProperty = getPaddingProperty(documentElement); const setStyle = () => assignStyle(body, { overflow: "hidden", [paddingProperty]: `${scrollbarWidth}px` }); const setIOSStyle = () => { var _a, _b; const { scrollX, scrollY, visualViewport } = win; const offsetLeft = (_a = visualViewport == null ? void 0 : visualViewport.offsetLeft) != null ? _a : 0; const offsetTop = (_b = visualViewport == null ? void 0 : visualViewport.offsetTop) != null ? _b : 0; const restoreStyle = assignStyle(body, { position: "fixed", overflow: "hidden", top: `${-(scrollY - Math.floor(offsetTop))}px`, left: `${-(scrollX - Math.floor(offsetLeft))}px`, right: "0", [paddingProperty]: `${scrollbarWidth}px` }); return () => { restoreStyle(); if (process.env.NODE_ENV !== "test") win.scrollTo({ left: scrollX, top: scrollY, behavior: "instant" }); }; }; const restore = _LZ6L3ECGcjs.chain.call(void 0, setScrollbarWidthProperty(), isIOS ? setIOSStyle() : setStyle()); if (isIOS) return restore; return () => { queueMicrotask(restore); }; }, [isRootDialog, contentElement]); } // ../ariakit-react-components/dist/dialog/dialog.js var TagName2 = "div"; var isSafariBrowser = _LZ6L3ECGcjs.isSafari.call(void 0, ); function isAlreadyFocusingAnotherElement(dialog) { const activeElement = _LZ6L3ECGcjs.getActiveElement.call(void 0, dialog); if (!activeElement) return false; if (dialog && _LZ6L3ECGcjs.contains.call(void 0, dialog, activeElement)) return false; if (_LZ6L3ECGcjs.isFocusable.call(void 0, activeElement)) return true; return false; } function getElementFromProp(prop, focusable = false) { if (!prop) return null; const element = "current" in prop ? prop.current : prop; if (!element) return null; if (focusable) return _LZ6L3ECGcjs.isFocusable.call(void 0, element) ? element : null; return element; } var useDialog = _LZ6L3ECGcjs.createHook.call(void 0, function useDialog2({ store: storeProp, open: openProp, onClose, focusable = true, modal = true, portal = modal, backdrop = modal, hideOnEscape = true, hideOnInteractOutside = true, getPersistentElements, preventBodyScroll = modal, autoFocusOnShow = true, autoFocusOnHide = true, initialFocus, finalFocus, unmountOnHide, unstable_treeSnapshotKey, ...props }) { const context = _ITEGEFBHcjs.useDialogProviderContext.call(void 0, ); const ref = _react.useRef.call(void 0, null); const backdropRef = _react.useRef.call(void 0, null); const store = useDialogStore({ store: storeProp || context, open: openProp, setOpen(open2) { if (open2) return; const dialog = ref.current; if (!dialog) return; const event = new Event("close", { bubbles: false, cancelable: true }); if (onClose) dialog.addEventListener("close", onClose, { once: true }); dialog.dispatchEvent(event); if (!event.defaultPrevented) return; store.setOpen(true); } }); const { portalRef, domReady } = _LZ6L3ECGcjs.usePortalRef.call(void 0, portal, props.portalRef); const preserveTabOrderProp = props.preserveTabOrder; const preserveTabOrder = _5FQZLBJ7cjs.useStoreState.call(void 0, store, (state) => preserveTabOrderProp && !modal && state.mounted); const id = _LZ6L3ECGcjs.useId.call(void 0, props.id); const open = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "open"); const mounted = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "mounted"); const contentElement = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "contentElement"); const hidden = _ITEGEFBHcjs.isHidden.call(void 0, mounted, props.hidden, props.alwaysVisible); usePreventBodyScroll(contentElement, id, preventBodyScroll && !hidden); const interactedOutsideRef = _react.useRef.call(void 0, false); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { return _5FQZLBJ7cjs.sync.call(void 0, store, ["open"], (state) => { if (!state.open) return; interactedOutsideRef.current = false; }); }, [store]); useHideOnInteractOutside(store, hideOnInteractOutside, domReady, interactedOutsideRef); const { wrapElement, nestedDialogs } = useNestedDialogs(store); props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, wrapElement, [wrapElement]); const lastMousedownRef = _react.useRef.call(void 0, null); if (isSafariBrowser) _react.useEffect.call(void 0, () => { if (!domReady) return; const dialog = ref.current; if (!dialog) return; const doc = _LZ6L3ECGcjs.getDocument.call(void 0, dialog); const onMousedown = (event) => { lastMousedownRef.current = event.target; }; doc.addEventListener("mousedown", onMousedown, true); return () => { doc.removeEventListener("mousedown", onMousedown, true); }; }, [domReady]); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { if (!open) return; const dialog = ref.current; const activeElement = _LZ6L3ECGcjs.getActiveElement.call(void 0, dialog, true); if (!activeElement) return; if (activeElement.tagName === "BODY") { const fallback = lastMousedownRef.current; lastMousedownRef.current = null; if (!(fallback == null ? void 0 : fallback.isConnected)) return; if (!_LZ6L3ECGcjs.isFocusable.call(void 0, fallback)) return; if (dialog && _LZ6L3ECGcjs.contains.call(void 0, dialog, fallback)) return; store.setDisclosureElement(fallback); return; } if (dialog && _LZ6L3ECGcjs.contains.call(void 0, dialog, activeElement)) return; store.setDisclosureElement(activeElement); }, [store, open]); _react.useEffect.call(void 0, () => { if (!mounted) return; if (!domReady) return; const dialog = ref.current; if (!dialog) return; const win = _LZ6L3ECGcjs.getWindow.call(void 0, dialog); const viewport = win.visualViewport || win; const setViewportHeight = () => { var _a, _b; const height = (_b = (_a = win.visualViewport) == null ? void 0 : _a.height) != null ? _b : win.innerHeight; dialog.style.setProperty("--dialog-viewport-height", `${height}px`); }; setViewportHeight(); viewport.addEventListener("resize", setViewportHeight); return () => { viewport.removeEventListener("resize", setViewportHeight); }; }, [mounted, domReady]); _react.useEffect.call(void 0, () => { if (!modal) return; if (!mounted) return; if (!domReady) return; const dialog = ref.current; if (!dialog) return; if (dialog.querySelector("[data-dialog-dismiss]")) return; return prependHiddenDismiss(dialog, store.hide); }, [ store, modal, mounted, domReady ]); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { if (!supportsInert()) return; if (open) return; if (!mounted) return; if (!domReady) return; const dialog = ref.current; if (!dialog) return; return disableTree(dialog); }, [ open, mounted, domReady ]); const canTakeTreeSnapshot = open && domReady; _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { if (!id) return; if (!canTakeTreeSnapshot) return; const dialog = ref.current; return createWalkTreeSnapshot(id, [dialog]); }, [ id, canTakeTreeSnapshot, unstable_treeSnapshotKey ]); const getPersistentElementsProp = _LZ6L3ECGcjs.useEvent.call(void 0, getPersistentElements); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { if (!id) return; if (!canTakeTreeSnapshot) return; const { disclosureElement } = store.getState(); const allElements = [ ref.current, ...getPersistentElementsProp() || [], ...nestedDialogs.map((dialog) => dialog.getState().contentElement) ]; const restoreInsideMarks = markTreeInside(id, allElements); if (modal) return _LZ6L3ECGcjs.chain.call(void 0, restoreInsideMarks, markAndDisableTreeOutside(id, allElements)); return _LZ6L3ECGcjs.chain.call(void 0, restoreInsideMarks, markTreeOutside(id, [disclosureElement, ...allElements])); }, [ id, store, canTakeTreeSnapshot, getPersistentElementsProp, nestedDialogs, modal, unstable_treeSnapshotKey ]); const mayAutoFocusOnShow = !!autoFocusOnShow; const autoFocusOnShowProp = _LZ6L3ECGcjs.useBooleanEvent.call(void 0, autoFocusOnShow); const [autoFocusEnabled, setAutoFocusEnabled] = _react.useState.call(void 0, false); _react.useEffect.call(void 0, () => { if (!open) return; if (!mayAutoFocusOnShow) return; if (!domReady) return; if (!(contentElement == null ? void 0 : contentElement.isConnected)) return; const element = getElementFromProp(initialFocus, true) || contentElement.querySelector("[data-autofocus=true],[autofocus]") || _LZ6L3ECGcjs.getFirstTabbableIn.call(void 0, contentElement, true, portal && preserveTabOrder) || contentElement; const isElementFocusable = _LZ6L3ECGcjs.isFocusable.call(void 0, element); if (!autoFocusOnShowProp(isElementFocusable ? element : null)) return; setAutoFocusEnabled(true); queueMicrotask(() => { if (!store.getState().open) return; element.focus(); if (!isSafariBrowser) return; if (!isElementFocusable) return; element.scrollIntoView({ block: "nearest", inline: "nearest" }); }); }, [ open, mayAutoFocusOnShow, domReady, contentElement, initialFocus, portal, preserveTabOrder, store, autoFocusOnShowProp ]); const mayAutoFocusOnHide = !!autoFocusOnHide; const autoFocusOnHideProp = _LZ6L3ECGcjs.useBooleanEvent.call(void 0, autoFocusOnHide); const [hasOpened, setHasOpened] = _react.useState.call(void 0, false); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { if (!open) return; setHasOpened(true); return () => setHasOpened(false); }, [open]); const focusOnHide = _react.useCallback.call(void 0, (dialog, retry = true) => { if (interactedOutsideRef.current) return; const { disclosureElement } = store.getState(); if (isAlreadyFocusingAnotherElement(dialog)) return; let element = getElementFromProp(finalFocus) || disclosureElement; if (element == null ? void 0 : element.id) { const doc = _LZ6L3ECGcjs.getDocument.call(void 0, element); const selector = `[aria-activedescendant="${element.id}"]`; const composite = doc.querySelector(selector); if (composite) element = composite; } if (element && !_LZ6L3ECGcjs.isFocusable.call(void 0, element)) { const maybeParentDialog = element.closest("[data-dialog]"); if (maybeParentDialog == null ? void 0 : maybeParentDialog.id) { const doc = _LZ6L3ECGcjs.getDocument.call(void 0, maybeParentDialog); const selector = `[aria-controls~="${maybeParentDialog.id}"]`; const control = doc.querySelector(selector); if (control) element = control; } } const isElementFocusable = element && _LZ6L3ECGcjs.isFocusable.call(void 0, element); if (!isElementFocusable && retry) { requestAnimationFrame(() => focusOnHide(dialog, false)); return; } if (!autoFocusOnHideProp(isElementFocusable ? element : null)) return; if (!isElementFocusable) return; element == null ? void 0 : element.focus(); }, [ store, finalFocus, autoFocusOnHideProp ]); const focusedOnHideRef = _react.useRef.call(void 0, false); _LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => { if (open) return; if (!hasOpened) return; if (!mayAutoFocusOnHide) return; const dialog = ref.current; focusedOnHideRef.current = true; focusOnHide(dialog); }, [ open, hasOpened, domReady, mayAutoFocusOnHide, focusOnHide ]); _react.useEffect.call(void 0, () => { if (!hasOpened) return; if (!mayAutoFocusOnHide) return; const dialog = ref.current; return () => { if (focusedOnHideRef.current) { focusedOnHideRef.current = false; return; } focusOnHide(dialog); }; }, [ hasOpened, mayAutoFocusOnHide, focusOnHide ]); const hideOnEscapeProp = _LZ6L3ECGcjs.useBooleanEvent.call(void 0, hideOnEscape); _react.useEffect.call(void 0, () => { if (!domReady) return; if (!mounted) return; const onKeyDown = (event) => { if (event.key !== "Escape") return; if (event.defaultPrevented) return; const dialog = ref.current; if (!dialog) return; if (isElementMarked(dialog)) return; const target = event.target; if (!_LZ6L3ECGcjs.isNode.call(void 0, target)) return; const { disclosureElement } = store.getState(); const isValidTarget = () => { if (_LZ6L3ECGcjs.isElement.call(void 0, target) && target.tagName === "BODY") return true; if (_LZ6L3ECGcjs.contains.call(void 0, dialog, target)) return true; if (!disclosureElement) return true; if (_LZ6L3ECGcjs.contains.call(void 0, disclosureElement, target)) return true; if (_LZ6L3ECGcjs.isElement.call(void 0, target) && isElementMarked(target, dialog.id)) return true; return false; }; if (!isValidTarget()) return; if (!hideOnEscapeProp(event)) return; store.hide(); }; return _LZ6L3ECGcjs.addGlobalEventListener.call(void 0, "keydown", onKeyDown, true, contentElement ? _LZ6L3ECGcjs.getWindow.call(void 0, contentElement) : void 0); }, [ store, domReady, mounted, contentElement, hideOnEscapeProp ]); props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _DWSXHZYMcjs.HeadingLevel, { level: modal ? 1 : void 0, children: element }), [modal]); const hiddenProp = props.hidden; const alwaysVisible = props.alwaysVisible; props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => { if (!backdrop) return element; return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, DialogBackdrop, { store, backdrop, backdropRef, hidden: hiddenProp, alwaysVisible }), element] }); }, [ store, backdrop, hiddenProp, alwaysVisible ]); const [headingId, setHeadingId] = _react.useState.call(void 0, ); const [descriptionId, setDescriptionId] = _react.useState.call(void 0, ); props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _ITEGEFBHcjs.DialogScopedContextProvider, { value: store, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _ITEGEFBHcjs.DialogHeadingContext.Provider, { value: setHeadingId, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _ITEGEFBHcjs.DialogDescriptionContext.Provider, { value: setDescriptionId, children: element }) }) }), [store]); props = { "data-dialog": "", role: "dialog", tabIndex: focusable ? -1 : void 0, "aria-labelledby": props["aria-label"] != null ? void 0 : headingId, "aria-describedby": descriptionId, ...props, id, ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref) }; props = useFocusableContainer({ ...props, autoFocusOnShow: autoFocusEnabled }); props = _ITEGEFBHcjs.useDisclosureContent.call(void 0, { store, ...props, unstable_otherElementRef: backdropRef }); props = _MTBSKKM4cjs.useFocusable.call(void 0, { ...props, focusable }); props = _5FTT4HCAcjs.usePortal.call(void 0, { portal, ...props, portalRef, preserveTabOrder }); return props; }); function createDialogComponent(Component, useProviderContext = _ITEGEFBHcjs.useDialogProviderContext) { return _LZ6L3ECGcjs.forwardRef.call(void 0, function DialogComponent(props) { const context = useProviderContext(); if (!_5FQZLBJ7cjs.useStoreState.call(void 0, props.store || context, (state) => !props.unmountOnHide || (state == null ? void 0 : state.mounted) || !!props.open)) return null; return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Component, { ...props }); }); } var DialogWithStore = createDialogComponent(_LZ6L3ECGcjs.forwardRef.call(void 0, function DialogImpl(props) { return _LZ6L3ECGcjs.createElement.call(void 0, TagName2, useDialog(props)); }), _ITEGEFBHcjs.useDialogProviderContext); var DialogWithInternalStore = _LZ6L3ECGcjs.forwardRef.call(void 0, function DialogWithInternalStore2(props) { const store = useDialogStore({ open: props.open }); return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, DialogWithStore, { ...props, store }); }); var Dialog = _LZ6L3ECGcjs.forwardRef.call(void 0, function Dialog2(props) { const context = _ITEGEFBHcjs.useDialogProviderContext.call(void 0, ); if (props.store || context || !props.unmountOnHide) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, DialogWithStore, { ...props }); return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, DialogWithInternalStore, { ...props }); }); exports.createDialogStore = createDialogStore; exports.useDialogStoreProps = useDialogStoreProps; exports.useDialogStore = useDialogStore; exports.useDialog = useDialog; exports.createDialogComponent = createDialogComponent; exports.Dialog = Dialog;