aboutsummaryrefslogtreecommitdiff
path: root/src/zenserver/frontend/html/nav.js
blob: a5de203f2092838a7143e738208f2f3d9d3db586 (plain) (blame)
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
/**
 * 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.
 */

class ZenNav extends HTMLElement {

  connectedCallback() {
    if (!this.shadowRoot) this.attachShadow({ mode: 'open' });
    this._render();
  }

  _render() {
    const currentPath = window.location.pathname;
    const items = Array.from(this.querySelectorAll(':scope > a'));

    const links = items.map(a => {
      const href = a.getAttribute('href') || '';
      const label = a.textContent.trim();
      const active = currentPath.endsWith(href);
      return `<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);
        }
      </style>
      <nav class="nav-bar">${links}</nav>
    `;
  }
}

customElements.define('zen-nav', ZenNav);