Confirmation Dialogs
Confirmation dialogs prompt users to make a choice before proceeding with an action. They're useful for confirming destructive operations, gathering simple input, or presenting important decisions.
Basic Usage
Use the confirm command from useMMAppCommands:
import { useMMAppCommands } from '@machinemetrics/mm-react-tools';
function DeleteButton() {
const { confirm } = useMMAppCommands();
const handleDelete = async () => {
const result = await confirm({
heading: 'Delete Machine?',
body: 'This action cannot be undone.',
choices: [
{ value: 'cancel', label: 'Cancel' },
{ value: 'delete', label: 'Delete' },
],
});
if (result === 'delete') {
await api.deleteMachine(machineId);
}
};
return <button onClick={handleDelete}>Delete</button>;
}
Important: The first choice is selected when the user presses Enter. The last choice is styled as the primary (blue) button. Order choices from least to most destructive.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
heading | string | Yes | Dialog title |
body | string | No | Explanatory text |
choices | array | Yes | Array of choice objects (minimum 1, maximum 2) |
Choice Object
| Property | Type | Required | Description |
|---|---|---|---|
value | string | Yes | Value returned when this choice is selected |
label | string | Yes | Button label |
Choice Styling
- The last choice in the array is styled as the primary (blue) button
- All other choices are gray buttons
- The first choice is automatically selected (activated on Enter key)
- Order your choices from least destructive to most destructive
Return Value
Returns a Promise that resolves to:
- The
valueof the selected choice button falseif the user presses Escape or closes the dialog
Examples
Unsaved Changes Warning
const result = await confirm({
heading: 'Unsaved Changes',
body: 'You have unsaved changes. Are you sure you want to leave?',
choices: [
{ value: 'stay', label: 'Stay' },
{ value: 'leave', label: 'Leave' },
],
});
if (result === 'leave') {
navigate('/');
}
The first choice "Stay" is selected by default (on Enter), and "Leave" appears as the blue primary button.
Informational Dialog
await confirm({
heading: 'Machine Offline',
body: 'This machine is currently offline and cannot be edited.',
choices: [{ value: 'ok', label: 'OK' }],
});
Yes/No Decision
const result = await confirm({
heading: 'Save Configuration?',
body: 'Would you like to save this configuration as the default?',
choices: [
{ value: 'no', label: 'No' },
{ value: 'yes', label: 'Yes' },
],
});
if (result === 'yes') {
await saveAsDefault();
}
Handling Escape Key
const result = await confirm({
heading: 'Delete Item?',
body: 'This action cannot be undone.',
choices: [
{ value: 'cancel', label: 'Cancel' },
{ value: 'delete', label: 'Delete' },
],
});
// result will be false if user pressed Escape
if (result === 'delete') {
await deleteItem();
}
Related
useMMAppCommands- Full command reference- Sidesheets - Modal panels for detailed content