Skip to Content
dimah-s3v2.8.1

Delete

Delete buttons that share one confirm dialog, with toasts, for useDelete.

Delete stored files after a confirmation. Delete holds one useDelete hook and one confirm dialog. Every DeleteTrigger inside it asks the root, which confirms, deletes, and reports the result.

  • meeting-notes.docx46.9 KB
  • old-logo.png214.8 KB
  • draft-v1.pdf1.3 MB

Installation

import { Delete, DeleteToasts, DeleteTrigger } from "@dimah-s3/ui";

Complete Installation first: the package or registry setup, the stylesheet, and one <Toaster /> in your layout.

Usage

"use client";

import { useDelete } from "@dimah-s3/react";
import { Delete, DeleteToasts, DeleteTrigger } from "@dimah-s3/ui";

type Attachment = { key: string; name: string; size: number };

export function AttachmentList({
  attachments,
  onRemoved,
}: {
  attachments: Attachment[];
  onRemoved: (keys: string[]) => void;
}) {
  const remove = useDelete({ route: "attachments" });

  return (
    <>
      <Delete delete={remove} onDeleted={(result) => onRemoved(result.deleted)}>
        <ul>
          {attachments.map((file) => (
            <li key={file.key}>
              {file.name}
              <DeleteTrigger
                objectKey={file.key}
                fileName={file.name}
                fileSize={file.size}
                size="sm"
              />
            </li>
          ))}
        </ul>
      </Delete>
      <DeleteToasts delete={remove} />
    </>
  );
}

onDeleted gets the keys that were deleted, failed, or blocked. Remove the deleted rows there.

Anatomy

<Delete delete={remove}>
  <DeleteTrigger objectKey={key} />
</Delete>
<DeleteToasts delete={remove} />
PartDescription
DeleteRoot. Holds the hook and renders one confirm dialog for its triggers
DeleteTriggerA destructive Button that asks the root to delete one object
DeleteConfirmDialogThe default dialog. Pass it to dialog with your own copy
DeleteToastsSuccess and error toasts. Mount once per hook

Examples

Triggers and dialogs

note on a trigger adds a line to the dialog. dialog on the root replaces its copy, and confirm={false} deletes on click.

Delete several files

A trigger of your own calls request from useDeleteContext() with every target. One dialog asks for all of them.

  • scan-001.pdf
  • scan-002.pdf
  • scan-003.pdf

Confirmation

Replace the dialog's copy with dialog:

<Delete
  delete={remove}
  dialog={
    <DeleteConfirmDialog
      title="Remove profile picture?"
      description="Your profile goes back to the default avatar."
      confirmLabel="Remove"
    />
  }
>
  <DeleteTrigger objectKey={avatarKey} />
</Delete>

For a dialog of your own, read confirmation and isDeleting from useDeleteContext(), and call confirm() and dismiss().

Your own triggers

request takes one target or a list, so one confirmation covers a bulk delete:

function DeleteSelected({ files }: { files: Attachment[] }) {
  const { request } = useDeleteContext();

  return (
    <Button
      variant="destructive"
      onClick={(event) =>
        request(
          files.map((file) => ({ objectKey: file.key, fileName: file.name })),
          { trigger: event.currentTarget },
        )
      }
    >
      Delete {files.length} files
    </Button>
  );
}

Focus

When a deleted row unmounts with its trigger, focus goes to the nearest [data-dimah-s3-focus-root]. Mark the list:

<ul data-dimah-s3-focus-root tabIndex={-1}>
  {/* rows with DeleteTrigger */}
</ul>

Toasts

Mount DeleteToasts once per hook. It shows one result toast per call.

Accessibility

  • The dialog is an alert dialog with initial focus on Cancel.
  • At an icon size the trigger keeps "Delete" as its accessible name.

API Reference

Delete

PropTypeDefaultDescription
deleteUseDeleteReturnrequiredThe useDelete return
confirmbooleantrueAsks before deleting
dialogReactNode<DeleteConfirmDialog />Replaces the confirm dialog
onDeleted(result: DeleteResult) => void—Runs once per confirmed delete, before the dialog closes
restoreFocus(targets: DeleteTarget[]) => HTMLElement | null—Where focus goes after the dialog closes
childrenReactNoderequiredThe triggers

DeleteTrigger

Also accepts every Button prop. variant defaults to "destructive".

PropTypeDefaultDescription
objectKeystringrequiredKey of the object to delete
fileNamestringLast segment of keyName in the dialog and toasts
fileSizenumber | null—Size shown in the dialog
noteReactNode—Extra line in the dialog
tooltipReactNode—Tooltip on hover and focus
childrenReactNodeIcon and "Delete"Replaces the icon and label

DeleteConfirmDialog

Each prop replaces the default copy.

PropTypeDescription
titleReactNodeDialog title
descriptionReactNodeDialog description
confirmLabelReactNodeConfirm button label
cancelLabelReactNodeCancel button label

DeleteToasts

PropTypeDefaultDescription
deleteUseDeleteReturnrequiredThe hook return to report

useDeleteContext

Returns the root's state inside Delete.

PropertyTypeDescription
deleteUseDeleteReturnThe root's hook
confirmation{ targets, title?, description?, note? } | nullThe request the dialog asks about, or null while it is closed
isDeletingbooleanThe confirmed request is deleting
request(targets, options?) => voidAsks to delete one target or a list. Replaces a waiting request
confirm() => Promise<DeleteResult>Deletes the targets of confirmation, then closes. Never rejects
dismiss() => voidCloses the dialog without deleting

A target is { objectKey, fileName?, fileSize? }. request options:

OptionTypeDescription
titleReactNodeThe dialog's title for this request
descriptionReactNodeThe dialog's description for this request
noteReactNodeExtra line in the dialog
run(remove) => Promise<DeleteResult>Deletes in place of the targets, such as a folder and its files. remove(targets) deletes with the root's hook. targets may then be empty
triggerHTMLElement | nullWhere focus returns. Defaults to the focused element
onDeleted(result: DeleteResult) => voidRuns after the root's onDeleted, once this request settles
onDismissed() => voidRuns when the request ends without deleting

On this page