roving-focus-group-impl.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var core = require('@vueuse/core');
  5. var rovingFocusGroup = require('./roving-focus-group2.js');
  6. var tokens = require('./tokens.js');
  7. var utils = require('./utils.js');
  8. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  9. var event = require('../../../utils/dom/event.js');
  10. const CURRENT_TAB_ID_CHANGE_EVT = "currentTabIdChange";
  11. const ENTRY_FOCUS_EVT = "rovingFocusGroup.entryFocus";
  12. const EVT_OPTS = { bubbles: false, cancelable: true };
  13. const _sfc_main = vue.defineComponent({
  14. name: "ElRovingFocusGroupImpl",
  15. inheritAttrs: false,
  16. props: rovingFocusGroup.rovingFocusGroupProps,
  17. emits: [CURRENT_TAB_ID_CHANGE_EVT, "entryFocus"],
  18. setup(props, { emit }) {
  19. var _a;
  20. const currentTabbedId = vue.ref((_a = props.currentTabId || props.defaultCurrentTabId) != null ? _a : null);
  21. const isBackingOut = vue.ref(false);
  22. const isClickFocus = vue.ref(false);
  23. const rovingFocusGroupRef = vue.ref();
  24. const { getItems } = vue.inject(rovingFocusGroup.ROVING_FOCUS_COLLECTION_INJECTION_KEY, void 0);
  25. const rovingFocusGroupRootStyle = vue.computed(() => {
  26. return [
  27. {
  28. outline: "none"
  29. },
  30. props.style
  31. ];
  32. });
  33. const onItemFocus = (tabbedId) => {
  34. emit(CURRENT_TAB_ID_CHANGE_EVT, tabbedId);
  35. };
  36. const onItemShiftTab = () => {
  37. isBackingOut.value = true;
  38. };
  39. const onMousedown = event.composeEventHandlers((e) => {
  40. var _a2;
  41. (_a2 = props.onMousedown) == null ? void 0 : _a2.call(props, e);
  42. }, () => {
  43. isClickFocus.value = true;
  44. });
  45. const onFocus = event.composeEventHandlers((e) => {
  46. var _a2;
  47. (_a2 = props.onFocus) == null ? void 0 : _a2.call(props, e);
  48. }, (e) => {
  49. const isKeyboardFocus = !vue.unref(isClickFocus);
  50. const { target, currentTarget } = e;
  51. if (target === currentTarget && isKeyboardFocus && !vue.unref(isBackingOut)) {
  52. const entryFocusEvt = new Event(ENTRY_FOCUS_EVT, EVT_OPTS);
  53. currentTarget == null ? void 0 : currentTarget.dispatchEvent(entryFocusEvt);
  54. if (!entryFocusEvt.defaultPrevented) {
  55. const items = getItems().filter((item) => item.focusable);
  56. const activeItem = items.find((item) => item.active);
  57. const currentItem = items.find((item) => item.id === vue.unref(currentTabbedId));
  58. const candidates = [activeItem, currentItem, ...items].filter(Boolean);
  59. const candidateNodes = candidates.map((item) => item.ref);
  60. utils.focusFirst(candidateNodes);
  61. }
  62. }
  63. isClickFocus.value = false;
  64. });
  65. const onBlur = event.composeEventHandlers((e) => {
  66. var _a2;
  67. (_a2 = props.onBlur) == null ? void 0 : _a2.call(props, e);
  68. }, () => {
  69. isBackingOut.value = false;
  70. });
  71. const handleEntryFocus = (...args) => {
  72. emit("entryFocus", ...args);
  73. };
  74. vue.provide(tokens.ROVING_FOCUS_GROUP_INJECTION_KEY, {
  75. currentTabbedId: vue.readonly(currentTabbedId),
  76. loop: vue.toRef(props, "loop"),
  77. tabIndex: vue.computed(() => {
  78. return vue.unref(isBackingOut) ? -1 : 0;
  79. }),
  80. rovingFocusGroupRef,
  81. rovingFocusGroupRootStyle,
  82. orientation: vue.toRef(props, "orientation"),
  83. dir: vue.toRef(props, "dir"),
  84. onItemFocus,
  85. onItemShiftTab,
  86. onBlur,
  87. onFocus,
  88. onMousedown
  89. });
  90. vue.watch(() => props.currentTabId, (val) => {
  91. currentTabbedId.value = val != null ? val : null;
  92. });
  93. core.useEventListener(rovingFocusGroupRef, ENTRY_FOCUS_EVT, handleEntryFocus);
  94. }
  95. });
  96. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  97. return vue.renderSlot(_ctx.$slots, "default");
  98. }
  99. var ElRovingFocusGroupImpl = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["render", _sfc_render], ["__file", "roving-focus-group-impl.vue"]]);
  100. exports["default"] = ElRovingFocusGroupImpl;
  101. //# sourceMappingURL=roving-focus-group-impl.js.map