-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
129 lines (111 loc) Β· 3.12 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
import 'wicg-inert'
const ButaneShowHide = (() => {
const FOCUSABLE_ELEMENTS = [
'a[href]',
'area[href]',
'input:not([disabled]):not([type="hidden"]):not([aria-hidden])',
'select:not([disabled]):not([aria-hidden])',
'textarea:not([disabled]):not([aria-hidden])',
'button:not([disabled]):not([aria-hidden])',
'iframe',
'object',
'embed',
'[contenteditable]',
'[tabindex]:not([tabindex^="-"])'
]
class ShowHide {
constructor({
containerEl,
trigger,
target,
showTrigger,
hideTrigger = 'data-butane-hide',
activeClass = 'is-active',
onShow = () => {},
onHide = () => {},
debugMode = false
}) {
this.containerEl = containerEl
this.trigger = trigger
this.target = target
this.config = {
debugMode,
showTrigger,
hideTrigger,
activeClass,
onShow,
onHide
}
this.target.inert = true
this.trigger.addEventListener('click', () => this.show())
this.onClick = this.onClick.bind(this)
this.onKeydown = this.onKeydown.bind(this)
}
addEventListeners() {
document.addEventListener('click', this.onClick)
document.addEventListener('keydown', this.onKeydown)
}
removeEventListeners() {
document.removeEventListener('click', this.onClick)
document.removeEventListener('keydown', this.onKeydown)
}
show() {
this.activeElement = document.activeElement
this.containerEl.inert = true
this.target.inert = false
this.target.classList.add(this.config.activeClass)
this.setFocusToFirstNode()
this.addEventListeners()
this.config.onShow(this.trigger, this.target)
}
hide() {
this.containerEl.inert = false
this.target.inert = true
this.target.classList.remove(this.config.activeClass)
this.removeEventListeners()
this.config.onHide(this.trigger, this.target)
this.activeElement.focus()
}
getFocusableNodes() {
const nodes = this.target.querySelectorAll(FOCUSABLE_ELEMENTS)
return Object.keys(nodes).map(key => nodes[key])
}
setFocusToFirstNode() {
const focusableNodes = this.getFocusableNodes()
if (focusableNodes.length) focusableNodes[0].focus()
}
onClick(event) {
if (event.target.hasAttribute(this.config.hideTrigger)) {
this.hide()
}
}
onKeydown(event) {
if (event.keyCode === 27) this.hide(event)
}
}
const init = config => {
const options = Object.assign(
{},
{
containerSelector: 'data-butane-container',
showTrigger: 'data-butane-show'
},
config
)
options.containerEl = document.querySelector(
`[${options.containerSelector}]`
)
const triggers = Array.from(
document.querySelectorAll(`[${options.showTrigger}]`)
)
triggers.forEach(trigger => {
options.trigger = trigger
options.target = document.getElementById(
trigger.getAttribute(options.showTrigger)
)
new ShowHide(options)
})
}
return { init }
})()
export default ButaneShowHide