close modals on escape key

This commit is contained in:
Vyacheslav Matyukhin 2022-07-26 20:27:10 +04:00
parent fd36e9ef9e
commit e76a4beb4d
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C

View File

@ -146,12 +146,24 @@ export const Modal: ModalType = ({ children, container, close }) => {
React.useEffect(() => { React.useEffect(() => {
document.body.appendChild(el); document.body.appendChild(el);
return () => { return () => {
document.body.removeChild(el); document.body.removeChild(el);
}; };
}, [el]); }, [el]);
React.useEffect(() => {
const handleEscape = (e: KeyboardEvent) => {
if (e.key === "Escape") {
close();
}
};
document.addEventListener("keydown", handleEscape);
return () => {
document.removeEventListener("keydown", handleEscape);
};
}, [close]);
const modal = ( const modal = (
<SquiggleContainer> <SquiggleContainer>
<ModalContext.Provider value={{ close }}> <ModalContext.Provider value={{ close }}>