From 6f73bf0c5597d3fda1393f7539291826346ba4b8 Mon Sep 17 00:00:00 2001 From: iris Date: Wed, 25 Nov 2026 22:25:40 +0100 Subject: [PATCH] frontend: one header menu for the links, user, settings and alerts popovers Part of the unification order in the UI sweep issue (step c). The four header menus each kept their own open state, dismiss hook and a copy of the popover CSS, and the copies had drifted (offset, padding, fade-in). A shared HeaderMenu now owns the state, the dismiss rules and the popover look; each menu supplies its button and its rows. The settings popover moves 0.15em lower and the settings and alerts popovers gain the fade-in. --- docs/5-project/web-ui/design-guide.md | 6 ++ frontend/packages/shared/package.json | 2 + .../shared/src/header-menu/HeaderMenu.css | 68 ++++++++++++++ .../shared/src/header-menu/HeaderMenu.tsx | 82 +++++++++++++++++ .../shared/src/jobq-graph/jobq-graph.css | 2 +- .../shared/src/links-menu/LinksMenu.css | 68 ++------------ .../shared/src/links-menu/LinksMenu.tsx | 56 ++++-------- .../shared/src/settings/SettingsMenu.css | 30 +------ .../shared/src/settings/SettingsMenu.tsx | 89 +++++++------------ .../shared/src/user-menu/UserMenu.css | 67 +------------- .../shared/src/user-menu/UserMenu.tsx | 51 ++++------- .../swarm-ui/src/shell/AlertsMenu.css | 33 +------ .../swarm-ui/src/shell/AlertsMenu.tsx | 87 ++++++++---------- .../swarm-ui/test/header-menu.test.mjs | 34 +++++++ 14 files changed, 310 insertions(+), 365 deletions(-) create mode 100644 frontend/packages/shared/src/header-menu/HeaderMenu.css create mode 100644 frontend/packages/shared/src/header-menu/HeaderMenu.tsx create mode 100644 frontend/packages/swarm-ui/test/header-menu.test.mjs diff --git a/docs/5-project/web-ui/design-guide.md b/docs/5-project/web-ui/design-guide.md index 4221624fe..ee4709fe8 100644 --- a/docs/5-project/web-ui/design-guide.md +++ b/docs/5-project/web-ui/design-guide.md @@ -155,6 +155,12 @@ list — this doc won't try to keep a duplicate inventory in sync. A yes/no option in a form uses `CheckboxField`, which sizes its row like the other fields, rather than a bare ``. +The header menus (links, user, settings and alerts) share one popover, +`HeaderMenu` in `@hive/shared`: it owns the open state, the dismiss rules (a +press outside or Escape closes it) and the popover look, and each menu supplies +its button and its rows. A new header menu uses it and doesn't write its own +popover stylesheet. + **Every new `ui/` component gets a demo section on `/components` the same day it lands** — no building block without a place to see it. One deliberate exception exists today: `FormField`, the internal label+ diff --git a/frontend/packages/shared/package.json b/frontend/packages/shared/package.json index c6c4e764a..1a528bdb0 100644 --- a/frontend/packages/shared/package.json +++ b/frontend/packages/shared/package.json @@ -42,6 +42,8 @@ "./warn-banner.css": "./src/warn-banner/WarnBanner.css", "./icon-button.js": "./src/icon-button/IconButton.tsx", "./icon-button.css": "./src/icon-button/IconButton.css", + "./header-menu.js": "./src/header-menu/HeaderMenu.tsx", + "./header-menu.css": "./src/header-menu/HeaderMenu.css", "./tone.js": "./src/tone.ts", "./badge.js": "./src/badge/Badge.tsx", "./badge.css": "./src/badge/Badge.css", diff --git a/frontend/packages/shared/src/header-menu/HeaderMenu.css b/frontend/packages/shared/src/header-menu/HeaderMenu.css new file mode 100644 index 000000000..c86ad77f1 --- /dev/null +++ b/frontend/packages/shared/src/header-menu/HeaderMenu.css @@ -0,0 +1,68 @@ +@import "../icon-button/IconButton.css"; +/* — the one popover every header menu (links, user, settings, + alerts) opens. Right-anchored: each trigger sits at the end of a header's + icon cluster, so a left anchor would push it off-screen. */ +.header-menu { + position: relative; + display: inline-block; +} +.header-menu-popover { + position: absolute; + top: calc(100% + 0.4em); + right: 0; + z-index: var(--z-popover); + display: flex; + flex-direction: column; + min-width: 12em; + padding: 0.4em; + border: 1px solid var(--border); + border-radius: var(--radius-md); + background: var(--bg-elev); + box-shadow: var(--shadow-pop); + animation: header-menu-enter 140ms ease; +} +/* A long list, such as the firing alerts: wide, and it scrolls. */ +.header-menu-wide { + gap: 0.4em; + width: min(26em, calc(100vw - 2em)); + max-height: 70vh; + overflow-y: auto; +} +/* Mount-triggered, not a state transition: the popover only exists in the DOM + while open, so a keyframe on entry is the right tool. */ +@keyframes header-menu-enter { + from { + opacity: 0; + transform: translateY(-0.25em) scale(0.98); + } + to { + opacity: 1; + transform: none; + } +} +@media (prefers-reduced-motion: reduce) { + .header-menu-popover { + animation: none; + } +} +:root[data-motion="reduce"] .header-menu-popover { + animation: none; +} +:root[data-motion="allow"] .header-menu-popover { + animation: header-menu-enter 140ms ease; +} +/* A row that is a link or a button. */ +.header-menu-item { + display: flex; + align-items: center; + gap: 0.5em; + min-height: 2.75em; + padding: 0.4em 0.6em; + border-radius: var(--radius-sm); + color: var(--fg); + text-decoration: none; + white-space: nowrap; +} +.header-menu-item:hover { + background: var(--bg); +} diff --git a/frontend/packages/shared/src/header-menu/HeaderMenu.tsx b/frontend/packages/shared/src/header-menu/HeaderMenu.tsx new file mode 100644 index 000000000..32a8f1533 --- /dev/null +++ b/frontend/packages/shared/src/header-menu/HeaderMenu.tsx @@ -0,0 +1,82 @@ +// — a header icon button that opens a popover. The links, user, +// settings and alerts menus all take this shape, so the open state, the +// dismiss rules and the popover's look live here once: a menu supplies its +// trigger and its rows and nothing else. +// +// A press outside or Escape closes it, and Escape puts the focus back on the +// button. `children` is a function of `close` for rows that shut the menu when +// picked (a link, a log-out), or plain content for rows that stay open. +// +// Styles live in `./HeaderMenu.css`, which each menu's own stylesheet +// `@import`s: the dashboard's JS bundle loads `.css` imports as text, so a +// component that imported it would not style there. +import type { ComponentChildren } from "preact"; +import { useRef, useState } from "preact/hooks"; +import { IconButton } from "../icon-button/IconButton.js"; +import { useDismiss } from "../use-dismiss.js"; + +export interface HeaderMenuProps { + /** The button's accessible name; also the popover's, unless `popoverLabel` is set. */ + label: string; + /** What the button shows: an icon, or the avatar's initial. */ + trigger: ComponentChildren; + popoverLabel?: string; + /** For the button: the user menu's avatar colour comes through these. */ + triggerClass?: string; + triggerStyle?: Record; + /** A class on the root, for a menu's own selectors. */ + class?: string; + /** A class on the popover, such as `header-menu-wide` for a long list. */ + popoverClass?: string; + children: ComponentChildren | ((close: () => void) => ComponentChildren); +} + +export function HeaderMenu({ + label, + trigger, + popoverLabel, + triggerClass, + triggerStyle, + class: rootClass, + popoverClass, + children, +}: HeaderMenuProps) { + const [open, setOpen] = useState(false); + const rootRef = useRef(null); + const close = () => setOpen(false); + + useDismiss({ + open, + onDismiss: close, + inside: [rootRef], + focusOnEscape: () => rootRef.current?.querySelector("button") ?? null, + }); + + return ( +
+ setOpen((v) => !v)} + expanded={open} + > + {trigger} + + {open ? ( +
+ {typeof children === "function" ? children(close) : children} +
+ ) : null} +
+ ); +} diff --git a/frontend/packages/shared/src/jobq-graph/jobq-graph.css b/frontend/packages/shared/src/jobq-graph/jobq-graph.css index 1cb9d20d1..1d753d554 100644 --- a/frontend/packages/shared/src/jobq-graph/jobq-graph.css +++ b/frontend/packages/shared/src/jobq-graph/jobq-graph.css @@ -44,7 +44,7 @@ creates a new `.jg-node` DOM node when its `key` (the node id) is genuinely new, so this only plays once per node, the first time it appears in a fetched snapshot. Same shape as Shell.css's - `shell-page-enter` / the links menu's `links-menu-popover-enter` — + `shell-page-enter` / the header menu's `header-menu-enter` — see either for the three-rule motion-guard this mirrors. */ .jg-node { margin: 0; diff --git a/frontend/packages/shared/src/links-menu/LinksMenu.css b/frontend/packages/shared/src/links-menu/LinksMenu.css index c4989ff09..57b109c11 100644 --- a/frontend/packages/shared/src/links-menu/LinksMenu.css +++ b/frontend/packages/shared/src/links-menu/LinksMenu.css @@ -1,63 +1,5 @@ -@import "../icon-button/IconButton.css"; -/* (@hive/shared) — a header icon button (the shared `IconButton`) - and its popover. No `margin-left: auto` here: the mounting page's header owns - pushing the action group to the right edge (the swarm UI's - `.shell-header-actions`, the dashboard's `.dashboard-chrome-actions`). */ -.links-menu { - position: relative; -} -.links-menu-popover { - position: absolute; - top: calc(100% + 0.4em); - right: 0; - z-index: var(--z-popover); - display: flex; - flex-direction: column; - min-width: 10em; - padding: 0.4em; - border: 1px solid var(--border); - border-radius: var(--radius-md); - background: var(--bg-elev); - box-shadow: var(--shadow-pop); - animation: links-menu-popover-enter 140ms ease; -} -/* Mount-triggered, not a state transition — the popover only exists in - the DOM while open (`{open ?
: null}`), so a keyframe animation - on entry is the right tool, same shape as `.shell-body`'s own - `shell-page-enter` (Shell.css) — that's the precedent every popover - in this issue's "menus should animate" ask follows. */ -@keyframes links-menu-popover-enter { - from { - opacity: 0; - transform: translateY(-0.25em) scale(0.98); - } - to { - opacity: 1; - transform: none; - } -} -@media (prefers-reduced-motion: reduce) { - .links-menu-popover { - animation: none; - } -} -:root[data-motion="reduce"] .links-menu-popover { - animation: none; -} -:root[data-motion="allow"] .links-menu-popover { - animation: links-menu-popover-enter 140ms ease; -} -.links-menu-item { - display: flex; - align-items: center; - gap: 0.5em; - min-height: 2.75em; - padding: 0.4em 0.6em; - border-radius: var(--radius-sm); - color: var(--fg); - text-decoration: none; - white-space: nowrap; -} -.links-menu-item:hover { - background: var(--bg); -} +/* (@hive/shared) — a `HeaderMenu` of links; the popover and the + link row are `HeaderMenu.css`'s. No `margin-left: auto` here: the mounting + page's header owns pushing the action group to the right edge (the swarm + UI's `.shell-header-actions`, the dashboard's `.dashboard-chrome-actions`). */ +@import "../header-menu/HeaderMenu.css"; diff --git a/frontend/packages/shared/src/links-menu/LinksMenu.tsx b/frontend/packages/shared/src/links-menu/LinksMenu.tsx index f4086357b..942dd45e2 100644 --- a/frontend/packages/shared/src/links-menu/LinksMenu.tsx +++ b/frontend/packages/shared/src/links-menu/LinksMenu.tsx @@ -10,10 +10,8 @@ // imports or `@import`s itself: the dashboard's JS bundle loads `.css` // imports as text, so a component that imported it would not style there // (the same split as `UserMenu`). -import { useRef, useState } from "preact/hooks"; -import { IconButton } from "../icon-button/IconButton.js"; +import { HeaderMenu } from "../header-menu/HeaderMenu.js"; import { LinkIcon } from "../icons.js"; -import { useDismiss } from "../use-dismiss.js"; import { linkAttrs, type MenuLink } from "./links.js"; export type { MenuLink }; @@ -26,44 +24,24 @@ export function LinksMenu({ /** The button's accessible name. */ label?: string; }) { - const [open, setOpen] = useState(false); - const rootRef = useRef(null); - - // A press outside or Escape closes it; Escape puts the focus back on the button. - useDismiss({ - open, - onDismiss: () => setOpen(false), - inside: [rootRef], - focusOnEscape: () => rootRef.current?.querySelector("button") ?? null, - }); - if (links.length === 0) return null; return ( - + }> + {(close) => + links.map((link) => ( + + {link.icon ? : null} + {link.label} + + )) + } + ); } diff --git a/frontend/packages/shared/src/settings/SettingsMenu.css b/frontend/packages/shared/src/settings/SettingsMenu.css index 6b82bb568..29b1b039a 100644 --- a/frontend/packages/shared/src/settings/SettingsMenu.css +++ b/frontend/packages/shared/src/settings/SettingsMenu.css @@ -1,31 +1,5 @@ -@import "../icon-button/IconButton.css"; -/* popover — matches `@hive/shared`'s `Dropdown` - (`../dropdown/Dropdown.css`, `.ui-dropdown`) values exactly - (background/border/radius/shadow): the values are reused, not the - component, because this popover holds ` setTheme((e.target as HTMLSelectElement).value)} - > - {THEME_OPTIONS.map((o) => ( - - ))} - - - - {children} -
- ) : null} - + }> + + + {children} + ); } diff --git a/frontend/packages/shared/src/user-menu/UserMenu.css b/frontend/packages/shared/src/user-menu/UserMenu.css index df8916648..354b64cd4 100644 --- a/frontend/packages/shared/src/user-menu/UserMenu.css +++ b/frontend/packages/shared/src/user-menu/UserMenu.css @@ -1,14 +1,7 @@ -@import "../icon-button/IconButton.css"; -/* (@hive/shared) — same header icon-button + popover chrome as - `LinksMenu`/`SettingsMenu`, except the trigger is a round, accent- - coloured initials avatar rather than a glyph-on-transparent button — - this is the "who is this" affordance, the other two are quiet chrome. - Kept as its own stylesheet for the same reason `SettingsMenu.css` - gives: three small, independently-evolving popovers that happen to - look alike today, not one component with three skins. */ -.user-menu { - position: relative; -} +@import "../header-menu/HeaderMenu.css"; +/* (@hive/shared) — a `HeaderMenu` whose trigger is a round, accent- + coloured initials avatar rather than a glyph on a transparent button: this is + the "who is this" affordance, the other menus are quiet chrome. */ /* The round shape and the open outline are the shared icon button's; the fill is the accent inline on the element. */ .ui-icon-button.user-menu-button { @@ -20,45 +13,6 @@ .ui-icon-button.user-menu-button[aria-expanded="true"] { outline: 1px solid var(--fg); } -.user-menu-popover { - position: absolute; - top: calc(100% + 0.4em); - right: 0; - z-index: var(--z-popover); - display: flex; - flex-direction: column; - min-width: 12em; - padding: 0.4em; - border: 1px solid var(--border); - border-radius: var(--radius-md); - background: var(--bg-elev); - box-shadow: var(--shadow-pop); - animation: user-menu-popover-enter 140ms ease; -} -/* Mount-triggered, same shape as `LinksMenu.css`'s own version — see - its comment for why a keyframe (not a transition) is the right tool - here. */ -@keyframes user-menu-popover-enter { - from { - opacity: 0; - transform: translateY(-0.25em) scale(0.98); - } - to { - opacity: 1; - transform: none; - } -} -@media (prefers-reduced-motion: reduce) { - .user-menu-popover { - animation: none; - } -} -:root[data-motion="reduce"] .user-menu-popover { - animation: none; -} -:root[data-motion="allow"] .user-menu-popover { - animation: user-menu-popover-enter 140ms ease; -} .user-menu-name { padding: 0.4em 0.6em 0.5em; border-bottom: 1px solid var(--border); @@ -69,16 +23,3 @@ overflow: hidden; text-overflow: ellipsis; } -.user-menu-item { - display: flex; - align-items: center; - min-height: 2.75em; - padding: 0.4em 0.6em; - border-radius: var(--radius-sm); - color: var(--fg); - text-decoration: none; - white-space: nowrap; -} -.user-menu-item:hover { - background: var(--bg); -} diff --git a/frontend/packages/shared/src/user-menu/UserMenu.tsx b/frontend/packages/shared/src/user-menu/UserMenu.tsx index 090bb2b55..da0fdac00 100644 --- a/frontend/packages/shared/src/user-menu/UserMenu.tsx +++ b/frontend/packages/shared/src/user-menu/UserMenu.tsx @@ -15,9 +15,8 @@ // Styles live in `@hive/shared/user-menu.css`, which the mounting page // imports or `@import`s itself: the dashboard's JS bundle loads `.css` // imports as text, so a component that imported it would not style there. -import { useDismiss } from "../use-dismiss.js"; -import { useEffect, useRef, useState } from "preact/hooks"; -import { IconButton } from "../icon-button/IconButton.js"; +import { useEffect, useState } from "preact/hooks"; +import { HeaderMenu } from "../header-menu/HeaderMenu.js"; import { accountLinks, initialOf, @@ -42,8 +41,6 @@ export function UserMenu({ onLogout?: () => void; }) { const [who, setWho] = useState(null); - const [open, setOpen] = useState(false); - const rootRef = useRef(null); useEffect(() => { let cancelled = false; @@ -62,56 +59,46 @@ export function UserMenu({ }; }, [whoamiUrl]); - // A press outside or Escape closes it; Escape puts the focus back on the button. - useDismiss({ - open, - onDismiss: () => setOpen(false), - inside: [rootRef], - focusOnEscape: () => rootRef.current?.querySelector("button") ?? null, - }); - const label = labelOf(who); const links = accountLinks(autheliaUrl); return ( -
- setOpen((v) => !v)} - expanded={open} - > - {initialOf(label)} - - {open ? ( - - ) : null} -
+ + )} +
); } diff --git a/frontend/packages/swarm-ui/src/shell/AlertsMenu.css b/frontend/packages/swarm-ui/src/shell/AlertsMenu.css index d8c7a4476..22ddb7d7f 100644 --- a/frontend/packages/swarm-ui/src/shell/AlertsMenu.css +++ b/frontend/packages/swarm-ui/src/shell/AlertsMenu.css @@ -1,8 +1,6 @@ /* — the header bell (the shared icon button) with a count badge, - and a popover shaped like the links menu's. */ -.alerts-menu { - position: relative; -} + and the shared `HeaderMenu` popover for the list. */ +@import "@hive/shared/header-menu.css"; .alerts-menu .ui-icon-button { position: relative; color: var(--red); @@ -21,23 +19,6 @@ line-height: 1.2; text-align: center; } -.alerts-menu-popover { - position: absolute; - top: calc(100% + 0.4em); - right: 0; - z-index: var(--z-popover); - display: flex; - flex-direction: column; - gap: 0.4em; - width: min(26em, calc(100vw - 2em)); - max-height: 70vh; - padding: 0.4em; - overflow-y: auto; - border: 1px solid var(--border); - border-radius: var(--radius-md); - background: var(--bg-elev); - box-shadow: var(--shadow-pop); -} .alerts-menu-list { display: flex; flex-direction: column; @@ -65,13 +46,3 @@ flex-basis: 100%; overflow-wrap: anywhere; } -.alerts-menu-link { - display: flex; - align-items: center; - min-height: 2.75em; - padding: 0.4em 0.6em; - border-radius: var(--radius-sm); -} -.alerts-menu-link:hover { - background: var(--bg); -} diff --git a/frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx b/frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx index 03b56eee2..2764ad373 100644 --- a/frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx +++ b/frontend/packages/swarm-ui/src/shell/AlertsMenu.tsx @@ -3,11 +3,8 @@ // alert fires, and while the alerts can't be read (the swarm may have no // Grafana): no button beats a dead one. It polls `GET /api/alerts`, the same // list the reports tab shows. -import { useRef, useState } from "preact/hooks"; import { BellIcon } from "@hive/shared/icons.js"; -import { IconButton } from "@hive/shared/icon-button.js"; -import { useDismiss } from "@hive/shared/use-dismiss.js"; -import "@hive/shared/icon-button.css"; +import { HeaderMenu } from "@hive/shared/header-menu.js"; import { ExternalLink } from "../ui/external-link/ExternalLink.js"; import { fetchJson, usePolled } from "../ui/polled/usePolled.js"; import { RelativeTime } from "../ui/relative-time/RelativeTime.js"; @@ -38,54 +35,42 @@ export function AlertsMenu() { } function AlertsButton({ firing }: { firing: Alert[] }) { - const [open, setOpen] = useState(false); - const rootRef = useRef(null); - - useDismiss({ - open, - onDismiss: () => setOpen(false), - inside: [rootRef], - focusOnEscape: () => rootRef.current?.querySelector("button") ?? null, - }); - const label = alertsLabel(firing.length); return ( -
- setOpen((v) => !v)} - expanded={open} - > - - - - {open ? ( -
-
    - {firing.map((a) => { - const since = activeSinceMs(a); - return ( -
  • - {a.name} - {since !== null ? ( - - - - ) : null} - {a.summary ? ( - {a.summary} - ) : null} -
  • - ); - })} -
- - open Grafana's alert list - -
- ) : null} -
+ + + + + } + > +
    + {firing.map((a) => { + const since = activeSinceMs(a); + return ( +
  • + {a.name} + {since !== null ? ( + + + + ) : null} + {a.summary ? ( + {a.summary} + ) : null} +
  • + ); + })} +
+ + open Grafana's alert list + +
); } diff --git a/frontend/packages/swarm-ui/test/header-menu.test.mjs b/frontend/packages/swarm-ui/test/header-menu.test.mjs new file mode 100644 index 000000000..3ebb8125f --- /dev/null +++ b/frontend/packages/swarm-ui/test/header-menu.test.mjs @@ -0,0 +1,34 @@ +// The header menus share one popover. Run by `npm test`. Each menu's +// stylesheet must pull in `HeaderMenu.css` and keep no popover of its own, so +// a fifth menu can't quietly bring back a copy with its own offset or shadow. +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { test } from "node:test"; + +const read = (rel) => readFileSync(new URL(rel, import.meta.url), "utf8"); + +const MENUS = [ + ["links", "../../shared/src/links-menu/LinksMenu.css"], + ["user", "../../shared/src/user-menu/UserMenu.css"], + ["settings", "../../shared/src/settings/SettingsMenu.css"], + ["alerts", "../src/shell/AlertsMenu.css"], +]; + +for (const [name, file] of MENUS) { + test(`the ${name} menu uses the shared popover`, () => { + const css = read(file); + assert.match(css, /@import "[^"]*header-menu(\/HeaderMenu)?\.css";/); + assert.ok(!/-popover\s*\{/.test(css), `${name}: has its own popover rule`); + assert.ok( + !/position:\s*absolute;[\s\S]*?z-index/.test( + css.replace(/\.alerts-menu-count \{[\s\S]*?\}/, ""), + ), + ); + }); +} + +test("the shared popover is the one that floats", () => { + const css = read("../../shared/src/header-menu/HeaderMenu.css"); + assert.match(css, /\.header-menu-popover \{[^}]*z-index: var\(--z-popover\)/); + assert.match(css, /box-shadow: var\(--shadow-pop\)/); +});