From 844fb0a21966c452a46c438e74873e555abab7fd Mon Sep 17 00:00:00 2001 From: iris Date: Thu, 19 Nov 2026 18:54:04 +0100 Subject: [PATCH] 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. --- docs/4-implementation/web-ui/swarm-ui.md | 12 ++ frontend/packages/shared/package.json | 1 + .../packages/shared/src/dropdown/Dropdown.tsx | 70 ++++++++---- .../shared/src/hive-dialog/hive-dialog.js | 3 + .../shared/src/links-menu/LinksMenu.tsx | 29 ++--- .../shared/src/settings/SettingsMenu.tsx | 33 ++---- .../shared/src/side-panel/hive-side-panel.js | 23 +++- frontend/packages/shared/src/use-dismiss.ts | 108 ++++++++++++++++++ .../shared/src/user-menu/UserMenu.tsx | 29 ++--- .../swarm-ui/src/ui/dialog/Dialog.css | 8 +- .../swarm-ui/src/ui/dialog/Dialog.tsx | 24 +++- .../swarm-ui/src/ui/popover/Popover.tsx | 37 ++---- .../swarm-ui/test/use-dismiss.test.mjs | 43 +++++++ 13 files changed, 299 insertions(+), 121 deletions(-) create mode 100644 frontend/packages/shared/src/use-dismiss.ts create mode 100644 frontend/packages/swarm-ui/test/use-dismiss.test.mjs diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index 6a5ba8c63..613cbdecd 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -25,6 +25,18 @@ the swarm name and its menus on one row and the nav on a second row that scrolls agent icon on a card shrinks, and the state badges over a terminal stay one row that scrolls sideways. Nothing makes the page itself scroll sideways at 360px. +A menu or popover (the agent's state menu, the model and effort badge, the settings, links +and account menus) closes on a press outside it or on **Escape**, and **Escape** puts the focus +back on the button that opened it. A press inside a dialog that the menu isn't in doesn't +count as outside, and **Escape** goes to that dialog first; a menu in a dialog closes +alone, without closing the dialog. A menu of choices, such as the wanted state +menu, takes the focus when it opens, on the chosen item or the first, and the arrow keys, +**Home** and **End** move between its items. The links, account and settings popovers are +plain groups, not menus: **Tab** moves through them. A dialog ignores a press that starts +inside it and ends on its backdrop, so selecting text can't close it by accident. The hive +dashboard's side panel is inert while shut, takes the focus when it opens and gives +it back when it closes. + A dialog grows to fit its content, up to 64em or nine tenths of the window, whichever is smaller; short confirm dialogs stay short. diff --git a/frontend/packages/shared/package.json b/frontend/packages/shared/package.json index 343dda276..5af7e58c7 100644 --- a/frontend/packages/shared/package.json +++ b/frontend/packages/shared/package.json @@ -24,6 +24,7 @@ "./pill.css": "./src/pill.css", "./forms.js": "./src/forms.js", "./prefs.js": "./src/prefs.ts", + "./use-dismiss.js": "./src/use-dismiss.ts", "./dom.js": "./src/dom.js", "./modal.js": "./src/modal.js", "./shadow-css.js": "./src/shadow-css.js", diff --git a/frontend/packages/shared/src/dropdown/Dropdown.tsx b/frontend/packages/shared/src/dropdown/Dropdown.tsx index dbd344483..16fa8242e 100644 --- a/frontend/packages/shared/src/dropdown/Dropdown.tsx +++ b/frontend/packages/shared/src/dropdown/Dropdown.tsx @@ -18,6 +18,7 @@ // that mode for the agents table, and dropped it with the table. import { useEffect, useRef } from "preact/hooks"; import type { ComponentChildren, RefObject } from "preact"; +import { useDismiss } from "../use-dismiss.js"; import "./Dropdown.css"; export interface DropdownOption { @@ -64,32 +65,61 @@ export function Dropdown({ anchorRef, }: DropdownProps) { const ref = useRef(null); + // A press outside, or Escape, closes it; the trigger (`anchorRef`) is inside, + // so its own toggle stays in charge of closing. + useDismiss({ + open, + onDismiss: onClose, + inside: anchorRef ? [ref, anchorRef] : [ref], + focusOnEscape: () => { + const anchor = anchorRef?.current; + if (!anchor) return null; + return anchor.matches("button") ? anchor : anchor.querySelector("button"); + }, + }); + + // A menu takes the focus when it opens, on the chosen item or the first, and + // the arrow keys, Home and End move between the items. useEffect(() => { if (!open) return; - function handlePointerDown(e: PointerEvent) { - if (!(e.target instanceof Node)) return; - if (ref.current?.contains(e.target)) return; - if (anchorRef?.current?.contains(e.target)) return; - onClose(); - } - function handleKeyDown(e: KeyboardEvent) { - if (e.key === "Escape") onClose(); - } - // `pointerdown` (not `click`) so a drag-to-select that ends outside - // still closes; capture phase so this sees the event before a - // stopPropagation() elsewhere in the tree could swallow it. - document.addEventListener("pointerdown", handlePointerDown, true); - document.addEventListener("keydown", handleKeyDown); - return () => { - document.removeEventListener("pointerdown", handlePointerDown, true); - document.removeEventListener("keydown", handleKeyDown); - }; - }, [open, onClose]); + const items = + ref.current?.querySelectorAll(".ui-dropdown-item"); + if (!items || items.length === 0) return; + const chosen = ref.current?.querySelector( + ".ui-dropdown-item-active", + ); + (chosen ?? items[0]).focus(); + }, [open]); + function onKeyDown(e: KeyboardEvent) { + const items = Array.from( + ref.current?.querySelectorAll(".ui-dropdown-item") ?? [], + ); + const at = items.indexOf(document.activeElement as HTMLElement); + const next = + e.key === "ArrowDown" + ? (at + 1) % items.length + : e.key === "ArrowUp" + ? (at - 1 + items.length) % items.length + : e.key === "Home" + ? 0 + : e.key === "End" + ? items.length - 1 + : null; + if (next === null || items.length === 0) return; + e.preventDefault(); + items[next].focus(); + } if (!open) return null; return ( -