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";
5
5
import CodePreview from "./CodePreview" ;
6
6
import { SnippetType } from "../types" ;
7
7
import slugify from "../utils/slugify" ;
8
+ import useEscapeKey from "../hooks/useEscapeKey" ;
8
9
9
10
type Props = {
10
11
snippet : SnippetType ;
@@ -19,6 +20,7 @@ const SnippetModal: React.FC<Props> = ({
19
20
} ) => {
20
21
const modalRoot = document . getElementById ( "modal-root" ) ;
21
22
if ( ! modalRoot ) return null ;
23
+ useEscapeKey ( handleCloseModal ) ;
22
24
23
25
return ReactDOM . createPortal (
24
26
< 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