-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (146 loc) · 4.21 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
import { tack } from 'tackjs'
const keys = {
up: 38,
down: 40,
enter: 13,
esc: 27
}
function h(tag, props, children) {
if (!props) return document.createTextNode(tag)
const e = document.createElement(tag)
const attrs = Object.keys(props)
for (let i = 0; i < attrs.length; i++) {
e.setAttribute(attrs[i], props[attrs[i]])
}
for (let i = 0; i < children.length; i++) {
e.appendChild(children[i])
}
return e
}
export default function drop (select) {
if (/iPad|iPhone|Android/i.test(navigator.userAgent)) return
let i = 0
let opts = []
let selectedIndex
let active = false
let focusNode = null
let scrollDisabled = false
while (select[i]) {
const opt = select[i]
if (opt.selected) selectedIndex = i
opts.push({
selected: opt.selected,
value: opt.value,
label: opt.text
})
i++
}
const button = h('button', { class: 'droptop-button' }, [ h(opts[0].label) ])
const options = opts.map(opt => {
return h('button', { class: 'droptop__option' }, [ h(opt.label) ])
})
const dropdown = h('div', {
class: 'droptop',
'aria-hidden': true
}, [
h('div', { class: 'droptop__inner' }, options)
])
const transitionDelay = parseFloat(window.getComputedStyle(dropdown).getPropertyValue('transition-duration'), 10) * 1000
function toggle () {
active ? close() : open()
}
function open () {
if (!active) {
active = true
document.body.appendChild(dropdown)
dropdown.setAttribute('aria-hidden', false)
dropdown.setAttribute('tabindex', 0)
const attachment = (window.innerHeight - button.getBoundingClientRect().bottom) < dropdown.clientHeight ? 'top' : 'bottom'
dropdown.classList.add(`droptop--${attachment}`)
tack(dropdown, button, attachment)
focusNode = document.activeElement
options[selectedIndex].focus()
setTimeout(() => {
dropdown.classList.add('active')
}, 0)
}
}
function close () {
if (active) {
active = false
enableScroll()
dropdown.classList.add('hiding')
focusNode && focusNode.focus()
setTimeout(() => {
dropdown.setAttribute('aria-hidden', true)
dropdown.removeAttribute('tabindex')
document.body.removeChild(dropdown)
dropdown.classList.remove('active')
dropdown.classList.remove('hiding')
dropdown.classList.remove(`droptop--top`)
dropdown.classList.remove(`droptop--bottom`)
}, transitionDelay)
}
}
function selectOption (cb) {
let i = 0
while (options[i]) {
if (i === selectedIndex) {
button.innerHTML = options[i].innerHTML
select[i].selected = true
selectedIndex = i
} else {
select[i].selected = false
}
i++
}
select.dispatchEvent(new Event('change'))
cb && cb()
}
function focusOption (type) {
if (document.activeElement === dropdown) {
return options[0].focus()
}
document.activeElement[type] && document.activeElement[type].focus()
}
function disableScroll () {
scrollDisabled = true
document.body.style.overflow = 'hidden'
}
function enableScroll () {
scrollDisabled = false
document.body.style.overflow = ''
}
select.onchange = function (e) {
selectedIndex = e.target.selectedIndex
button.innerHTML = options[selectedIndex].innerHTML
}
dropdown.onmouseenter = disableScroll
dropdown.onmouseleave = enableScroll
button.onclick = toggle
document.addEventListener('click', e => {
if (dropdown.contains(e.target) || e.target === button) return
close()
})
document.addEventListener('keydown', e => {
if (!active) return
e.preventDefault()
const key = e.keyCode
if (key === keys.esc) close()
if (key === keys.enter) {
selectedIndex = options.indexOf(document.activeElement)
selectOption(close)
}
if (key === keys.down) focusOption('nextSibling')
if (key === keys.up) focusOption('previousSibling')
})
for (let i = 0; i < options.length; i++) {
options[i].onclick = () => {
selectedIndex = i
selectOption(close)
}
}
button.innerHTML = options[selectedIndex].innerHTML
select.parentNode.insertBefore(button, select)
select.style.display = 'none'
}