Compare Slider

Before/after comparison with a draggable divider

Two layers stay perfectly registered while a draggable divider clips between them on a soft spring. Works with images or any React content — drag anywhere, or move the handle with arrow keys.

Preview

RR
RR
WireframeFinal

Installation

Install the component using the CLI.

npx shadcn@latest add https://rohan.run/r/compare-slider.json

Usage

Import the component:

import { CompareSlider } from "@/components/rohan/compare-slider"

Use it in your code:

<CompareSlider
  before={<img src="/photo-raw.jpg" alt="Unedited" />}
  after={<img src="/photo-edited.jpg" alt="Edited" />}
  beforeLabel="Raw"
  afterLabel="Edited"
/>

Props

PropertyTypeDefaultDescription
beforeReactNodeLayer revealed on the left of the divider.
afterReactNodeLayer revealed on the right of the divider.
beforeLabel / afterLabelstring"Before" / "After"Corner labels that fade in on hover.
defaultPositionnumber50Initial divider position, 0–100.
classNamestringOptional CSS classes for the container.
Components