// useDismiss — close a floating box (a menu, a popover) on a press outside it or // on Escape. One rule for every such box, in place of a copy in each: the press // is seen in the capture phase, so a handler that stops it further down cannot // hide it, and an open modal `` is respected. // // - A press inside the box, or on its trigger (`inside`), is not outside. // - A press inside an open modal dialog that the box is not in is not outside // either: that dialog sits over the box (it opened from a menu item, say), // took the press, and the box stays. A press elsewhere in the dialog the box // is in is an outside press. // - Escape closes the box, unless a modal dialog is open that the box is not // in: then the dialog's own Escape is the one that counts. When the box is in // a dialog, only the box closes, not the dialog behind it. import { useEffect, useRef } from "preact/hooks"; import type { RefObject } from "preact"; /** * The open dialogs that do not hold the box: `holdsBox` says whether a dialog * has the box or its trigger in it. A press in one of these, or an Escape while * one is open, belongs to that dialog and not to the box. */ export function dialogsOverBox( dialogs: D[], holdsBox: (dialog: D) => boolean, ): D[] { return dialogs.filter((d) => !holdsBox(d)); } /** * Whether a press at a place outside every `inside` element closes the box, * given whether the press is inside a dialog that sits over the box. */ export function pointerDismisses( inside: boolean, insideDialogOverBox: boolean, ): boolean { return !inside && !insideDialogOverBox; } /** * Whether Escape closes the box, given whether a modal dialog is open that the * box is not in. */ export function escapeDismisses(openDialogOutsideBox: boolean): boolean { return !openDialogOutsideBox; } function contains(el: Element | null | undefined, target: EventTarget | null) { return target instanceof Node && el != null && el.contains(target); } export function useDismiss({ open, onDismiss, inside, focusOnEscape, }: { open: boolean; onDismiss: () => void; /** The box and its trigger: a press on either is not an outside press. */ inside: Array>; /** Where the focus goes when Escape closes the box, usually its trigger. */ focusOnEscape?: () => HTMLElement | null; }) { // The latest callbacks, so the listeners are not re-added on every render. const latest = useRef({ onDismiss, inside, focusOnEscape }); latest.current = { onDismiss, inside, focusOnEscape }; useEffect(() => { if (!open) return; function onPointerDown(e: PointerEvent) { const { onDismiss, inside } = latest.current; const members = inside.map((ref) => ref.current); const over = dialogsOverBox( Array.from(document.querySelectorAll("dialog[open]")), (d) => members.some((m) => d.contains(m)), ); if ( pointerDismisses( members.some((m) => contains(m, e.target)), over.some((d) => contains(d, e.target)), ) ) { onDismiss(); } } function onKeyDown(e: KeyboardEvent) { if (e.key !== "Escape") return; const { onDismiss, inside, focusOnEscape } = latest.current; const members = inside.map((ref) => ref.current); const dialogs = Array.from(document.querySelectorAll("dialog[open]")); const over = dialogsOverBox(dialogs, (d) => members.some((m) => d.contains(m)), ); if (!escapeDismisses(over.length > 0)) return; // In a dialog, the box closes alone: the dialog would close on this Escape too. if (dialogs.length > 0) e.preventDefault(); onDismiss(); focusOnEscape?.()?.focus(); } document.addEventListener("pointerdown", onPointerDown, true); document.addEventListener("keydown", onKeyDown); return () => { document.removeEventListener("pointerdown", onPointerDown, true); document.removeEventListener("keydown", onKeyDown); }; }, [open]); }