close modals on escape key
This commit is contained in:
parent
fd36e9ef9e
commit
e76a4beb4d
|
@ -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 }}>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user