You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
16 KiB
1 line
16 KiB
{"ast":null,"code":"import \"core-js/modules/es.array.push.js\";\nimport { ref, onMounted, onBeforeUnmount } from 'vue';\nimport { FOCUSOUT_PREVENTED, FOCUSOUT_PREVENTED_OPTS } from './tokens.mjs';\nconst focusReason = ref();\nconst lastUserFocusTimestamp = ref(0);\nconst lastAutomatedFocusTimestamp = ref(0);\nlet focusReasonUserCount = 0;\nconst obtainAllFocusableElements = element => {\n const nodes = [];\n const walker = document.createTreeWalker(element, NodeFilter.SHOW_ELEMENT, {\n acceptNode: node => {\n const isHiddenInput = node.tagName === \"INPUT\" && node.type === \"hidden\";\n if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;\n return node.tabIndex >= 0 || node === document.activeElement ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;\n }\n });\n while (walker.nextNode()) nodes.push(walker.currentNode);\n return nodes;\n};\nconst getVisibleElement = (elements, container) => {\n for (const element of elements) {\n if (!isHidden(element, container)) return element;\n }\n};\nconst isHidden = (element, container) => {\n if (process.env.NODE_ENV === \"test\") return false;\n if (getComputedStyle(element).visibility === \"hidden\") return true;\n while (element) {\n if (container && element === container) return false;\n if (getComputedStyle(element).display === \"none\") return true;\n element = element.parentElement;\n }\n return false;\n};\nconst getEdges = container => {\n const focusable = obtainAllFocusableElements(container);\n const first = getVisibleElement(focusable, container);\n const last = getVisibleElement(focusable.reverse(), container);\n return [first, last];\n};\nconst isSelectable = element => {\n return element instanceof HTMLInputElement && \"select\" in element;\n};\nconst tryFocus = (element, shouldSelect) => {\n if (element && element.focus) {\n const prevFocusedElement = document.activeElement;\n element.focus({\n preventScroll: true\n });\n lastAutomatedFocusTimestamp.value = window.performance.now();\n if (element !== prevFocusedElement && isSelectable(element) && shouldSelect) {\n element.select();\n }\n }\n};\nfunction removeFromStack(list, item) {\n const copy = [...list];\n const idx = list.indexOf(item);\n if (idx !== -1) {\n copy.splice(idx, 1);\n }\n return copy;\n}\nconst createFocusableStack = () => {\n let stack = [];\n const push = layer => {\n const currentLayer = stack[0];\n if (currentLayer && layer !== currentLayer) {\n currentLayer.pause();\n }\n stack = removeFromStack(stack, layer);\n stack.unshift(layer);\n };\n const remove = layer => {\n var _a, _b;\n stack = removeFromStack(stack, layer);\n (_b = (_a = stack[0]) == null ? void 0 : _a.resume) == null ? void 0 : _b.call(_a);\n };\n return {\n push,\n remove\n };\n};\nconst focusFirstDescendant = (elements, shouldSelect = false) => {\n const prevFocusedElement = document.activeElement;\n for (const element of elements) {\n tryFocus(element, shouldSelect);\n if (document.activeElement !== prevFocusedElement) return;\n }\n};\nconst focusableStack = createFocusableStack();\nconst isFocusCausedByUserEvent = () => {\n return lastUserFocusTimestamp.value > lastAutomatedFocusTimestamp.value;\n};\nconst notifyFocusReasonPointer = () => {\n focusReason.value = \"pointer\";\n lastUserFocusTimestamp.value = window.performance.now();\n};\nconst notifyFocusReasonKeydown = () => {\n focusReason.value = \"keyboard\";\n lastUserFocusTimestamp.value = window.performance.now();\n};\nconst useFocusReason = () => {\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 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 return {\n focusReason,\n lastUserFocusTimestamp,\n lastAutomatedFocusTimestamp\n };\n};\nconst createFocusOutPreventedEvent = detail => {\n return new CustomEvent(FOCUSOUT_PREVENTED, {\n ...FOCUSOUT_PREVENTED_OPTS,\n detail\n });\n};\nexport { createFocusOutPreventedEvent, focusFirstDescendant, focusableStack, getEdges, getVisibleElement, isFocusCausedByUserEvent, isHidden, obtainAllFocusableElements, tryFocus, useFocusReason };","map":{"version":3,"names":["focusReason","ref","lastUserFocusTimestamp","lastAutomatedFocusTimestamp","focusReasonUserCount","obtainAllFocusableElements","element","nodes","walker","document","createTreeWalker","NodeFilter","SHOW_ELEMENT","acceptNode","node","isHiddenInput","tagName","type","disabled","hidden","FILTER_SKIP","tabIndex","activeElement","FILTER_ACCEPT","nextNode","push","currentNode","getVisibleElement","elements","container","isHidden","process","env","NODE_ENV","getComputedStyle","visibility","display","parentElement","getEdges","focusable","first","last","reverse","isSelectable","HTMLInputElement","tryFocus","shouldSelect","focus","prevFocusedElement","preventScroll","value","window","performance","now","select","removeFromStack","list","item","copy","idx","indexOf","splice","createFocusableStack","stack","layer","currentLayer","pause","unshift","remove","_a","_b","resume","call","focusFirstDescendant","focusableStack","isFocusCausedByUserEvent","notifyFocusReasonPointer","notifyFocusReasonKeydown","useFocusReason","onMounted","addEventListener","onBeforeUnmount","removeEventListener","createFocusOutPreventedEvent","detail","CustomEvent","FOCUSOUT_PREVENTED","FOCUSOUT_PREVENTED_OPTS"],"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<number>(0)\nconst lastAutomatedFocusTimestamp = ref<number>(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<T>(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"],"mappings":";;;AAEA,MAAMA,WAAW,GAAGC,GAAG,EAAE;AACzB,MAAMC,sBAAsB,GAAGD,GAAG,CAAC,CAAC,CAAC;A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