forked from sparkdesignsystem/spark-design-system
/
wide-navigation.js
53 lines (47 loc) · 1.75 KB
/
wide-navigation.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
/* global document */
import getElements from '../utilities/getElements';
const showDropDown = (container) => {
const subNavContainer = container.querySelector('.sprk-c-WideNavigation__sub-menu-container');
if (subNavContainer) {
subNavContainer.classList.remove('sprk-u-Display--none');
container.setAttribute('aria-expanded', 'true');
}
};
const hideAllDropDowns = (subNavs, listItems) => {
subNavs.forEach((item) => {
item.classList.add('sprk-u-Display--none');
});
listItems.forEach((item) => {
item.setAttribute('aria-expanded', 'false');
});
};
const bindUIEvents = (element) => {
const subNavContainers = document.querySelectorAll('.sprk-c-WideNavigation__sub-menu-container');
const expandableListItems = document.querySelectorAll('.sprk-c-WideNavigation [aria-expanded]');
element.addEventListener('focusin', (e) => {
e.stopPropagation();
hideAllDropDowns(subNavContainers, expandableListItems);
showDropDown(element);
});
element.addEventListener('mouseenter', (e) => {
e.stopPropagation();
if (e.target.querySelector('.sprk-c-WideNavigation--sub')) {
e.target.classList.add('sprk-c-WideNavigation__item--open');
hideAllDropDowns(subNavContainers, expandableListItems);
showDropDown(element);
}
});
element.addEventListener('mouseleave', (e) => {
e.stopPropagation();
if (e.target.querySelector('.sprk-c-WideNavigation--sub')) {
e.target.classList.remove('sprk-c-WideNavigation__item--open');
hideAllDropDowns(subNavContainers, expandableListItems);
}
});
};
const WideNavigation = () => {
getElements('[data-sprk-navigation="wide"] > .sprk-c-WideNavigation__item', bindUIEvents);
};
export {
WideNavigation, showDropDown, hideAllDropDowns, bindUIEvents,
};