Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/shared/src/links-menu/LinksMenu.tsx
iris 844fb0a219 frontend: one dismiss rule for menus and popovers, menu keys, and dialog and side panel focus
useDismiss (shared) closes a floating box on a press outside or Escape: the press is seen in the capture phase, a press in an open modal dialog is not outside, Escape yields to a modal dialog the box is not in, and a box in a dialog closes alone. Popover, Dropdown, SettingsMenu, LinksMenu and UserMenu use it in place of five copies with different rules; Escape returns the focus to the button. Dropdown takes the focus on open and moves with the arrow keys, Home and End. The link, account and settings popovers drop role=menu (they had no menu keys) for a labelled group. Dialog no longer reports a close the caller asked for, ignores a drag that started inside and ended on the backdrop, and has a 2.75em close button. Popover uses the shared clampLeft. The dashboard's side panel is inert while closed, takes the focus on open and gives it back, and no longer claims aria-modal; hive-dialog gives the focus back on close.
2026-11-20 02:20:58 +01:00

69 lines
2.2 KiB
TypeScript

// <LinksMenu> — a single header icon-button opening a popover of links, next
// to the profile avatar. The swarm UI hands it the swarm's service list and
// the hive dashboard its own pages; both look and behave the same, which is
// why it lives here (it was the swarm UI's, and the dashboard had its own
// copy with a hamburger on the left).
//
// A menu with no links renders nothing: no button beats a dead one.
//
// Styles live in `@hive/shared/links-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
// (the same split as `UserMenu`).
import { useRef, useState } from "preact/hooks";
import { IconButton } from "../icon-button/IconButton.js";
import { LinkIcon } from "../icons.js";
import { useDismiss } from "../use-dismiss.js";
import { linkAttrs, type MenuLink } from "./links.js";
export type { MenuLink };
export function LinksMenu({
links,
label = "links",
}: {
links: MenuLink[];
/** 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>
);
}