# Compare Slider

> Drag a divider across two images or panels to compare before and after.

Source: https://www.honestui.com/docs/animated/compare-slider

```tsx
"use client";

import { CompareSlider } from "@/registry/default/animated/compare-slider";

const PHOTO =
  "https://images.unsplash.com/photo-1553933420-77617bac448e?auto=format&fit=crop&w=1600&q=85";

export function CompareSliderDemo() {
  return (
    <div className="flex w-full max-w-2xl flex-col gap-3">
      <CompareSlider
        className="aspect-[16/10] w-full"
        beforeLabel="Original"
        afterLabel="Graded"
        defaultPosition={42}
        before={
          <img
            src={PHOTO}
            alt="Apartment blocks painted teal and orange, flat and unedited"
            draggable={false}
            className="[filter:saturate(0.25)_contrast(0.82)_brightness(1.08)]"
          />
        }
        after={
          <img
            src={PHOTO}
            alt="The same buildings after color grading, with richer teal and orange"
            draggable={false}
            className="[filter:saturate(1.35)_contrast(1.08)]"
          />
        }
      />
      <p className="text-center text-sm text-muted-foreground">
        Drag the divider, click anywhere on the photo, or focus the handle and use the arrow keys.
      </p>
    </div>
  );
}

```

## Overview [#overview]

Compare Slider stacks two versions of the same view and reveals the first up to a divider. Drag the divider, press anywhere to glide it there, or focus the handle and use the keyboard. Use it for photo edits, redesigns, light and dark themes, or any change that is easier to see than to describe.

`before` sits on the start side of the divider, left or top, and `after` on the end side. Both layers fill the slider, so give it a size or an aspect ratio. Images placed directly inside a layer cover it like `object-fit: cover`.

## Installation [#installation]

<CliBlock commands="[&#x22;compare-slider&#x22;]" />

## Usage [#usage]

```tsx
import { CompareSlider } from "@/components/animated/compare-slider"

<CompareSlider
  className="aspect-video w-full"
  beforeLabel="Original"
  afterLabel="Graded"
  before={<img src="/photo-raw.jpg" alt="The photo before editing" />}
  after={<img src="/photo-graded.jpg" alt="The photo after color grading" />}
/>
```

### Any content [#any-content]

Each side can be any element, such as the same card in two themes. With `hover`, the divider follows a mouse without pressing; touch and pen still drag.

```tsx
"use client";

import type { ReactNode } from "react";
import { CompareSlider } from "@/registry/default/animated/compare-slider";

function InvoiceCard({ tone }: { tone: "light" | "dark" }) {
  const dark = tone === "dark";
  const rows: [string, string][] = [
    ["Design system", "$4,800"],
    ["Usability study", "$2,150"],
    ["Prototype build", "$3,400"],
  ];
  return (
    <div
      className={
        dark
          ? "flex size-full flex-col gap-4 bg-neutral-950 p-6 pt-14 text-neutral-100"
          : "flex size-full flex-col gap-4 bg-white p-6 pt-14 text-neutral-900"
      }
    >
      <div className="flex items-baseline justify-between">
        <p className="text-sm font-semibold tracking-tight">Invoice 1042</p>
        <span
          className={
            dark
              ? "rounded-full bg-emerald-400/15 px-2 py-0.5 text-xs font-medium text-emerald-300"
              : "rounded-full bg-emerald-600/10 px-2 py-0.5 text-xs font-medium text-emerald-700"
          }
        >
          Paid
        </span>
      </div>
      <dl className="grid gap-2 text-sm">
        {rows.map(([name, amount]) => (
          <div
            key={name}
            className={
              dark
                ? "flex justify-between border-b border-white/10 pb-2"
                : "flex justify-between border-b border-black/10 pb-2"
            }
          >
            <dt className={dark ? "text-neutral-400" : "text-neutral-500"}>{name}</dt>
            <dd className="tabular-nums">{amount}</dd>
          </div>
        ))}
      </dl>
      <div className="mt-auto flex items-baseline justify-between">
        <span className={dark ? "text-sm text-neutral-400" : "text-sm text-neutral-500"}>Total</span>
        <span className="text-xl font-semibold tabular-nums">$10,350</span>
      </div>
    </div>
  );
}

function Frame({ children }: { children: ReactNode }) {
  return <div className="w-full max-w-sm rounded-xl border border-border shadow-sm">{children}</div>;
}

export function CompareSliderInterface() {
  return (
    <Frame>
      <CompareSlider
        hover
        label="Theme divider"
        beforeLabel="Light"
        afterLabel="Dark"
        className="h-80 w-full"
        before={<InvoiceCard tone="light" />}
        after={<InvoiceCard tone="dark" />}
      />
    </Frame>
  );
}

```

```tsx
<CompareSlider
  hover
  label="Theme divider"
  beforeLabel="Light"
  afterLabel="Dark"
  className="h-80 w-full"
  before={<InvoiceCard tone="light" />}
  after={<InvoiceCard tone="dark" />}
/>
```

### Controlled [#controlled]

Pass `position` and `onPositionChange` to drive the divider from your own state, for example to link it to a separate control.

```tsx
const [position, setPosition] = useState(50)

<CompareSlider position={position} onPositionChange={setPosition} before={before} after={after} />
```

## Accessibility [#accessibility]

The handle is a slider. It is named "Comparison divider" by default (change it with `label`), and its value is read as how much of each side shows, such as "60% Original, 40% Graded", using the labels when they are strings. Pass `getValueText` to word it differently.

| Key                                    | Action                                                                                        |
| -------------------------------------- | --------------------------------------------------------------------------------------------- |
| Arrow keys                             | Move the divider 1%. Right and Up move toward the end side; when vertical, Down and Right do. |
| Shift + Arrow keys, Page Up, Page Down | Move the divider 10%.                                                                         |
| Home, End                              | Show only `after`, or only `before`.                                                          |

Both layers stay in the page for assistive technology, so describe each one: give images alt text that says which version they are. For panels that repeat the same text, consider hiding one copy from screen readers and describing the difference in nearby text.

The corner labels are hidden from screen readers because the slider's value already names both sides. On touch screens, the page still scrolls across the slider in the direction it does not split.

With reduced motion enabled, the divider moves straight to a pressed point or key step instead of gliding.

## API Reference [#api-reference]


  ### `before`

type: `ReactNode`

Content on the start side: left, or top when vertical.

  ### `after`

type: `ReactNode`

Content on the end side: right, or bottom when vertical.

  ### `position`

type: `number`

Divider position from 0 to 100, measured from the start side. Makes the slider controlled.

  ### `defaultPosition`

type: `number` · default: `50`

Starting position when uncontrolled.

  ### `onPositionChange`



void">
    Called with the new position while the divider moves.

  ### `orientation`

type: `&#x22;horizontal&#x22; | &#x22;vertical&#x22;` · default: `&#x22;horizontal&#x22;`

Split side by side or top and bottom.

  ### `beforeLabel`

type: `ReactNode`

Chip in the start corner. Fades out as the divider covers it.

  ### `afterLabel`

type: `ReactNode`

Chip in the end corner.

  ### `hover`

type: `boolean` · default: `false`

Follows a mouse without pressing. Touch and pen still drag.

  ### `label`

type: `string` · default: `&#x22;Comparison divider&#x22;`

Accessible name of the handle.

  ### `getValueText`



string">
    Text read for the handle's value.

  ### `disabled`

type: `boolean` · default: `false`

Stops dragging and key presses.

  ### `className`

type: `string`

Class for the root. Set its size here.

  ### `handleClassName`

type: `string`

Class for the round handle.
