File size: 7,065 Bytes
39a9177 dcd43e2 39a9177 dcd43e2 39a9177 dcd43e2 39a9177 dcd43e2 39a9177 9a6cb9a 39a9177 dcd43e2 39a9177 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | // ---------------------------------------------------------------------------
// 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>
);
}
|