-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrc.js
80 lines (76 loc) · 2.33 KB
/
src.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import React from 'react'
import ReactDOM from 'react-dom'
const backdropStyles = {
position: 'fixed',
top: 0,
left: 0,
width: '100%',
height: '100%',
zIndex: 10,
transform: 'translateZ(0)',
backgroundColor: 'rgba(0, 0, 0, 0.3)',
}
const modalStyles = {
position: 'fixed',
padding: '2.5rem 1.5rem 1.5rem 1.5rem',
backgroundColor: 'white',
boxShadow: '0 0 10px 3px rgba(0, 0, 0, 0.1)',
overflowY: 'auto',
left: '50%',
top: '50%',
height: 'auto',
transform: 'translate(-50%, -50%)',
maxWidth: '30em',
borderRadius: '0.25rem',
maxHeight: 'calc(100% - 1em)',
}
const useModal = (modalCreator, { target, style, open, required }) => {
let openModal
let resolveCallback
const Modal = () => {
const [isModalOpen, setModalOpen] = React.useState(open || false)
openModal = () => {
setModalOpen(true)
return new Promise(r => {
resolveCallback = r
})
}
const closeModal = resolveWith => {
setModalOpen(false)
resolveCallback(resolveWith)
}
const modalRef = React.useRef()
return isModalOpen
? ReactDOM.createPortal(
<div
className="modal-background"
onClick={e => {
!required &&
(modalRef.current &&
!modalRef.current.contains(e.target)) &&
setModalOpen(false)
}}
style={{
...backdropStyles,
...((style && style.backdrop) || {}),
}}
>
<div
className="modal-wrapper"
ref={modalRef}
style={{
...modalStyles,
...((style && style.modal) || {}),
}}
>
{modalCreator(x => closeModal(x))}
</div>
</div>,
target || document.body
)
: null
}
const modal = <Modal />
return [modal, () => openModal()]
}
export default useModal