Composed
Confirm dialog
Ready-made confirmation dialog opened with a payload, with a pending state and optional typed confirmation.
import { Button, Dialog, DialogConfirm, type DialogConfirmPayload } from "@openperf/deft";
const handle = Dialog.createHandle<DialogConfirmPayload>();
<>
<Dialog.Trigger
handle={handle}
payload={{
title: "Revoke this token?",
description: "Requests that use it start to fail right away.",
confirmLabel: "Revoke",
confirmVariant: "danger",
onConfirm: () => revoke.mutate(id, { onSuccess: () => handle.close() }),
}}
render={<Button.Root variant="danger" />}
>
Revoke
</Dialog.Trigger>
<DialogConfirm handle={handle} pending={revoke.isPending} />
</>;When to use
| Need | Component |
|---|---|
| Confirm an action that removes something or is hard to undo | Confirm dialog |
| Dialog with a form or any other custom content | Dialog |
| Panel that slides in from the edge of the screen | Drawer |
Opening
The dialog has no open prop. It is driven by a handle from Dialog.createHandle<DialogConfirmPayload>(); create it once, in a module constant or a useState initializer.
| Way to open | Use |
|---|---|
Dialog.Trigger with handle | Any number of triggers share one handle, each with its own payload: one dialog serves a list. |
handle.openWithPayload(payload) | From an event handler when there is no trigger element, such as a bulk action. |
Closing and pending
onConfirm only runs your callback: the dialog does not close itself, so it stays open while a request runs and after it fails. Call handle.close() when the action succeeds.
While pending is set, the confirm button shows a spinner, Cancel is disabled, and Escape or a press outside no longer closes the dialog; handle.close() still does.
The dialog has no close button in the corner: Cancel, Escape and a press outside are the ways out.
Typed confirmation
With expected: "acme" the dialog adds a text field labeled "Type acme to confirm" and disables the confirm button until the typed text, once trimmed, matches exactly, including letter case. Enter in the field confirms once it matches.
Extra content
A function passed as children receives the current payload and renders above the typed confirmation field. Extend the payload type when it needs extra data:
import { DialogConfirm, type DialogConfirmPayload, type DialogHandle } from "@openperf/deft";
interface DeleteProjectsPayload extends DialogConfirmPayload {
names: string[];
}
interface DeleteProjectsDialogProps {
handle: DialogHandle<DeleteProjectsPayload>;
pending: boolean;
}
export const DeleteProjectsDialog = ({ handle, pending }: DeleteProjectsDialogProps) => (
<DialogConfirm handle={handle} pending={pending}>
{(payload) => (
<ul>
{payload?.names.map((name) => (
<li key={name}>{name}</li>
))}
</ul>
)}
</DialogConfirm>
);API
DialogConfirm
Generic over the payload type, which must extend DialogConfirmPayload. The default labels are Lingui messages, so the dialog must render inside the root providers.
Prop
Type
DialogConfirmPayload
The payload of the handle and of each trigger. DialogConfirmContent is the same type without onConfirm, for a component that supplies the callback itself.
Prop
Type