From f01a871f8e155108f30e8402e183ab64ef2d387c Mon Sep 17 00:00:00 2001 From: Przemek Spaczek Date: Mon, 15 Jan 2024 15:44:51 +0100 Subject: [PATCH] fix: no focus after open new page (#330) * fix: no focus after open new page * feat: focus menu button on back * fix(ts): tabindex in menuItems * fix: use nextTick to prevent to fast focusing * fix(ts): solve TS18047 and TS2464 errors * fix(ts): solve TS7053 error --- .../UiHorizontalPaging/UiHorizontalPaging.vue | 26 +++++++++++++++++++ .../_internal/UiHorizontalPagingItem.vue | 20 ++++++++++++++ src/components/organisms/UiMenu/UiMenu.vue | 8 +++++- src/utilities/helpers/focus-element/index.ts | 2 +- 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/components/organisms/UiHorizontalPaging/UiHorizontalPaging.vue b/src/components/organisms/UiHorizontalPaging/UiHorizontalPaging.vue index e942aec4f..3d975605c 100644 --- a/src/components/organisms/UiHorizontalPaging/UiHorizontalPaging.vue +++ b/src/components/organisms/UiHorizontalPaging/UiHorizontalPaging.vue @@ -58,6 +58,7 @@ v-bind="{ items: menuItems }" > @@ -96,10 +97,13 @@ import { computed, provide, inject, + watch, type ComputedRef, type WritableComputedRef, type Ref, + nextTick, } from 'vue'; +import { focusElement } from '../../../utilities/helpers'; import type { Icon, DefineAttrsProps, @@ -226,9 +230,31 @@ const menuItems = computed(() => itemsAsArray.value.map((i item, ]; }, + ...(isActive.value ? { tabindex: '-1' } : {}), ...rest, }; })); +const menu = ref | null>(null); +const menuButtons = computed < Record>(() => { + if (!menu.value) return {}; + return itemsAsArray.value.reduce((elements, { name }, order) => { + if (!name + || !menu.value + || !menu.value.menuItems) { + return elements; + } + return { + ...elements, + [name]: menu.value.menuItems[order].$el.querySelector('button'), + }; + }, {}); +}); +watch(activeItemName, async (moveTo, backFrom) => { + if (backFrom) { + await nextTick(); + focusElement(menuButtons.value[backFrom]); + } +});