---
title: "Comparison"
description: "Two versions of one picture in one frame. Drag the line, swipe, or use the arrow keys to compare before and after."
url: https://gelatinui.com/components/comparison
markdown_url: https://gelatinui.com/components/comparison.md
access: free
install: "npx shadcn@latest add @gelatin-ui/comparison"
registry_item: https://gelatinui.com/r/comparison.json
version: "2026-11-09"
index: https://gelatinui.com/llms.txt
docs_index: https://gelatinui.com/docs/llms.txt
---

# Comparison

> Two versions of one picture in one frame. Drag the line, swipe, or use the arrow keys to compare before and after.

Page: https://gelatinui.com/components/comparison

## When to use it

Best for: Showing two versions of one picture in one frame, with a divider people move.

- Use it when the change is in the same place of both pictures.
- Put a `ComparisonAfter`, a `ComparisonBefore`, and a `ComparisonHandle` inside.
- Use `interaction="handle"` when the pictures hold links or text to select.

## When not to use it

- For many pictures in a row, use a [Carousel](https://gelatinui.com/components/carousel.md).
- To show one picture large, use an [Image](https://gelatinui.com/components/image.md).

## Notes for agents

- `value` is the share of the before, 0 to 100; `orientation="vertical"` puts the before on top.
- `mode` is `split` (default), `single` (hold to compare), or `side-by-side`.
- `name` sends the share with a native form.

## Anatomy

```text
Comparison
├─ ComparisonBefore (0..n)
├─ ComparisonAfter (0..n)
├─ ComparisonHandle (0..n)
├─ ComparisonLabel (0..n)
└─ ComparisonCaption (0..n)
```

## API reference

### Comparison

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `number` |  | The share of the before, 0–100 (controlled). |
| `defaultValue` | `number` | 50 | The share of the before at the start, 0–100 (uncontrolled). Default 50. |
| `onValueChange` | `function` |  | Runs on every change, with the new share of the before and why it changed: the Base UI Slider reasons (`keyboard`, `input-change`), plus the ones of the pointer layer of this component: `track-press` (a mouse press on the frame), `drag`, `swipe` (a finger across the frame), `tap` (a finger tap on the frame), and `hover`. |
| `onValueCommitted` | `function` |  | Runs when a change ends: a key press, a pointer release, a tap. The same reasons; hover never commits (it has no end). The details are `{ reason, event }` only: a change cannot be canceled from here (make the Comparison controlled to keep a value). |
| `orientation` | `"horizontal" \| "vertical"` | "horizontal" | `horizontal` (default): a vertical divider, the before on the start side. `vertical`: the before on top. |
| `interaction` | `"drag" \| "handle" \| "hover"` | "drag" | How a pointer moves the divider. Keys and touch work the same in every mode. - `drag` (default): a press anywhere on the frame moves the divider to the pointer, and the drag goes on. - `handle`: only the handle (the line and the knob) takes a press, so the pictures can hold links, a video with controls, or text to select. - `hover`: as `drag`, and a mouse over the frame moves the divider with no press. |
| `mode` | `ComparisonMode` | "split" | How the two versions show (the "Before and After" upgrade of the AI category). - `split` (default): one frame cut by a divider (the handle). - `single`: one version at a time, in one frame. `value` 100 shows the before and 0 the after; a change wipes across on the `panel` clock. A press and hold on the frame shows the other version until the release (the "hold to compare" of image editors). The handle does not show: put a switch (Tabs "Before" and "After") outside the frame as the keyboard path. - `side-by-side`: two frames next to each other (one above the other when `vertical`), 8px apart, each at `ratio`. No divider, no pointer, no keys; the value does nothing. |
| `ratio` | `number` |  | The ratio of one picture (width over height, `16 / 9`). Split and single: the frame takes it (an `aspect-*` class in `className` also works and wins). Side by side: each frame takes it (default 16 / 9), because the root holds two frames and has no ratio of its own. |
| `className` | `string` |  |  |
| `style` | `CSSProperties` |  |  |

Also takes every prop of Base UI `Slider.Root`: https://base-ui.com/react/components/slider

### ComparisonBefore

No props of its own: `className`, `children`, and every other prop go to the element it renders.

### ComparisonAfter

No props of its own: `className`, `children`, and every other prop go to the element it renders.

### ComparisonHandle

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  |  |
| `children` | `ReactNode` |  | The knob content. Default: `ChevronsLeftRight`, or `ChevronsUpDown` when vertical. |
| `getAriaValueText` | `function` | defaultValueText | The value text for screen readers, from the share of the before. Default "40% before, 60% after". |

Also takes every prop of Base UI `Slider.Thumb`: https://base-ui.com/react/components/slider

### ComparisonLabel

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `placement` | `"bottom" \| "top"` | "top" | `top` (default) or `bottom`, for a horizontal Comparison. The before label takes the start corner, the after label the end corner. Vertical: the before label is at the top start and the after label at the bottom start, whatever this says. |

### ComparisonCaption

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `mode` | `ComparisonMode` | "split" | The mode of the Comparison under it, so each name sits over its version: `split` (default) puts the first name at the start and the last at the end; `side-by-side` puts each over its frame (two columns, 8px apart); `single` keeps the names together at the start. |

## Styling

| Part | Slot | States | CSS variables | Tokens |
| --- | --- | --- | --- | --- |
| Comparison | `comparison` |  | `--comparison-position`, `--comparison-ratio` |  |
| ComparisonBefore | `comparison-before` | `data-mode`, `data-orientation` | `--comparison-position`, `--comparison-ratio`, `--foreground` |  |
| ComparisonAfter | `comparison-after` | `data-mode` | `--comparison-ratio`, `--foreground` |  |
| ComparisonHandle | `comparison-handle` | `data-disabled`, `data-dragging`, `data-orientation`, `data-stage-focused` | `--comparison-knob-room`, `--comparison-position`, `--comparison-sign` |  |
| ComparisonLabel | `comparison-label` | `data-mode`, `data-orientation`, `data-side` | `--comparison-label-room`, `--comparison-position`, `--comparison-sign`, `--radius` |  |
| ComparisonCaption | `comparison-caption` |  |  |  |

## Usage

Put the edited image in `ComparisonAfter`, the original in `ComparisonBefore`, and add the handle that people drag.

The sample also uses `image`, which this add does not write. One command adds them with this item:

```bash
npx shadcn@latest add @gelatin-ui/comparison @gelatin-ui/image
```

```tsx title="color-grade.tsx"
import {
  Comparison,
  ComparisonAfter,
  ComparisonBefore,
  ComparisonHandle,
  ComparisonLabel,
} from "@/components/media/comparison";
import { Image } from "@/components/ui/image";

export function ColorGrade() {
  return (
    <Comparison defaultValue={40}>
      <ComparisonAfter>
        <Image fill src="/photos/lake-bohinj-graded.webp" alt="A boat on Lake Bohinj, graded" />
        <ComparisonLabel>Graded</ComparisonLabel>
      </ComparisonAfter>
      <ComparisonBefore>
        <Image fill src="/photos/lake-bohinj-raw.webp" alt="A boat on Lake Bohinj, raw file" />
        <ComparisonLabel>Raw</ComparisonLabel>
      </ComparisonBefore>
      <ComparisonHandle aria-label="Raw and graded" />
    </Comparison>
  );
}
```

## Compose

Rules:

- Compose the parts (`compose-parts`): Build each item from its named parts as children, and pass your text and icons in as parts.
- Every state is designed (`every-state`): Give each view of data its loading, empty, and error states: a skeleton in the final layout, an `Empty` with an illustration and one next action, and an error that says what failed and how to retry.

## Add it

```bash
npx shadcn@latest add @gelatin-ui/comparison
```

It adds 13 registry items: 12 files, 2446 lines, and the npm packages `@base-ui/react`, `@mantine/hooks`, `@phosphor-icons/react`, `@xstate/react`, `clsx`, `tailwind-merge`, `tailwind-variants`, `xstate`.

## For AI agents

- Every item, one file: https://gelatinui.com/llms.txt
- MCP server (`search_items`, `get_item`, `get_install_plan`): https://gelatinui.com/mcp

## Related

- [Image](https://gelatinui.com/components/image.md)
- [Slider](https://gelatinui.com/components/slider.md)
- [Carousel](https://gelatinui.com/components/carousel.md)

## Keyboard

| Keys | Action |
| --- | --- |
| Arrow keys | Moves the divider one step. |
| Home, End | Moves the divider to the start or the end. |

## Motion

| Part | Motion |
| --- | --- |
| Divider | panel · after a press on the frame the divider glides to the pointer |
