Skip to content

Commit 1f29875

Browse files
committed
Add hook to close modal
1 parent 4256139 commit 1f29875

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

src/components/SnippetModal.tsx

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
import React from "react";
2-
import ReactDOM from "react-dom";
3-
import Button from "./Button";
4-
import { CloseIcon } from "./Icons";
5-
import CodePreview from "./CodePreview";
6-
import { SnippetType } from "../types";
7-
import slugify from "../utils/slugify";
1+
import React from 'react'
2+
import ReactDOM from 'react-dom'
3+
import Button from './Button'
4+
import { CloseIcon } from './Icons'
5+
import CodePreview from './CodePreview'
6+
import { SnippetType } from '../types'
7+
import slugify from '../utils/slugify'
8+
import useEscapeKey from '../hooks/useEscapeKey'
89

910
type Props = {
10-
snippet: SnippetType;
11-
language: string;
12-
handleCloseModal: () => void;
13-
};
11+
snippet: SnippetType
12+
language: string
13+
handleCloseModal: () => void
14+
}
1415

1516
const SnippetModal: React.FC<Props> = ({
1617
snippet,
1718
language,
1819
handleCloseModal,
1920
}) => {
20-
const modalRoot = document.getElementById("modal-root");
21-
if (!modalRoot) return null;
21+
const modalRoot = document.getElementById('modal-root')
22+
if (!modalRoot) return null
23+
useEscapeKey(handleCloseModal)
2224

2325
return ReactDOM.createPortal(
24-
<div className="modal-overlay">
25-
<div className="modal | flow" data-flow-space="lg">
26-
<div className="modal__header">
27-
<h2 className="section-title">{snippet.title}</h2>
26+
<div className='modal-overlay'>
27+
<div className='modal | flow' data-flow-space='lg'>
28+
<div className='modal__header'>
29+
<h2 className='section-title'>{snippet.title}</h2>
2830
<Button isIcon={true} onClick={handleCloseModal}>
2931
<CloseIcon />
3032
</Button>
@@ -35,27 +37,27 @@ const SnippetModal: React.FC<Props> = ({
3537
{snippet.description}
3638
</p>
3739
<p>
38-
Contributed by{" "}
40+
Contributed by{' '}
3941
<a
4042
href={`https://github.com/${snippet.author}`}
41-
target="_blank"
42-
rel="noopener noreferrer"
43-
className="styled-link"
43+
target='_blank'
44+
rel='noopener noreferrer'
45+
className='styled-link'
4446
>
4547
@{snippet.author}
4648
</a>
4749
</p>
48-
<ul role="list" className="modal__tags">
50+
<ul role='list' className='modal__tags'>
4951
{snippet.tags.map((tag) => (
50-
<li key={tag} className="modal__tag">
52+
<li key={tag} className='modal__tag'>
5153
{tag}
5254
</li>
5355
))}
5456
</ul>
5557
</div>
5658
</div>,
5759
modalRoot
58-
);
59-
};
60+
)
61+
}
6062

61-
export default SnippetModal;
63+
export default SnippetModal

src/hooks/useEscapeKey.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

0 commit comments

Comments
 (0)