-
Notifications
You must be signed in to change notification settings - Fork 44
/
modal.js
82 lines (75 loc) · 2 KB
/
modal.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
81
82
import { useState, useEffect } from 'preact/hooks'
import withStyles from './jss'
const ANIMATION_DURATION = 150
function Modal ({
classes, open, onClose, children
}) {
const [isLinger, setIsLinger] = useState(open)
useEffect(() => {
if (open) {
setIsLinger(true)
} else {
const timer = setTimeout(() => {
setIsLinger(false)
}, ANIMATION_DURATION)
return () => clearTimeout(timer)
}
}, [open])
useEffect(() => {
function listener (e) {
if (e.key === 'Escape') {
onClose()
}
}
if (open) {
document.addEventListener('keyup', listener)
return () => document.removeEventListener('keyup', listener)
}
}, [open, onClose])
// For some reason unmounting portals causes Preact to crash, so just don't
// render the modal in a portal as Cirrus already applies styles to make the
// modal appear over everything from anywhere in the tree.
return (open || isLinger) &&
<div class={`modal shown ${classes.animated}${open ? '' : ' leaving'}`} hidden={!(open || isLinger)}>
<div class='modal-overlay' onClick={onClose} aria-label='Close' />
<div class='modal-content' role='document'>
{children}
</div>
</div>
}
const ANIMATION_INITIAL_SCALE = 0.8
export default withStyles({
'@keyframes container': {
from: {
opacity: 0
},
to: {
opactiy: 1
}
},
'@keyframes content': {
from: {
transform: `scale(${ANIMATION_INITIAL_SCALE})`
},
to: {
transform: 'scale(1)'
}
},
animated: {
'&': {
display: 'flex',
animation: `$container ${ANIMATION_DURATION}ms ease-out`
},
'& .modal-content': {
animation: `$content ${ANIMATION_DURATION}ms ease-out`
},
'&.leaving': {
opacity: 0,
transition: `opacity ${ANIMATION_DURATION}ms ease-in`
},
'&.leaving .modal-content': {
transform: `scale(${ANIMATION_INITIAL_SCALE})`,
transition: `transform ${ANIMATION_DURATION}ms ease-in`
}
}
}, Modal)