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
1 changed files with 46 additions and 2 deletions
|
|
@ -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(() => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue