File size: 1,621 Bytes
74aacd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { XMarkIcon } from '@heroicons/react/24/outline'
import React, { ReactNode } from 'react'
import { useRecoilState } from 'recoil'
import * as DialogPrimitive from '@radix-ui/react-dialog'
import Button from './Button'
import { appState } from '../../store/Atoms'

export interface ModalProps {
  show: boolean
  children?: ReactNode
  onClose?: () => void
  title: string | ReactNode
  showCloseIcon?: boolean
  className?: string
}

const Modal = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Root>,
  ModalProps
>((props, forwardedRef) => {
  const { show, children, onClose, className, title, showCloseIcon } = props
  const [_, setAppState] = useRecoilState(appState)

  const onOpenChange = (open: boolean) => {
    if (!open) {
      onClose?.()
      setAppState(old => {
        return { ...old, disableShortCuts: false }
      })
    }
  }

  return (
    <DialogPrimitive.Root open={show} onOpenChange={onOpenChange}>
      <DialogPrimitive.Portal>
        <DialogPrimitive.Overlay className="modal-mask" />
        <DialogPrimitive.Content
          ref={forwardedRef}
          className={`modal ${className}`}
        >
          <div className="modal-header">
            <DialogPrimitive.Title>{title}</DialogPrimitive.Title>
            {showCloseIcon ? (
              <Button icon={<XMarkIcon />} onClick={onClose} />
            ) : (
              <></>
            )}
          </div>
          {children}
        </DialogPrimitive.Content>
      </DialogPrimitive.Portal>
    </DialogPrimitive.Root>
  )
})

Modal.defaultProps = {
  showCloseIcon: true,
}

export default Modal