Watch
0
0
Fork
You've already forked hyperhive
0

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.
This commit is contained in:
iris 2026-11-25 22:25:40 +01:00
commit 6f73bf0c55
14 changed files with 310 additions and 365 deletions

View file

@ -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 option in a form uses `CheckboxField`, which sizes its row like the other
fields, rather than a bare `<input type="checkbox">`. fields, rather than a bare `<input type="checkbox">`.
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 **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 same day it lands** — no building block without a place to see it. One
deliberate exception exists today: `FormField`, the internal label+ deliberate exception exists today: `FormField`, the internal label+

View file

@ -42,6 +42,8 @@
"./warn-banner.css": "./src/warn-banner/WarnBanner.css", "./warn-banner.css": "./src/warn-banner/WarnBanner.css",
"./icon-button.js": "./src/icon-button/IconButton.tsx", "./icon-button.js": "./src/icon-button/IconButton.tsx",
"./icon-button.css": "./src/icon-button/IconButton.css", "./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", "./tone.js": "./src/tone.ts",
"./badge.js": "./src/badge/Badge.tsx", "./badge.js": "./src/badge/Badge.tsx",
"./badge.css": "./src/badge/Badge.css", "./badge.css": "./src/badge/Badge.css",

View file

@ -0,0 +1,68 @@
@import "../icon-button/IconButton.css";
/* <HeaderMenu> — 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);
}

View file

@ -0,0 +1,82 @@
// <HeaderMenu> — 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<string, string>;
/** 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<HTMLDivElement>(null);
const close = () => setOpen(false);
useDismiss({
open,
onDismiss: close,
inside: [rootRef],
focusOnEscape: () => rootRef.current?.querySelector("button") ?? null,
});
return (
<div
class={["header-menu", rootClass].filter(Boolean).join(" ")}
ref={rootRef}
>
<IconButton
class={triggerClass}
style={triggerStyle}
label={label}
onClick={() => setOpen((v) => !v)}
expanded={open}
>
{trigger}
</IconButton>
{open ? (
<div
class={["header-menu-popover", popoverClass]
.filter(Boolean)
.join(" ")}
role="group"
aria-label={popoverLabel ?? label}
>
{typeof children === "function" ? children(close) : children}
</div>
) : null}
</div>
);
}

View file

@ -44,7 +44,7 @@
creates a new `.jg-node` DOM node when its `key` (the node id) is 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 genuinely new, so this only plays once per node, the first time it
appears in a fetched snapshot. Same shape as Shell.css's 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. */ see either for the three-rule motion-guard this mirrors. */
.jg-node { .jg-node {
margin: 0; margin: 0;

View file

@ -1,63 +1,5 @@
@import "../icon-button/IconButton.css"; /* <LinksMenu> (@hive/shared) — a `HeaderMenu` of links; the popover and the
/* <LinksMenu> (@hive/shared) — a header icon button (the shared `IconButton`) link row are `HeaderMenu.css`'s. No `margin-left: auto` here: the mounting
and its popover. No `margin-left: auto` here: the mounting page's header owns page's header owns pushing the action group to the right edge (the swarm
pushing the action group to the right edge (the swarm UI's UI's `.shell-header-actions`, the dashboard's `.dashboard-chrome-actions`). */
`.shell-header-actions`, the dashboard's `.dashboard-chrome-actions`). */ @import "../header-menu/HeaderMenu.css";
.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 ? <div> : 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);
}

View file

@ -10,10 +10,8 @@
// imports or `@import`s itself: the dashboard's JS bundle loads `.css` // 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 // imports as text, so a component that imported it would not style there
// (the same split as `UserMenu`). // (the same split as `UserMenu`).
import { useRef, useState } from "preact/hooks"; import { HeaderMenu } from "../header-menu/HeaderMenu.js";
import { IconButton } from "../icon-button/IconButton.js";
import { LinkIcon } from "../icons.js"; import { LinkIcon } from "../icons.js";
import { useDismiss } from "../use-dismiss.js";
import { linkAttrs, type MenuLink } from "./links.js"; import { linkAttrs, type MenuLink } from "./links.js";
export type { MenuLink }; export type { MenuLink };
@ -26,44 +24,24 @@ export function LinksMenu({
/** The button's accessible name. */ /** The button's accessible name. */
label?: string; label?: string;
}) { }) {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(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; if (links.length === 0) return null;
return ( return (
<div class="links-menu" ref={rootRef}> <HeaderMenu label={label} trigger={<LinkIcon />}>
<IconButton {(close) =>
label={label} links.map((link) => (
onClick={() => setOpen((v) => !v)} <a
expanded={open} key={link.url}
> class="header-menu-item"
<LinkIcon /> href={link.url}
</IconButton> {...linkAttrs(link)}
{open ? ( onClick={close}
<div class="links-menu-popover" role="group" aria-label={label}> >
{links.map((link) => ( {link.icon ? <span aria-hidden="true">{link.icon}</span> : null}
<a {link.label}
key={link.url} </a>
class="links-menu-item" ))
href={link.url} }
{...linkAttrs(link)} </HeaderMenu>
onClick={() => setOpen(false)}
>
{link.icon ? <span aria-hidden="true">{link.icon}</span> : null}
{link.label}
</a>
))}
</div>
) : null}
</div>
); );
} }

View file

@ -1,31 +1,5 @@
@import "../icon-button/IconButton.css"; @import "../header-menu/HeaderMenu.css";
/* <SettingsMenu> popover — matches `@hive/shared`'s `Dropdown` /* <SettingsMenu> — the theme and motion rows inside a `HeaderMenu` popover. */
(`../dropdown/Dropdown.css`, `.ui-dropdown`) values exactly
(background/border/radius/shadow): the values are reused, not the
component, because this popover holds `<select>` rows, not `Dropdown`'s
command-dispatch `<button>`s. Right-anchored — this trigger lives at
the end of a header's icon-trigger cluster in every page that mounts
it, so a left anchor would push it off-screen. */
.settings-menu-anchor {
position: relative;
display: inline-block;
}
.settings-menu-popover {
position: absolute;
top: calc(100% + 0.25em);
right: 0;
left: auto;
z-index: var(--z-popover);
display: flex;
flex-direction: column;
gap: 0.15em;
min-width: 12em;
padding: 0.5em;
background: var(--bg-elev);
border: 1px solid var(--border);
border-radius: var(--radius-md);
box-shadow: var(--shadow-pop);
}
.settings-menu-row { .settings-menu-row {
display: flex; display: flex;
align-items: center; align-items: center;

View file

@ -23,11 +23,9 @@
// once, high in its tree, with the SAME key strings passed to this // once, high in its tree, with the SAME key strings passed to this
// component, so the two stay in sync without this component owning any // component, so the two stay in sync without this component owning any
// page-specific naming decision. // page-specific naming decision.
import { useRef, useState } from "preact/hooks";
import type { ComponentChildren } from "preact"; import type { ComponentChildren } from "preact";
import { IconButton } from "../icon-button/IconButton.js"; import { HeaderMenu } from "../header-menu/HeaderMenu.js";
import { GearIcon } from "../icons.js"; import { GearIcon } from "../icons.js";
import { useDismiss } from "../use-dismiss.js";
import { useThemeOverride } from "./theme-apply.js"; import { useThemeOverride } from "./theme-apply.js";
import { AUTO_THEME, THEMES } from "./themes.js"; import { AUTO_THEME, THEMES } from "./themes.js";
import { useMotionOverride, type MotionOverride } from "./motion-apply.js"; import { useMotionOverride, type MotionOverride } from "./motion-apply.js";
@ -61,63 +59,40 @@ export function SettingsMenu({
motionKey, motionKey,
children, children,
}: SettingsMenuProps) { }: SettingsMenuProps) {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
const [theme, setTheme] = useThemeOverride(themeKey); const [theme, setTheme] = useThemeOverride(themeKey);
const [motion, setMotion] = useMotionOverride(motionKey); const [motion, setMotion] = useMotionOverride(motionKey);
// 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,
});
return ( return (
<div class="settings-menu-anchor" ref={rootRef}> <HeaderMenu label="settings" trigger={<GearIcon />}>
<IconButton <label class="settings-menu-row">
label="settings" <span>theme</span>
onClick={() => setOpen((v) => !v)} <select
expanded={open} value={theme}
> onChange={(e) => setTheme((e.target as HTMLSelectElement).value)}
<GearIcon /> >
</IconButton> {THEME_OPTIONS.map((o) => (
{open ? ( <option key={o.id} value={o.id}>
<div class="settings-menu-popover" role="group" aria-label="settings"> {o.label}
<label class="settings-menu-row"> </option>
<span>theme</span> ))}
<select </select>
value={theme} </label>
onChange={(e) => setTheme((e.target as HTMLSelectElement).value)} <label class="settings-menu-row">
> <span>motion</span>
{THEME_OPTIONS.map((o) => ( <select
<option key={o.id} value={o.id}> value={motion}
{o.label} onChange={(e) =>
</option> setMotion((e.target as HTMLSelectElement).value as MotionOverride)
))} }
</select> >
</label> {MOTION_OPTIONS.map((o) => (
<label class="settings-menu-row"> <option key={o} value={o}>
<span>motion</span> {o}
<select </option>
value={motion} ))}
onChange={(e) => </select>
setMotion( </label>
(e.target as HTMLSelectElement).value as MotionOverride, {children}
) </HeaderMenu>
}
>
{MOTION_OPTIONS.map((o) => (
<option key={o} value={o}>
{o}
</option>
))}
</select>
</label>
{children}
</div>
) : null}
</div>
); );
} }

View file

@ -1,14 +1,7 @@
@import "../icon-button/IconButton.css"; @import "../header-menu/HeaderMenu.css";
/* <UserMenu> (@hive/shared) — same header icon-button + popover chrome as /* <UserMenu> (@hive/shared) — a `HeaderMenu` whose trigger is a round, accent-
`LinksMenu`/`SettingsMenu`, except the trigger is a round, accent- coloured initials avatar rather than a glyph on a transparent button: this is
coloured initials avatar rather than a glyph-on-transparent button — the "who is this" affordance, the other menus are quiet chrome. */
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;
}
/* The round shape and the open outline are the shared icon button's; the fill is /* The round shape and the open outline are the shared icon button's; the fill is
the accent inline on the element. */ the accent inline on the element. */
.ui-icon-button.user-menu-button { .ui-icon-button.user-menu-button {
@ -20,45 +13,6 @@
.ui-icon-button.user-menu-button[aria-expanded="true"] { .ui-icon-button.user-menu-button[aria-expanded="true"] {
outline: 1px solid var(--fg); 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 { .user-menu-name {
padding: 0.4em 0.6em 0.5em; padding: 0.4em 0.6em 0.5em;
border-bottom: 1px solid var(--border); border-bottom: 1px solid var(--border);
@ -69,16 +23,3 @@
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; 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);
}

View file

@ -15,9 +15,8 @@
// Styles live in `@hive/shared/user-menu.css`, which the mounting page // 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 or `@import`s itself: the dashboard's JS bundle loads `.css`
// imports as text, so a component that imported it would not style there. // imports as text, so a component that imported it would not style there.
import { useDismiss } from "../use-dismiss.js"; import { useEffect, useState } from "preact/hooks";
import { useEffect, useRef, useState } from "preact/hooks"; import { HeaderMenu } from "../header-menu/HeaderMenu.js";
import { IconButton } from "../icon-button/IconButton.js";
import { import {
accountLinks, accountLinks,
initialOf, initialOf,
@ -42,8 +41,6 @@ export function UserMenu({
onLogout?: () => void; onLogout?: () => void;
}) { }) {
const [who, setWho] = useState<WhoAmI | null>(null); const [who, setWho] = useState<WhoAmI | null>(null);
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
useEffect(() => { useEffect(() => {
let cancelled = false; let cancelled = false;
@ -62,56 +59,46 @@ export function UserMenu({
}; };
}, [whoamiUrl]); }, [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 label = labelOf(who);
const links = accountLinks(autheliaUrl); const links = accountLinks(autheliaUrl);
return ( return (
<div class="user-menu" ref={rootRef}> <HeaderMenu
<IconButton class="user-menu"
class="user-menu-button" triggerClass="user-menu-button"
style={{ background: pickAccent(label ?? "operator") }} triggerStyle={{ background: pickAccent(label ?? "operator") }}
label={label ? `signed in as ${label}` : "account"} label={label ? `signed in as ${label}` : "account"}
onClick={() => setOpen((v) => !v)} popoverLabel="account"
expanded={open} trigger={initialOf(label)}
> >
{initialOf(label)} {(close) => (
</IconButton> <>
{open ? (
<div class="user-menu-popover" role="group" aria-label="account">
{label ? <div class="user-menu-name">{label}</div> : null} {label ? <div class="user-menu-name">{label}</div> : null}
{links ? ( {links ? (
<> <>
<a <a
class="user-menu-item" class="header-menu-item"
href={links.settings} href={links.settings}
target="_blank" target="_blank"
rel="noreferrer" rel="noreferrer"
onClick={() => setOpen(false)} onClick={close}
> >
authelia settings authelia settings
</a> </a>
<a <a
class="user-menu-item" class="header-menu-item"
href={links.logout} href={links.logout}
onClick={() => { onClick={() => {
onLogout?.(); onLogout?.();
setOpen(false); close();
}} }}
> >
log out log out
</a> </a>
</> </>
) : null} ) : null}
</div> </>
) : null} )}
</div> </HeaderMenu>
); );
} }

View file

@ -1,8 +1,6 @@
/* <AlertsMenu> — the header bell (the shared icon button) with a count badge, /* <AlertsMenu> — the header bell (the shared icon button) with a count badge,
and a popover shaped like the links menu's. */ and the shared `HeaderMenu` popover for the list. */
.alerts-menu { @import "@hive/shared/header-menu.css";
position: relative;
}
.alerts-menu .ui-icon-button { .alerts-menu .ui-icon-button {
position: relative; position: relative;
color: var(--red); color: var(--red);
@ -21,23 +19,6 @@
line-height: 1.2; line-height: 1.2;
text-align: center; 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 { .alerts-menu-list {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -65,13 +46,3 @@
flex-basis: 100%; flex-basis: 100%;
overflow-wrap: anywhere; 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);
}

View file

@ -3,11 +3,8 @@
// alert fires, and while the alerts can't be read (the swarm may have no // 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 // Grafana): no button beats a dead one. It polls `GET /api/alerts`, the same
// list the reports tab shows. // list the reports tab shows.
import { useRef, useState } from "preact/hooks";
import { BellIcon } from "@hive/shared/icons.js"; import { BellIcon } from "@hive/shared/icons.js";
import { IconButton } from "@hive/shared/icon-button.js"; import { HeaderMenu } from "@hive/shared/header-menu.js";
import { useDismiss } from "@hive/shared/use-dismiss.js";
import "@hive/shared/icon-button.css";
import { ExternalLink } from "../ui/external-link/ExternalLink.js"; import { ExternalLink } from "../ui/external-link/ExternalLink.js";
import { fetchJson, usePolled } from "../ui/polled/usePolled.js"; import { fetchJson, usePolled } from "../ui/polled/usePolled.js";
import { RelativeTime } from "../ui/relative-time/RelativeTime.js"; import { RelativeTime } from "../ui/relative-time/RelativeTime.js";
@ -38,54 +35,42 @@ export function AlertsMenu() {
} }
function AlertsButton({ firing }: { firing: Alert[] }) { function AlertsButton({ firing }: { firing: Alert[] }) {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
useDismiss({
open,
onDismiss: () => setOpen(false),
inside: [rootRef],
focusOnEscape: () => rootRef.current?.querySelector("button") ?? null,
});
const label = alertsLabel(firing.length); const label = alertsLabel(firing.length);
return ( return (
<div class="alerts-menu" ref={rootRef}> <HeaderMenu
<IconButton class="alerts-menu"
label={label} label={label}
onClick={() => setOpen((v) => !v)} popoverClass="header-menu-wide"
expanded={open} trigger={
> <>
<BellIcon /> <BellIcon />
<span class="alerts-menu-count" aria-hidden="true"> <span class="alerts-menu-count" aria-hidden="true">
{firing.length} {firing.length}
</span> </span>
</IconButton> </>
{open ? ( }
<div class="alerts-menu-popover" role="group" aria-label={label}> >
<ul class="alerts-menu-list"> <ul class="alerts-menu-list">
{firing.map((a) => { {firing.map((a) => {
const since = activeSinceMs(a); const since = activeSinceMs(a);
return ( return (
<li key={alertKey(a)} class="alerts-menu-item"> <li key={alertKey(a)} class="alerts-menu-item">
<span class="alerts-menu-name">{a.name}</span> <span class="alerts-menu-name">{a.name}</span>
{since !== null ? ( {since !== null ? (
<span class="alerts-menu-since"> <span class="alerts-menu-since">
<RelativeTime epochMs={since} /> <RelativeTime epochMs={since} />
</span> </span>
) : null} ) : null}
{a.summary ? ( {a.summary ? (
<span class="alerts-menu-summary">{a.summary}</span> <span class="alerts-menu-summary">{a.summary}</span>
) : null} ) : null}
</li> </li>
); );
})} })}
</ul> </ul>
<ExternalLink href={GRAFANA_ALERTS_URL} class="alerts-menu-link"> <ExternalLink href={GRAFANA_ALERTS_URL} class="header-menu-item">
open Grafana's alert list open Grafana's alert list
</ExternalLink> </ExternalLink>
</div> </HeaderMenu>
) : null}
</div>
); );
} }

View file

@ -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\)/);
});