Confirm

<Confirm> is a generic confirmation dialog component, used by Shadmin internally for destructive actions. You can use it in your own custom actions as well.

Installation

pnpm dlx shadcn@latest add @shadmin/confirm

Usage

<Confirm> is a controlled component. You must manage its isOpen state and provide onClose and onConfirm handlers.

For example, here is how to use it in a custom delete button with a confirmation dialog:

import { useState } from "react";
import {
  useDelete,
  useRecordContext,
  useResourceContext,
  useRedirect,
} from "ra-core";
import { Button } from "@/components/ui/button";
import { Confirm } from "@/components/admin/confirm";
 
const DeleteButton = () => {
  const resource = useResourceContext();
  const record = useRecordContext();
  const [isOpen, setIsOpen] = useState(false);
  const [deleteOne, { isPending }] = useDelete();
  const redirect = useRedirect();
 
  const handleDelete = () => {
    deleteOne(
      resource,
      { id: record?.id, previousData: record },
      {
        onSuccess: () => {
          setIsOpen(false);
          redirect("list", resource);
        },
      },
    );
  };
 
  return (
    <>
      <Button variant="destructive" onClick={() => setIsOpen(true)}>
        Delete
      </Button>
      <Confirm
        isOpen={isOpen}
        title="Are you sure you want to delete this element?"
        content="This action cannot be undone."
        onConfirm={handleDelete}
        onClose={() => setIsOpen(false)}
        loading={isPending}
      />
    </>
  );
};

Props

PropRequiredTypeDefaultDescription
isOpenRequiredbooleanfalseWhether dialog is shown
onCloseRequired() => void-Close handler
onConfirmRequired(e) => void-Confirm handler
titleRequiredReactNode-Title (i18n key or node)
contentOptionalReactNode-Body content
cancelOptionalstringra.action.canceli18n key for cancel button
confirmOptionalstringra.action.confirmi18n key for confirm button
confirmColorOptional"primary" | "warning"primaryStyle variant
ConfirmIconOptionalComponentTypeCheckCircleIcon for confirm
CancelIconOptionalComponentTypeAlertCircleIcon for cancel
loadingOptionalboolean-Disable buttons while true