{"version":3,"file":"utils.js","sources":["../../../../../../packages/components/focus-trap/src/utils.ts"],"sourcesContent":["import { onBeforeUnmount, onMounted, ref } from 'vue'\nimport { FOCUSOUT_PREVENTED, FOCUSOUT_PREVENTED_OPTS } from './tokens'\n\nconst focusReason = ref<'pointer' | 'keyboard'>()\nconst lastUserFocusTimestamp = ref(0)\nconst lastAutomatedFocusTimestamp = ref(0)\nlet focusReasonUserCount = 0\n\nexport type FocusLayer = {\n paused: boolean\n pause: () => void\n resume: () => void\n}\n\nexport type FocusStack = FocusLayer[]\n\nexport const obtainAllFocusableElements = (\n element: HTMLElement\n): HTMLElement[] => {\n const nodes: HTMLElement[] = []\n const walker = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT, {\n acceptNode: (\n node: Element & {\n disabled: boolean\n hidden: boolean\n type: string\n tabIndex: number\n }\n ) => {\n const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden'\n if (node.disabled || node.hidden || isHiddenInput)\n return NodeFilter.FILTER_SKIP\n return node.tabIndex >= 0 || node === document.activeElement\n ? NodeFilter.FILTER_ACCEPT\n : NodeFilter.FILTER_SKIP\n },\n })\n while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement)\n\n return nodes\n}\n\nexport const getVisibleElement = (\n elements: HTMLElement[],\n container: HTMLElement\n) => {\n for (const element of elements) {\n if (!isHidden(element, container)) return element\n }\n}\n\nexport const isHidden = (element: HTMLElement, container: HTMLElement) => {\n if (process.env.NODE_ENV === 'test') return false\n if (getComputedStyle(element).visibility === 'hidden') return true\n\n while (element) {\n if (container && element === container) return false\n if (getComputedStyle(element).display === 'none') return true\n element = element.parentElement as HTMLElement\n }\n\n return false\n}\n\nexport const getEdges = (container: HTMLElement) => {\n const focusable = obtainAllFocusableElements(container)\n const first = getVisibleElement(focusable, container)\n const last = getVisibleElement(focusable.reverse(), container)\n return [first, last]\n}\n\nconst isSelectable = (\n element: any\n): element is HTMLInputElement & { select: () => void } => {\n return element instanceof HTMLInputElement && 'select' in element\n}\n\nexport const tryFocus = (\n element?: HTMLElement | { focus: () => void } | null,\n shouldSelect?: boolean\n) => {\n if (element && element.focus) {\n const prevFocusedElement = document.activeElement\n element.focus({ preventScroll: true })\n lastAutomatedFocusTimestamp.value = window.performance.now()\n if (\n element !== prevFocusedElement &&\n isSelectable(element) &&\n shouldSelect\n ) {\n element.select()\n }\n }\n}\n\nfunction removeFromStack(list: T[], item: T) {\n const copy = [...list]\n\n const idx = list.indexOf(item)\n\n if (idx !== -1) {\n copy.splice(idx, 1)\n }\n return copy\n}\n\nconst createFocusableStack = () => {\n let stack = [] as FocusStack\n\n const push = (layer: FocusLayer) => {\n const currentLayer = stack[0]\n\n if (currentLayer && layer !== currentLayer) {\n currentLayer.pause()\n }\n\n stack = removeFromStack(stack, layer)\n stack.unshift(layer)\n }\n\n const remove = (layer: FocusLayer) => {\n stack = removeFromStack(stack, layer)\n stack[0]?.resume?.()\n }\n\n return {\n push,\n remove,\n }\n}\n\nexport const focusFirstDescendant = (\n elements: HTMLElement[],\n shouldSelect = false\n) => {\n const prevFocusedElement = document.activeElement\n for (const element of elements) {\n tryFocus(element, shouldSelect)\n if (document.activeElement !== prevFocusedElement) return\n }\n}\n\nexport const focusableStack = createFocusableStack()\n\nexport const isFocusCausedByUserEvent = (): boolean => {\n return lastUserFocusTimestamp.value > lastAutomatedFocusTimestamp.value\n}\n\nconst notifyFocusReasonPointer = () => {\n focusReason.value = 'pointer'\n lastUserFocusTimestamp.value = window.performance.now()\n}\n\nconst notifyFocusReasonKeydown = () => {\n focusReason.value = 'keyboard'\n lastUserFocusTimestamp.value = window.performance.now()\n}\n\nexport const useFocusReason = (): {\n focusReason: typeof focusReason\n lastUserFocusTimestamp: typeof lastUserFocusTimestamp\n lastAutomatedFocusTimestamp: typeof lastAutomatedFocusTimestamp\n} => {\n onMounted(() => {\n if (focusReasonUserCount === 0) {\n document.addEventListener('mousedown', notifyFocusReasonPointer)\n document.addEventListener('touchstart', notifyFocusReasonPointer)\n document.addEventListener('keydown', notifyFocusReasonKeydown)\n }\n focusReasonUserCount++\n })\n\n onBeforeUnmount(() => {\n focusReasonUserCount--\n if (focusReasonUserCount <= 0) {\n document.removeEventListener('mousedown', notifyFocusReasonPointer)\n document.removeEventListener('touchstart', notifyFocusReasonPointer)\n document.removeEventListener('keydown', notifyFocusReasonKeydown)\n }\n })\n\n return {\n focusReason,\n lastUserFocusTimestamp,\n lastAutomatedFocusTimestamp,\n }\n}\n\nexport const createFocusOutPreventedEvent = (\n detail: CustomEventInit['detail']\n) => {\n return new CustomEvent(FOCUSOUT_PREVENTED, {\n ...FOCUSOUT_PREVENTED_OPTS,\n detail,\n })\n}\n"],"names":["ref","onMounted","onBeforeUnmount","FOCUSOUT_PREVENTED","FOCUSOUT_PREVENTED_OPTS"],"mappings":";;;;;;;AAEA,MAAM,WAAW,GAAGA,OAAG,EAAE,CAAC;AAC1B,MAAM,sBAAsB,GAAGA,OAAG,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,2BAA2B,GAAGA,OAAG,CAAC,CAAC,CAAC,CAAC;AAC3C,IAAI,oBAAoB,GAAG,CAAC,CAAC;AACjB,MAAC,0BAA0B,GAAG,CAAC,OAAO,KAAK;AACvD,EAAE,MAAM,KAAK,GAAG,EAAE,CAAC;AACnB,EAAE,MAAM,MAAM,GAAG,QAAQ,CAAC,gBAAgB,CAAC,OAAO,EAAE,UAAU,CAAC,YAAY,EAAE;AAC7E,IAAI,UAAU,EAAE,CAAC,IAAI,KAAK;AAC1B,MAAM,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,KAAK,OAAO,IAAI,IAAI,CAAC,IAAI,KAAK,QAAQ,CAAC;AAC/E,MAAM,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,MAAM,IAAI,aAAa;AACvD,QAAQ,OAAO,UAAU,CAAC,WAAW,CAAC;AACtC,MAAM,OAAO,IAAI,CAAC,QAAQ,IAAI,CAAC,IAAI,IAAI,KAAK,QAAQ,CAAC,aA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