-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathmodal.js
More file actions
61 lines (52 loc) · 1.48 KB
/
modal.js
File metadata and controls
61 lines (52 loc) · 1.48 KB
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
const nest = require('depnest')
const { h, Value } = require('mutant')
exports.gives = nest('app.html.modal')
exports.create = (api) => {
return nest('app.html.modal', (content, { isOpen, onOpen, onClose, className = '' } = {}) => {
if (typeof isOpen !== 'function') isOpen = Value(false)
const openMe = () => {
isOpen.set(true)
}
const closeMe = () => {
isOpen.set(false)
if (typeof onClose === 'function') onClose()
}
const modal = h('Modal -closed',
{
className,
'ev-click': closeMe,
'ev-keydown': ev => {
if (ev.keyCode === 27) closeMe() // Escape
}
},
[
h('div.content', { 'ev-click': (ev) => ev.stopPropagation() }, [
content
// I think content must be in the DOM for any downstream mutant Observers to be updating
])
]
)
isOpen(state => {
if (state === true) {
modal.classList.remove('-closed')
modal.classList.add('-open')
} else {
modal.classList.remove('-open')
modal.classList.add('-closed')
return
}
if (typeof onOpen === 'function') onOpen()
focus()
function focus () {
if (!modal.isConnected) setTimeout(focus, 200)
else {
const target = modal.querySelector('input') || modal.querySelector('textarea')
if (target) target.focus()
}
}
})
modal.open = openMe
modal.close = closeMe
return modal
})
}