Appearance
useConfirmDialog
Creates event hooks to support modals and confirmation dialog chains
Demo
Functions and hooks
reveal()- triggersonRevealhook and setsisRevealedtotrue. Returns promise that resolves byconfirm()orcancel().confirm()- setsisRevealedtofalseand triggersonConfirmhook.cancel()- setsisRevealedtofalseand triggersonCancelhook.
Basic Usage
Using hooks
The returned onReveal / onConfirm / onCancel are stable registration functions following the useListener protocol — each accepts a callback and returns the off function that unsubscribes it, so listeners never leak and never fire after the component unmounts:
tsx
import { useConfirmDialog } from '@reause/core'
import { useListener } from '@reause/shared'
const { isRevealed, reveal, confirm, cancel, onReveal, onConfirm, onCancel }
= useConfirmDialog()
useListener(onReveal, () => {
// modal shown
})
function Component() {
return (
<>
<button type="button" onClick={() => reveal()}>
Reveal Modal
</button>
{isRevealed && (
<div className="modal-bg">
<div className="modal">
<h2>Confirm?</h2>
<button type="button" onClick={() => confirm()}>
Yes
</button>
<button type="button" onClick={() => cancel()}>
Cancel
</button>
</div>
</div>
)}
</>
)
}Promise
If you prefer working with promises:
tsx
import { useConfirmDialog } from '@reause/core'
const {
isRevealed,
reveal,
confirm,
cancel,
} = useConfirmDialog()
async function openDialog() {
const { data, isCanceled } = await reveal()
if (!isCanceled)
console.log(data)
}useConfirmDialog accepts an optional React ref source (RefObject<boolean>, e.g. the result of useRef) that the controls keep in sync — mirroring upstream's optional shallowRef parameter. When omitted, the revealed state is internal:
tsx
import { useConfirmDialog } from '@reause/core'
import { useRef } from 'react'
const show = useRef(false)
const { isRevealed, reveal, confirm, cancel } = useConfirmDialog(show)Type Declarations
ts
export type UseConfirmDialogRevealResult<C, D> = {
data?: C;
isCanceled: false;
} | {
data?: D;
isCanceled: true;
}
export interface UseConfirmDialogReturn<RevealData, ConfirmData, CancelData> {
isRevealed: boolean;
reveal: (data?: RevealData) => Promise<UseConfirmDialogRevealResult<ConfirmData, CancelData>>;
confirm: (data?: ConfirmData) => void;
cancel: (data?: CancelData) => void;
onReveal: (fn: (data: RevealData) => void) => () => void;
onConfirm: (fn: (data: ConfirmData) => void) => () => void;
onCancel: (fn: (data: CancelData) => void) => () => void;
}
export function useConfirmDialog<RevealData = any, ConfirmData = any, CancelData = any>(revealed?: RefObject<boolean>): UseConfirmDialogReturn<RevealData, ConfirmData, CancelData>