// --------------------------------------------------------------------------- // ui/ConfirmDialog.tsx — W51-T01, the wave-51 base kit. // // THE app's confirm dialog. One shape for every irreversible act, so a delete in // the view rail, a delete in the field rail and a discard in the sheet all read // as the same promise. DESIGN.md section 4: a second control shape for the same // act is a new shape joining no rhythm. // // ⚠ IT CARRIES ITS OWN SURFACE, AND THAT IS NOT DECORATION. This renders through // `BodyPortal`, so it is a direct child of and every descendant-scoped rule // in the app ("#something .child") is dead to it. `.cg-pop` shipped transparent // over the canvas for exactly this reason. Every class here is top-level and lives // in styles/wave51/kit.css, which index.css imports GLOBALLY — a per-component CSS // import would put the sheet in one lazy chunk and leave the dialog unstyled on // every page that does not load that chunk, with tsc and the build both green. // // Not adopted anywhere yet, on purpose: the lanes adopt it in their own files. // --------------------------------------------------------------------------- import { useCallback, useId, useRef } from "react"; import type { ReactNode } from "react"; import { BodyPortal, useOverlayLayer } from "../customer-grid/OverlaySurface"; export interface ConfirmDialogProps { open: boolean; /** A short noun phrase, sentence case. Not a question, not a sentence. */ title: string; body?: ReactNode; confirmLabel: string; cancelLabel?: string; tone: "destructive" | "primary"; onConfirm: () => void; onCancel: () => void; /** The action is in flight: both buttons lock, and the dialog stops dismissing. */ busy?: boolean; /** The host layer must name this surface in `outsideElements` or a press here dismisses it. */ surfaceRef?: (element: HTMLElement | null) => void; } export function ConfirmDialog({ open, title, body, confirmLabel, cancelLabel = "Cancel", tone, onConfirm, onCancel, busy = false, surfaceRef, }: ConfirmDialogProps) { if (!open) return null; return ( ); } /** * ⚠ THE HOOKS LIVE HERE, BELOW THE `open` GATE, and the split is the reason this * component exists at all. `useOverlayLayer` traps focus and claims Escape the * moment it runs, so calling it from a component that renders while closed would * have a hidden dialog eating the key. Putting the early return in the wrapper * keeps the hook ORDER constant inside this one (verify_hooks.py's rule) while the * subscription genuinely starts and stops with `open`. */ function ConfirmDialogSurface({ title, body, confirmLabel, cancelLabel, tone, onConfirm, onCancel, busy, surfaceRef, }: Omit & { cancelLabel: string; busy: boolean }) { const panelRef = useRef(null); const titleId = useId(); // A busy dialog is mid-write. Dismissing it would leave the user with no way to // learn how the write ended, so the request is simply refused until it settles. const dismiss = useCallback(() => { if (!busy) onCancel(); }, [busy, onCancel]); useOverlayLayer({ panelRef, onDismiss: dismiss, // DESIGN.md §4: a stray outside click must not discard a modal's draft. dismissOnOutside: false, initialFocus: "[data-overlay-autofocus]", restoreFocus: true, trapFocus: true, cancelEditFirst: true, }); return (

{title}

{body === undefined ? null :
{body}
} {/* ⛔ CANCEL FIRST IN SOURCE ORDER, and it is not a styling accident: it is the initial focus target and the safe default, so the destructive button is never one stray Enter away when the dialog opens. */}
); } export interface DialogToggleProps { label: string; checked: boolean; onChange: (next: boolean) => void; id?: string; } /** * A labelled switch row for a dialog body, such as Airtable's "Duplicate cells". * * ⚠ SAME SWITCH SHAPE AS `.conn-db-switch`, DIFFERENT SELECTOR, and the duplication * is deliberate. `styles/wave49/connectors.css` is imported from the connectors * components, so its rules only reach the bundle chunk those pages pull in; a * dialog opened anywhere else would render a bare ); }