-
Notifications
You must be signed in to change notification settings - Fork 5
/
NavBar.js
86 lines (73 loc) · 2.06 KB
/
NavBar.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
import CustomElement from '../core/CustomElement.js';
import DelegatesFocusMixin from '../mixins/DelegatesFocusMixin.js';
import ThemableMixin from '../mixins/ThemableMixin.js';
import NavItem from './NavItem.js';
export default CustomElement
.extend()
.mixin(DelegatesFocusMixin)
.mixin(ThemableMixin)
.observe({
open: 'boolean',
autoClose: {
type: 'float',
empty: 728,
},
})
.set({
color: 'surface-container',
_ariaRole: 'navigation',
})
.html`<slot id=slot></slot>`
.css`
/* https://m3.material.io/components/navigation-bar/specs */
:host {
--mdw-bg: var(--mdw-color__surface-container);
--mdw-ink: var(--mdw-color__on-surface);
display: none;
align-content: flex-start;
align-items: flex-start;
gap: 8px;
grid-auto-columns: minmax(48px, 1fr);
grid-auto-flow: column;
overflow: hidden; /* Don't expand viewport when contents translates */
box-sizing: border-box;
min-block-size: 80px;
inline-size: 100%;
pointer-events: auto;
background-color: rgb(var(--mdw-bg));
color: rgb(var(--mdw-ink));
font: var(--mdw-typescale__label-medium__font);
letter-spacing: var(--mdw-typescale__label-medium__letter-spacing);
text-align: center;
}
:host(:where([open])) {
display: grid;
}
`
.methods({
onWindowResize() {
const { autoClose } = this;
const containerWidth = window.innerWidth;
this.open = (autoClose === -1 || containerWidth < autoClose);
},
})
.events({
'~click'(event) {
// Abort if not child
if (event.target === this) return;
if (event.target instanceof NavItem === false) return;
for (const el of this.querySelectorAll('*')) {
if (el instanceof NavItem === false) continue;
el.active = (el === event.target);
}
},
})
.on({
constructed() {
window.addEventListener('resize', this.onWindowResize.bind(this));
},
connected() {
this.onWindowResize();
},
})
.autoRegister('mdw-nav-bar');