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
2 changed files with 310 additions and 365 deletions

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>
);
}