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
|
|
@ -10,9 +10,10 @@
|
|||
// imports or `@import`s itself: the dashboard's JS bundle loads `.css`
|
||||
// imports as text, so a component that imported it would not style there
|
||||
// (the same split as `UserMenu`).
|
||||
import { useEffect, useRef, useState } from "preact/hooks";
|
||||
import { useRef, useState } from "preact/hooks";
|
||||
import { IconButton } from "../icon-button/IconButton.js";
|
||||
import { LinkIcon } from "../icons.js";
|
||||
import { useDismiss } from "../use-dismiss.js";
|
||||
import { linkAttrs, type MenuLink } from "./links.js";
|
||||
|
||||
export type { MenuLink };
|
||||
|
|
@ -28,22 +29,13 @@ export function LinksMenu({
|
|||
const [open, setOpen] = useState(false);
|
||||
const rootRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return;
|
||||
function onPointerDown(e: MouseEvent) {
|
||||
if (rootRef.current && !rootRef.current.contains(e.target as Node))
|
||||
setOpen(false);
|
||||
}
|
||||
function onKeyDown(e: KeyboardEvent) {
|
||||
if (e.key === "Escape") setOpen(false);
|
||||
}
|
||||
document.addEventListener("pointerdown", onPointerDown);
|
||||
document.addEventListener("keydown", onKeyDown);
|
||||
return () => {
|
||||
document.removeEventListener("pointerdown", onPointerDown);
|
||||
document.removeEventListener("keydown", onKeyDown);
|
||||
};
|
||||
}, [open]);
|
||||
// A press outside or Escape closes it; Escape puts the focus back on the button.
|
||||
useDismiss({
|
||||
open,
|
||||
onDismiss: () => setOpen(false),
|
||||
inside: [rootRef],
|
||||
focusOnEscape: () => rootRef.current?.querySelector("button") ?? null,
|
||||
});
|
||||
|
||||
if (links.length === 0) return null;
|
||||
|
||||
|
|
@ -57,14 +49,13 @@ export function LinksMenu({
|
|||
<LinkIcon />
|
||||
</IconButton>
|
||||
{open ? (
|
||||
<div class="links-menu-popover" role="menu">
|
||||
<div class="links-menu-popover" role="group" aria-label={label}>
|
||||
{links.map((link) => (
|
||||
<a
|
||||
key={link.url}
|
||||
class="links-menu-item"
|
||||
href={link.url}
|
||||
{...linkAttrs(link)}
|
||||
role="menuitem"
|
||||
onClick={() => setOpen(false)}
|
||||
>
|
||||
{link.icon ? <span aria-hidden="true">{link.icon}</span> : null}
|
||||
|
|
|
|||
Loading…
Reference in a new issue