Skip to content

Instantly share code, notes, and snippets.

@tadeaspetak
Last active February 27, 2022 05:44
Show Gist options
  • Save tadeaspetak/c54534a7eacdcc95d2761be13df54441 to your computer and use it in GitHub Desktop.
Save tadeaspetak/c54534a7eacdcc95d2761be13df54441 to your computer and use it in GitHub Desktop.
// resolve focus on opening and closing
useEffect(() => {
if (open) {
previousFocus.current = (document.activeElement as HTMLElement) ?? null;
nextFocus(getFocusableElements(container.current));
} else {
previousFocus.current?.focus?.();
previousFocus.current = null;
}
}, [open, portal]);
// ... in our key listener, manage `tab` presses
switch (e.key) {
case "Tab": {
e.preventDefault();
nextFocus(getFocusableElements(container.current), !e.shiftKey);
break;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment