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:
parent
25965341e0
commit
6f73bf0c55
14 changed files with 310 additions and 365 deletions
|
|
@ -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 `<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
|
||||
same day it lands** — no building block without a place to see it. One
|
||||
deliberate exception exists today: `FormField`, the internal label+
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
68
frontend/packages/shared/src/header-menu/HeaderMenu.css
Normal file
68
frontend/packages/shared/src/header-menu/HeaderMenu.css
Normal 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);
|
||||
}
|
||||
82
frontend/packages/shared/src/header-menu/HeaderMenu.tsx
Normal file
82
frontend/packages/shared/src/header-menu/HeaderMenu.tsx
Normal 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>
|
||||
);
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -1,63 +1,5 @@
|
|||
@import "../icon-button/IconButton.css";
|
||||
/* <LinksMenu> (@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 ? <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);
|
||||
}
|
||||
/* <LinksMenu> (@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";
|
||||
|
|
|
|||
|
|
@ -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<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;
|
||||
|
||||
return (
|
||||
<div class="links-menu" ref={rootRef}>
|
||||
<IconButton
|
||||
label={label}
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
expanded={open}
|
||||
>
|
||||
<LinkIcon />
|
||||
</IconButton>
|
||||
{open ? (
|
||||
<div class="links-menu-popover" role="group" aria-label={label}>
|
||||
{links.map((link) => (
|
||||
<a
|
||||
key={link.url}
|
||||
class="links-menu-item"
|
||||
href={link.url}
|
||||
{...linkAttrs(link)}
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
{link.icon ? <span aria-hidden="true">{link.icon}</span> : null}
|
||||
{link.label}
|
||||
</a>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<HeaderMenu label={label} trigger={<LinkIcon />}>
|
||||
{(close) =>
|
||||
links.map((link) => (
|
||||
<a
|
||||
key={link.url}
|
||||
class="header-menu-item"
|
||||
href={link.url}
|
||||
{...linkAttrs(link)}
|
||||
onClick={close}
|
||||
>
|
||||
{link.icon ? <span aria-hidden="true">{link.icon}</span> : null}
|
||||
{link.label}
|
||||
</a>
|
||||
))
|
||||
}
|
||||
</HeaderMenu>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +1,5 @@
|
|||
@import "../icon-button/IconButton.css";
|
||||
/* <SettingsMenu> 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 `<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);
|
||||
}
|
||||
@import "../header-menu/HeaderMenu.css";
|
||||
/* <SettingsMenu> — the theme and motion rows inside a `HeaderMenu` popover. */
|
||||
.settings-menu-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
|
|
|||
|
|
@ -23,11 +23,9 @@
|
|||
// 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
|
||||
// page-specific naming decision.
|
||||
import { useRef, useState } from "preact/hooks";
|
||||
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 { useDismiss } from "../use-dismiss.js";
|
||||
import { useThemeOverride } from "./theme-apply.js";
|
||||
import { AUTO_THEME, THEMES } from "./themes.js";
|
||||
import { useMotionOverride, type MotionOverride } from "./motion-apply.js";
|
||||
|
|
@ -61,63 +59,40 @@ export function SettingsMenu({
|
|||
motionKey,
|
||||
children,
|
||||
}: SettingsMenuProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
const [theme, setTheme] = useThemeOverride(themeKey);
|
||||
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 (
|
||||
<div class="settings-menu-anchor" ref={rootRef}>
|
||||
<IconButton
|
||||
label="settings"
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
expanded={open}
|
||||
>
|
||||
<GearIcon />
|
||||
</IconButton>
|
||||
{open ? (
|
||||
<div class="settings-menu-popover" role="group" aria-label="settings">
|
||||
<label class="settings-menu-row">
|
||||
<span>theme</span>
|
||||
<select
|
||||
value={theme}
|
||||
onChange={(e) => setTheme((e.target as HTMLSelectElement).value)}
|
||||
>
|
||||
{THEME_OPTIONS.map((o) => (
|
||||
<option key={o.id} value={o.id}>
|
||||
{o.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label class="settings-menu-row">
|
||||
<span>motion</span>
|
||||
<select
|
||||
value={motion}
|
||||
onChange={(e) =>
|
||||
setMotion(
|
||||
(e.target as HTMLSelectElement).value as MotionOverride,
|
||||
)
|
||||
}
|
||||
>
|
||||
{MOTION_OPTIONS.map((o) => (
|
||||
<option key={o} value={o}>
|
||||
{o}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{children}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<HeaderMenu label="settings" trigger={<GearIcon />}>
|
||||
<label class="settings-menu-row">
|
||||
<span>theme</span>
|
||||
<select
|
||||
value={theme}
|
||||
onChange={(e) => setTheme((e.target as HTMLSelectElement).value)}
|
||||
>
|
||||
{THEME_OPTIONS.map((o) => (
|
||||
<option key={o.id} value={o.id}>
|
||||
{o.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label class="settings-menu-row">
|
||||
<span>motion</span>
|
||||
<select
|
||||
value={motion}
|
||||
onChange={(e) =>
|
||||
setMotion((e.target as HTMLSelectElement).value as MotionOverride)
|
||||
}
|
||||
>
|
||||
{MOTION_OPTIONS.map((o) => (
|
||||
<option key={o} value={o}>
|
||||
{o}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{children}
|
||||
</HeaderMenu>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,7 @@
|
|||
@import "../icon-button/IconButton.css";
|
||||
/* <UserMenu> (@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";
|
||||
/* <UserMenu> (@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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<WhoAmI | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const rootRef = useRef<HTMLDivElement>(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 (
|
||||
<div class="user-menu" ref={rootRef}>
|
||||
<IconButton
|
||||
class="user-menu-button"
|
||||
style={{ background: pickAccent(label ?? "operator") }}
|
||||
label={label ? `signed in as ${label}` : "account"}
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
expanded={open}
|
||||
>
|
||||
{initialOf(label)}
|
||||
</IconButton>
|
||||
{open ? (
|
||||
<div class="user-menu-popover" role="group" aria-label="account">
|
||||
<HeaderMenu
|
||||
class="user-menu"
|
||||
triggerClass="user-menu-button"
|
||||
triggerStyle={{ background: pickAccent(label ?? "operator") }}
|
||||
label={label ? `signed in as ${label}` : "account"}
|
||||
popoverLabel="account"
|
||||
trigger={initialOf(label)}
|
||||
>
|
||||
{(close) => (
|
||||
<>
|
||||
{label ? <div class="user-menu-name">{label}</div> : null}
|
||||
{links ? (
|
||||
<>
|
||||
<a
|
||||
class="user-menu-item"
|
||||
class="header-menu-item"
|
||||
href={links.settings}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
onClick={() => setOpen(false)}
|
||||
onClick={close}
|
||||
>
|
||||
authelia settings
|
||||
</a>
|
||||
<a
|
||||
class="user-menu-item"
|
||||
class="header-menu-item"
|
||||
href={links.logout}
|
||||
onClick={() => {
|
||||
onLogout?.();
|
||||
setOpen(false);
|
||||
close();
|
||||
}}
|
||||
>
|
||||
log out
|
||||
</a>
|
||||
</>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</HeaderMenu>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
/* <AlertsMenu> — 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<HTMLDivElement>(null);
|
||||
|
||||
useDismiss({
|
||||
open,
|
||||
onDismiss: () => setOpen(false),
|
||||
inside: [rootRef],
|
||||
focusOnEscape: () => rootRef.current?.querySelector("button") ?? null,
|
||||
});
|
||||
|
||||
const label = alertsLabel(firing.length);
|
||||
return (
|
||||
<div class="alerts-menu" ref={rootRef}>
|
||||
<IconButton
|
||||
label={label}
|
||||
onClick={() => setOpen((v) => !v)}
|
||||
expanded={open}
|
||||
>
|
||||
<BellIcon />
|
||||
<span class="alerts-menu-count" aria-hidden="true">
|
||||
{firing.length}
|
||||
</span>
|
||||
</IconButton>
|
||||
{open ? (
|
||||
<div class="alerts-menu-popover" role="group" aria-label={label}>
|
||||
<ul class="alerts-menu-list">
|
||||
{firing.map((a) => {
|
||||
const since = activeSinceMs(a);
|
||||
return (
|
||||
<li key={alertKey(a)} class="alerts-menu-item">
|
||||
<span class="alerts-menu-name">{a.name}</span>
|
||||
{since !== null ? (
|
||||
<span class="alerts-menu-since">
|
||||
<RelativeTime epochMs={since} />
|
||||
</span>
|
||||
) : null}
|
||||
{a.summary ? (
|
||||
<span class="alerts-menu-summary">{a.summary}</span>
|
||||
) : null}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
<ExternalLink href={GRAFANA_ALERTS_URL} class="alerts-menu-link">
|
||||
open Grafana's alert list
|
||||
</ExternalLink>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<HeaderMenu
|
||||
class="alerts-menu"
|
||||
label={label}
|
||||
popoverClass="header-menu-wide"
|
||||
trigger={
|
||||
<>
|
||||
<BellIcon />
|
||||
<span class="alerts-menu-count" aria-hidden="true">
|
||||
{firing.length}
|
||||
</span>
|
||||
</>
|
||||
}
|
||||
>
|
||||
<ul class="alerts-menu-list">
|
||||
{firing.map((a) => {
|
||||
const since = activeSinceMs(a);
|
||||
return (
|
||||
<li key={alertKey(a)} class="alerts-menu-item">
|
||||
<span class="alerts-menu-name">{a.name}</span>
|
||||
{since !== null ? (
|
||||
<span class="alerts-menu-since">
|
||||
<RelativeTime epochMs={since} />
|
||||
</span>
|
||||
) : null}
|
||||
{a.summary ? (
|
||||
<span class="alerts-menu-summary">{a.summary}</span>
|
||||
) : null}
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
<ExternalLink href={GRAFANA_ALERTS_URL} class="header-menu-item">
|
||||
open Grafana's alert list
|
||||
</ExternalLink>
|
||||
</HeaderMenu>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
34
frontend/packages/swarm-ui/test/header-menu.test.mjs
Normal file
34
frontend/packages/swarm-ui/test/header-menu.test.mjs
Normal 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\)/);
|
||||
});
|
||||
Loading…
Reference in a new issue