# Shatter away

> One call to discard: the element cracks into shards that kick outward and drop, bottom first.

For throwing something out rather than deleting it. Call shatterAway on the take, draft or card being rejected: it cracks along a hidden grid into shards of a few dozen grains each, the bottom ones go first, and every shard is kicked outward and then pulled down as one piece, so you see pieces falling, not dust. It suits the moment you reject an option (the generations you didn’t keep, a draft you scrap) where sand would feel too gentle. The promise resolves and you remove it from your state; undoAway puts the shards back.

## Install

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

## Usage

```tsx
import { shatterAway } from "@/components/shatter-away";

await shatterAway(cardRef.current);
setDrafts((list) => list.filter((d) => d.id !== draft.id));
```

## Rejecting the takes you didn’t keep

Keep one generation and break up the rest at once, then save the choice.

```tsx
"use client";
import * as React from "react";
import { shatterAway } from "@/components/shatter-away";

export function Takes({ images, onKeep }: { images: { id: string; url: string }[]; onKeep: (id: string) => Promise<void> }) {
  const tiles = React.useRef(new Map<string, HTMLElement>());
  async function keep(id: string) {
    await Promise.all(images.filter((i) => i.id !== id).map((i) => shatterAway(tiles.current.get(i.id))));
    await onKeep(id);
  }
  return (
    <div className="grid grid-cols-2 gap-2">
      {images.map((i) => (
        <button key={i.id} ref={(el) => { if (el) tiles.current.set(i.id, el); }} onClick={() => keep(i.id)}>
          <img src={i.url} alt="" crossOrigin="anonymous" className="aspect-square rounded-xl object-cover" />
        </button>
      ))}
    </div>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `shatterAway(element, options?)` | `Promise<boolean>` | – | Shatters the element. 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>` | – | Puts the shards back 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` | `1050 (630 on rows)` | Milliseconds for the whole removal. |

## States

- **shattering**: Shards (a 7 × 9 grid, broken up by noise) let go bottom first over the first 58% of the run. Each is kicked up to 32px outward and 12px up, then falls 90 to 170px, fading late. About 1.05s on a picture, 0.6s on a row.
- **gone**: The last shard fades. shatterAway resolves true; the element stays hidden in place until you remove it.
- **undoing**: undoAway plays it back 30% quicker: the shards rise and lock together.
- **reduced motion**: The element fades out and back in, in 160ms.

## What it handles

A reject usually looks like a delete, so it carries no sense of the choice you just made.

- **Pieces, not dust**: Grains in a shard share their seeds, so each shard moves as one piece with a hairline crack around it.
- **One call, any element**: shatterAway(element) works on an image, a card or a row; your state and API stay yours.
- **Bottom first**: The lower shards drop first, so the break reads as falling, not exploding.
- **Undo puts it back**: undoAway plays it back: the shards rise and lock into place.
- **Real colours**: Pictures are read pixel by pixel, so the shards are the picture itself.
- **Pictures from anywhere**: Without CORS the element is masked in place and the shards take its own colours.

## Replaces

shatter effect css, glass break animation, framer-motion exit animation, discard animation

## Where it fits

- **Image variations**: Reject the takes you didn’t pick from a batch of generations.
- **Drafts**: Scrap a draft, a version or a suggestion you won’t use.
- **Review queues**: Reject a submission or a flagged item and see it break up.
- **Games and rewards**: Break a card, a crate or a badge open in a product moment.

## Notes

- Shards are groups of grains on one grid that share their motion and meet edge to edge, so each is a solid piece; the cracks open as neighbouring shards part.
- 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.
- Move focus somewhere sensible after a discard; 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)
