# Image compare

> Two images, one precise divide. Drag to reveal what changed, then let the handle settle.

A before-and-after study that stays registered and never repaints while you drag. The after image sits in a window that slides one way while the picture inside slides back the other, so the reveal, the divider and the handle are all moving layers driven by one coordinate, and React only hears about where you let go. Press anywhere and the divide is thrown to your finger; keyboard adjustments glide into place. Release close to the middle and the divide settles exactly at fifty percent. Edge controls stay reachable even when either image is fully revealed.

## Install

```bash
npx shadcn@latest add @hairline/image-compare
```

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

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/image-compare
- Registry item (source): https://hairlineui.com/r/image-compare.json

## Usage

```tsx
import { ImageCompare } from "@/components/image-compare";

<ImageCompare
  before={{ src: "/images/before.webp", alt: "Room before renovation" }}
  after={{ src: "/images/after.webp", alt: "Room after renovation" }}
  beforeLabel="Before" afterLabel="After"
/>
```

## Keep the comparison position

Use photos taken from the same position, with matching composition. The component uses object-cover for both images.

```tsx
"use client";
import { useState } from "react";
import { ImageCompare } from "@/components/image-compare";

export function Comparison() {
  const [position, setPosition] = useState(50);
  return <ImageCompare value={position} onValueChange={setPosition}
    before={{ src: "/before.webp", alt: "Original photograph" }}
    after={{ src: "/after.webp", alt: "Edited photograph" }}
    onValueCommit={(value) => console.log("Comparison position", value)} />;
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `before / after` | `CompareImage` | – | src, alt and optional className for each image. Supply aligned images for a meaningful comparison. |
| `beforeLabel / afterLabel` | `string` | `Before / After` | Labels on each visible side. |
| `value / defaultValue` | `number` | `50` | Controlled or initial reveal percentage, clamped to 0–100. |
| `onValueChange / onValueCommit` | `(value: number) => void` | – | Continuous position updates and committed releases or keyboard changes. |
| `aspectRatio` | `number` | `1.6` | Width divided by height. Both images occupy this same frame. |
| `label` | `string` | `Image comparison` | Accessible name for the region and slider. |

## States

- **drag**: A press throws the divide to the pointer over 420ms, then it follows exactly. The window, picture, divider and handle move with transforms on their own layers; no React render or repaint per move.
- **settle**: A release within 3% of centre settles at 50%. Keyboard and controlled changes ease over 420ms.
- **ends**: Home and End reveal either image completely. The handle remains inside a 22px inset so it can still be grabbed.
- **cancel**: An interrupted pointer gesture restores the position from before the drag.

## What it handles

Before and after sliders are easy to build and hard to make smooth. This one keeps both images registered and moves only layers while you drag, so it stays smooth.

- **Drag to reveal**: Press anywhere and the divide is thrown to your finger, then follows it exactly with no repaint.
- **It settles on the middle**: Release close to the centre and it settles at exactly 50%. Double-click centres it.
- **Keyboard control**: Arrows move 1%, Shift with arrows moves 10%, and Home and End reveal either side.
- **Edges stay reachable**: The handle stays inside a small inset, so it can still be grabbed when one image is fully shown.
- **Touch scrolling still works**: Vertical touch movement scrolls the page, and an interrupted drag restores the position.
- **Broken images are handled**: A failed image shows a labelled fallback with the frame’s size preserved.

## Replaces

an image comparison library, clip-path drag code, slider semantics, keyboard handling, an image fallback

## Where it fits

- **Photo editing and retouching**: Show the original beside the edited picture.
- **AI image tools**: Upscaling, background removal and restoration results.
- **Redesigns**: Compare an old screen with the new one.
- **Before and after results**: Renovations, landscaping, dental or skincare work.

## Notes

- The handle exposes slider semantics and percentage value. Arrows move 1%; Shift and arrows move 10%; Home and End reveal either side.
- Double-click centres the divide. Pointer cancellation restores the original value, and vertical touch scrolling remains available.
- Reduced motion removes settling transitions. Direct dragging still follows the pointer.
- Image load failures show a labelled fallback with the frame dimensions preserved.

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