-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathflare-navigation.es6.js
More file actions
143 lines (124 loc) · 4.74 KB
/
flare-navigation.es6.js
File metadata and controls
143 lines (124 loc) · 4.74 KB
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
// Sticky header
import { createFocusTrap } from 'focus-trap';
import Headroom from 'headroom.js';
(function () {
const headerEl = document.querySelector('.fl-header.enable-sticky');
if (headerEl) {
const headroomInstance = new Headroom(headerEl, {
offset: 80,
classes: {
pinned: 'headroom-pinned',
unpinned: 'headroom-unpinned',
notTop: 'headroom-not-top',
notBottom: 'headroom-not-bottom'
}
});
headroomInstance.init();
}
// Hamburger menu
const buttonEl = document.querySelector('.fl-show-mobile-menu');
const mobileNavEl = document.querySelector('.fl-nav');
const trap = createFocusTrap(headerEl);
if (buttonEl && mobileNavEl) {
buttonEl.addEventListener('click', function (e) {
e.preventDefault();
const mobileNavIsOpen =
e.currentTarget.classList.contains('is-open');
const elements = [e.currentTarget, mobileNavEl];
if (mobileNavIsOpen) {
elements.forEach(function (el) {
el.classList.remove('is-open');
});
document.body.classList.remove('fl-modal-open');
trap.deactivate();
} else {
elements.forEach(function (el) {
el.classList.add('is-open');
});
document.body.classList.add('fl-modal-open');
trap.activate();
}
});
}
// Menu panels
const menuCategories = document.querySelectorAll('.fl-menu-category');
menuCategories.forEach(function (category) {
// keyboard is being used
category.addEventListener('keyup', function (event) {
if (event.key === 'Escape') {
menuCategories.forEach(function (category) {
category.classList.remove('is-active');
});
}
});
// mouse is being used
category.addEventListener('mouseover', function () {
menuCategories.forEach(function (category) {
category.classList.remove('is-active');
});
category.classList.add('is-active');
});
category.addEventListener('mouseout', function () {
category.classList.remove('is-active');
});
});
const menuTitles = document.querySelectorAll('.fl-menu-title');
let focusedMenu = null;
// keyboard is being used
menuTitles.forEach(function (title) {
// when focusing a menu title, close all menus
title.addEventListener('focus', function () {
menuCategories.forEach(function (category) {
if (category.classList.contains('is-active')) {
focusedMenu = category;
}
category.classList.remove('is-active');
});
});
// when leaving the last link of a menu, close all menus
const menuLinks = title
.closest('.fl-menu-category')
.querySelectorAll('a');
menuLinks[menuLinks.length - 1].addEventListener(
'keydown',
function (event) {
if (event.key === 'Tab' && !event.shiftKey) {
menuCategories.forEach(function (category) {
category.classList.remove('is-active');
});
}
}
);
// when clicking or pressing enter, toggle the menu
title.addEventListener('click', function (event) {
event.preventDefault();
const menuPanel = event.target.closest('.fl-menu-category');
// focus runs first then click. if we click a menu that was closed
// by focus don't open it again immediately
if (focusedMenu === menuPanel) {
focusedMenu = null;
return;
}
if (menuPanel.classList.contains('is-active')) {
menuPanel.classList.remove('is-active');
} else {
menuPanel.classList.add('is-active');
}
});
});
const menuPanelCloseButtons = document.querySelectorAll(
'.fl-menu-close-button'
);
menuPanelCloseButtons.forEach(function (button) {
button.addEventListener('click', function (event) {
event.preventDefault();
const menuPanel = event.target.closest('.fl-menu-category');
menuPanel.classList.remove('is-active');
});
});
})();