useDismiss (shared) closes a floating box on a press outside or Escape: the press is seen in the capture phase, a press in an open modal dialog is not outside, Escape yields to a modal dialog the box is not in, and a box in a dialog closes alone. Popover, Dropdown, SettingsMenu, LinksMenu and UserMenu use it in place of five copies with different rules; Escape returns the focus to the button. Dropdown takes the focus on open and moves with the arrow keys, Home and End. The link, account and settings popovers drop role=menu (they had no menu keys) for a labelled group. Dialog no longer reports a close the caller asked for, ignores a drag that started inside and ended on the backdrop, and has a 2.75em close button. Popover uses the shared clampLeft. The dashboard's side panel is inert while closed, takes the focus on open and gives it back, and no longer claims aria-modal; hive-dialog gives the focus back on close.
144 lines
5.2 KiB
TypeScript
144 lines
5.2 KiB
TypeScript
// <Dropdown> — a small anchored option list, meant to sit directly under
|
|
// the `Badge` that opened it (../badge/Badge.tsx) so the control is
|
|
// visually attached to the thing it affects, instead of living three
|
|
// clicks away in a catch-all menu (the junk-drawer pattern
|
|
// `docs/5-project/web-ui/design-guide.md` calls out by name). Not a native `<dialog>` — a
|
|
// `showModal()` dialog's backdrop would visually detach it from its
|
|
// anchor badge, and a non-modal `show()` gets no click-outside-to-close
|
|
// for free, so this hand-rolls the same "outside click or Escape
|
|
// closes" contract `Dialog` gets from the platform.
|
|
//
|
|
// 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
|
|
// 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 type { ComponentChildren, RefObject } from "preact";
|
|
import { useDismiss } from "../use-dismiss.js";
|
|
import "./Dropdown.css";
|
|
|
|
export interface DropdownOption {
|
|
value: string;
|
|
label: ComponentChildren;
|
|
/** Optional dim secondary text, e.g. "sonnet (balanced)". */
|
|
description?: ComponentChildren;
|
|
/** Red-toned row for a destructive action (e.g. cancel turn), not a picker option. */
|
|
danger?: boolean;
|
|
}
|
|
|
|
export interface DropdownProps {
|
|
open: boolean;
|
|
options: DropdownOption[];
|
|
activeValue?: string;
|
|
onSelect: (value: string) => void;
|
|
onClose: () => void;
|
|
/** `aria-label` for the option list (e.g. "select model"). */
|
|
label: string;
|
|
/**
|
|
* Ref to the trigger badge/button that opened this dropdown. Without
|
|
* it, a click on the trigger while open closes-then-reopens instead
|
|
* of closing: the trigger's own `onClick` toggles `open`, but this
|
|
* component's outside-click listener doesn't know the trigger is
|
|
* "part of" the dropdown (it's a sibling, not a descendant of this
|
|
* `<div>`), so it *also* fires `onClose` on the same click — the two
|
|
* updates race, and the toggle's `!open` reads the pre-close value
|
|
* and wins, reopening it (mara: "clicking badge with open drop down
|
|
* reopens it instead of closing it"). Passing the same ref the
|
|
* caller's wrapper div already needs for CSS positioning excludes
|
|
* clicks on the trigger from the outside-click check, matching how
|
|
* a popover that wraps its trigger and popup in one ref avoids this.
|
|
*/
|
|
anchorRef?: RefObject<HTMLElement | null>;
|
|
}
|
|
|
|
export function Dropdown({
|
|
open,
|
|
options,
|
|
activeValue,
|
|
onSelect,
|
|
onClose,
|
|
label,
|
|
anchorRef,
|
|
}: DropdownProps) {
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
// A press outside, or Escape, closes it; the trigger (`anchorRef`) is inside,
|
|
// so its own toggle stays in charge of closing.
|
|
useDismiss({
|
|
open,
|
|
onDismiss: onClose,
|
|
inside: anchorRef ? [ref, anchorRef] : [ref],
|
|
focusOnEscape: () => {
|
|
const anchor = anchorRef?.current;
|
|
if (!anchor) return null;
|
|
return anchor.matches("button") ? anchor : anchor.querySelector("button");
|
|
},
|
|
});
|
|
|
|
// 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(() => {
|
|
if (!open) return;
|
|
const items =
|
|
ref.current?.querySelectorAll<HTMLElement>(".ui-dropdown-item");
|
|
if (!items || items.length === 0) return;
|
|
const chosen = ref.current?.querySelector<HTMLElement>(
|
|
".ui-dropdown-item-active",
|
|
);
|
|
(chosen ?? items[0]).focus();
|
|
}, [open]);
|
|
function onKeyDown(e: KeyboardEvent) {
|
|
const items = Array.from(
|
|
ref.current?.querySelectorAll<HTMLElement>(".ui-dropdown-item") ?? [],
|
|
);
|
|
const at = items.indexOf(document.activeElement as HTMLElement);
|
|
const next =
|
|
e.key === "ArrowDown"
|
|
? (at + 1) % items.length
|
|
: e.key === "ArrowUp"
|
|
? (at - 1 + items.length) % items.length
|
|
: e.key === "Home"
|
|
? 0
|
|
: e.key === "End"
|
|
? items.length - 1
|
|
: null;
|
|
if (next === null || items.length === 0) return;
|
|
e.preventDefault();
|
|
items[next].focus();
|
|
}
|
|
|
|
if (!open) return null;
|
|
|
|
return (
|
|
<div
|
|
class="ui-dropdown"
|
|
role="menu"
|
|
aria-label={label}
|
|
ref={ref}
|
|
onKeyDown={onKeyDown}
|
|
>
|
|
{options.map((opt) => (
|
|
<button
|
|
type="button"
|
|
key={opt.value}
|
|
role="menuitemradio"
|
|
aria-checked={opt.value === activeValue}
|
|
class={
|
|
"ui-dropdown-item" +
|
|
(opt.value === activeValue ? " ui-dropdown-item-active" : "") +
|
|
(opt.danger ? " ui-dropdown-item-danger" : "")
|
|
}
|
|
onClick={() => onSelect(opt.value)}
|
|
>
|
|
<span class="ui-dropdown-item-label">{opt.label}</span>
|
|
{opt.description ? (
|
|
<span class="ui-dropdown-item-desc">{opt.description}</span>
|
|
) : null}
|
|
</button>
|
|
))}
|
|
</div>
|
|
);
|
|
}
|