Create confirmation dialogs as simple as window.confirm(), but with full customization and Promise-based API.
π― Simple confirmation dialogs
const result = await confirm({ message: 'Delete this item?' });
if (result) {
// User confirmed
}π¨ Fully customizable UI - No built-in styling. Use your own components, UI libraries, or design system.
β‘ Promise-based API - Works seamlessly with async/await, no complex state management needed.
π React Context support - Access your app's context, themes, and providers from within dialogs.
π¦ Lightweight - No dependencies, small bundle size.
npm install react-confirmimport React from 'react';
import { confirmable, createConfirmation, type ConfirmDialogProps } from 'react-confirm';
const MyDialog = ({ show, proceed, message }: ConfirmDialogProps<{ message: string }, boolean>) => (
<div className={`dialog-overlay ${show ? 'show' : 'hide'}`}>
<div className="dialog">
<p>{message}</p>
<button onClick={() => proceed(true)}>Yes</button>
<button onClick={() => proceed(false)}>No</button>
</div>
</div>
);
export const confirm = createConfirmation(confirmable(MyDialog));import { confirm } from './confirm';
const handleDelete = async (): Promise<void> => {
// Fully type-safe: message is required, result is boolean
const result = await confirm({
message: 'Are you sure you want to delete this item?'
});
if (result) {
// User confirmed - proceed with deletion
deleteItem();
}
};
// In your component
<button onClick={handleDelete}>Delete Item</button>You can control pending confirmation dialogs from outside using the proceed and dismiss functions.
Resolves the promise with a value and closes the dialog.
import { proceed } from 'react-confirm';
const p = confirm({ message: 'Continue?' });
// Auto-close after 10 seconds with a result
setTimeout(() => {
proceed(p, false);
}, 10000);
const result = await p; // falseCloses the dialog without resolving or rejecting the promise. The promise remains pending.
import { dismiss } from 'react-confirm';
const p = confirm({ message: 'Continue?' });
// Close dialog after 10 seconds without resolving
setTimeout(() => {
dismiss(p);
}, 10000);
// Note: await p will never complete since the promise is not resolvedIf your dialog needs to access React Context (themes, authentication, etc.), use the context-aware approach:
Key differences from Quick Start:
// 1. Import ContextAwareConfirmation instead of createConfirmation
import { confirmable, ContextAwareConfirmation, type ConfirmDialogProps } from 'react-confirm';
// 2. Add ConfirmationRoot to your app
function App(): JSX.Element {
return (
<ThemeProvider>
<div>
<ContextAwareConfirmation.ConfirmationRoot />
<YourAppContent />
</div>
</ThemeProvider>
);
}
// 3. Your dialog can now use context
const ThemedDialog = ({ show, proceed, message }: ConfirmDialogProps<Props, boolean>) => {
const theme = useContext(ThemeContext); // β
Context works!
// ... rest of dialog implementation
};
// 4. Use ContextAwareConfirmation.createConfirmation
const confirm = ContextAwareConfirmation.createConfirmation(confirmable(ThemedDialog));TypeScript automatically infers types from your dialog's Props definition, making the confirmation function fully type-safe.
// Option 1: Using React.FC with ConfirmDialogProps
const Confirmation1: React.FC<ConfirmDialogProps<Props, Response>> = (props) => (<Dialog />);
// Option 2: Using ConfirmDialog type
const Confirmation2: ConfirmDialog<Props, Response> = (props) => (<Dialog />);- React 18+: Use
react-confirmversion 0.2.x or 0.3.x - React β€17: Use
react-confirmversion 0.1.x
- Simple Example - Complete TypeScript implementation.
- Context Example - Using React Context with themes and providers
Source code for these examples can be found in the react-confirm-sample repository, which also contains some archived older implementations.