Add keyboard motion for modal

This commit is contained in:
Arnaud 2024-11-11 09:32:37 +07:00
parent 130157af51
commit 3b275e723d
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
1 changed files with 14 additions and 0 deletions

View File

@ -89,6 +89,20 @@ export function Modal({
setTimeout(onClose, 250); setTimeout(onClose, 250);
}; };
useEffect(() => {
const onKeyPress = (event: Event) => {
const e = event as KeyboardEvent;
if (e.key === "Escape") {
setInternalOpen(false);
setTimeout(onClose, 250);
}
};
document.addEventListener("keydown", onKeyPress);
return () => document.removeEventListener("keydown", onKeyPress);
}, [setInternalOpen, onClose]);
return ( return (
<div <div
className={classnames( className={classnames(