Move grouped navigation to top bar and remove help button

This commit is contained in:
2026-10-05 21:18:57 -04:00
parent f8d0ba3936
commit ddcdb547e3
2 changed files with 54 additions and 94 deletions
+42 -62
View File
@@ -1,81 +1,61 @@
<script lang="ts">
import { onMount, untrack } from 'svelte';
import { tick } from 'svelte';
import { page } from '$app/stores';
import type { UserRole } from '$lib/auth.js';
import { navigationItemActive, navigationLocation, visibleNavigation } from '$lib/navigation.js';
let { role, open = $bindable(false) }: { role: UserRole; open?: boolean } = $props();
let collapsed = $state<Record<string, boolean>>({});
let drawer = $state<HTMLDialogElement>();
let { role, variant = 'primary' }: { role: UserRole; variant?: 'primary' | 'secondary' } = $props();
let navigation = $state<HTMLElement>();
const groups = $derived(visibleNavigation(role));
const location = $derived(navigationLocation($page.url.pathname, role));
const storageKey = 'electric-cherry:navigation';
function save() {
try { localStorage.setItem(storageKey, JSON.stringify(collapsed)); } catch { /* Navigation works without storage. */ }
}
function toggle(id: string) { collapsed = { ...collapsed, [id]: !collapsed[id] }; save(); }
// Keep the current section or page visible in horizontally scrolling rows.
$effect(() => {
const path = $page.url.pathname;
const currentRole = role;
untrack(() => {
const current = navigationLocation(path, currentRole);
if (current) collapsed = { ...collapsed, [current.group.id]: false };
const current = location;
const node = navigation;
if (!current || !node) return;
let cancelled = false;
let observer: ResizeObserver | undefined;
void tick().then(() => {
if (cancelled) return;
const selected = node.querySelector<HTMLElement>('[aria-current]');
if (!selected) return;
const reveal = () => {
const bounds = node.getBoundingClientRect();
const target = selected.getBoundingClientRect();
if (target.left < bounds.left) node.scrollLeft -= bounds.left - target.left;
else if (target.right > bounds.right) node.scrollLeft += target.right - bounds.right;
};
reveal();
observer = new ResizeObserver(reveal);
observer.observe(node);
});
});
$effect(() => {
if (open && drawer && !drawer.open) drawer.showModal();
else if (!open && drawer?.open) drawer.close();
});
onMount(() => {
try {
const saved = JSON.parse(localStorage.getItem(storageKey) ?? '{}');
if (saved && typeof saved === 'object') collapsed = Object.fromEntries(groups.map(group => [group.id, saved[group.id] === true]));
} catch { /* Ignore invalid or unavailable preferences. */ }
if (location) collapsed = { ...collapsed, [location.group.id]: false };
const desktop = matchMedia('(min-width: 1000px)');
const resized = () => { if (desktop.matches) open = false; };
desktop.addEventListener('change', resized);
return () => desktop.removeEventListener('change', resized);
return () => { cancelled = true; observer?.disconnect(); };
});
</script>
{#snippet links(prefix: string)}
<nav aria-label="Primary">
{#if variant === 'primary'}
<nav class="primary" aria-label="Primary" bind:this={navigation}>
{#each groups as group (group.id)}
<section class:current={location?.group.id === group.id}>
<button class="group" aria-expanded={!collapsed[group.id]} aria-controls={`${prefix}-${group.id}`} onclick={() => toggle(group.id)}>
{group.label}<span aria-hidden="true">{collapsed[group.id] ? '+' : '−'}</span>
</button>
<div id={`${prefix}-${group.id}`} hidden={collapsed[group.id]}>
{#each group.items as item (item.href)}
<a href={item.href} aria-current={navigationItemActive(item, $page.url.pathname) ? 'page' : undefined}>{item.label}</a>
{/each}
</div>
</section>
<a href={group.items[0].href} aria-current={location?.group.id === group.id ? 'location' : undefined}>{group.label}</a>
{/each}
</nav>
{/snippet}
<aside class="sidebar">{@render links('desktop-navigation')}</aside>
<dialog class="drawer" id="navigation-drawer" bind:this={drawer} aria-label="Navigation" onclose={() => open = false}
onclick={event => { if (event.target === drawer) { const box = drawer!.getBoundingClientRect(); if (event.clientX > box.right || event.clientX < box.left || event.clientY < box.top || event.clientY > box.bottom) open = false; } }}>
<div class="drawer-heading"><strong>Navigation</strong><button aria-label="Close navigation" onclick={() => open = false}>×</button></div>
{@render links('mobile-navigation')}
</dialog>
{:else if location}
<nav class="secondary" aria-label={`${location.group.label} pages`} bind:this={navigation}>
{#each location.group.items as item (item.href)}
<a href={item.href} aria-current={navigationItemActive(item, $page.url.pathname) ? 'page' : undefined}>{item.label}</a>
{/each}
</nav>
{/if}
<style>
.sidebar { position: fixed; inset: 60px auto 0 0; width: 232px; overflow-y: auto; overscroll-behavior: contain; background: var(--surface); border-right: 1px solid var(--border-subtle); padding: 16px 12px 32px; z-index: 15; }
nav { display: grid; gap: 12px; }
.group { display: flex; justify-content: space-between; align-items: center; width: 100%; padding: 8px 12px; color: var(--text-secondary); font: inherit; font-size: var(--text-xs); font-weight: 600; background: none; border: 0; border-radius: var(--radius-sm); cursor: pointer; }
.current .group { color: var(--text-primary); }
a { display: block; padding: 8px 12px; border-radius: var(--radius-sm); color: var(--text-secondary); text-decoration: none; font-size: var(--text-sm); line-height: 1.5; }
a:hover, .group:hover { background: var(--surface-raised); color: var(--text-primary); }
a[aria-current] { color: var(--accent-primary-hover); background: color-mix(in oklab, var(--accent-primary) 12%, var(--surface)); box-shadow: inset 3px 0 var(--accent-primary); }
.drawer { position: fixed; inset: 0 auto 0 0; margin: 0; width: min(320px, 90vw); height: 100dvh; max-height: 100dvh; border: 0; border-right: 1px solid var(--border-strong); border-radius: 0; padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom)); color: var(--text-primary); background: var(--surface); }
.drawer::backdrop { background: #0008; }
.drawer-heading { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.drawer-heading button { width: 44px; height: 44px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: var(--surface-raised); color: inherit; font: inherit; font-size: 24px; cursor: pointer; }
.drawer a, .drawer .group { min-height: 44px; display: flex; align-items: center; }
@media (max-width: 999px) { .sidebar { display: none; } }
nav { display:flex; align-items:stretch; gap:4px; min-width:0; max-width:100%; overflow-x:auto; overscroll-behavior-x:contain; scrollbar-width:thin; }
a { display:flex; align-items:center; flex-shrink:0; min-height:44px; padding:10px 16px; border-bottom:2px solid transparent; color:var(--text-secondary); text-decoration:none; white-space:nowrap; font-size:var(--text-sm); font-weight:500; }
a:hover { color:var(--text-primary); background:var(--surface-raised); }
a:focus-visible { outline-offset:-3px; }
a[aria-current] { color:var(--accent-primary-hover); border-bottom-color:var(--accent-primary); }
.primary a { min-height:59px; }
.secondary { margin-bottom:var(--space-6); border-bottom:1px solid var(--border-subtle); }
@media (max-width:999px) { .primary a { min-height:48px; } }
@media (max-width:720px) { a { padding-inline:12px; } }
</style>