-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnav-menu.js
50 lines (41 loc) · 1.37 KB
/
nav-menu.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
import ShadowElement, {html, template, css, adoptedStyleSheets, define} from '@cfware/shadow-element';
import './nav-section.js';
import './nav-item.js';
class NavMenu extends ShadowElement {
update() {
const {pathname} = window.location;
const items = [...this.querySelectorAll('nav-item')];
const matches = items
.filter(item => pathname.startsWith(item.pathname))
.sort((a, b) => b.pathname.length - a.pathname.length);
for (const item of items) {
item.active = item === matches[0];
}
const sections = [...this.querySelectorAll('nav-section:not([hidden])')];
if (sections.length === 0) {
return;
}
const activeSections = sections.filter(section => {
if (!section.active) {
section.active = section.querySelectorAll('nav-item[active]:not([hidden])').length > 0;
}
return section.active;
});
if (matches.length === 0 || activeSections.length === 0) {
sections[0].active = true;
}
}
static [adoptedStyleSheets] = [
css`
:host {
width: 15rem;
font-weight: 400;
user-select: none;
}
`
];
get [template]() {
return html`<slot />`;
}
}
NavMenu[define]('nav-menu');