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 ( -