loopable / web /src /ui /ConfirmDialog.tsx
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
9a6cb9a verified
Raw History Blame Contribute Delete
7.07 kB
// ---------------------------------------------------------------------------
// 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 <body> 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 (
<ConfirmDialogSurface
title={title}
body={body}
confirmLabel={confirmLabel}
cancelLabel={cancelLabel}
tone={tone}
onConfirm={onConfirm}
onCancel={onCancel}
busy={busy}
surfaceRef={surfaceRef}
/>
);
}
/**
* ⚠ 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<ConfirmDialogProps, "open"> & { cancelLabel: string; busy: boolean }) {
const panelRef = useRef<HTMLDivElement>(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 (
<BodyPortal>
<div className="lp-dialog-scrim" ref={surfaceRef}>
<div
ref={panelRef}
className="lp-dialog"
role="dialog"
aria-modal="true"
aria-labelledby={titleId}
>
<div className="lp-dialog-head">
<h2 className="lp-dialog-title" id={titleId}>
{title}
</h2>
<button
type="button"
className="lp-dialog-x"
aria-label="Close"
disabled={busy}
onClick={dismiss}
>
<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" focusable="false">
<path
d="M4 4l8 8M12 4l-8 8"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
/>
</svg>
</button>
</div>
{body === undefined ? null : <div className="lp-dialog-body">{body}</div>}
{/* ⛔ 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. */}
<div className="lp-dialog-actions">
<button
type="button"
className="lp-dialog-btn lp-dialog-btn-quiet"
data-overlay-autofocus=""
disabled={busy}
onClick={dismiss}
>
{cancelLabel}
</button>
<button
type="button"
className={
"lp-dialog-btn " +
(tone === "destructive" ? "lp-dialog-btn-destructive" : "lp-dialog-btn-primary")
}
disabled={busy}
onClick={onConfirm}
>
{confirmLabel}
</button>
</div>
</div>
</div>
</BodyPortal>
);
}
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 <button> with no track and no
* knob, silently. `.lp-switch` in kit.css repeats that shape declaration for
* declaration and is globally available. It is one shape, reachable everywhere,
* not a second shape.
*/
export function DialogToggle({ label, checked, onChange, id }: DialogToggleProps) {
const generated = useId();
const labelId = id ?? generated;
return (
<div className="lp-toggle-row">
<span className="lp-toggle-label" id={labelId}>
{label}
</span>
<button
type="button"
role="switch"
aria-checked={checked}
aria-labelledby={labelId}
className={"lp-switch" + (checked ? " is-on" : "")}
onClick={() => onChange(!checked)}
>
<span className="lp-switch-knob" aria-hidden="true" />
</button>
</div>
);
}