frontend: the swarm UI's page chrome moves to @hive/shared as AppShell
The header bar, the nav row with its sliding underline, the settings menu slot, the skip link and the content column move out of the swarm UI's Shell into a router-free AppShell in @hive/shared, with their CSS. It takes the brand, the nav items, the current location and the link component, plus slots for extra tabs, settings entries and menus. The swarm UI's Shell keeps what is its own on top of it: the open agent-terminal tabs (their CSS moves to DynamicTabs.css), the swarm's settings, links and user menus, the swarm name and the full-bleed routes. No visible change; this is the base for the hive UI to reuse the same chrome.
This commit is contained in:
parent
1a4f35f669
commit
c6f8f26b16
3 changed files with 604 additions and 530 deletions
|
|
@ -10,6 +10,8 @@
|
|||
"preact": "11.2.0"
|
||||
},
|
||||
"exports": {
|
||||
"./app-shell.js": "./src/app-shell/AppShell.tsx",
|
||||
"./app-shell.css": "./src/app-shell/AppShell.css",
|
||||
"./term-msg.js": "./src/terminal/termMsg.ts",
|
||||
"./term-row.js": "./src/terminal/Row.tsx",
|
||||
"./term-linkify.js": "./src/terminal/linkify.tsx",
|
||||
|
|
|
|||
258
frontend/packages/shared/src/app-shell/AppShell.css
Normal file
258
frontend/packages/shared/src/app-shell/AppShell.css
Normal file
|
|
@ -0,0 +1,258 @@
|
|||
/* <AppShell> — header bar + nav row + the content column every route
|
||||
renders into. Colour vars come from @hive/shared's theme.css, the base16
|
||||
slots every hyperhive UI uses.
|
||||
|
||||
`flex-wrap` on the header and nav rather than a hamburger/collapse
|
||||
menu: a narrow viewport (phone or a tiled desktop window) wraps onto
|
||||
a second line instead of overflowing — "don't break, don't
|
||||
over-invest" is the explicit scope here, not full mobile navigation
|
||||
redesign.
|
||||
|
||||
Page-switch animation (see AppShell.tsx's file-top comment for the JS
|
||||
half): both the nav indicator's slide and the page-body pop follow
|
||||
the same three-rule motion-guard shape `@hive/shared/motion-apply.js`'s
|
||||
own doc comment specifies — a base rule,
|
||||
`@media (prefers-reduced-motion: reduce)` to respect the OS default,
|
||||
`:root[data-motion='reduce']` as the explicit override (same
|
||||
specificity as the media rule, so source order after it wins), then
|
||||
`:root[data-motion='allow']` last to re-enable despite an OS-level
|
||||
reduce preference. */
|
||||
/* Sticky + frosted glass (mara: "sticky header bar ... frosted glass")
|
||||
— same `color-mix` + `backdrop-filter` recipe `.terminal-wrap`/
|
||||
`.ui-badge` (@hive/shared) already use, dark-only same as those (a
|
||||
light bg behind glass washes out; `--glass-clear` in theme.css).
|
||||
`z-index` keeps it above `.shell-body` once content scrolls underneath. */
|
||||
.shell-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 1.5em;
|
||||
padding: 0.75em 1.25em;
|
||||
border-bottom: 1px solid var(--border);
|
||||
background: color-mix(
|
||||
in srgb,
|
||||
var(--bg-elev) calc(100% - 20% * var(--glass-clear)),
|
||||
transparent
|
||||
);
|
||||
-webkit-backdrop-filter: var(--glass-filter);
|
||||
backdrop-filter: var(--glass-filter);
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 10;
|
||||
}
|
||||
/* The swarm name links to the start page; the link adds no look of its own,
|
||||
so the brand's accent and glow (on the span inside) are all that show. */
|
||||
.shell-brand-link {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
.shell-brand {
|
||||
font-weight: 600;
|
||||
/* Base colour only — AppShell.tsx overrides it inline to the same
|
||||
currently-active-tab accent `.shell-nav-indicator` uses (falling
|
||||
back to this `--purple` before the first measurement lands, which
|
||||
is also the "hives" default route's own accent, so there's no
|
||||
visible mismatch at first paint). Transition lives here, same
|
||||
140ms hop cadence + the same three-rule motion guard as the
|
||||
indicator, so this and the indicator read as one thing changing,
|
||||
not two independently-synced ones. */
|
||||
color: var(--purple);
|
||||
letter-spacing: 0.02em;
|
||||
transition: color 140ms ease;
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.shell-brand {
|
||||
transition: none;
|
||||
}
|
||||
}
|
||||
:root[data-motion="reduce"] .shell-brand {
|
||||
transition: none;
|
||||
}
|
||||
:root[data-motion="allow"] .shell-brand {
|
||||
transition: color 140ms ease;
|
||||
}
|
||||
.shell-nav {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 1em;
|
||||
position: relative; /* anchor for .shell-nav-indicator */
|
||||
}
|
||||
/* A narrow screen (40em, the one width breakpoint the swarm UI has): the brand
|
||||
and the header's menus share the first row, and the nav takes the second as
|
||||
one row that scrolls sideways, instead of wrapping into two or three rows
|
||||
that cover a third of a phone. */
|
||||
@media (max-width: 40em) {
|
||||
.shell-header {
|
||||
gap: 0.25em 0.75em;
|
||||
padding: 0.5em 0.75em;
|
||||
}
|
||||
.shell-header-actions {
|
||||
order: 1;
|
||||
}
|
||||
.shell-nav {
|
||||
order: 2;
|
||||
flex: 1 0 100%;
|
||||
flex-wrap: nowrap;
|
||||
overflow-x: auto;
|
||||
scrollbar-width: none;
|
||||
}
|
||||
.shell-nav::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
.shell-nav-link,
|
||||
.shell-tabs {
|
||||
flex: none;
|
||||
white-space: nowrap;
|
||||
}
|
||||
}
|
||||
/* Holds `SettingsMenu` + `LinksMenu` — one `margin-left: auto` on the
|
||||
wrapper, not one on each child (see AppShell.tsx's comment for why two
|
||||
adjacent auto margins don't sit flush together). */
|
||||
.shell-header-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.25em;
|
||||
margin-left: auto;
|
||||
}
|
||||
/* Touch target and active-state underline are deliberately on two
|
||||
different elements. An earlier version put both `min-height: 2.75em`
|
||||
and `border-bottom` on the same box: centering the text within a
|
||||
2.75em box pushes a bottom-edge border ~0.7em away from the text,
|
||||
which argus caught by actually rendering it — a real visual
|
||||
regression, not just a touch-target win. So: `.shell-nav-link` (the
|
||||
`<a>`) owns the full-height tappable box (same 2.75em / ~44px WCAG
|
||||
2.5.5 floor as the rest of the shared kit — this is the primary nav,
|
||||
the single most-clicked/tapped element in the whole shell), and
|
||||
`.shell-nav-link-text` (an inner span hugging just the text) owns
|
||||
the underline, so the indicator stays directly under the label
|
||||
regardless of how tall the tappable box around it is. */
|
||||
.shell-nav-link {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-height: 2.75em;
|
||||
color: var(--muted);
|
||||
text-decoration: none;
|
||||
}
|
||||
.shell-nav-link:hover {
|
||||
color: var(--fg);
|
||||
}
|
||||
.shell-nav-link-text {
|
||||
padding-bottom: 0.25em; /* gap between the text and .shell-nav-indicator below it */
|
||||
}
|
||||
.shell-nav-link-active {
|
||||
color: var(--fg);
|
||||
}
|
||||
/* Shared underline, one element instead of one border-bottom per link
|
||||
— see AppShell.tsx's file-top comment. `left`/`top`/`width` are set
|
||||
inline per-render from a real measurement; only the *transition*
|
||||
between those values lives here. 140ms matches AppShell.tsx's `HOP_MS`
|
||||
exactly, so one hop's CSS transition finishes right as the next
|
||||
hop's JS-driven style update lands, instead of the two blending into
|
||||
an averaged easing curve. */
|
||||
.shell-nav-indicator {
|
||||
position: absolute;
|
||||
height: 2px;
|
||||
border-radius: 1px;
|
||||
transition:
|
||||
left 140ms ease,
|
||||
top 140ms ease,
|
||||
width 140ms ease,
|
||||
background-color 140ms ease;
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.shell-nav-indicator {
|
||||
transition: none;
|
||||
}
|
||||
}
|
||||
:root[data-motion="reduce"] .shell-nav-indicator {
|
||||
transition: none;
|
||||
}
|
||||
:root[data-motion="allow"] .shell-nav-indicator {
|
||||
transition:
|
||||
left 140ms ease,
|
||||
top 140ms ease,
|
||||
width 140ms ease,
|
||||
background-color 140ms ease;
|
||||
}
|
||||
/* Suppresses the transition for exactly one render — the indicator's
|
||||
very first real position, so it snaps into place instead of visibly
|
||||
growing from the unmounted `left:0/width:0/transparent` fallback (see
|
||||
AppShell.tsx's `indicatorSettledOnce`). Two-class selector so it wins
|
||||
over the plain `.shell-nav-indicator` rule above without `!important`,
|
||||
same reasoning `:root[data-motion='reduce']`'s extra attribute
|
||||
selector already relies on. */
|
||||
.shell-nav-indicator.shell-nav-indicator-instant {
|
||||
transition: none;
|
||||
}
|
||||
/* A skip link, drawn only while it has the keyboard focus. */
|
||||
.shell-skip-link {
|
||||
position: absolute;
|
||||
left: 0.5em;
|
||||
top: -4em;
|
||||
z-index: 100;
|
||||
padding: 0.5em 1em;
|
||||
border-radius: 0.3em;
|
||||
background: var(--bg-elev);
|
||||
color: var(--fg);
|
||||
border: 1px solid var(--purple);
|
||||
}
|
||||
.shell-skip-link:focus {
|
||||
top: 0.5em;
|
||||
}
|
||||
/* The page container takes the focus when a page opens, and is not a control. */
|
||||
.shell-body:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
/* One width for every tab's contents: 90em still centers with visible margins
|
||||
on a typical desktop viewport; it isn't full-bleed. */
|
||||
.shell-body {
|
||||
max-width: 90em;
|
||||
margin: 0 auto;
|
||||
padding: 1.5em 1.25em;
|
||||
/* Plain fade, deliberately — mara's call on review: a scale+overshoot
|
||||
"pop" (this rule's first cut) read as too much for a first pass.
|
||||
Keeping it simple leaves room to make this fancier later without
|
||||
having shipped something to walk back first. */
|
||||
animation: shell-page-enter 160ms ease;
|
||||
}
|
||||
/* Full-bleed route (the agent terminal): the shell becomes a viewport-high
|
||||
column, the header keeps its natural height, and the body takes every
|
||||
remaining pixel with no width cap or padding. `100dvh`, not `100vh` —
|
||||
`vh` includes mobile browser chrome that is on screen. */
|
||||
.shell-full {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100dvh;
|
||||
}
|
||||
.shell-full .shell-header {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.shell-body-full {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
max-width: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
@keyframes shell-page-enter {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.shell-body {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
:root[data-motion="reduce"] .shell-body {
|
||||
animation: none;
|
||||
}
|
||||
:root[data-motion="allow"] .shell-body {
|
||||
animation: shell-page-enter 160ms ease;
|
||||
}
|
||||
410
frontend/packages/shared/src/app-shell/AppShell.tsx
Normal file
410
frontend/packages/shared/src/app-shell/AppShell.tsx
Normal file
|
|
@ -0,0 +1,410 @@
|
|||
// <AppShell> — the page chrome of a hyperhive UI: a header bar with the brand,
|
||||
// a nav row linking the app's routes, the settings menu and the caller's own
|
||||
// menus, and the content column every route renders into. The swarm UI and the
|
||||
// hive UI both mount it, each with its own brand, tabs and menus.
|
||||
//
|
||||
// Routing stays the caller's: it passes the current `location` and the
|
||||
// `LinkComponent` that makes a link (a router's `Link`, or a plain anchor for
|
||||
// hash routes), so this package needs no router.
|
||||
//
|
||||
// Page-switch animation, two pieces (motion-guard rules live in
|
||||
// AppShell.css): `.shell-body` remounts on every navigation
|
||||
// (`key={location}`) to replay a plain fade-in, and a single shared underline
|
||||
// (`.shell-nav-indicator`) hops through every nav item it passes over on its
|
||||
// way to the new active one, position and colour together. Accent is a discrete
|
||||
// cycle through the base16 chromatic slots (each item's `accent`), not a
|
||||
// continuous hue rotation. `.shell-brand` rides the identical accent value
|
||||
// (`navAccent` below), so the header reads as one accent changing, not the
|
||||
// underline alone.
|
||||
import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import type { ComponentChildren, ComponentType } from "preact";
|
||||
import { SettingsMenu } from "../settings/SettingsMenu.js";
|
||||
import { useApplyThemeOverride } from "../settings/theme-apply.js";
|
||||
import { useApplyMotionOverride } from "../settings/motion-apply.js";
|
||||
import {
|
||||
SolidBackgroundsSetting,
|
||||
useApplySolidBackgrounds,
|
||||
} from "../settings/solid-backgrounds.js";
|
||||
import "./AppShell.css";
|
||||
|
||||
export interface NavItem {
|
||||
href: string;
|
||||
label: string;
|
||||
/** The CSS colour the underline and the brand take while this item is active. */
|
||||
accent: string;
|
||||
}
|
||||
|
||||
/** What the shell needs of a link: a router's `Link`, or an anchor. */
|
||||
export type LinkComponent = ComponentType<{
|
||||
href: string;
|
||||
className?: string;
|
||||
"aria-current"?: "page";
|
||||
children?: ComponentChildren;
|
||||
}>;
|
||||
|
||||
export interface AppShellProps {
|
||||
/** The name in the header and the document title. */
|
||||
brand: string;
|
||||
/** Where the brand links to. */
|
||||
brandHref: string;
|
||||
items: readonly NavItem[];
|
||||
/** The current route, as the `href` of the nav item it belongs to. */
|
||||
location: string;
|
||||
LinkComponent: LinkComponent;
|
||||
/** The page's own title for the document title and its heading; the nav label when absent. */
|
||||
pageLabel?: string;
|
||||
/** Whether the page gets a hidden `<h1>`; a page that draws its own heading says no. */
|
||||
heading?: boolean;
|
||||
/** The content fills the window below the header. */
|
||||
fullBleed?: boolean;
|
||||
/** More tabs after the nav items, drawn inside the nav row. */
|
||||
navExtra?: ComponentChildren;
|
||||
/** Changes when `navExtra` changes size, so the underline measures again. */
|
||||
extraKey?: string | number;
|
||||
/** The app's own entries in the settings menu, above the shared ones. */
|
||||
settings?: ComponentChildren;
|
||||
/** The app's menus after the settings menu (links, the signed-in user). */
|
||||
menus?: ComponentChildren;
|
||||
/** The browser-local keys of the shared theme, motion and solid-background settings. */
|
||||
themeKey: string;
|
||||
motionKey: string;
|
||||
solidKey: string;
|
||||
children: ComponentChildren;
|
||||
}
|
||||
|
||||
interface IndicatorRect {
|
||||
left: number;
|
||||
top: number;
|
||||
width: number;
|
||||
accent: string;
|
||||
}
|
||||
|
||||
// One hop's travel time — also the interval between hops, so each leg
|
||||
// finishes (per `.shell-nav-indicator`'s own CSS transition duration,
|
||||
// kept equal to this) before the next one starts rather than the two
|
||||
// blending into a single averaged easing curve.
|
||||
const HOP_MS = 140;
|
||||
|
||||
// `data-motion`/`prefers-reduced-motion` gate for the JS-driven hop
|
||||
// sequence — the CSS-only pieces (the fade, the indicator's own
|
||||
// transition) gate via the three-rule pattern in AppShell.css, but a
|
||||
// multi-step `setTimeout` sequence has no CSS equivalent to hook, so it
|
||||
// checks the same two sources directly. `data-motion` takes precedence
|
||||
// over the OS preference either direction (reduce *or* allow), same
|
||||
// override semantics `@hive/shared/motion-apply.js` establishes.
|
||||
function prefersReducedMotion(): boolean {
|
||||
const override = document.documentElement.dataset.motion;
|
||||
if (override === "reduce") return true;
|
||||
if (override === "allow") return false;
|
||||
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
||||
}
|
||||
|
||||
function NavLink({
|
||||
LinkComponent,
|
||||
href,
|
||||
label,
|
||||
active,
|
||||
accent,
|
||||
textRef,
|
||||
}: {
|
||||
LinkComponent: LinkComponent;
|
||||
href: string;
|
||||
label: string;
|
||||
active: boolean;
|
||||
accent: string;
|
||||
textRef: (el: HTMLSpanElement | null) => void;
|
||||
}) {
|
||||
return (
|
||||
<LinkComponent
|
||||
href={href}
|
||||
className="shell-nav-link"
|
||||
aria-current={active ? "page" : undefined}
|
||||
>
|
||||
{/* Touch target (min-height) lives on the <a> so the whole row is
|
||||
tappable. The underline lives on the shared
|
||||
`.shell-nav-indicator`, not here — this span is still what
|
||||
gets measured to position it, so it hugs just the text rather
|
||||
than the full-height tappable box. */}
|
||||
<span
|
||||
ref={textRef}
|
||||
class={"shell-nav-link-text" + (active ? " shell-nav-link-active" : "")}
|
||||
// Inline, not a CSS rule: the glow colour is this item's own
|
||||
// `accent` (NAV_ITEMS), a different value per nav item, the
|
||||
// same reason `.shell-nav-indicator`'s own colour is set this
|
||||
// way rather than in AppShell.css.
|
||||
style={
|
||||
active
|
||||
? {
|
||||
textShadow: `0 0 8px color-mix(in srgb, ${accent} 45%, transparent)`,
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
>
|
||||
{label}
|
||||
</span>
|
||||
</LinkComponent>
|
||||
);
|
||||
}
|
||||
|
||||
export function AppShell({
|
||||
brand,
|
||||
brandHref,
|
||||
items,
|
||||
location,
|
||||
LinkComponent,
|
||||
pageLabel,
|
||||
heading = true,
|
||||
fullBleed = false,
|
||||
navExtra,
|
||||
extraKey,
|
||||
settings,
|
||||
menus,
|
||||
themeKey,
|
||||
motionKey,
|
||||
solidKey,
|
||||
children,
|
||||
}: AppShellProps) {
|
||||
const navRef = useRef<HTMLElement | null>(null);
|
||||
const mainRef = useRef<HTMLElement | null>(null);
|
||||
const itemsKey = items.map((item) => item.href).join("\n");
|
||||
// Applied once here, not inside `SettingsMenu`: every route mounts through
|
||||
// this one shell, so the override takes effect whichever page is showing and
|
||||
// whether or not the menu was ever opened.
|
||||
useApplyThemeOverride(themeKey);
|
||||
useApplyMotionOverride(motionKey);
|
||||
useApplySolidBackgrounds(solidKey);
|
||||
const textRefs = useRef<Record<string, HTMLSpanElement | null>>({});
|
||||
const [indicator, setIndicator] = useState<IndicatorRect | null>(null);
|
||||
// The nav-item index the indicator is currently sitting at (or mid-hop
|
||||
// from) — distinct from `location`'s own index, since the two only
|
||||
// agree once every intermediate hop has landed. `-1` = not settled
|
||||
// anywhere yet (first mount) or on a route with no nav item (404).
|
||||
const settledIndexRef = useRef(-1);
|
||||
// False until the indicator's very first real position has committed —
|
||||
// gates `.shell-nav-indicator-instant` (see AppShell.css) so that first
|
||||
// placement snaps instead of *transitioning in* from the unmounted
|
||||
// `left:0/width:0/transparent` fallback, which read as "no underline at
|
||||
// all" on a fresh page load (reported live by mara, reproduced here with
|
||||
// a zoomed pixel-level screenshot check — real, not a rendering fluke).
|
||||
// Every subsequent change (a real navigation) still animates normally.
|
||||
const [indicatorSettledOnce, setIndicatorSettledOnce] = useState(false);
|
||||
|
||||
// Measures nav item `index`'s rect relative to `.shell-nav`.
|
||||
// `getBoundingClientRect()` on both elements at the same tick keeps
|
||||
// this scroll-position-agnostic — a difference of two
|
||||
// viewport-relative rects is scroll-invariant, no separate
|
||||
// scroll-offset bookkeeping needed.
|
||||
function measureIndex(index: number): IndicatorRect | null {
|
||||
const navEl = navRef.current;
|
||||
const item = items[index];
|
||||
const textEl = item ? textRefs.current[item.href] : null;
|
||||
if (!navEl || !item || !textEl) return null;
|
||||
const navRect = navEl.getBoundingClientRect();
|
||||
const textRect = textEl.getBoundingClientRect();
|
||||
return {
|
||||
// Plus the nav's own scroll: on a narrow screen it scrolls sideways, and
|
||||
// the indicator lives in the scrolled content.
|
||||
left: textRect.left - navRect.left + navEl.scrollLeft,
|
||||
top: textRect.bottom - navRect.top + navEl.scrollTop,
|
||||
width: textRect.width,
|
||||
accent: item.accent,
|
||||
};
|
||||
}
|
||||
|
||||
// Drives the indicator through every nav item between where it was
|
||||
// and the new active one — see the file-top comment for why (mara:
|
||||
// "navigating from a to c animates [a's colour] -> [b's colour] ->
|
||||
// [c's colour]"). Re-runs on navigation; a separate resize listener
|
||||
// below re-measures the already-settled position without re-hopping
|
||||
// (a reflow isn't a navigation).
|
||||
// The list changed (the components tab came or went), so the settled index
|
||||
// no longer names the same item: land directly on the active one again.
|
||||
useEffect(() => {
|
||||
settledIndexRef.current = -1;
|
||||
}, [itemsKey]);
|
||||
|
||||
useEffect(() => {
|
||||
const targetIndex = items.findIndex((item) => item.href === location);
|
||||
if (targetIndex === -1) {
|
||||
settledIndexRef.current = -1;
|
||||
setIndicator(null);
|
||||
return;
|
||||
}
|
||||
const fromIndex = settledIndexRef.current;
|
||||
// First mount, a route with no nav item just left, or reduced
|
||||
// motion: land directly, no intermediate hops to sweep through.
|
||||
if (fromIndex === -1 || prefersReducedMotion()) {
|
||||
settledIndexRef.current = targetIndex;
|
||||
setIndicator(measureIndex(targetIndex));
|
||||
return;
|
||||
}
|
||||
const step = targetIndex > fromIndex ? 1 : -1;
|
||||
let cancelled = false;
|
||||
let timeoutId: number | undefined;
|
||||
function hop(current: number) {
|
||||
const next = current + step;
|
||||
setIndicator(measureIndex(next));
|
||||
settledIndexRef.current = next;
|
||||
if (next !== targetIndex) {
|
||||
timeoutId = window.setTimeout(() => {
|
||||
if (!cancelled) hop(next);
|
||||
}, HOP_MS);
|
||||
}
|
||||
}
|
||||
hop(fromIndex);
|
||||
return () => {
|
||||
cancelled = true;
|
||||
if (timeoutId !== undefined) window.clearTimeout(timeoutId);
|
||||
};
|
||||
}, [location, itemsKey]);
|
||||
|
||||
// Fires the render *after* the indicator's first non-null commit —
|
||||
// deliberately one tick behind, not folded into the effect above,
|
||||
// because the instant/animated class has to still read "instant" on
|
||||
// the very render where `indicator` itself first goes non-null (that's
|
||||
// the snap this exists for); only from the render after that should
|
||||
// transitions be armed.
|
||||
useEffect(() => {
|
||||
if (indicator && !indicatorSettledOnce) setIndicatorSettledOnce(true);
|
||||
}, [indicator, indicatorSettledOnce]);
|
||||
|
||||
// Re-measures the current (already-settled) position on resize — the
|
||||
// nav's own `flex-wrap` means a link's position genuinely changes at
|
||||
// narrow widths, not just its route. Not part of the hop effect above
|
||||
// since a reflow shouldn't restart the sweep animation.
|
||||
useEffect(() => {
|
||||
function onResize() {
|
||||
if (settledIndexRef.current === -1) return;
|
||||
setIndicator(measureIndex(settledIndexRef.current));
|
||||
}
|
||||
window.addEventListener("resize", onResize);
|
||||
return () => window.removeEventListener("resize", onResize);
|
||||
}, []);
|
||||
|
||||
// Same re-measure, for the same "a reflow, not a navigation" reason: the
|
||||
// caller's extra tabs came or went, which shrinks or grows the row without a
|
||||
// navigation to trigger the hop effect's own re-measure.
|
||||
useEffect(() => {
|
||||
if (settledIndexRef.current === -1) return;
|
||||
setIndicator(measureIndex(settledIndexRef.current));
|
||||
}, [extraKey]);
|
||||
|
||||
// Per-route label so browser tabs of different pages tell apart: the page's
|
||||
// own title when it gave one, else its nav label. An unmatched path gets the
|
||||
// bare brand.
|
||||
const label =
|
||||
pageLabel ?? items.find((item) => item.href === location)?.label;
|
||||
useEffect(() => {
|
||||
document.title = label ? `${label} · ${brand}` : brand;
|
||||
}, [brand, label]);
|
||||
|
||||
// A new page starts at the top for the keyboard and a screen reader: the
|
||||
// focus goes to the page's container, not left on the link that was used.
|
||||
const firstPage = useRef(true);
|
||||
useEffect(() => {
|
||||
if (firstPage.current) {
|
||||
firstPage.current = false;
|
||||
return;
|
||||
}
|
||||
mainRef.current?.focus({ preventScroll: true });
|
||||
}, [location]);
|
||||
|
||||
// Same value the indicator itself is drawn with, including mid-sweep —
|
||||
// the brand text rides the identical hop sequence rather than
|
||||
// computing its own, so "the header's accent" reads as one thing
|
||||
// changing, not two things that happen to agree at rest.
|
||||
const navAccent = indicator?.accent ?? "var(--purple)";
|
||||
|
||||
return (
|
||||
<div class={"shell" + (fullBleed ? " shell-full" : "")}>
|
||||
<a
|
||||
class="shell-skip-link"
|
||||
href="#main"
|
||||
onClick={(e) => {
|
||||
// A plain hash would hand the click to the router; move the focus
|
||||
// here instead.
|
||||
e.preventDefault();
|
||||
mainRef.current?.focus();
|
||||
}}
|
||||
>
|
||||
skip to content
|
||||
</a>
|
||||
<header class="shell-header">
|
||||
<LinkComponent href={brandHref} className="shell-brand-link">
|
||||
<span
|
||||
class="shell-brand"
|
||||
// Same glow recipe as the active nav tab / panel titles, riding
|
||||
// `navAccent` — the same value `color` already uses here — so
|
||||
// the brand's glow tracks the identical hop sequence as its own
|
||||
// text colour instead of introducing a second, disagreeing
|
||||
// accent source.
|
||||
style={{
|
||||
color: navAccent,
|
||||
textShadow: `0 0 8px color-mix(in srgb, ${navAccent} 45%, transparent)`,
|
||||
}}
|
||||
>
|
||||
{brand}
|
||||
</span>
|
||||
</LinkComponent>
|
||||
<nav class="shell-nav" aria-label="main" ref={navRef}>
|
||||
{items.map((item) => (
|
||||
<NavLink
|
||||
LinkComponent={LinkComponent}
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
label={item.label}
|
||||
active={item.href === location}
|
||||
accent={item.accent}
|
||||
textRef={(el) => {
|
||||
textRefs.current[item.href] = el;
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
{/* Shared sliding underline — see the file-top comment. Hidden
|
||||
(zero width) rather than unmounted when there's no active
|
||||
nav item (404), so it doesn't pop in with a stale position
|
||||
the next time a real nav item becomes active. */}
|
||||
<span
|
||||
class={
|
||||
"shell-nav-indicator" +
|
||||
(indicatorSettledOnce ? "" : " shell-nav-indicator-instant")
|
||||
}
|
||||
style={{
|
||||
left: `${indicator?.left ?? 0}px`,
|
||||
top: `${indicator?.top ?? 0}px`,
|
||||
width: `${indicator?.width ?? 0}px`,
|
||||
backgroundColor: indicator?.accent ?? "transparent",
|
||||
}}
|
||||
/>
|
||||
{navExtra}
|
||||
</nav>
|
||||
{/* Single `margin-left: auto` on the wrapper, not on each menu
|
||||
individually — two adjacent flex items both set to
|
||||
`margin-left: auto` split the available space between them
|
||||
instead of sitting flush together at the right edge. */}
|
||||
<div class="shell-header-actions">
|
||||
<SettingsMenu themeKey={themeKey} motionKey={motionKey}>
|
||||
{settings}
|
||||
<SolidBackgroundsSetting storageKey={solidKey} />
|
||||
</SettingsMenu>
|
||||
{menus}
|
||||
</div>
|
||||
</header>
|
||||
{/* Keyed by route so it remounts (and replays its entrance
|
||||
animation) on every navigation — see the file-top comment. */}
|
||||
<main
|
||||
id="main"
|
||||
tabIndex={-1}
|
||||
ref={mainRef}
|
||||
class={"shell-body" + (fullBleed ? " shell-body-full" : "")}
|
||||
key={location}
|
||||
>
|
||||
{/* The page's name for a screen reader. Only the start page draws an
|
||||
`<h1>` of its own (the swarm's name), so it gets no second one;
|
||||
the document title is the only other place the name shows. */}
|
||||
{heading && <h1 class="ui-visually-hidden">{label ?? brand}</h1>}
|
||||
{children}
|
||||
</main>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in a new issue