Watch
0
0
Fork
You've already forked hyperhive
0

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:
iris 2026-11-27 16:56:35 +01:00 • committed by mara
commit 608eb9dcd9
3 changed files with 46 additions and 2 deletions

View file

@ -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;

View file

@ -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(() => {

View 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;
}