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.
This commit is contained in:
1 parent
07a1dbf9c3
commit
17d052ee50
6 files changed
+138
-4
No files matched your search
@@ -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
|
||||
@@ -163,4 +163,52 @@ describe('OcDrop', () => {
|
||||
expect(hide).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('focus leaving the drop', () => {
|
||||
const mountWithFocusableItem = () => {
|
||||
document.body.innerHTML = ''
|
||||
const wrapper = mount(
|
||||
{
|
||||
template: `<div>
|
||||
<button id="trigger">trigger</button>
|
||||
<input id="elsewhere" />
|
||||
<oc-drop toggle="#trigger">
|
||||
<button id="item">item</button>
|
||||
</oc-drop>
|
||||
</div>`,
|
||||
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)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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<T>(
|
||||
getOptionLabel: (o: T) => string
|
||||
modelValue: T
|
||||
}
|
||||
> = {}
|
||||
> = {},
|
||||
{ attachTo }: { attachTo?: HTMLElement } = {}
|
||||
) {
|
||||
return mount(OcSelect, {
|
||||
attachTo,
|
||||
props: {
|
||||
label: 'Select label',
|
||||
...props
|
||||
|
||||
@@ -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 })
|
||||
</script>
|
||||
<style lang="scss">
|
||||
.vs--disabled {
|
||||
|
||||
@@ -166,6 +166,7 @@
|
||||
<oc-td data-testid="language">
|
||||
<oc-select
|
||||
v-if="languageOptions"
|
||||
ref="languageSelectRef"
|
||||
:model-value="selectedLanguageValue"
|
||||
:label="$gettext('Language')"
|
||||
:label-hidden="true"
|
||||
@@ -354,7 +355,7 @@
|
||||
import { storeToRefs } from 'pinia'
|
||||
import EditPasswordModal from '../components/EditPasswordModal.vue'
|
||||
import { SettingsBundle, LanguageOption, SettingsValue } from '../helpers/settings'
|
||||
import { computed, defineComponent, onMounted, onBeforeUnmount, unref, ref } from 'vue'
|
||||
import { computed, defineComponent, onMounted, onBeforeUnmount, unref, ref, nextTick } from 'vue'
|
||||
import {
|
||||
useAppsStore,
|
||||
useAuthStore,
|
||||
@@ -415,6 +416,7 @@ export default defineComponent({
|
||||
const graphUser = ref<User>()
|
||||
const accountBundle = ref<SettingsBundle>()
|
||||
const selectedLanguageValue = ref<LanguageOption>()
|
||||
const languageSelectRef = ref<{ focus: () => void }>()
|
||||
const disableEmailNotificationsValue = ref<boolean>()
|
||||
const viewOptionWebDavDetailsValue = ref<boolean>(resourcesStore.areWebDavDetailsShown)
|
||||
const { dispatchModal } = useModals()
|
||||
@@ -647,7 +649,23 @@ export default defineComponent({
|
||||
loadAccountBundleTask.cancelAll()
|
||||
}
|
||||
|
||||
loadAccountBundleTask.perform()
|
||||
// await it instead of firing and forgetting: it reactively replaces
|
||||
// accountBundle, and we need that re-render to be done before we try
|
||||
// to restore focus below, not racing it.
|
||||
await loadAccountBundleTask.perform()
|
||||
await nextTick()
|
||||
|
||||
/*
|
||||
* Reloading translations and switching language.current above
|
||||
* re-render most of the page, which can leave the select's own
|
||||
* focus-restore targeting a stale, already-replaced search input.
|
||||
* Refocus explicitly now that everything has settled, before
|
||||
* announcing the save, so the announcement isn't cancelled by a
|
||||
* focus change right after it. Don't touch focus again after this,
|
||||
* even defensively: a focus change right as the announcement below
|
||||
* is about to be read is exactly what cancels it.
|
||||
*/
|
||||
unref(languageSelectRef)?.focus()
|
||||
showMessage({ title: $gettext('Preference saved.') })
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
@@ -855,6 +873,7 @@ export default defineComponent({
|
||||
languageOptions,
|
||||
extensionPointsWithUserPreferences,
|
||||
selectedLanguageValue,
|
||||
languageSelectRef,
|
||||
updateSelectedLanguage,
|
||||
updateDisableEmailNotifications,
|
||||
updateViewOptionsWebDavDetails,
|
||||
|
||||
Reference in new issue
Block a user