{"version":3,"file":"roving-focus-item.js","sources":["../../../../../../packages/components/roving-focus-group/src/roving-focus-item.vue"],"sourcesContent":["<template>\n  <el-roving-focus-collection-item\n    :id=\"id\"\n    :focusable=\"focusable\"\n    :active=\"active\"\n  >\n    <slot />\n  </el-roving-focus-collection-item>\n</template>\n\n<script lang=\"ts\">\nimport {\n  computed,\n  defineComponent,\n  inject,\n  nextTick,\n  provide,\n  ref,\n  unref,\n} from 'vue'\nimport { useId } from '@element-plus/hooks'\nimport { composeEventHandlers } from '@element-plus/utils'\nimport { EVENT_CODE } from '@element-plus/constants'\nimport {\n  ElCollectionItem as ElRovingFocusCollectionItem,\n  ROVING_FOCUS_COLLECTION_INJECTION_KEY,\n} from './roving-focus-group'\nimport {\n  ROVING_FOCUS_GROUP_INJECTION_KEY,\n  ROVING_FOCUS_GROUP_ITEM_INJECTION_KEY,\n} from './tokens'\nimport { focusFirst, getFocusIntent, reorderArray } from './utils'\n\nexport default defineComponent({\n  components: {\n    ElRovingFocusCollectionItem,\n  },\n  props: {\n    focusable: {\n      type: Boolean,\n      default: true,\n    },\n    active: {\n      type: Boolean,\n      default: false,\n    },\n  },\n  emits: ['mousedown', 'focus', 'keydown'],\n  setup(props, { emit }) {\n    const { currentTabbedId, loop, onItemFocus, onItemShiftTab } = inject(\n      ROVING_FOCUS_GROUP_INJECTION_KEY,\n      undefined\n    )!\n\n    const { getItems } = inject(\n      ROVING_FOCUS_COLLECTION_INJECTION_KEY,\n      undefined\n    )!\n\n    const id = useId()\n    const rovingFocusGroupItemRef = ref<HTMLElement | null>(null)\n\n    const handleMousedown = composeEventHandlers(\n      (e: Event) => {\n        emit('mousedown', e)\n      },\n      (e) => {\n        if (!props.focusable) {\n          e.preventDefault()\n        } else {\n          onItemFocus(unref(id))\n        }\n      }\n    )\n\n    const handleFocus = composeEventHandlers(\n      (e: Event) => {\n        emit('focus', e)\n      },\n      () => {\n        onItemFocus(unref(id))\n      }\n    )\n\n    const handleKeydown = composeEventHandlers(\n      (e: Event) => {\n        emit('keydown', e)\n      },\n      (e) => {\n        const { key, shiftKey, target, currentTarget } = e as KeyboardEvent\n        if (key === EVENT_CODE.tab && shiftKey) {\n          onItemShiftTab()\n          return\n        }\n        if (target !== currentTarget) return\n        const focusIntent = getFocusIntent(e as KeyboardEvent)\n\n        if (focusIntent) {\n          e.preventDefault()\n          const items = getItems<typeof props>().filter(\n            (item) => item.focusable\n          )\n\n          let elements = items.map((item) => item.ref!)\n\n          switch (focusIntent) {\n            case 'last': {\n              elements.reverse()\n              break\n            }\n            case 'prev':\n            case 'next': {\n              if (focusIntent === 'prev') {\n                elements.reverse()\n              }\n              const currentIdx = elements.indexOf(currentTarget as HTMLElement)\n              elements = loop.value\n                ? reorderArray(elements, currentIdx + 1)\n                : elements.slice(currentIdx + 1)\n              break\n            }\n            default: {\n              break\n            }\n          }\n\n          nextTick(() => {\n            focusFirst(elements)\n          })\n        }\n      }\n    )\n\n    const isCurrentTab = computed(() => currentTabbedId.value === unref(id))\n\n    provide(ROVING_FOCUS_GROUP_ITEM_INJECTION_KEY, {\n      rovingFocusGroupItemRef,\n      tabIndex: computed(() => (unref(isCurrentTab) ? 0 : -1)),\n      handleMousedown,\n      handleFocus,\n      handleKeydown,\n    })\n\n    return {\n      id,\n      handleKeydown,\n      handleFocus,\n      handleMousedown,\n    }\n  },\n})\n</script>\n"],"names":["defineComponent","ElRovingFocusCollectionItem","inject","ROVING_FOCUS_GROUP_INJECTION_KEY","ROVING_FOCUS_COLLECTION_INJECTION_KEY","useId","ref","composeEventHandlers","unref","EVENT_CODE","getFocusIntent","reorderArray","nextTick","focusFirst","computed","provide","ROVING_FOCUS_GROUP_ITEM_INJECTION_KEY","_createBlock","_renderSlot"],"mappings":";;;;;;;;;;;;;;;;AAiCA,MAAK,YAAaA,mBAAa,CAAA;AAAA,EAC7B,UAAY,EAAA;AAAA,iCACVC,iCAAA;AAAA,GACF;AAAA,EACA,KAAO,EAAA;AAAA,IACL,SAAW,EAAA;AAAA,MACT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