-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
162 lines (133 loc) · 4 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import tack from 'tackjs'
import { tarry, queue } from 'tarry.js'
export default class Popover {
constructor ({
target,
popover = null,
position = 'bottom',
transitionSpeed = 0,
onChange = null
}) {
this.target = target
this.popover = this.createPopover(popover)
this.position = position
this.transitionSpeed = transitionSpeed
this.onChange = onChange
this.state = {
pinned: false,
busy: false,
requestClose: false
}
this.pin = this.pin.bind(this)
this.unpin = this.unpin.bind(this)
this.block = this.block.bind(this)
this.unblock = this.unblock.bind(this)
this.isExternalClick = this.isExternalClick.bind(this)
this.handleKeyup = this.handleKeyup.bind(this)
this.focusNode = null
}
setState (state, cb) {
this.state = Object.assign(
this.state,
state
)
cb && tarry(cb, 0)()
}
block () {
this.setState({
busy: true
})
}
unblock () {
this.setState({
busy: false
}, () => {
this.state.requestClose && this.unpin()
})
}
toggle () {
this.state.pinned ? this.unpin() : this.pin()
}
pin () {
if (this.state.busy || this.state.pinned) return
this.setState({
busy: true
})
this.focusNode = document.activeElement
const render = tarry(() => document.body.appendChild(this.popover))
const pin = tarry(() => tack(this.popover, this.target, this.position))
const show = tarry(() => {
this.popover.classList.add('is-visible')
this.popover.setAttribute('tabindex', '0')
this.popover.setAttribute('aria-hidden', 'false')
})
const focus = tarry(() => this.popover.focus())
const done = tarry(() => this.setState({
busy: false,
pinned: true
}))
queue(render, pin, show(0), focus(0), done)()
this.popover.addEventListener('mouseenter', this.block)
this.popover.addEventListener('mouseleave', this.unblock)
window.addEventListener('click', this.isExternalClick)
window.addEventListener('touchstart', this.isExternalClick)
window.addEventListener('keyup', this.handleKeyup)
window.addEventListener('resize', this.unpin)
this.onChange && this.onChange({
pinned: true
})
}
unpin (force) {
this.setState({
requestClose: true
})
if (!force && (this.state.busy || !this.state.pinned)) return
tarry(() => {
this.setState({
busy: true
})
this.popover.removeEventListener('mouseenter', this.block)
this.popover.removeEventListener('mouseleave', this.unblock)
window.removeEventListener('click', this.isExternalClick)
window.removeEventListener('touchstart', this.isExternalClick)
window.removeEventListener('keyup', this.handleKeyup)
window.removeEventListener('resize', this.unpin)
const hide = tarry(() => this.popover.classList.add('is-hiding'))
const remove = tarry(() => document.body.removeChild(this.popover))
const blur = tarry(() => this.focusNode.focus())
const done = tarry(() => {
this.popover.classList.remove('is-hiding')
this.popover.classList.remove('is-visible')
this.setState({
busy: false,
pinned: false,
requestClose: false
})
})
queue(hide, remove(this.transitionSpeed), blur, done)()
this.onChange && this.onChange({
pinned: false
})
}, 0)()
}
handleKeyup (e) {
e.keyCode === 27 && this.unpin()
}
isExternalClick (e) {
if (
(e.target !== this.popover && !this.popover.contains(e.target)) &&
(e.target !== this.target && !this.target.contains(e.target))
) {
this.unpin()
}
}
createPopover (pop) {
const popover = document.createElement('div')
popover.className = 'poppy'
popover.role = 'dialog'
popover.setAttribute('aria-label', 'Share Dialog')
popover.setAttribute('aria-hidden', 'true')
typeof pop === 'string' ? popover.innerHTML = pop : popover.appendChild(pop)
return popover
}
}