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
2 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 : (