fix(a11y): drawers keep keyboard focus inside and are announced as dialogs (#33547)

This commit is contained in:
G30 authored and GitHub committed 2026-11-01 17:49:45 +09:00
1 parent 3cda94d403
commit 1dd3793d4e
10 files changed
+77 -39

No files matched your search

@@ -26,7 +26,7 @@
});
</script>
<Drawer bind:show className="h-full" zIndexClass="z-99999">
<Drawer bind:show className="h-full" zIndexClass="z-99999" ariaLabel={$i18n.t('Code Editor')}>
<div class="flex h-full flex-col">
<div
class=" sticky top-0 z-30 flex justify-between bg-white px-4.5 pt-3 pb-3 dark:bg-gray-900 dark:text-gray-100"
+59
View File
@@ -2,6 +2,7 @@
import { onDestroy, onMount } from 'svelte';
import { flyAndScale } from '$lib/utils/transitions';
import { fade, fly, slide } from 'svelte/transition';
import * as FocusTrap from 'focus-trap';
import { settings } from '$lib/stores';
import { matchKeybinding, Shortcut } from '$lib/shortcuts';
@@ -13,10 +14,12 @@
export let swipeSide: 'left' | 'right' | undefined = undefined;
export let onClose = () => {};
export let onRequestClose: (() => void) | null = null;
export let ariaLabel: string | undefined = undefined;
const requestClose = () => (onRequestClose ? onRequestClose() : (show = false));
let modalElement = null;
let mounted = false;
let focusTrap: FocusTrap.FocusTrap | null = null;
const handleKeyDown = (event: KeyboardEvent) => {
if (
@@ -34,16 +37,67 @@
return modals.length && modals[modals.length - 1] === modalElement;
};
let handleOutsideInteraction: ((event: Event) => void) | undefined;
let handleModalFocusIn: (() => void) | undefined;
const release = () => {
focusTrap?.deactivate();
focusTrap = null;
if (handleOutsideInteraction) {
document.removeEventListener('pointerdown', handleOutsideInteraction, true);
window.removeEventListener('focusin', handleOutsideInteraction, true);
}
if (handleModalFocusIn) modalElement?.removeEventListener('focusin', handleModalFocusIn);
};
onMount(() => {
mounted = true;
});
$: if (show && modalElement) {
document.body.appendChild(modalElement);
if (!focusTrap) {
focusTrap = FocusTrap.createFocusTrap(modalElement, {
escapeDeactivates: false,
allowOutsideClick: true,
fallbackFocus: modalElement
});
focusTrap.activate();
handleOutsideInteraction = (e) => {
if (
focusTrap &&
modalElement &&
e.target instanceof Node &&
!modalElement.contains(e.target) &&
(e.type === 'pointerdown' ||
(e.target instanceof Element &&
e.target.closest('[role="menu"], [role="listbox"], [role="combobox"]')))
) {
focusTrap.pause();
} else if (
focusTrap &&
modalElement &&
e.target instanceof Element &&
modalElement.compareDocumentPosition(e.target) & Node.DOCUMENT_POSITION_PRECEDING &&
!e.target.closest('[data-sonner-toast]')
) {
focusTrap.unpause();
}
};
handleModalFocusIn = () => {
if (focusTrap) {
focusTrap.unpause();
}
};
document.addEventListener('pointerdown', handleOutsideInteraction, true);
window.addEventListener('focusin', handleOutsideInteraction, true);
modalElement.addEventListener('focusin', handleModalFocusIn);
}
window.addEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'hidden';
} else if (modalElement) {
onClose();
release();
window.removeEventListener('keydown', handleKeyDown);
if (document.body.contains(modalElement)) {
@@ -53,6 +107,7 @@
}
onDestroy(() => {
release();
window.removeEventListener('keydown', handleKeyDown);
show = false;
if (modalElement) {
@@ -69,6 +124,10 @@
{#if show}
<div
bind:this={modalElement}
aria-modal="true"
aria-label={ariaLabel}
role="dialog"
tabindex="-1"
data-swipe-panel={swipeSide}
class="modal fixed right-0 bottom-0 left-0 {zIndexClass} flex h-screen max-h-[100dvh] w-full justify-center overflow-hidden overscroll-contain {swipeSide
? ''
+1 -1
View File
@@ -23,7 +23,7 @@
let inputElement;
</script>
<Drawer bind:show>
<Drawer bind:show ariaLabel={$i18n.t('Input')}>
<div class="flex h-full min-h-screen flex-col" {oninput}>
<div
class=" sticky top-0 z-30 flex justify-between bg-white px-4.5 pt-3 pb-3 dark:bg-gray-900 dark:text-gray-100"