roving-focus-item.mjs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { defineComponent, inject, ref, computed, unref, provide, resolveComponent, openBlock, createBlock, withCtx, renderSlot } from 'vue';
  2. import { ElCollectionItem } from './roving-focus-group.mjs';
  3. import { ROVING_FOCUS_GROUP_INJECTION_KEY, ROVING_FOCUS_GROUP_ITEM_INJECTION_KEY } from './tokens.mjs';
  4. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  5. import { useId } from '../../../hooks/use-id/index.mjs';
  6. import { composeEventHandlers, getEventCode } from '../../../utils/dom/event.mjs';
  7. import { EVENT_CODE } from '../../../constants/aria.mjs';
  8. const _sfc_main = defineComponent({
  9. components: {
  10. ElRovingFocusCollectionItem: ElCollectionItem
  11. },
  12. props: {
  13. focusable: {
  14. type: Boolean,
  15. default: true
  16. },
  17. active: Boolean
  18. },
  19. emits: ["mousedown", "focus", "keydown"],
  20. setup(props, { emit }) {
  21. const { currentTabbedId, onItemFocus, onItemShiftTab, onKeydown } = inject(
  22. ROVING_FOCUS_GROUP_INJECTION_KEY,
  23. void 0
  24. );
  25. const id = useId();
  26. const rovingFocusGroupItemRef = ref();
  27. const handleMousedown = composeEventHandlers(
  28. (e) => {
  29. emit("mousedown", e);
  30. },
  31. (e) => {
  32. if (!props.focusable) {
  33. e.preventDefault();
  34. } else {
  35. onItemFocus(unref(id));
  36. }
  37. }
  38. );
  39. const handleFocus = composeEventHandlers(
  40. (e) => {
  41. emit("focus", e);
  42. },
  43. () => {
  44. onItemFocus(unref(id));
  45. }
  46. );
  47. const handleKeydown = composeEventHandlers(
  48. (e) => {
  49. emit("keydown", e);
  50. },
  51. (e) => {
  52. const { shiftKey, target, currentTarget } = e;
  53. const code = getEventCode(e);
  54. if (code === EVENT_CODE.tab && shiftKey) {
  55. onItemShiftTab();
  56. return;
  57. }
  58. if (target !== currentTarget)
  59. return;
  60. onKeydown(e);
  61. }
  62. );
  63. const isCurrentTab = computed(() => currentTabbedId.value === unref(id));
  64. provide(ROVING_FOCUS_GROUP_ITEM_INJECTION_KEY, {
  65. rovingFocusGroupItemRef,
  66. tabIndex: computed(() => unref(isCurrentTab) ? 0 : -1),
  67. handleMousedown,
  68. handleFocus,
  69. handleKeydown
  70. });
  71. return {
  72. id,
  73. handleKeydown,
  74. handleFocus,
  75. handleMousedown
  76. };
  77. }
  78. });
  79. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  80. const _component_el_roving_focus_collection_item = resolveComponent("el-roving-focus-collection-item");
  81. return openBlock(), createBlock(_component_el_roving_focus_collection_item, {
  82. id: _ctx.id,
  83. focusable: _ctx.focusable,
  84. active: _ctx.active
  85. }, {
  86. default: withCtx(() => [
  87. renderSlot(_ctx.$slots, "default")
  88. ]),
  89. _: 3
  90. }, 8, ["id", "focusable", "active"]);
  91. }
  92. var ElRovingFocusItem = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "/home/runner/work/element-plus/element-plus/packages/components/roving-focus-group/src/roving-focus-item.vue"]]);
  93. export { ElRovingFocusItem as default };
  94. //# sourceMappingURL=roving-focus-item.mjs.map