{"version":3,"file":"useDropdown.js","sources":["../../../../../../packages/components/dropdown/src/useDropdown.ts"],"sourcesContent":["// @ts-nocheck\nimport { computed, inject, ref } from 'vue'\nimport { addClass } from '@element-plus/utils'\nimport { EVENT_CODE } from '@element-plus/constants'\nimport { useId, useNamespace } from '@element-plus/hooks'\nimport type { Nullable } from '@element-plus/utils'\nimport type { IElDropdownInstance } from './dropdown'\n\nexport const useDropdown = () => {\n const elDropdown = inject('elDropdown', {})\n const _elDropdownSize = computed(() => elDropdown?.dropdownSize)\n\n return {\n elDropdown,\n _elDropdownSize,\n }\n}\n\nexport const initDropdownDomEvent = (\n dropdownChildren,\n triggerElm,\n _instance\n) => {\n const ns = useNamespace('dropdown')\n const menuItems = ref>(null)\n const menuItemsArray = ref>(null)\n const dropdownElm = ref>(null)\n const listId = useId()\n dropdownElm.value = dropdownChildren?.subTree.el\n\n function removeTabindex() {\n triggerElm.setAttribute('tabindex', '-1')\n menuItemsArray.value?.forEach((item) => item.setAttribute('tabindex', '-1'))\n }\n\n function resetTabindex(ele) {\n removeTabindex()\n ele?.setAttribute('tabindex', '0')\n }\n\n function handleTriggerKeyDown(ev: KeyboardEvent) {\n const code = ev.code\n if ([EVENT_CODE.up, EVENT_CODE.down].includes(code)) {\n removeTabindex()\n resetTabindex(menuItems.value[0])\n menuItems.value[0].focus()\n ev.preventDefault()\n ev.stopPropagation()\n } else if (code === EVENT_CODE.enter) {\n _instance.handleClick()\n } else if ([EVENT_CODE.tab, EVENT_CODE.esc].includes(code)) {\n _instance.hide()\n }\n }\n\n function handleItemKeyDown(ev) {\n const code = ev.code\n const target = ev.target\n const currentIndex = menuItemsArray.value.indexOf(target)\n const max = menuItemsArray.value.length - 1\n let nextIndex\n if ([EVENT_CODE.up, EVENT_CODE.down].includes(code)) {\n if (code === EVENT_CODE.up) {\n nextIndex = currentIndex !== 0 ? currentIndex - 1 : 0\n } else {\n nextIndex = currentIndex < max ? currentIndex + 1 : max\n }\n removeTabindex()\n resetTabindex(menuItems.value[nextIndex])\n menuItems.value[nextIndex].focus()\n ev.preventDefault()\n ev.stopPropagation()\n } else if (code === EVENT_CODE.enter) {\n triggerElmFocus()\n target.click()\n if (_instance.props.hideOnClick) {\n _instance.hide()\n }\n } else if ([EVENT_CODE.tab, EVENT_CODE.esc].includes(code)) {\n _instance.hide()\n triggerElmFocus()\n }\n }\n\n function initAria() {\n dropdownElm.value.setAttribute('id', listId.value)\n triggerElm.setAttribute('aria-haspopup', 'list')\n triggerElm.setAttribute('aria-controls', listId.value)\n if (!_instance.props.splitButton) {\n triggerElm.setAttribute('role', 'button')\n triggerElm.setAttribute('tabindex', _instance.props.tabindex)\n addClass(triggerElm, ns.b('selfdefine'))\n }\n }\n\n function initEvent() {\n triggerElm?.addEventListener('keydown', handleTriggerKeyDown)\n dropdownElm.value?.addEventListener('keydown', handleItemKeyDown, true)\n }\n\n function initDomOperation() {\n menuItems.value = dropdownElm.value.querySelectorAll(\n \"[tabindex='-1']\"\n ) as unknown as HTMLButtonElement[]\n menuItemsArray.value = Array.from(menuItems.value)\n\n initEvent()\n initAria()\n }\n\n function triggerElmFocus() {\n triggerElm.focus()\n }\n\n initDomOperation()\n}\n"],"names":["inject","computed","useNamespace","ref","useId","EVENT_CODE","addClass"],"mappings":";;;;;;;;;;;;;AAIY,MAAC,WAAW,GAAG,MAAM;AACjC,EAAE,MAAM,UAAU,GAAGA,UAAM,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;AAC9C,EAAE,MAAM,eAAe,GAAGC,YAAQ,CAAC,MAAM,UAAU,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,UAAU,CAAC,YAAY,CAAC,CAAC;AAChG,EAAE,OAAO;AACT,IAAI,UAAU;AACd,IAAI,eAAe;AACnB,GAAG,CAAC;AACJ,EAAE;AACU,MAAC,oBAAoB,GAAG,CAAC,gBAAgB,EAAE,UAAU,EAAE,SAAS,KAAK;AACjF,EAAE,MAAM,EAAE,GAAGC,kBAAY,CAAC,UAAU,CAAC,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