Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(accessibility): fix issue where esc key would not close navigation drawer #1107

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/components/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function Drawer({
onClose = () => null
}: DrawerProps) {
const drawerRef = useRef<HTMLDivElement>(null);
const triggerElement = useRef<HTMLElement | null>(null);

const handleClickOutside = () => {
if (!open) {
Expand All @@ -34,14 +35,15 @@ export default function Drawer({
const listener = (e: KeyboardEvent) => {
if (e.which === 27 && open) {
onClose();
triggerElement.current?.focus();
}
};

document.body.addEventListener('keydown', listener);
return () => {
document.body.removeEventListener('keydown', listener);
};
}, []);
}, [open]);

// Ensure that focusable elements aren't focusable when the drawer is closed
useEffect(() => {
Expand All @@ -54,6 +56,7 @@ export default function Drawer({
(element as HTMLInputElement).tabIndex = -1;
});
} else {
triggerElement.current = document.activeElement as HTMLElement;
Array.from(elements).forEach(element => {
const tabIndexAttr = Number(element.getAttribute('tabindex'));
(element as HTMLInputElement).tabIndex =
Expand Down
Loading