roving-focus-item.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var rovingFocusGroup = require('./roving-focus-group2.js');
  5. var tokens = require('./tokens.js');
  6. var utils = require('./utils.js');
  7. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  8. var index = require('../../../hooks/use-id/index.js');
  9. var event = require('../../../utils/dom/event.js');
  10. var aria = require('../../../constants/aria.js');
  11. const _sfc_main = vue.defineComponent({
  12. components: {
  13. ElRovingFocusCollectionItem: rovingFocusGroup.ElCollectionItem
  14. },
  15. props: {
  16. focusable: {
  17. type: Boolean,
  18. default: true
  19. },
  20. active: {
  21. type: Boolean,
  22. default: false
  23. }
  24. },
  25. emits: ["mousedown", "focus", "keydown"],
  26. setup(props, { emit }) {
  27. const { currentTabbedId, loop, onItemFocus, onItemShiftTab } = vue.inject(tokens.ROVING_FOCUS_GROUP_INJECTION_KEY, void 0);
  28. const { getItems } = vue.inject(rovingFocusGroup.ROVING_FOCUS_COLLECTION_INJECTION_KEY, void 0);
  29. const id = index.useId();
  30. const rovingFocusGroupItemRef = vue.ref();
  31. const handleMousedown = event.composeEventHandlers((e) => {
  32. emit("mousedown", e);
  33. }, (e) => {
  34. if (!props.focusable) {
  35. e.preventDefault();
  36. } else {
  37. onItemFocus(vue.unref(id));
  38. }
  39. });
  40. const handleFocus = event.composeEventHandlers((e) => {
  41. emit("focus", e);
  42. }, () => {
  43. onItemFocus(vue.unref(id));
  44. });
  45. const handleKeydown = event.composeEventHandlers((e) => {
  46. emit("keydown", e);
  47. }, (e) => {
  48. const { code, shiftKey, target, currentTarget } = e;
  49. if (code === aria.EVENT_CODE.tab && shiftKey) {
  50. onItemShiftTab();
  51. return;
  52. }
  53. if (target !== currentTarget)
  54. return;
  55. const focusIntent = utils.getFocusIntent(e);
  56. if (focusIntent) {
  57. e.preventDefault();
  58. const items = getItems().filter((item) => item.focusable);
  59. let elements = items.map((item) => item.ref);
  60. switch (focusIntent) {
  61. case "last": {
  62. elements.reverse();
  63. break;
  64. }
  65. case "prev":
  66. case "next": {
  67. if (focusIntent === "prev") {
  68. elements.reverse();
  69. }
  70. const currentIdx = elements.indexOf(currentTarget);
  71. elements = loop.value ? utils.reorderArray(elements, currentIdx + 1) : elements.slice(currentIdx + 1);
  72. break;
  73. }
  74. }
  75. vue.nextTick(() => {
  76. utils.focusFirst(elements);
  77. });
  78. }
  79. });
  80. const isCurrentTab = vue.computed(() => currentTabbedId.value === vue.unref(id));
  81. vue.provide(tokens.ROVING_FOCUS_GROUP_ITEM_INJECTION_KEY, {
  82. rovingFocusGroupItemRef,
  83. tabIndex: vue.computed(() => vue.unref(isCurrentTab) ? 0 : -1),
  84. handleMousedown,
  85. handleFocus,
  86. handleKeydown
  87. });
  88. return {
  89. id,
  90. handleKeydown,
  91. handleFocus,
  92. handleMousedown
  93. };
  94. }
  95. });
  96. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  97. const _component_el_roving_focus_collection_item = vue.resolveComponent("el-roving-focus-collection-item");
  98. return vue.openBlock(), vue.createBlock(_component_el_roving_focus_collection_item, {
  99. id: _ctx.id,
  100. focusable: _ctx.focusable,
  101. active: _ctx.active
  102. }, {
  103. default: vue.withCtx(() => [
  104. vue.renderSlot(_ctx.$slots, "default")
  105. ]),
  106. _: 3
  107. }, 8, ["id", "focusable", "active"]);
  108. }
  109. var ElRovingFocusItem = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["render", _sfc_render], ["__file", "roving-focus-item.vue"]]);
  110. exports["default"] = ElRovingFocusItem;
  111. //# sourceMappingURL=roving-focus-item.js.map