Skip to content

CommandDialog for running Arc commands

CommandDialog runs one Cratis Arc command in a dialog without leaving the current page. Pass the generated command proxy and the fields to edit, and seed required values that have no field, such as the id of a project being renamed. It combines CommandForm validation with Dialog presentation: confirm stays disabled while the form is invalid, execution manages the busy state, and failures leave the dialog open for corrections.

Use the typed dialog pattern below to await the outcome from the calling page. For field-specific checks or value transformations, see Advanced Features.

To let the calling page wait for the user’s decision:

  1. Open the dialog through useDialog<TResult>().
  2. Use useDialogContext<TRequest, TResult>() inside the dialog to return a value when it closes.
  3. await the dialog at the call site and handle [dialogResult, value].

useDialogContext takes the request (input props) type first and the result type second. When the dialog returns the command’s response, use the response type as TResult and pass it to closeDialog from onSuccess.

The example assumes CreateProject is the proxy Arc generates from a C# [Command] whose Handle() returns a CreateProjectResponse, and that the app renders <Arc> around CratisComponentsProvider.

import { DialogResult, useDialog, useDialogContext } from '@cratis/arc.react/dialogs';
import { CommandDialog } from '@cratis/components/CommandDialog';
import { InputTextField } from '@cratis/components/CommandForm';
import { CreateProject } from './CreateProject';
type CreateProjectResponse = {
projectId: string;
};
const CreateProjectDialog = () => {
const { closeDialog } = useDialogContext<object, CreateProjectResponse>();
return (
<CommandDialog<CreateProject, CreateProjectResponse>
command={CreateProject}
title='Create project'
okLabel='Create'
onSuccess={(response) => closeDialog(DialogResult.Ok, response)}
>
<InputTextField<CreateProject> value={(c) => c.name} title='Name' />
<InputTextField<CreateProject> value={(c) => c.email} title='Contact email' type='email' />
</CommandDialog>
);
};
export const Projects = () => {
const [CreateProjectDialogWrapper, showCreateProjectDialog] =
useDialog<CreateProjectResponse>(CreateProjectDialog);
const createProject = async () => {
const [result, response] = await showCreateProjectDialog();
if (result === DialogResult.Ok && response) {
console.log('Created project', response.projectId);
}
};
return (
<>
<button type='button' onClick={createProject}>
Create project
</button>
<CreateProjectDialogWrapper />
</>
);
};

What happens when the user clicks Create:

  1. The command runs. While it is in flight the dialog is busy (see Busy State).
  2. On failure the matching result callbacks fire, field errors from the server appear on the form, and the dialog stays open.
  3. On success onSuccess receives the typed response. Here it calls closeDialog(DialogResult.Ok, response), which closes the dialog and resolves the caller’s await with the response. CommandDialog then also closes through the dialog context with DialogResult.Ok; useDialog has already resolved, so that second close changes nothing.

Cancel, the header close (X), Escape, and a backdrop click close the dialog with DialogResult.Cancelled without any callback. You only need onSuccess here because the caller wants the response; a dialog whose caller only needs to know that it succeeded can leave out every callback.

A generated command proxy starts with its command values unset. Arc’s client validation reports non-optional payload properties that are still undefined or null, so confirm stays disabled until those properties have values. Properties supplied as route parameters are excluded from that required-property check. Seed required values that no field renders, such as the id of the item being edited, before the user can confirm.

Prop When it applies Counts for validation before confirm
initialValues Seeds the command when the form mounts and becomes the change-tracking baseline Yes
currentValues Overlays values the parent keeps changing; the form re-validates when they change Yes
onBeforeExecute Transforms the values only after the user clicks confirm, immediately before execution No

Pass required ids and the values being edited through initialValues:

import { useDialog } from '@cratis/arc.react/dialogs';
import { CommandDialog } from '@cratis/components/CommandDialog';
import { InputTextField } from '@cratis/components/CommandForm';
import { Guid } from '@cratis/fundamentals';
import { RenameProject } from './RenameProject';
type RenameProjectDialogProps = {
projectId: Guid;
name: string;
};
const RenameProjectDialog = ({ projectId, name }: RenameProjectDialogProps) => (
<CommandDialog<RenameProject>
command={RenameProject}
title='Rename project'
initialValues={{ projectId, name }}
>
<InputTextField<RenameProject> value={(c) => c.name} title='Name' />
</CommandDialog>
);
export const RenameProjectButton = (props: RenameProjectDialogProps) => {
const [RenameProjectDialogWrapper, showRenameProjectDialog] = useDialog(RenameProjectDialog);
return (
<>
<button type='button' onClick={() => showRenameProjectDialog(props)}>
Rename
</button>
<RenameProjectDialogWrapper />
</>
);
};

For a client-generated id, create it once and seed it the same way, for example const [projectId] = useState(() => Guid.create()); followed by initialValues={{ projectId }}. Keeping the value stable avoids generating a new id every time the component renders.

Field display defaults are not command values either. A CheckboxField shows an unchecked box and a NumberField shows 0 while the bound property is still unset, so a non-optional boolean or number keeps confirm disabled until the user edits it. Seed those values, for example initialValues={{ isActive: false }}, when the default is an acceptable answer.

Wrap fields in CommandDialog.Column to lay them out in columns. It is the same as CommandForm.Column: the form still binds the fields inside it. The column does not position itself, so supply the grid or flex container yourself:

<CommandDialog<UpdateProfile> command={UpdateProfile} title='Edit profile' okLabel='Save'>
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '1.5rem' }}>
<CommandDialog.Column>
<InputTextField<UpdateProfile> value={(c) => c.firstName} title='First name' />
<InputTextField<UpdateProfile> value={(c) => c.email} title='Email' type='email' />
</CommandDialog.Column>
<CommandDialog.Column>
<InputTextField<UpdateProfile> value={(c) => c.lastName} title='Last name' />
<InputTextField<UpdateProfile> value={(c) => c.phone} title='Phone' />
</CommandDialog.Column>
</div>
<TextAreaField<UpdateProfile> value={(c) => c.bio} title='Bio' rows={3} />
</CommandDialog>

Here UpdateProfile is a command proxy, and InputTextField and TextAreaField come from @cratis/components/CommandForm. A field outside the container, such as Bio, renders below the columns.

The confirm button is enabled when all of the following hold:

  • Arc’s client validation passes: required payload properties have values and the command’s client-side validation rules pass. The form validates on mount and after every change.
  • No custom field error is set, including messages returned by onFieldValidate.
  • The isValid prop is not false. It can only disable a valid form; isValid={true} never enables an invalid one.
  • No submission is in progress, including transformation or pre-execution confirmation.

The form is treated as invalid until its first validation finishes, so confirm starts out disabled for a moment even when every value is already present.

validateOn ('blur' by default, or 'change' / 'both') and validateOnInit only control when error messages appear. They do not change when confirm enables. Set autoServerValidate to also ask the server’s validate endpoint once client validation passes (throttled by autoServerValidateThrottle, 500 ms by default); its result also enables or disables confirm.

A message returned by onFieldValidate appears on the field and disables confirm until that custom error is cleared. The callback runs when the field changes, not on mount; put rules that must also check initial values on the command itself. See Field Validation for the callback contract.

  • command: Constructor for the command type
  • title: Dialog title text
  • visible: Boolean controlling dialog visibility (defaults to true). See Controlled visibility
  • initialValues: Initial values for the command form. See Initialize command values
  • currentValues: Values that the parent keeps in sync with the command
  • populateFromQuery, populateFromObservableQuery, populateFromQueryArgs: Seed the form from a query result (Arc CommandForm props)
  • onSuccess: Callback invoked on successful command execution with the typed response
  • onFailed: Callback invoked when command execution fails with the full CommandResult<TResponse>
  • onException: Callback invoked when the command throws an exception with error messages and stack trace
  • onUnauthorized: Callback invoked when authorization fails
  • onValidationFailure: Callback invoked on validation errors with the validation results
  • onConfirm: Runs only after successful command execution; return true to close the dialog
  • onCancel: Runs for Cancel, the header close (X), Escape, and a backdrop click; return true to close the dialog
  • onClose: Fallback used for whichever of onConfirm / onCancel is absent; closes unless it returns false
  • okLabel: Custom text for confirm button (default: the provider’s messages.dialog.ok, then 'Ok')
  • cancelLabel: Custom text for cancel button (default: the provider’s messages.dialog.cancel, then 'Cancel')
  • yesLabel, noLabel: Labels for YesNo and YesNoCancel button modes
  • buttons: DialogButtons value or custom footer content (default: DialogButtons.OkCancel). A custom footer does not run the command; keep the predefined buttons unless you execute the command yourself
  • initialFocus: Where keyboard focus lands when the dialog opens — forwarded to Dialog (see below)
  • dismissable: Whether the header close (X), Escape, and a backdrop click are offered. Defaults to true for predefined buttons
  • closeAriaLabel: Accessible name for the header close button
  • resizable: Accepted for source compatibility; the viewport-bounded Cratis dialog has no resize handle
  • isValid: Additional validity gate combined with command form validity
  • validateOn, validateOnInit, validateAllFieldsOnChange: When validation messages are shown
  • autoServerValidate, autoServerValidateThrottle: Server-side validation while the user edits
  • onFieldValidate: Custom validation function for fields
  • onFieldChange: Callback when field values change
  • onBeforeExecute: Transform command values before execution. It must return the values to run with and may be async
  • confirmBeforeExecute(values): Optional synchronous or async guard called with the transformed values after validation. Return true to execute or false to stay open without executing
  • style: Custom CSS styles
  • contentStyle: Custom CSS styles for the dialog content area
  • width: Dialog width (default: '450px')
  • headerActions: Actions rendered in the dialog header before the close button
  • hideGlobalHeaderActions: Leaves out the app-wide header actions registered on CratisComponentsProvider
  • sliceLocation: The { module, feature, slice } the dialog was opened from, handed to app-wide header actions
  • className, pt: Styling hooks for the Cratis-owned dialog root and stable parts
  • ptOptions, unstyled: Retained temporarily for source compatibility; ignored because Cratis part attributes always merge and styling is CSS-owned

CommandDialog supports the following result callbacks that are invoked based on the command execution outcome:

  • onSuccess(response: TResponse): Invoked when the command executes successfully. Receives the typed response.
  • onFailed(commandResult: CommandResult<TResponse>): Invoked when command execution fails for any reason.
  • onException(messages: string[], stackTrace: string): Invoked when the command throws an exception.
  • onUnauthorized(): Invoked when authorization fails.
  • onValidationFailure(validationResults: ValidationResult[]): Invoked on validation errors.

On success, the dialog awaits onSuccess before asking onConfirm, or its onClose fallback, whether to close. On failure, it awaits onFailed first, then each matching callback in order: onException, onUnauthorized, and onValidationFailure. A validation failure therefore calls onFailed before onValidationFailure; the dialog then applies the result to the form and stays open.

If onBeforeExecute throws, the command does not execute, no result callback runs, and the dialog stays open. A rejection from confirmBeforeExecute also keeps the dialog open and skips execution; it calls onException with the error message and stack trace (or logs the error if no callback is supplied), not onFailed.

“Closes” below means that the dialog calls closeDialog from the surrounding useDialog context.

User action onConfirm / onCancel present Only onClose present No callback
Confirm, after the command succeeds onConfirm() runs; closes only if it returns true onClose(DialogResult.Ok) runs; closes unless it returns false Closes with DialogResult.Ok
Confirm, confirmBeforeExecute returns false Not called; dialog stays open Not called; dialog stays open Dialog stays open
Confirm, command fails Not called; dialog stays open Not called; dialog stays open Dialog stays open
Cancel, X, Escape, backdrop onCancel() runs; closes only if it returns true onClose(DialogResult.Cancelled) runs; closes unless it returns false Closes with DialogResult.Cancelled

A callback that calls closeDialog(...) itself closes the dialog regardless of its return value. Use closeDialog(DialogResult.Ok, value) when the caller needs a value; the automatic close never passes one.

Use confirmBeforeExecute for a second, explicit decision. Unlike onConfirm, which runs after successful execution to decide whether to close, the guard runs before execution. A false result leaves the command dialog open, does not execute, and calls neither onSuccess nor onFailed. If you also provide onBeforeExecute, the guard receives its returned values, not the original form values. Client validation still happens first. If the values change while confirmation is pending (for example, through currentValues), the command is not executed and no result callback runs; confirm again with the new values.

Register ConfirmationDialog in DialogComponents above the component using the hook. The hook must run in a child of that provider, not in the component that creates the provider. The confirmation opens on a higher dialog z-index tier and its No result leaves the command form ready to edit or retry:

import { DialogButtons, DialogComponents, DialogResult, useConfirmationDialog } from '@cratis/arc.react/dialogs';
import { CommandDialog } from '@cratis/components/CommandDialog';
import { ConfirmationDialog } from '@cratis/components/Dialogs';
import { UpdateProject } from './UpdateProject';
function UpdateProjectDialog() {
const [showConfirmation] = useConfirmationDialog(
'Save changes?', 'Apply these changes?', DialogButtons.YesNo,
);
return (
<CommandDialog<UpdateProject>
command={UpdateProject}
title='Update project'
initialValues={{ name: 'Example Project' }}
confirmBeforeExecute={async (_values) =>
(await showConfirmation()) === DialogResult.Yes}
/>
);
}
export function ProjectDialogs() {
return (
<DialogComponents confirmation={ConfirmationDialog}>
<UpdateProjectDialog />
</DialogComponents>
);
}

The equivalent runnable, type-checked example is the WithPreExecutionConfirmation story in Source/CommandDialog/CommandDialog.stories.tsx. In an app using useDialog, render the command dialog’s wrapper inside DialogComponents instead of rendering it permanently as above.

Without useDialog there is no dialog context, so nothing closes automatically. Close the dialog yourself from onSuccess and onCancel:

import { useState } from 'react';
import { CommandDialog } from '@cratis/components/CommandDialog';
import { InputTextField } from '@cratis/components/CommandForm';
import { Guid } from '@cratis/fundamentals';
import { RenameProject } from './RenameProject';
export const RenameProjectInline = ({ projectId, name }: { projectId: Guid; name: string }) => {
const [visible, setVisible] = useState(false);
return (
<>
<button type='button' onClick={() => setVisible(true)}>
Rename
</button>
{visible && (
<CommandDialog<RenameProject>
command={RenameProject}
title='Rename project'
initialValues={{ projectId, name }}
onSuccess={() => setVisible(false)}
onCancel={() => setVisible(false)}
>
<InputTextField<RenameProject> value={(c) => c.name} title='Name' />
</CommandDialog>
)}
</>
);
};

Rendering the dialog only while it is open gives the user a fresh form each time. If you keep it mounted and toggle visible instead, the command form stays mounted while hidden, so values typed before a cancel are still there when it opens again. useDialog unmounts the dialog when it closes.

The dialog targets the confirm button for initial focus, and Enter activates a focused native button. Missing required values keep confirm disabled, but a command with valid initial values or no input can become confirmable as soon as initial validation finishes. For a destructive action, do not rely on missing input as protection against an unintended confirmation.

Set initialFocus={DialogInitialFocus.Cancel} to focus Cancel instead. The prop is forwarded to Dialog; it changes the focus target, not the command execution or dismissal behavior.

import { CommandDialog } from '@cratis/components/CommandDialog';
import { DialogInitialFocus } from '@cratis/components/Dialogs';
import { Guid } from '@cratis/fundamentals';
import { DeletePersonalData } from './DeletePersonalData';
export const DeletePersonalDataDialog = ({ personId }: { personId: Guid }) => (
<CommandDialog<DeletePersonalData>
command={DeletePersonalData}
title='Delete personal data?'
okLabel='Delete'
initialValues={{ personId }}
initialFocus={DialogInitialFocus.Cancel}
>
This cannot be undone.
</CommandDialog>
);

Pressing Enter inside a text field does not submit the command. The footer buttons are plain type="button" buttons, so the user confirms by activating the confirm button.

CommandDialog automatically manages a busy state from the start of onBeforeExecute, through confirmBeforeExecute, until command execution settles:

  • The header close and secondary buttons are disabled and the primary button shows a loading spinner. The primary button stays focusable but is aria-disabled and ignores activation, so focus stays inside the dialog while the command runs.
  • The form fields are disabled and inert, so values cannot change while the command runs.
  • Escape and backdrop dismissal are ignored while work is in flight.
  • Once execution completes (success or failure), the buttons return to their normal state. This happens before the result callbacks run; see When a result callback rejects.
  • After a refused submit the dialog stays open, focus returns to the confirm button, and a polite live region announces messages.dialog.refused (default 'The action was not completed. Review the dialog and try again.'). When the command result carries validation results, the live region instead announces how many problems there are and the first message, and focus moves to the first invalid field. See Announcing validation failures. A successful submit follows the dialog callback close decision.

CommandDialog is built on top of CommandForm and Dialog, and uses command form context internally for values, validation, and execution state.

Field children are bound to the dialog’s own command instance. Do not nest another CommandForm or an AutoCommandForm inside a CommandDialog: the inner form has its own command instance, so its fields never reach the command the dialog executes. To generate fields inside a dialog, use AutoCommandFields.

CommandDialog integrates with:

  • @cratis/arc/commands for command execution
  • @cratis/arc.react/commands for form handling
  • @cratis/arc.react/dialogs for useDialog, useDialogContext, DialogResult, and DialogButtons
  • React Aria modal/focus behavior behind Cratis-owned markup