diff --git a/docs/swarm/ui.md b/docs/swarm/ui.md index 054b04b02..26d77b006 100644 --- a/docs/swarm/ui.md +++ b/docs/swarm/ui.md @@ -11,15 +11,6 @@ The signed-in user's avatar sits at the right end of the header on every page. It opens a menu with the user's name and links to authelia's settings and sign-out pages; the hive dashboard and the per-agent page have the same menu. -Every dropdown (a form's select, the refresh interval, the history window) opens a -list in the swarm UI's own theme, not the browser's, and works the same in every -current browser. Open it with a click, or with the arrow keys, **Enter** or -**Space** while it has focus. Then the arrow keys, **Home**, **End**, **Page Up** -and **Page Down** move through the options, and **Enter**, **Space** or a click take -the highlighted one. **Escape**, **Tab** or a click elsewhere shut the list without -changing anything. Typing the start of an option's name picks it, also with the list -shut. A dropdown inside a dialog opens above the dialog and the dialog doesn't cut it off. - | route | what | | ---------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `/` | the start page: the swarm's name over a digital-rain backdrop, plus a paperclip offering to set up an agent while no `ruth` exists (it opens the create form with `ruth` filled in); the swarm name in the header links here | diff --git a/frontend/packages/swarm-ui/src/pages/CreateAgentForm.tsx b/frontend/packages/swarm-ui/src/pages/CreateAgentForm.tsx index fbb0ceec4..dce9f8730 100644 --- a/frontend/packages/swarm-ui/src/pages/CreateAgentForm.tsx +++ b/frontend/packages/swarm-ui/src/pages/CreateAgentForm.tsx @@ -14,7 +14,7 @@ // // `hive` field added because agent creation had no way to record which // hive an agent runs on. `POST /api/agents` now requires it, so the -// roster fetched off `GET /api/hives` backs a required dropdown here +// roster fetched off `GET /api/hives` backs a required select here // rather than a free-text field — a spawn target has to be one of the // swarm's actual hives, same reasoning `Ident.parse` gets client-side // pattern validation for `name`. @@ -159,8 +159,8 @@ export function CreateAgentForm({ setHives(data); // Single-hive swarms are the common case — default it rather than // making the operator pick the only option. Multi-hive swarms get - // no default (the placeholder stays selected), so the identity step - // cannot be left until one is picked (`stepProblem`), per mara's scope. + // no default (the placeholder stays selected), so `required` on + // the select below forces an explicit choice per mara's scope. if (data.length === 1) setHive(data[0].name); })().catch((e: unknown) => setHivesError({ detail: String(e) })); }, []); diff --git a/frontend/packages/swarm-ui/src/ui/refresh-interval/RefreshInterval.css b/frontend/packages/swarm-ui/src/ui/refresh-interval/RefreshInterval.css index bc7cde1e9..0e1a134d2 100644 --- a/frontend/packages/swarm-ui/src/ui/refresh-interval/RefreshInterval.css +++ b/frontend/packages/swarm-ui/src/ui/refresh-interval/RefreshInterval.css @@ -13,6 +13,7 @@ border-radius: 0.4em; color: var(--muted); font-size: 0.9em; + cursor: pointer; } .ui-refresh-picker:hover, .ui-refresh-picker:focus-within { @@ -20,21 +21,22 @@ background: var(--bg); color: var(--fg); } -/* The Listbox contributes only its value and chevron: its own box is - invisible and as wide as the value, so the picker's border is the one - that shows. */ -.ui-refresh-picker .ui-listbox { - width: auto; -} -.ui-refresh-picker-select.ui-form-control { - width: auto; - min-height: 2.5em; - padding: 0; +/* The select drives the interaction (native listbox, keyboard, a11y) but + contributes no chrome of its own — its own box is invisible, sized to + just its selected option's text, and its default arrow is stripped + since the chevron span stands in for it. */ +.ui-refresh-picker-select { + appearance: none; border: none; background: none; color: inherit; + font: inherit; + padding: 0; + margin: 0; + cursor: pointer; } -.ui-refresh-picker-icon { +.ui-refresh-picker-icon, +.ui-refresh-picker-chevron { font-size: 0.85em; line-height: 1; } diff --git a/frontend/packages/swarm-ui/src/ui/refresh-interval/RefreshInterval.tsx b/frontend/packages/swarm-ui/src/ui/refresh-interval/RefreshInterval.tsx index 9dc97977e..a42cb1b88 100644 --- a/frontend/packages/swarm-ui/src/ui/refresh-interval/RefreshInterval.tsx +++ b/frontend/packages/swarm-ui/src/ui/refresh-interval/RefreshInterval.tsx @@ -14,17 +14,18 @@ // passive-until-touched setting that lives in a panel's title row // (`Panel`'s `actions` slot), not a form. Visually quiet (no border/ // fill until hovered/focused) the same way `LinksMenu`'s header button -// is, so it reads as chrome rather than another input to fill in. The -// dropdown is the shared `Listbox` (its list is themed, not the browser's), -// restyled to have no chrome of its own: the chevron is the Listbox's, and the -// visible value is its selected option. +// is, so it reads as chrome rather than another input to fill in. A +// native `` for the accessible + // name a sighted-only icon+chevron can't supply). + ); } diff --git a/frontend/packages/swarm-ui/src/ui/select-field/Listbox.css b/frontend/packages/swarm-ui/src/ui/select-field/Listbox.css deleted file mode 100644 index c386952f9..000000000 --- a/frontend/packages/swarm-ui/src/ui/select-field/Listbox.css +++ /dev/null @@ -1,84 +0,0 @@ -/* : the control is `.ui-form-control`'s box (FormField.css) laid out as - value + chevron, and the list is a themed box in the top layer. */ -.ui-listbox { - position: relative; - width: 100%; -} -.ui-select-trigger { - display: flex; - align-items: center; - justify-content: space-between; - gap: 0.5em; - text-align: start; - cursor: pointer; -} -.ui-select-trigger:disabled { - cursor: default; - opacity: 0.6; -} -.ui-select-trigger[aria-expanded="true"] { - border-color: var(--purple); -} -.ui-select-value { - min-width: 0; - overflow: hidden; - text-overflow: ellipsis; - white-space: nowrap; -} -.ui-select-empty { - color: var(--muted); -} -/* A chevron drawn with borders, so it takes the text colour and needs no glyph - a font might lack. */ -.ui-select-chevron { - flex: none; - width: 0.45em; - height: 0.45em; - border-right: 2px solid currentColor; - border-bottom: 2px solid currentColor; - transform: translateY(-0.12em) rotate(45deg); - opacity: 0.7; -} -.ui-select-trigger[aria-expanded="true"] .ui-select-chevron { - transform: translateY(0.1em) rotate(225deg); -} - -/* A popover's own UA styles centre it with `inset: 0; margin: auto`; this puts - it where `placePopup` says. */ -.ui-select-list { - position: fixed; - inset: auto; - margin: 0; - padding: 0.25em; - box-sizing: border-box; - overflow-y: auto; - scrollbar-width: thin; - scrollbar-color: var(--purple-dim) transparent; - list-style: none; - background: var(--bg-elev); - color: var(--fg); - border: 1px solid var(--purple-dim); - border-radius: 0.4em; - box-shadow: 0 0.25em 0.75em rgba(0, 0, 0, 0.3); - font: inherit; -} -.ui-select-option { - display: flex; - align-items: center; - justify-content: space-between; - gap: 0.75em; - min-height: 2.5em; - padding: 0.3em 0.6em; - border-radius: 0.3em; - cursor: pointer; -} -.ui-select-option-active { - background: var(--bg); -} -.ui-select-option-selected { - font-weight: 600; -} -.ui-select-option-selected::after { - content: "✓"; - color: var(--purple); -} diff --git a/frontend/packages/swarm-ui/src/ui/select-field/Listbox.tsx b/frontend/packages/swarm-ui/src/ui/select-field/Listbox.tsx deleted file mode 100644 index 9b3f528a8..000000000 --- a/frontend/packages/swarm-ui/src/ui/select-field/Listbox.tsx +++ /dev/null @@ -1,272 +0,0 @@ -// — the dropdown a native ``, `TextField`'s sibling for the // "pick one of these" shape (the hive-choice dropdown the create-agent // form needs is the motivating caller). Options are plain // value/label pairs, not `ComponentChildren` — every real caller so far // has flat string options, and generic children would need a second // primitive (`SelectField.Option`) for zero real benefit today. import { FormField } from "../form-field/FormField.js"; -import { Listbox } from "./Listbox.js"; export interface SelectOption { value: string; @@ -28,31 +26,36 @@ export function SelectField({ value: string; onChange: (value: string) => void; options: SelectOption[]; - // Only `aria-required`: the control is not a native select, so it has no - // form validation. A caller that needs a choice checks it itself. required?: boolean; disabled?: boolean; - // Shown on the control while `value` matches no option (it is not a - // listed option) — for a required select with no default (e.g. a - // multi-hive roster: no single right answer to pre-select), so the - // control shows something other than silently landing on whatever - // option happens to be first. Omit when the field always has a real - // default (`value` never `''`). + // Rendered as a disabled, always-first `value=""` option — for a + // required select with no default (e.g. a multi-hive roster: no + // single right answer to pre-select), so the control shows something + // other than silently landing on whatever option happens to be first. + // Omit when the field always has a real default (`value` never `''`). placeholder?: string; }) { - const labelId = `${id}-label`; return ( - - + ); } diff --git a/frontend/packages/swarm-ui/src/ui/select-field/select-nav.ts b/frontend/packages/swarm-ui/src/ui/select-field/select-nav.ts deleted file mode 100644 index 6e6e76075..000000000 --- a/frontend/packages/swarm-ui/src/ui/select-field/select-nav.ts +++ /dev/null @@ -1,132 +0,0 @@ -// What decides without a DOM: which option the arrow keys, Home and -// End land on, which one typing picks, and where the list opens. Plain -// functions of their arguments, so `npm test` can run them. - -export interface ListboxOption { - value: string; - label: string; -} - -/** The index of the option holding `value`, or -1. */ -export function indexOfValue(options: ListboxOption[], value: string): number { - return options.findIndex((o) => o.value === value); -} - -// How far Page Up and Page Down jump. -const PAGE = 10; - -/** - * The active option after `key`, among `count` options. Arrow keys stop at the - * ends, they do not wrap. A key that moves nothing leaves `active` as it was. - */ -export function moveIndex(active: number, count: number, key: string): number { - if (count <= 0) return -1; - const last = count - 1; - switch (key) { - case "ArrowDown": - return Math.min(active + 1, last); - case "ArrowUp": - return Math.max(active - 1, 0); - case "PageDown": - return Math.min(active + PAGE, last); - case "PageUp": - return Math.max(active - PAGE, 0); - case "Home": - return 0; - case "End": - return last; - default: - return active; - } -} - -/** How long a pause ends a typed word, in milliseconds. */ -export const TYPE_AHEAD_MS = 700; - -/** - * The text typed so far once `key` is pressed: it continues the last word when - * the pause since the previous key was short, and starts a new one otherwise. - */ -export function nextQuery( - previous: string, - key: string, - gapMs: number, -): string { - return gapMs > TYPE_AHEAD_MS ? key : previous + key; -} - -/** - * The option typing `query` picks, searching on from `from` and wrapping - * round, or -1 when none starts with it. One letter, or that letter typed over - * and over, steps through the options that start with it; a longer word stays - * on the current option while it still matches. - */ -export function typeAhead( - options: ListboxOption[], - from: number, - query: string, -): number { - const q = query.toLowerCase(); - if (q === "" || options.length === 0) return -1; - const repeated = q.length > 1 && [...q].every((c) => c === q[0]); - const needle = repeated ? q[0] : q; - const start = needle.length === 1 ? from + 1 : Math.max(from, 0); - for (let step = 0; step < options.length; step++) { - const i = (start + step) % options.length; - if (options[i].label.toLowerCase().startsWith(needle)) return i; - } - return -1; -} - -export interface Box { - top: number; - bottom: number; - left: number; - width: number; -} - -export interface Placement { - top: number; - left: number; - width: number; - maxHeight: number; -} - -// Space kept between the list and the viewport's edge, and between the list -// and the control. -const EDGE_PX = 8; -const GAP_PX = 4; - -/** - * Where a list `contentHeight` tall opens for a control at `trigger`: under it - * when it fits, else above it if that has more room, else under it with a - * scroll. At least as wide as the control, and kept inside the viewport. - */ -export function placePopup( - trigger: Box, - viewport: { width: number; height: number }, - contentHeight: number, - minWidth = 0, -): Placement { - const below = Math.max( - viewport.height - trigger.bottom - EDGE_PX - GAP_PX, - 0, - ); - const above = Math.max(trigger.top - EDGE_PX - GAP_PX, 0); - const openUp = contentHeight > below && above > below; - const maxHeight = Math.min(contentHeight, openUp ? above : below); - const width = Math.min( - Math.max(trigger.width, minWidth), - Math.max(viewport.width - 2 * EDGE_PX, 0), - ); - const left = Math.min( - Math.max(trigger.left, EDGE_PX), - Math.max(viewport.width - EDGE_PX - width, EDGE_PX), - ); - return { - top: openUp ? trigger.top - GAP_PX - maxHeight : trigger.bottom + GAP_PX, - left, - width, - maxHeight, - }; -} diff --git a/frontend/packages/swarm-ui/test/select-nav.test.mjs b/frontend/packages/swarm-ui/test/select-nav.test.mjs deleted file mode 100644 index 7ae05b1df..000000000 --- a/frontend/packages/swarm-ui/test/select-nav.test.mjs +++ /dev/null @@ -1,113 +0,0 @@ -// What the styled dropdown's keys and placement do. Run by `npm test`. -import assert from "node:assert/strict"; -import { test } from "node:test"; -import { - indexOfValue, - moveIndex, - nextQuery, - placePopup, - TYPE_AHEAD_MS, - typeAhead, -} from "../src/ui/select-field/select-nav.ts"; - -const opts = ["alpha", "beta", "bravo", "charlie", "Delta"].map((label) => ({ - value: label.toLowerCase(), - label, -})); - -test("the option holding a value is found, or -1", () => { - assert.equal(indexOfValue(opts, "bravo"), 2); - assert.equal(indexOfValue(opts, "nope"), -1); -}); - -test("arrow keys step and stop at the ends; Home and End jump", () => { - assert.equal(moveIndex(1, 5, "ArrowDown"), 2); - assert.equal(moveIndex(4, 5, "ArrowDown"), 4, "stops at the last"); - assert.equal(moveIndex(1, 5, "ArrowUp"), 0); - assert.equal(moveIndex(0, 5, "ArrowUp"), 0, "stops at the first"); - assert.equal(moveIndex(2, 5, "Home"), 0); - assert.equal(moveIndex(2, 5, "End"), 4); - assert.equal(moveIndex(0, 30, "PageDown"), 10); - assert.equal(moveIndex(25, 30, "PageDown"), 29); - assert.equal(moveIndex(3, 30, "PageUp"), 0); -}); - -test("a key that moves nothing, or no options, leave the active one alone", () => { - assert.equal(moveIndex(2, 5, "a"), 2); - assert.equal(moveIndex(0, 0, "ArrowDown"), -1); - assert.equal( - moveIndex(-1, 3, "ArrowDown"), - 0, - "from nothing active, the first", - ); -}); - -test("typing a letter steps through the options starting with it, wrapping", () => { - assert.equal(typeAhead(opts, 0, "b"), 1); - assert.equal(typeAhead(opts, 1, "b"), 2); - assert.equal(typeAhead(opts, 2, "b"), 1, "wraps round to the first b"); - assert.equal(typeAhead(opts, 0, "d"), 4, "case does not matter"); - assert.equal(typeAhead(opts, 0, "z"), -1); -}); - -test("the same letter typed again steps on; a longer word narrows in place", () => { - assert.equal(typeAhead(opts, 1, "bb"), 2, "bb is b again"); - assert.equal(typeAhead(opts, 1, "br"), 2); - assert.equal(typeAhead(opts, 2, "br"), 2, "stays while it still matches"); - assert.equal(typeAhead(opts, 0, ""), -1); - assert.equal(typeAhead([], 0, "a"), -1); -}); - -test("a short pause continues the word, a long one starts a new one", () => { - assert.equal(nextQuery("b", "r", 100), "br"); - assert.equal(nextQuery("br", "c", TYPE_AHEAD_MS + 1), "c"); -}); - -const viewport = { width: 1000, height: 800 }; - -test("the list opens under the control when it fits", () => { - const p = placePopup( - { top: 100, bottom: 140, left: 50, width: 200 }, - viewport, - 120, - ); - assert.deepEqual(p, { top: 144, left: 50, width: 200, maxHeight: 120 }); -}); - -test("it opens above when under is too short and above has more room", () => { - const p = placePopup( - { top: 700, bottom: 740, left: 50, width: 200 }, - viewport, - 300, - ); - assert.equal(p.maxHeight, 300); - assert.equal(p.top, 700 - 4 - 300); -}); - -test("it scrolls under the control when neither side fits it", () => { - const p = placePopup( - { top: 100, bottom: 140, left: 50, width: 200 }, - { width: 1000, height: 300 }, - 900, - ); - // 300 - 140 - 8 - 4 under; 100 - 12 above: under has more here. - assert.equal(p.top, 144); - assert.equal(p.maxHeight, 148); -}); - -test("it is at least the minimum width and stays inside the viewport", () => { - const p = placePopup( - { top: 100, bottom: 140, left: 960, width: 30 }, - viewport, - 100, - 120, - ); - assert.equal(p.width, 120); - assert.equal(p.left, 1000 - 8 - 120); - const q = placePopup( - { top: 0, bottom: 40, left: -20, width: 100 }, - viewport, - 50, - ); - assert.equal(q.left, 8); -});