# Delete with undo

> One call on any element: it folds in half and in half again, turns edge-on and is gone. unfold() brings it back.

A delete that is one line in your own code. Call foldAway on the photo, card or row being deleted and it is folded like a print in your hands, picture side out: in half, and in half again, each fold a little quicker than the last, until it is a small square; then the square turns edge-on and vanishes, because paper has no thickness. The promise resolves and you remove the item from your state the way you already do. If your app offers Undo, unfold plays the whole thing back from wherever it is. Your list, your toast, your timer and your API stay yours.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/delete-undo.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/delete-undo
- Registry item (source): https://hairlineui.com/r/delete-undo.json

## Usage

```tsx
import { foldAway } from "@/components/delete-undo";

<button
  onClick={async (event) => {
    await foldAway(event.currentTarget.closest("figure"));
    setPhotos((list) => list.filter((p) => p.id !== photo.id));
  }}
>
  Delete
</button>
```

## With your toast and its Undo

Fold the photo away, keep it in your state until your undo window closes, and call unfold if the visitor presses Undo. This uses sonner, but any toast works.

```tsx
"use client";
import * as React from "react";
import { Trash2 } from "lucide-react";
import { toast } from "sonner";
import { foldAway, unfold } from "@/components/delete-undo";

type Photo = { id: string; url: string; alt: string };

export function PhotoTile({ photo, onDeleted }: { photo: Photo; onDeleted: (id: string) => void }) {
  const tile = React.useRef<HTMLElement>(null);

  async function remove() {
    if (!(await foldAway(tile.current, { sound: true }))) return;
    toast("Photo deleted", {
      action: { label: "Undo", onClick: () => unfold(tile.current) },
      onAutoClose: async () => {
        await fetch(`/api/photos/${photo.id}`, { method: "DELETE" });
        onDeleted(photo.id);
      },
    });
  }

  return (
    <figure ref={tile} className="relative aspect-[3/4] overflow-hidden rounded-2xl">
      <img src={photo.url} alt={photo.alt} className="size-full object-cover" />
      <button onClick={remove} aria-label={`Delete ${photo.alt}`} className="absolute top-2 right-2">
        <Trash2 className="size-4" />
      </button>
    </figure>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `foldAway(element, options?)` | `Promise<boolean>` | – | Folds the element away. Resolves true once it has gone (it stays hidden in the page until you remove it), or false if unfold turned it back first. |
| `unfold(element)` | `Promise<boolean>` | – | Plays the fold back, from wherever it is, and shows the element again. Resolves true once it is back. |
| `isFoldedAway(element)` | `boolean` | – | True while the element is folded away and not yet unfolded. |
| `options.sound` | `boolean` | `false` | A soft crease for each fold and a tick as it goes, synthesised with Web Audio (no files). |
| `options.back` | `string` | `card to muted gradient` | The colour of the paper's back, any CSS background. |

## States

- **folding**: The bottom half folds back behind the top (or the right behind the left, whichever side is longer), then again, five folds at most, each about 16% quicker; corners round as it gets smaller. The paper stays centred where the element was. About 2 seconds in all.
- **gone**: The last small square turns edge-on and disappears. foldAway resolves true; the element is still in the page, hidden, until you remove it.
- **unfolding**: unfold plays it back a little quicker, from wherever it is; if foldAway was still running, it resolves false.
- **reduced motion**: The element fades out and back in, in 160ms.

## What it handles

Most deletes just blink out. Animating one usually means a layout-animation library and wrapping your list in it.

- **One call, any element**: foldAway(element) works on whatever you pass it: an image, a card, a row. No wrapper, no list, no provider.
- **Your state stays yours**: It only hides the element and resolves; you remove the item from your state, call your API and show your own toast.
- **Undo from anywhere**: unfold(element) plays it back, even mid-fold, so your toast's Undo button just calls it.
- **Follows the page**: The fold follows the element every frame, so scrolling or a layout change during the fold never leaves it behind.
- **Folds the right way**: It always folds the longer side, so a portrait, a landscape and a long row all end as a small square.

## Replaces

framer-motion exit animation, AnimatePresence, delete animation, confirm dialog

## Where it fits

- **Photo library**: Delete a photo from a camera roll or a gallery and see it folded away.
- **Moodboards**: Remove an image or a card from a board, with your own Undo calling unfold.
- **File manager**: Delete a file tile, then remove it from your state once it has gone.
- **Saved items**: Clear a bookmark, a cart item or a draft without a confirm dialog.

## Notes

- The fold is a copy of the element made when you call foldAway (clipped clones hinged in 3D), drawn in a layer over the page. The element itself only gets visibility: hidden, so the layout does not move until you remove it.
- Only transforms change while it folds; the copy is removed when it has gone or is back.
- Hidden content leaves the accessibility tree; if the delete button had focus, move focus somewhere sensible (the next item, or your toast).
- Under reduced motion the element fades out and in instead.

License: MIT (https://hairlineui.com/license#components)
