shared: a dropdown that would run past the window's right edge hangs from the right of its trigger
The state menu on an agent card that fills the width hung from the trigger's left edge, so the window cut the list off. The list now measures itself when it opens and flips to the trigger's right edge when it would overflow.
This commit is contained in:
parent
dd17b20dac
commit
608eb9dcd9
5 changed files with 46 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -10,15 +10,17 @@
|
|||
//
|
||||
// Positioning is the caller's job: render `<Dropdown>` 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(() => {
|
||||
|
|
|
|||
9
frontend/packages/shared/src/dropdown/edge.ts
Normal file
9
frontend/packages/shared/src/dropdown/edge.ts
Normal file
|
|
@ -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;
|
||||
}
|
||||
12
frontend/packages/swarm-ui/test/dropdown-edge.test.mjs
Normal file
12
frontend/packages/swarm-ui/test/dropdown-edge.test.mjs
Normal file
|
|
@ -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);
|
||||
});
|
||||
Loading…
Reference in a new issue