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