Components / Layout
Comparison Slider
Drag a divider to compare before and after. Works with mouse, touch and arrow keys.
Installation
Requires React 18+ and Tailwind CSS.
- 1
Install dependencies
$ npm install motion - 2
Add the component
Copy the code from the Code tab above into
components/ui/comparison-slider.tsx. - 3
Use it
example.tsximport { ComparisonSlider } from "@/components/ui/comparison-slider"; function Mock({ colored }: { colored: boolean }) { const block = colored ? "" : "bg-zinc-300 dark:bg-zinc-700"; return ( <div className={`flex size-full flex-col gap-3 p-6 ${colored ? "bg-gradient-to-br from-orange-100 to-rose-100 dark:from-orange-950 dark:to-rose-950" : "bg-zinc-100 dark:bg-zinc-900"}`}> <div className={`h-4 w-1/3 rounded ${colored ? "bg-zinc-900 dark:bg-white" : block}`} /> <div className={`h-3 w-2/3 rounded ${colored ? "bg-zinc-500" : block}`} /> <div className="mt-2 grid flex-1 grid-cols-3 gap-3"> {["from-orange-400 to-rose-500", "from-sky-400 to-indigo-500", "from-lime-400 to-emerald-500"].map((g) => ( <div key={g} className={`rounded-xl ${colored ? `bg-gradient-to-br ${g}` : block}`} /> ))} </div> </div> ); } export default function ComparisonSliderDemo() { return ( <ComparisonSlider className="aspect-video w-full max-w-md" before={<Mock colored={false} />} after={<Mock colored />} beforeLabel="Wireframe" afterLabel="Final" /> ); }
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| before | ReactNode | — | Left side content. |
| after | ReactNode | — | Right side content. |
| defaultPosition | number | 50 | Starting divider position (0–100). |
| beforeLabel / afterLabel | string | "Before" / "After" | Corner labels. |
| className | string | "aspect-video w-full" | Size. |