diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index 5b4d0040c..b1aa3c3ea 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -25,6 +25,7 @@ and **Page Down** move through the options, and **Enter**, **Space** or a press the highlighted one. **Escape**, **Tab** or a press 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. +A list that would run past the window's right edge, such as an agent's state menu on a card that fills the width, opens from the right edge of its trigger instead. On a narrow screen (40em or less, the one width breakpoint the UI has) the header puts the swarm name and its menus on one row and the nav on a second row that scrolls sideways, the diff --git a/frontend/packages/shared/src/dropdown/Dropdown.css b/frontend/packages/shared/src/dropdown/Dropdown.css index 972a759ac..3271c9c23 100644 --- a/frontend/packages/shared/src/dropdown/Dropdown.css +++ b/frontend/packages/shared/src/dropdown/Dropdown.css @@ -30,6 +30,10 @@ border-radius: var(--radius-md); box-shadow: var(--shadow-pop); } +.ui-dropdown-end { + right: 0; + left: auto; +} .ui-dropdown-item { display: flex; flex-direction: column; diff --git a/frontend/packages/shared/src/dropdown/Dropdown.tsx b/frontend/packages/shared/src/dropdown/Dropdown.tsx index 16fa8242e..8f12c19b8 100644 --- a/frontend/packages/shared/src/dropdown/Dropdown.tsx +++ b/frontend/packages/shared/src/dropdown/Dropdown.tsx @@ -10,15 +10,17 @@ // // Positioning is the caller's job: render `` as a child of a // `position: relative` wrapper (the badge + dropdown pair) and it anchors to -// that box's bottom-left via CSS. Put the wrapper outside any scrolling +// that box's bottom-left via CSS, or to its bottom-right (`.ui-dropdown-end`, +// set when it opens) when the list would run past the window. Put the wrapper outside any scrolling // container: an ancestor with `overflow-x: auto` computes `overflow-y` to `auto` // too (per the CSS overflow spec), so it clips the dropdown vertically. A // `position: fixed` list rendered through a portal escapes that, as // `../../ui/table/Table.tsx` does for its column filters; this component had // that mode for the agents table, and dropped it with the table. -import { useEffect, useRef } from "preact/hooks"; +import { useEffect, useLayoutEffect, useRef } from "preact/hooks"; import type { ComponentChildren, RefObject } from "preact"; import { useDismiss } from "../use-dismiss.js"; +import { hangsFromEnd } from "./edge.js"; import "./Dropdown.css"; export interface DropdownOption { @@ -78,6 +80,22 @@ export function Dropdown({ }, }); + // A list hangs from the left of its trigger. One that would run past the + // window (a trigger at the right edge) hangs from the right instead. + useLayoutEffect(() => { + const el = ref.current; + if (!open || !el) return; + el.classList.remove("ui-dropdown-end"); + if ( + hangsFromEnd( + el.getBoundingClientRect().right, + document.documentElement.clientWidth, + ) + ) { + el.classList.add("ui-dropdown-end"); + } + }, [open]); + // 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(() => { diff --git a/frontend/packages/shared/src/dropdown/edge.ts b/frontend/packages/shared/src/dropdown/edge.ts new file mode 100644 index 000000000..2637ce615 --- /dev/null +++ b/frontend/packages/shared/src/dropdown/edge.ts @@ -0,0 +1,9 @@ +// Where an option list opens, from the box it would take if it hung from the +// left of its trigger. Kept apart from the component so a test reaches it +// without a DOM. + +/** Whether a list whose left-hung box ends at `right` needs to hang from the + * right of its trigger instead, to stay inside a window `viewportWidth` wide. */ +export function hangsFromEnd(right: number, viewportWidth: number): boolean { + return right > viewportWidth; +} diff --git a/frontend/packages/swarm-ui/test/dropdown-edge.test.mjs b/frontend/packages/swarm-ui/test/dropdown-edge.test.mjs new file mode 100644 index 000000000..30f3c82dd --- /dev/null +++ b/frontend/packages/swarm-ui/test/dropdown-edge.test.mjs @@ -0,0 +1,12 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { hangsFromEnd } from "../../shared/src/dropdown/edge.ts"; + +test("a list that ends inside the window stays hung from the left", () => { + assert.equal(hangsFromEnd(300, 1000), false); + assert.equal(hangsFromEnd(1000, 1000), false); +}); + +test("a list that ends past the window edge hangs from the right", () => { + assert.equal(hangsFromEnd(1001, 1000), true); +});