File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { CloseIcon } from "./Icons";
55import CodePreview from "./CodePreview" ;
66import { SnippetType } from "../types" ;
77import slugify from "../utils/slugify" ;
8+ import useEscapeKey from "../hooks/useEscapeKey" ;
89
910type Props = {
1011 snippet : SnippetType ;
@@ -19,6 +20,7 @@ const SnippetModal: React.FC<Props> = ({
1920} ) => {
2021 const modalRoot = document . getElementById ( "modal-root" ) ;
2122 if ( ! modalRoot ) return null ;
23+ useEscapeKey ( handleCloseModal ) ;
2224
2325 return ReactDOM . createPortal (
2426 < div className = "modal-overlay" >
Original file line number Diff line number Diff line change 1+ import { useEffect } from "react"
2+
3+ const useEscapeKey = ( onEscapeEvent : ( ) => void ) => {
4+ useEffect ( ( ) => {
5+ const handleEscape = ( event : { key : string } ) => {
6+ if ( event . key === "Escape" ) onEscapeEvent ( ) ;
7+ }
8+ window . addEventListener ( "keydown" , handleEscape ) ;
9+
10+ return ( ) => {
11+ window . removeEventListener ( "keydown" , handleEscape ) ;
12+ }
13+ } , [ onEscapeEvent ] ) ;
14+ } ;
15+
16+ export default useEscapeKey ;
You can’t perform that action at this time.
0 commit comments