Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/shared/src/settings/SettingsMenu.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

121 lines
4.6 KiB
TypeScript

// <SettingsMenu> — a single header icon-button + popover holding the
// client-local overrides a page has (theme, reduced motion). Originated
// in swarm-ui, duplicated to the agent terminal page (mara: "agent
// terminal page should get the settings panel from swarm ui as well"),
// then unified here per her review on that PR: "i think the component
// should be shared. motion setting is missing." — one component, both
// pages import it, motion included unconditionally rather than
// agent-only.
//
// Trigger is `Badge` (`../badge/Badge.js`) with `../icons.js`'s
// `GearIcon` — an inline SVG, not a text/emoji glyph (see that file's
// comment for why a glyph is the wrong tool for a trigger's own icon;
// this file went through one regression on exactly that, caught by
// mara: "settings icon looks weird since component extract").
// `variant="quiet"` drops `Badge`'s default filled-pill look, which is
// right for a status/picker chip but wrong for this icon-only header
// button (mara again: "link and settings button should not have the
// badge bg") — see `Badge.tsx`'s `BadgeVariant` comment.
//
// Storage keys are the caller's responsibility (`themeKey`/`motionKey`
// props), not derived here — a page mounts `useApplyThemeOverride`/
// `useApplyMotionOverride` (`./theme-apply.js`/`./motion-apply.js`)
// 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 { GearIcon } from "../icons.js";
import { useDismiss } from "../use-dismiss.js";
import { useThemeOverride, type ThemeOverride } from "./theme-apply.js";
import { useMotionOverride, type MotionOverride } from "./motion-apply.js";
import "./SettingsMenu.css";
const THEME_OPTIONS: ThemeOverride[] = ["system", "light", "dark"];
const MOTION_OPTIONS: MotionOverride[] = ["system", "allow", "reduce"];
export interface SettingsMenuProps {
themeKey: string;
motionKey: string;
/**
* Consumer-owned extra row(s), rendered inside the popover after the
* built-in theme/motion rows. Not a named boolean prop per setting —
* a `showExpandDetails?: boolean` attempt drew mara's review: "you
* cannot just add it like this - if we have more and more options
* there in different places we will keep accumulating cruft in the
* shared component." A caller owns its row(s) entirely (state,
* storage key, markup); this component never learns they exist. Use
* `settings-menu-row` (this file's CSS) on each row's outer element
* to match spacing/typography — `HideDebugSetting` is a worked example.
*/
children?: ComponentChildren;
}
export function SettingsMenu({
themeKey,
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 as ThemeOverride)
}
>
{THEME_OPTIONS.map((o) => (
<option key={o} value={o}>
{o}
</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>
);
}