Dialog
Base dialog component for creating typed dialogs that can be awaited.
Recommended Pattern
Section titled “Recommended Pattern”Use useDialog<T>() at the call site and useDialogContext<T>() inside the dialog component.
- The caller opens the dialog with
awaitand receives[dialogResult, value] - The dialog closes itself through
closeDialog(...) - The generic
Tis the value returned from the dialog
This pattern gives strongly typed dialog results and a simple async flow.
Example
Section titled “Example”import { useState } from 'react';import { DialogResult, useDialog, useDialogContext } from '@cratis/arc.react/dialogs';import { Dialog } from '@cratis/components/Dialogs';
type Project = { id: string; name: string;};
const AddProjectDialog = () => { const { closeDialog } = useDialogContext<Project>(); const [name, setName] = useState('');
return ( <Dialog title='Add project' isValid={name.trim().length > 0} onConfirm={() => closeDialog(DialogResult.Ok, { id: crypto.randomUUID(), name })} onCancel={() => closeDialog(DialogResult.Cancelled)} > {/* Dialog content */} </Dialog> );};
const MyComponent = () => { const [AddProjectDialogWrapper, showAddProjectDialog] = useDialog<Project>(AddProjectDialog);
const handleAddProject = async () => { const [result, project] = await showAddProjectDialog(); if (result === DialogResult.Ok && project) { // Use the typed result } };
return ( <> <button onClick={handleAddProject}>Add project</button> <AddProjectDialogWrapper /> </> );};title: Dialog header textvisible: Controls visibility (defaults totrue)onConfirm: Callback for confirm actionsonCancel: Callback for cancel actionsonClose: Fallback close callbackbuttons: PredefinedDialogButtonsor custom footer contentwidth: Dialog widthstyle: Custom dialog style forwarded to PrimeReactDialogcontentStyle: Custom content area style forwarded to PrimeReactDialogresizable: Enables resizeisValid: Enables or disables confirm actionsisBusy: Whentrue, disables all buttons and shows a loading spinner on the primary action buttonokLabel,cancelLabel,yesLabel,noLabel: Button labels
- Prefer
onConfirmandonCanceloveronClosefor clear intent. onConfirmandonCancelshould returntrueto close when used.onClosecloses unless it returnsfalse.- For typed, awaitable dialogs, let the dialog call
closeDialog(...)fromuseDialogContext<T>().