-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathindex.js
49 lines (38 loc) · 1.67 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
const COMPONENT_SELECTOR = '[data-dropdown-menu]'
const DROPDOWN_HEAD = '[data-ref="dropdown-menu[head]"]'
const DROPDOWN_HEAD_INNER = '[data-ref="dropdown-menu[head-inner]"]'
const DROPDOWN_BODY = '[data-ref="dropdown-menu[body]"]'
const DROPDOWN_LINK = '[data-ref="dropdown-menu[link]"]'
const ACTIVE_CLASS_ATTR = 'data-active-class'
export default function () {
const components = document.querySelectorAll(COMPONENT_SELECTOR)
components.forEach(component => {
const dropdownHeadInner = component.querySelector(DROPDOWN_HEAD_INNER)
const itemActiveClass = component.getAttribute(ACTIVE_CLASS_ATTR)
const dropdownHead = component.querySelector(DROPDOWN_HEAD)
const dropdownHeadActive = dropdownHead.getAttribute(ACTIVE_CLASS_ATTR)
const dropdownBody = component.querySelector(DROPDOWN_BODY)
const dropdownBodyActive = dropdownBody.getAttribute(ACTIVE_CLASS_ATTR)
const links = component.querySelectorAll(DROPDOWN_LINK)
dropdownHead.addEventListener('click', () => {
dropdownHead.classList.toggle(dropdownHeadActive)
dropdownBody.classList.toggle(dropdownBodyActive)
})
const updateValue = (dest) => {
dropdownHeadInner.innerHTML = dest.innerHTML
}
links.forEach(link => {
link.addEventListener('click', () => {
updateValue(link)
dropdownHead.classList.remove(dropdownHeadActive)
dropdownBody.classList.remove(dropdownBodyActive)
})
})
window.addEventListener('scroll', () => {
const currentActiveIndicator = document.querySelector(`${DROPDOWN_LINK}.${itemActiveClass}`)
if (currentActiveIndicator) {
updateValue(currentActiveIndicator)
}
})
})
}