Watch
0
0
Fork
You've already forked hyperhive
0

frontend: one dismiss rule for menus and popovers, menu keys, and dialog and side panel focus

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.
This commit is contained in:
iris 2026-11-19 18:54:04 +01:00 • committed by mara
commit 844fb0a219
4 changed files with 299 additions and 121 deletions

View file

@ -51,8 +51,8 @@
display: flex;
align-items: center;
justify-content: center;
width: 2.2em;
height: 2.2em;
width: 2.75em;
height: 2.75em;
border: 1px solid transparent;
border-radius: 0.4em;
background: none;
@ -66,11 +66,11 @@
background: var(--bg);
}
.ui-dialog-body {
/* Clears the close button's own box (0.75em inset + 2.2em size) with
/* Clears the close button's own box (0.75em inset + 2.75em size) with
room to spare — verified against a real screenshot; a smaller value
let wide content (e.g. the create-agent form's two-panel layout)
render directly under the button instead of beside it. */
padding-right: 3.25em;
padding-right: 3.75em;
}
/* `plain` has no floating close button of its own to clear — the
content's own `Panel` header carries one instead (see `.ui-panel-close`

View file

@ -45,6 +45,12 @@ export function Dialog({
plain?: boolean;
}) {
const ref = useRef<HTMLDialogElement>(null);
// The `close` event comes after a close the `open` prop asked for, too; only a
// close the caller did not ask for (Escape) is reported back.
const closedByProp = useRef(false);
// Where the press began: a drag that starts inside and ends over the backdrop
// is not a click on it.
const pressedOnBackdrop = useRef(false);
// Drives the native open/closed state from the `open` prop rather
// than mounting/unmounting the element — `showModal()`/`close()` are
@ -53,8 +59,13 @@ export function Dialog({
useEffect(() => {
const el = ref.current;
if (!el) return;
if (open && !el.open) el.showModal();
else if (!open && el.open) el.close();
if (open && !el.open) {
closedByProp.current = false;
el.showModal();
} else if (!open && el.open) {
closedByProp.current = true;
el.close();
}
}, [open]);
// The dialog's own `close` event fires on Escape (and would fire on
@ -65,6 +76,10 @@ export function Dialog({
const el = ref.current;
if (!el) return;
function handleClose() {
if (closedByProp.current) {
closedByProp.current = false;
return;
}
onClose();
}
el.addEventListener("close", handleClose);
@ -80,8 +95,11 @@ export function Dialog({
// exactly when it's outside the rendered content box — inside
// the box, the click target is always some descendant. Standard
// "click the backdrop to close" trick for native `<dialog>`.
onPointerDown={(e) => {
pressedOnBackdrop.current = e.target === ref.current;
}}
onClick={(e) => {
if (e.target === ref.current) onClose();
if (e.target === ref.current && pressedOnBackdrop.current) onClose();
}}
>
{plain ? null : (

View file

@ -27,11 +27,12 @@ import {
} from "preact/hooks";
import { createPortal } from "preact/compat";
import type { ComponentChildren, RefObject } from "preact";
import { useDismiss } from "@hive/shared/use-dismiss.js";
import { clampLeft } from "../table/popover-position.js";
import { focusInto, restoreFocus, triggerOf } from "./focus.js";
import "./Popover.css";
// Space kept between the popover and the viewport's edges.
const EDGE_PX = 8;
// Space between the popover and its anchor.
const GAP_PX = 4;
export function Popover({
@ -96,9 +97,8 @@ export function Popover({
if (!anchor) return;
const r = anchor.getBoundingClientRect();
const width = ref.current?.offsetWidth ?? 0;
const maxLeft = window.innerWidth - width - EDGE_PX;
const top = r.bottom + GAP_PX;
const left = Math.max(EDGE_PX, Math.min(r.left, maxLeft));
const left = clampLeft(r.left, width, window.innerWidth);
// Scrolling anywhere (the streaming terminal included) calls this; keep
// the old object when nothing moved so the subtree does not re-render.
setPos((prev) =>
@ -114,32 +114,9 @@ export function Popover({
};
}, [open, anchorRef]);
useEffect(() => {
if (!open) return;
function inside(target: EventTarget | null, el: Element | null) {
return target instanceof Node && el !== null && el.contains(target);
}
function onPointerDown(e: PointerEvent) {
if (
inside(e.target, ref.current) ||
inside(e.target, anchorRef.current) ||
inside(e.target, document.querySelector("dialog[open]"))
) {
return;
}
onClose();
}
function onKeyDown(e: KeyboardEvent) {
if (e.key !== "Escape" || document.querySelector("dialog[open]")) return;
onClose();
}
document.addEventListener("pointerdown", onPointerDown);
document.addEventListener("keydown", onKeyDown);
return () => {
document.removeEventListener("pointerdown", onPointerDown);
document.removeEventListener("keydown", onKeyDown);
};
}, [open, onClose, anchorRef]);
// A press outside or Escape closes it. A modal dialog opened from inside it
// takes both, and leaves it alone.
useDismiss({ open, onDismiss: onClose, inside: [ref, anchorRef] });
if (!open) return null;
return createPortal(

View file

@ -0,0 +1,43 @@
// When a press or Escape closes a floating box: the rule `useDismiss` applies.
// Run by `npm test`.
import assert from "node:assert/strict";
import { test } from "node:test";
import {
dialogsOverBox,
escapeDismisses,
pointerDismisses,
} from "../../shared/src/use-dismiss.ts";
test("a press outside the box and every dialog closes it", () => {
assert.equal(pointerDismisses(false, false), true);
});
test("a press inside the box, or in a dialog over it, does not", () => {
assert.equal(pointerDismisses(true, false), false);
assert.equal(pointerDismisses(false, true), false);
assert.equal(pointerDismisses(true, true), false);
});
test("Escape closes the box unless a dialog it is not in is open", () => {
assert.equal(escapeDismisses(false), true);
assert.equal(escapeDismisses(true), false);
});
test("a dialog the box is in is not over it", () => {
const holding = { holdsBox: true };
const over = { holdsBox: false };
assert.deepEqual(
dialogsOverBox([holding, over], (d) => d.holdsBox),
[over],
);
assert.deepEqual(
dialogsOverBox([holding], (d) => d.holdsBox),
[],
);
});
test("a press in the dialog the box is in dismisses it", () => {
const holding = { holdsBox: true };
const over = dialogsOverBox([holding], (d) => d.holdsBox);
assert.equal(pointerDismisses(false, over.includes(holding)), true);
});