Skip to content

Commit

Permalink
Closing modal on esc
Browse files Browse the repository at this point in the history
  • Loading branch information
BudzioT committed Dec 21, 2024
1 parent 4758cfa commit b323f19
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/components/ui/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type React from 'react'
import React, { useEffect } from 'react'
import { AnimatePresence, motion } from 'framer-motion'
import Icon from '@hackclub/icons'
import JaggedCard from '../jagged-card'
Expand All @@ -17,6 +17,18 @@ export default function Modal({
children: React.ReactNode
props?: any
}) {
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape' && !hideCloseButton) {
close(event)
}
}
document.addEventListener('keydown', handleKeyDown)
return () => {
document.removeEventListener('keydown', handleKeyDown)
}
}, [hideCloseButton, close])

return (
<AnimatePresence>
{isOpen ? (
Expand Down

0 comments on commit b323f19

Please sign in to comment.