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:
parent
a19745d30d
commit
844fb0a219
1 changed files with 299 additions and 121 deletions
|
|
@ -18,6 +18,7 @@
|
|||
// 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 {
|
||||
|
|
@ -64,32 +65,61 @@ export function Dropdown({
|
|||
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;
|
||||
function handlePointerDown(e: PointerEvent) {
|
||||
if (!(e.target instanceof Node)) return;
|
||||
if (ref.current?.contains(e.target)) return;
|
||||
if (anchorRef?.current?.contains(e.target)) return;
|
||||
onClose();
|
||||
}
|
||||
function handleKeyDown(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") onClose();
|
||||
}
|
||||
// `pointerdown` (not `click`) so a drag-to-select that ends outside
|
||||
// still closes; capture phase so this sees the event before a
|
||||
// stopPropagation() elsewhere in the tree could swallow it.
|
||||
document.addEventListener("pointerdown", handlePointerDown, true);
|
||||
document.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
document.removeEventListener("pointerdown", handlePointerDown, true);
|
||||
document.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [open, onClose]);
|
||||
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}>
|
||||
<div
|
||||
class="ui-dropdown"
|
||||
role="menu"
|
||||
aria-label={label}
|
||||
ref={ref}
|
||||
onKeyDown={onKeyDown}
|
||||
>
|
||||
{options.map((opt) => (
|
||||
<button
|
||||
type="button"
|
||||
|
|
|
|||
Loading…
Reference in a new issue