Skip to content

Commit

Permalink
feat: make the modal more accessible
Browse files Browse the repository at this point in the history
  • Loading branch information
remvze committed Apr 22, 2024
1 parent 48291a6 commit 0252fa9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
28 changes: 24 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@types/react-dom": "^18.2.10",
"astro": "4.0.3",
"deepmerge": "4.3.1",
"focus-trap-react": "10.2.3",
"framer-motion": "10.16.4",
"howler": "2.2.4",
"react": "^18.2.0",
Expand Down
43 changes: 29 additions & 14 deletions src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from 'react';
import { AnimatePresence, motion } from 'framer-motion';
import { IoClose } from 'react-icons/io5/index';
import FocusTrap from 'focus-trap-react';

import { Portal } from '@/components/portal';

Expand Down Expand Up @@ -37,6 +38,18 @@ export function Modal({
}
}, [show, lockBody]);

useEffect(() => {
function keyListener(e) {
if (e.keyCode === 27) {
onClose();
}
}

document.addEventListener('keydown', keyListener);

return () => document.removeEventListener('keydown', keyListener);
});

return (
<Portal>
<AnimatePresence>
Expand All @@ -51,21 +64,23 @@ export function Modal({
onClick={onClose}
onKeyDown={onClose}
/>
<div className={styles.modal}>
<motion.div
animate="show"
className={cn(styles.content, wide && styles.wide)}
exit="hidden"
initial="hidden"
variants={variants.modal}
>
<button className={styles.close} onClick={onClose}>
<IoClose />
</button>
<FocusTrap>
<div className={styles.modal}>
<motion.div
animate="show"
className={cn(styles.content, wide && styles.wide)}
exit="hidden"
initial="hidden"
variants={variants.modal}
>
<button className={styles.close} onClick={onClose}>
<IoClose />
</button>

{children}
</motion.div>
</div>
{children}
</motion.div>
</div>
</FocusTrap>
</>
)}
</AnimatePresence>
Expand Down

0 comments on commit 0252fa9

Please sign in to comment.