mirror of
https://github.com/vercel/commerce.git
synced 2025-04-24 20:07:50 +00:00
* Updated log * Updates to root * Updates to pnpm * successfully moved to pnpm * type issue * Local as the default provider * Upgrade dependencies * Revert to local * Upgrade React * Update node-fetch deps * Fix types * Ignore warnings * Fix missing dependency * Update pnpm-lock.yaml * Add missing @types/cookie * Upgrade dependencies * Fix missing dependencies * Update README.md Co-authored-by: Bel Curcio <curciobel@gmail.com>
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
import { FC, useRef, useEffect, useCallback, ReactNode } from 'react'
|
|
import s from './Modal.module.css'
|
|
import FocusTrap from '@lib/focus-trap'
|
|
import { Cross } from '@components/icons'
|
|
import { disableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock'
|
|
|
|
interface ModalProps {
|
|
className?: string
|
|
children?: ReactNode
|
|
onClose: () => void
|
|
}
|
|
|
|
const Modal: FC<ModalProps> = ({ children, onClose }) => {
|
|
const ref = useRef() as React.MutableRefObject<HTMLDivElement>
|
|
|
|
const handleKey = useCallback(
|
|
(e: KeyboardEvent) => {
|
|
if (e.key === 'Escape') {
|
|
return onClose()
|
|
}
|
|
},
|
|
[onClose]
|
|
)
|
|
|
|
useEffect(() => {
|
|
const modal = ref.current
|
|
|
|
if (modal) {
|
|
disableBodyScroll(modal, { reserveScrollBarGap: true })
|
|
window.addEventListener('keydown', handleKey)
|
|
}
|
|
return () => {
|
|
clearAllBodyScrollLocks()
|
|
window.removeEventListener('keydown', handleKey)
|
|
}
|
|
}, [handleKey])
|
|
|
|
return (
|
|
<div className={s.root}>
|
|
<div className={s.modal} role="dialog" ref={ref}>
|
|
<button
|
|
onClick={() => onClose()}
|
|
aria-label="Close panel"
|
|
className={s.close}
|
|
>
|
|
<Cross className="h-6 w-6" />
|
|
</button>
|
|
<FocusTrap focusFirst>{children}</FocusTrap>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default Modal
|