From 17d052ee50aa0d047ca52c65c9396228e9ec6b09 Mon Sep 17 00:00:00 2001 From: Matteo <193476828+mzner@users.noreply.github.com> Date: Mon, 5 Oct 2026 13:09:34 +0200 Subject: [PATCH] fix(web): [OCISDEV-1485] restore focus after language preference change (#13045) Selecting a language dropped focus to the page body and the save confirmation went unannounced, because the select's own best-effort refocus raced against the handler's translation reload and network save. Restore focus explicitly once that work settles, before announcing the save. --- ...fix-restore-focus-after-language-change.md | 14 ++++++ .../src/components/OcDrop/OcDrop.spec.ts | 48 +++++++++++++++++++ .../src/components/OcDrop/OcDrop.vue | 6 ++- .../src/components/OcSelect/OcSelect.spec.ts | 27 ++++++++++- .../src/components/OcSelect/OcSelect.vue | 24 ++++++++++ .../web-runtime/src/pages/account.vue | 23 ++++++++- 6 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 changelog/unreleased/bugfix-restore-focus-after-language-change.md diff --git a/changelog/unreleased/bugfix-restore-focus-after-language-change.md b/changelog/unreleased/bugfix-restore-focus-after-language-change.md new file mode 100644 index 00000000000..82cda9c1979 --- /dev/null +++ b/changelog/unreleased/bugfix-restore-focus-after-language-change.md @@ -0,0 +1,14 @@ +Bugfix: Restore focus after changing the account language + +Selecting a new language in the account page's Language dropdown dropped +keyboard focus to the page body instead of keeping it on the dropdown, and +the confirmation message was not announced to screen readers afterward. + +The language change triggers a fair amount of background work (reloading +translations, switching the active language, saving the preference) before +it finishes, which raced against the dropdown's own best-effort focus +handling. Focus is now explicitly restored only once that work has fully +settled, and before the confirmation message is shown, so the announcement +is no longer cancelled by a focus change happening right after it. + +https://github.com/owncloud/ocis/pull/13045 diff --git a/web/packages/design-system/src/components/OcDrop/OcDrop.spec.ts b/web/packages/design-system/src/components/OcDrop/OcDrop.spec.ts index 2113e463677..f61e958fbfd 100644 --- a/web/packages/design-system/src/components/OcDrop/OcDrop.spec.ts +++ b/web/packages/design-system/src/components/OcDrop/OcDrop.spec.ts @@ -163,4 +163,52 @@ describe('OcDrop', () => { expect(hide).toHaveBeenCalled() }) }) + + describe('focus leaving the drop', () => { + const mountWithFocusableItem = () => { + document.body.innerHTML = '' + const wrapper = mount( + { + template: `
+ + + + + +
`, + components: { 'oc-drop': Drop } + }, + { attachTo: document.body } + ) + const tippy = wrapper.findComponent({ name: 'oc-drop' }).vm.tippyInstance + const wait = () => new Promise((resolve) => setTimeout(resolve, 100)) + const open = async () => { + await wrapper.find('#trigger').trigger('click') + await wait() + } + return { wrapper, tippy, open, wait } + } + + it('hides when focus moves outside of the drop', async () => { + const { tippy, open } = mountWithFocusableItem() + await open() + document.getElementById('item').focus() + document.getElementById('elsewhere').focus() + + expect(tippy.state.isVisible).toBe(false) + }) + + it('closes instead of reopening when the trigger is clicked while focus is inside', async () => { + const { wrapper, tippy, open, wait } = mountWithFocusableItem() + await open() + document.getElementById('item').focus() + + // clicking the trigger focuses it before the click toggles the drop + document.getElementById('trigger').focus() + await wrapper.find('#trigger').trigger('click') + await wait() + + expect(tippy.state.isVisible).toBe(false) + }) + }) }) diff --git a/web/packages/design-system/src/components/OcDrop/OcDrop.vue b/web/packages/design-system/src/components/OcDrop/OcDrop.vue index b64dfca0a86..627015c1a7c 100644 --- a/web/packages/design-system/src/components/OcDrop/OcDrop.vue +++ b/web/packages/design-system/src/components/OcDrop/OcDrop.vue @@ -185,7 +185,11 @@ const onFocusOut = (event: FocusEvent) => { if (!tippyBox) { return } - const focusLeft = event.relatedTarget && !tippyBox.contains(event.relatedTarget as Node) + const relatedTarget = event.relatedTarget as Node | null + // focus moving to the trigger means it's being clicked, which toggles the drop + // itself. Hiding here too would make that click reopen it instead of closing it + const focusMovedToTrigger = relatedTarget && unref(triggerEl)?.contains(relatedTarget) + const focusLeft = relatedTarget && !tippyBox.contains(relatedTarget) && !focusMovedToTrigger if (focusLeft) { hide() } diff --git a/web/packages/design-system/src/components/OcSelect/OcSelect.spec.ts b/web/packages/design-system/src/components/OcSelect/OcSelect.spec.ts index 2f36ac7a0b0..a5ebd10c1a0 100644 --- a/web/packages/design-system/src/components/OcSelect/OcSelect.spec.ts +++ b/web/packages/design-system/src/components/OcSelect/OcSelect.spec.ts @@ -1,3 +1,4 @@ +import { nextTick } from 'vue' import OcSelect from './OcSelect.vue' import { defaultPlugins, mount, PartialComponentProps } from '../../testing' @@ -83,6 +84,28 @@ describe('OcSelect', () => { expect(wrapper.find(selectors.deselectLockIcon).exists()).toBeTruthy() }) }) + describe('focus', () => { + it('restores focus to the search input after selecting an option', async () => { + const options = [{ label: 'label1' }, { label: 'label2' }] + const wrapper = getWrapper({ options }, { attachTo: document.body }) + wrapper.findComponent({ name: 'VueSelect' }).vm.$emit('option:selected', options[0]) + await nextTick() + expect(document.activeElement).toBe(wrapper.find(selectors.searchInput).element) + wrapper.unmount() + }) + it('does not focus the search input when deselecting via the remove button', async () => { + const options = [{ label: 'label1' }, { label: 'label2' }] + const wrapper = getWrapper( + { options, modelValue: options[0], multiple: true }, + { attachTo: document.body } + ) + await wrapper.find(selectors.deselectBtn).trigger('click') + await nextTick() + expect(wrapper.emitted('update:modelValue')).toBeTruthy() + expect(document.activeElement).not.toBe(wrapper.find(selectors.searchInput).element) + wrapper.unmount() + }) + }) describe('accessibility', () => { it('sets the combobox role and an accessible name on the focusable search input', () => { const wrapper = getWrapper({ label: 'Language' }) @@ -130,9 +153,11 @@ function getWrapper( getOptionLabel: (o: T) => string modelValue: T } - > = {} + > = {}, + { attachTo }: { attachTo?: HTMLElement } = {} ) { return mount(OcSelect, { + attachTo, props: { label: 'Select label', ...props diff --git a/web/packages/design-system/src/components/OcSelect/OcSelect.vue b/web/packages/design-system/src/components/OcSelect/OcSelect.vue index 99915684ba3..f64eb2aaab4 100644 --- a/web/packages/design-system/src/components/OcSelect/OcSelect.vue +++ b/web/packages/design-system/src/components/OcSelect/OcSelect.vue @@ -21,6 +21,7 @@ :map-keydown="selectMapKeydown" v-bind="additionalAttributes" @update:model-value="$emit('update:modelValue', $event)" + @option:selected="onSelectOptionSelected()" @click="onSelectClick()" @search:blur="onSelectBlur()" @keydown="onSelectKeyDown($event)" @@ -281,6 +282,16 @@ const onSelectBlur = () => { setDropdownEnabled(false) } +const onSelectOptionSelected = async () => { + // vue-select blurs the search input itself right after a mouse-click + // selection, dropping focus to the page. Restore it once that blur has + // run so the combobox keeps focus, as the APG combobox pattern expects. + // Deliberately not done on deselect (e.g. a chip's remove button): that + // never blurs, and focusing the search input there would pop the dropdown open. + await nextTick() + unref(select)?.searchEl?.focus() +} + /** * Sets the outline for the highlighted option. This needs to be applied when * navigating via keyboard because of a11y. @@ -424,6 +435,19 @@ onBeforeUnmount(() => { window.removeEventListener('resize', setDropdownPosition) } }) + +/** + * Focuses the combobox's search input. Exposed for consumers whose + * update:modelValue handler does enough async/reactive work of its own + * (e.g. reloading translations) that it needs to restore focus itself, + * once that work has actually settled, instead of relying on OcSelect's + * own best-effort refocus racing against it. + */ +const focus = () => { + unref(select)?.searchEl?.focus() +} + +defineExpose({ focus })