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.

0 lines
27 KiB

1 month ago
  1. {"ast":null,"code":"import { defineComponent, ref, provide, watch, unref, onMounted, onBeforeUnmount, nextTick, renderSlot } from 'vue';\nimport { isNil } from 'lodash-unified';\nimport { useFocusReason, tryFocus, createFocusOutPreventedEvent, getEdges, focusableStack, focusFirstDescendant, obtainAllFocusableElements, isFocusCausedByUserEvent } from './utils.mjs';\nimport { ON_TRAP_FOCUS_EVT, ON_RELEASE_FOCUS_EVT, FOCUS_TRAP_INJECTION_KEY, FOCUS_AFTER_TRAPPED, FOCUS_AFTER_TRAPPED_OPTS, FOCUS_AFTER_RELEASED } from './tokens.mjs';\nimport _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';\nimport { useEscapeKeydown } from '../../../hooks/use-escape-keydown/index.mjs';\nimport { EVENT_CODE } from '../../../constants/aria.mjs';\nimport { isString } from '@vue/shared';\nconst _sfc_main = defineComponent({\n name: \"ElFocusTrap\",\n inheritAttrs: false,\n props: {\n loop: Boolean,\n trapped: Boolean,\n focusTrapEl: Object,\n focusStartEl: {\n type: [Object, String],\n default: \"first\"\n }\n },\n emits: [ON_TRAP_FOCUS_EVT, ON_RELEASE_FOCUS_EVT, \"focusin\", \"focusout\", \"focusout-prevented\", \"release-requested\"],\n setup(props, {\n emit\n }) {\n const forwardRef = ref();\n let lastFocusBeforeTrapped;\n let lastFocusAfterTrapped;\n const {\n focusReason\n } = useFocusReason();\n useEscapeKeydown(event => {\n if (props.trapped && !focusLayer.paused) {\n emit(\"release-requested\", event);\n }\n });\n const focusLayer = {\n paused: false,\n pause() {\n this.paused = true;\n },\n resume() {\n this.paused = false;\n }\n };\n const onKeydown = e => {\n if (!props.loop && !props.trapped) return;\n if (focusLayer.paused) return;\n const {\n code,\n altKey,\n ctrlKey,\n metaKey,\n currentTarget,\n shiftKey\n } = e;\n const {\n loop\n } = props;\n const isTabbing = code === EVENT_CODE.tab && !altKey && !ctrlKey && !metaKey;\n const currentFocusingEl = document.activeElement;\n if (isTabbing && currentFocusingEl) {\n const container = currentTarget;\n const [first, last] = getEdges(container);\n const isTabbable = first && last;\n if (!isTabbable) {\n if (currentFocusingEl === container) {\n const focusoutPreventedEvent = createFocusOutPreventedEvent({\n focusReason: focusReason.value\n });\n emit(\"focusout-prevented\", focusoutPreventedEvent);\n if (!focusoutPreventedEvent.defaultPrevented) {\n e.preventDefault();\n }\n }\n } else {\n if (!shiftKey && currentFocusingEl === last) {\n const focusoutPreventedEvent = createFocusOutPreventedEvent({\n focusReason: focusReason.value\n });\n emit(\"focusout-prevented\", focusoutPreventedEvent);\n if (!focusoutPreventedEvent.defaultPrevented) {\n e.preventDefault();\n if (loop) tryFocus(first, true);\n }\n } else if (shiftKey && [first, container].includes(currentFocusingEl)) {\n const focusoutPreventedEvent = createFocusOutPreventedEvent({\n focusReason: focusReason.value\n });\n emit(\"focusout-prevented\", focusoutPreventedEvent);\n if (!focusoutPreventedEvent.defaultPrevented) {\n e.preventDefault();\n if (loop) tryFocus(last, true);\n }\n }\n }\n }\n };\n provide(FOCUS_TRAP_INJECTION_KEY, {\n focusTrapRef: forwardRef,\n onKeydown\n });\n watch(() => props.focusTrapEl, focusTrapEl => {\n if (focusTrapEl) {\n forwardRef.value = focusTrapEl;\n }\n }, {\n immediate: true\n });\n watch([forwardRef], ([forwardRef2], [oldForwardRef]) => {\n if (forwardRef2) {\n forwardRef2.addEventListener(\"keydown\", onKeydown);\n forwardRef2.addEv