# Drain away

> One call for a move: the element’s edges let go first and everything spirals into its own centre, as if it went somewhere.

For the removal that isn’t a delete. When something is archived, moved to a folder or sent, it hasn’t been destroyed, it has gone somewhere, and sand or embers would say the wrong thing. Call drainAway on it: the edges let go first and every grain spirals in towards the middle, shrinking as it turns, until the picture is a point and then nothing. The promise resolves and you remove it from the list it left. undoAway spins it back out from the centre.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/drain-away.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/drain-away
- Registry item (source): https://hairlineui.com/r/drain-away.json

## Usage

```tsx
import { drainAway } from "@/components/drain-away";

await drainAway(cardRef.current);
await archive(card.id);
setCards((list) => list.filter((c) => c.id !== card.id));
```

## Archive with Undo

Drain the card away, archive it on your server, and let Undo spin it back out. This uses sonner, but any toast works.

```tsx
"use client";
import * as React from "react";
import { toast } from "sonner";
import { drainAway, undoAway } from "@/components/drain-away";

export function ArchiveButton({ target, id, onArchived }: { target: React.RefObject<HTMLElement | null>; id: string; onArchived: (id: string) => void }) {
  return (
    <button
      onClick={async () => {
        if (!(await drainAway(target.current))) return;
        await fetch(`/api/cards/${id}/archive`, { method: "POST" });
        toast("Archived", {
          action: {
            label: "Undo",
            onClick: async () => {
              await fetch(`/api/cards/${id}/unarchive`, { method: "POST" });
              undoAway(target.current);
            },
          },
          onAutoClose: () => onArchived(id),
        });
      }}
    >
      Archive
    </button>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `drainAway(element, options?)` | `Promise<boolean>` | – | Drains the element away. Resolves true once it has gone (it stays hidden in place until you remove it), or false if undoAway brought it back first. |
| `undoAway(element)` | `Promise<boolean>` | – | Spins it back out and shows the element again. Resolves true once it is back. |
| `isAway(element)` | `boolean` | – | True while the element is removed or being removed and not brought back. |
| `options.duration` | `number` | `1000 (600 on rows)` | Milliseconds for the whole removal. |

## States

- **draining**: The edge lets go first over the first 58% of the run; each grain is pulled to the centre as the square of its life, turning up to 3.6 radians and shrinking to a quarter. About 1s on a picture, 0.6s on a row.
- **gone**: The last grain reaches the centre and fades. drainAway resolves true; the element stays hidden in place until you remove it.
- **undoing**: undoAway plays it back 30% quicker: the grains spin out from the centre into place.
- **reduced motion**: The element fades out and back in, in 160ms.

## What it handles

Archiving usually looks like deleting, which makes people wonder whether they just lost something.

- **Says where it went**: A spiral into the centre reads as moved, not destroyed, so archive and delete look different.
- **One call, any element**: drainAway(element) works on an image, a card or a row; your state and API stay yours.
- **Edges first**: The outside lets go first and the middle last, so the picture shrinks into itself.
- **Undo spins it out**: undoAway plays it back from the centre, from wherever it is.
- **Real colours**: Pictures are read pixel by pixel, so the spiral is the picture itself.
- **Pictures from anywhere**: Without CORS the element closes behind a shrinking round mask and the grains take its own colours.

## Replaces

vortex animation, genie effect, archive animation, framer-motion exit animation

## Where it fits

- **Inbox and feeds**: Archive a message, a card or a notification.
- **File manager**: Move an image into a folder and see it go in.
- **AI image history**: Send a generation to a collection or a project.
- **Task boards**: Mark a card done and drain it from the board.

## Notes

- The spiral is worked out per grain from where it sat, so wide rows drain into a flat point and squares into a round one.
- Pictures are drawn from their pixels in a canvas over the page while the element is hidden in place; serve pictures from other origins with CORS and crossOrigin="anonymous" for their real colours.
- Announce the move in your own status line; the element leaves the accessibility tree once hidden.
- Under reduced motion the element fades out and in instead.

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