import { useEffect, useRef } from "react";
/** A native modal `<dialog>`: Escape and "Cancel" call `onCancel`, the parent owns `open`. */
export function ConfirmDialog({
open,
message,
confirmLabel = "Delete",
onConfirm,
onCancel,
}: {
open: boolean;
message: string;
confirmLabel?: string;
onConfirm: () => void;
onCancel: () => void;
}) {
const dialog = useRef<HTMLDialogElement>(null);
useEffect(() => {
const element = dialog.current;
if (element === null) return;
if (open && !element.open) element.showModal();
if (!open && element.open) element.close();
}, [open]);
return (
<dialog
ref={dialog}
className="modal"
onCancel={(event) => {
event.preventDefault();
onCancel();
}}
>
<div className="modal-box">
<p>{message}</p>
<div className="modal-action">
<button type="button" className="btn" onClick={onCancel}>
Cancel
</button>
<button type="button" className="btn btn-error" onClick={onConfirm}>
{confirmLabel}
</button>
</div>
</div>
</dialog>
);
}