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
|
/**
* zen-nav.js — Zen dashboard navigation bar Web Component
*
* Usage:
* <script src="nav.js" defer></script>
*
* <zen-nav>
* <a href="compute.html">Node</a>
* <a href="orchestrator.html">Orchestrator</a>
* </zen-nav>
*
* Each child <a> becomes a nav link. The current page is
* highlighted automatically based on the href.
*
* Links may be added or removed dynamically — the component
* re-renders automatically via MutationObserver.
*/
class ZenNav extends HTMLElement {
connectedCallback() {
if (!this.shadowRoot) this.attachShadow({ mode: 'open' });
this._render();
this._observer = new MutationObserver(() => this._render());
this._observer.observe(this, { childList: true });
}
disconnectedCallback() {
if (this._observer) {
this._observer.disconnect();
this._observer = null;
}
}
_render() {
const currentPath = window.location.pathname;
const currentSearch = window.location.search;
const items = Array.from(this.querySelectorAll(':scope > a'));
const currentParams = new URLSearchParams(currentSearch);
let spacerInserted = false;
const links = items.map(a => {
const href = a.getAttribute('href') || '';
const label = a.textContent.trim();
const alignRight = a.hasAttribute('data-align') && a.getAttribute('data-align') === 'right';
let active = false;
try {
const linkUrl = new URL(href, window.location.origin);
if (linkUrl.pathname === currentPath || currentPath.endsWith(href)) {
// All of the link's query params must be present in the current URL
const linkParams = linkUrl.searchParams;
active = Array.from(linkParams.entries()).every(
([k, v]) => currentParams.get(k) === v
);
// A bare path with no params only matches when the current URL also has no page param
if (linkParams.toString() === '' && currentParams.has('page')) {
active = false;
}
}
} catch (e) {
active = currentPath.endsWith(href);
}
let prefix = '';
if (alignRight && !spacerInserted) {
prefix = '<span class="nav-spacer"></span>';
spacerInserted = true;
}
return `${prefix}<a class="nav-link${active ? ' active' : ''}" href="${href}">${label}</a>`;
}).join('');
this.shadowRoot.innerHTML = `
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:host {
display: block;
margin-bottom: 16px;
}
.nav-bar {
display: flex;
align-items: center;
gap: 4px;
padding: 4px;
background: var(--theme_g3);
border: 1px solid var(--theme_g2);
border-radius: 6px;
}
.nav-link {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
font-size: 13px;
font-weight: 500;
color: var(--theme_g1);
text-decoration: none;
padding: 6px 14px;
border-radius: 4px;
transition: color 0.15s, background 0.15s;
}
.nav-link:hover {
color: var(--theme_g0);
background: var(--theme_p4);
}
.nav-link.active {
color: var(--theme_bright);
background: var(--theme_g2);
}
.nav-spacer {
flex: 1;
}
</style>
<nav class="nav-bar" part="nav-bar">${links}</nav>
`;
}
}
customElements.define('zen-nav', ZenNav);
|