import * as React from "react"; import { X } from "lucide-react"; import type { SiteAdminToast } from "../context"; import { cn } from "../lib/cn"; type Tone = "success" | "info" | "warning" | "error"; type Notice = { id: number; tone: Tone; message: string }; /** What is not a problem, said politely and gone by itself. */ const isPolite = (tone: Tone) => tone === "success" || tone === "info"; /** Only a success or a note goes away by itself; a problem stays until * dismissed. */ const SUCCESS_DISMISS_MS = 8000; /** * The bundle's toasts. wp-admin has none of its own the bundle may use * (`wp.data` notices would mean touching WordPress's globals), so this is a * small live region: polite for a success or a note, assertive for problems. */ export function useNotices(): { toast: SiteAdminToast; notices: Notice[]; dismiss: (id: number) => void; } { const [notices, setNotices] = React.useState([]); const nextId = React.useRef(0); const dismiss = React.useCallback((id: number) => { setNotices((all) => all.filter((n) => n.id !== id)); }, []); const toast = React.useMemo(() => { const push = (tone: Tone) => (message: string) => { const id = ++nextId.current; setNotices((all) => [...all, { id, tone, message }]); return id; }; return { success: push("success"), info: push("info"), warning: push("warning"), error: push("error"), }; }, []); return { toast, notices, dismiss }; } const TONE_CLASS: Record = { success: "border-emerald-200 bg-emerald-50 text-emerald-900", info: "border-sky-200 bg-sky-50 text-sky-900", warning: "border-amber-300 bg-amber-50 text-amber-900", error: "border-destructive/30 bg-destructive/10 text-destructive", }; function NoticeItem({ notice, dismissLabel, onDismiss, }: { notice: Notice; dismissLabel: string; onDismiss: (id: number, hadFocus: boolean) => void; }) { const ref = React.useRef(null); // Paused while the pointer is over it or focus is inside it, so a success // being read or reached by keyboard does not vanish under the reader. Two // flags: the pointer leaving must not unpause a notice that has focus. const [hovered, setHovered] = React.useState(false); const [focused, setFocused] = React.useState(false); const paused = hovered || focused; React.useEffect(() => { if (!isPolite(notice.tone) || paused) return; const timer = setTimeout( () => onDismiss(notice.id, !!ref.current?.contains(document.activeElement)), SUCCESS_DISMISS_MS, ); return () => clearTimeout(timer); }, [notice, paused, onDismiss]); return (
setHovered(true)} onPointerLeave={() => setHovered(false)} onFocus={() => setFocused(true)} onBlur={(e) => { if (!e.currentTarget.contains(e.relatedTarget as Node | null)) { setFocused(false); } }} className={cn( "pointer-events-auto flex items-start gap-3 rounded-lg border px-4 py-3 text-sm shadow-sm", TONE_CLASS[notice.tone], )} >

{notice.message}

); } /** * Both regions stay mounted and empty when idle: a live region added to the * DOM together with its text is not announced by every screen reader. */ export function Notices({ notices, dismiss, dismissLabel, focusFallback, }: { notices: Notice[]; dismiss: (id: number) => void; dismissLabel: string; /** Where focus goes when the focused notice is the last one removed. */ focusFallback: HTMLElement; }) { const containerRef = React.useRef(null); // Removing the focused notice would drop focus to : hand it to the // next notice's button, or to the app. const pendingFocus = React.useRef(false); const onDismiss = React.useCallback( (id: number, hadFocus: boolean) => { pendingFocus.current = hadFocus; dismiss(id); }, [dismiss], ); React.useEffect(() => { if (!pendingFocus.current) return; pendingFocus.current = false; const next = containerRef.current?.querySelector("button"); // `preventScroll`: the fallback is the whole app, and focusing it would // jump wp-admin back to its top. if (next) { next.focus({ preventScroll: true }); } else { if (!focusFallback.hasAttribute("tabindex")) { // Focusable by script only, and with no ring around the whole app. focusFallback.setAttribute("tabindex", "-1"); focusFallback.style.outline = "none"; } focusFallback.focus({ preventScroll: true }); } }, [notices, focusFallback]); const render = (list: Notice[]) => list.map((n) => ( )); return (
{render(notices.filter((n) => isPolite(n.tone)))}
{render(notices.filter((n) => !isPolite(n.tone)))}
); }