fix(a11y): opening a filter list from the keyboard moves focus to its options (#33025)

This commit is contained in:
G30 2026-10-24 16:13:38 -04:00 • committed by GitHub
parent 988602e135
commit 9aae241770
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -105,11 +105,21 @@
}
}
async function toggleOpen() {
async function toggleOpen(focusContent = false) {
open = !open;
if (open) {
await tick();
positionContent();
if (focusContent) {
contentEl?.querySelector<HTMLElement>('button:not([disabled]), a[href], input')?.focus();
}
}
}
function handleTriggerKeydown(event) {
if (event.target === triggerEl && (event.key === 'Enter' || event.key === ' ')) {
event.preventDefault();
toggleOpen(true);
}
}
@ -123,15 +133,19 @@
function handleKeydown(event) {
if (event.key === 'Escape' && open) {
const restoreFocus = contentEl?.contains(document.activeElement);
open = false;
onClose();
if (restoreFocus) triggerEl?.focus();
}
}
export function selectItem(item) {
const restoreFocus = contentEl?.contains(document.activeElement);
value = item.value;
open = false;
onChange(value);
if (restoreFocus) triggerEl?.focus();
}
</script>
@ -147,7 +161,8 @@
class="focus-ring {triggerClass}"
type="button"
aria-expanded={open}
on:click={toggleOpen}
on:click={() => toggleOpen()}
on:keydown={handleTriggerKeydown}
>
<slot name="trigger" {selectedLabel} {open}>
<span class={labelClass}>