Before / After Compare, animated React component
A before and after comparison for images or any two pieces of content. Press or drag anywhere and the divider springs to your finger with a little follow-through, the handle stretches in the direction it travels and each label fades as the divider reaches it; arrow keys move it too.
- Free
- Motion Effects
- Hover
- Spring
Photo
Any content
Like Before / After Compare? 256 more are in Pro.
Pro unlocks all 256 Pro components, every template and block, and everything we release next. Before / After Compare stays free.
Play with it
Move the controls and watch it change. Copy code gives you a snippet with only the props you changed.
Props
| Prop | Type | Default | What it does |
|---|---|---|---|
| beforeImage | string | — | Image for the left (before) side. |
| afterImage | string | — | Image for the right (after) side. |
| beforeAlt | string | "Before" | Alt text for beforeImage. |
| afterAlt | string | "After" | Alt text for afterImage. |
| beforeContent | React.ReactNode | — | Any content for the before side; overrides beforeImage. |
| afterContent | React.ReactNode | — | Any content for the after side; overrides afterImage. |
| beforeLabel | string | — | Label chip on the left. |
| afterLabel | string | — | Label chip on the right. |
| initialPosition | number | 50 | Starting divider position, 0-100. |
| mode | "drag" | "hover" | "drag" | Drag: press or drag anywhere. Hover: the divider follows the pointer and returns on leave. |
| onChange | (position: number) => void | — | Called with the divider position when the user moves it. |
| autoplay | boolean | false | Sweeps the divider back and forth until someone touches it. |
| className | string | — | Extra classes; set the aspect ratio and max width here (defaults to 4:3, 480px). |
Add it to your project
- 1
Set up your project once
You need a Next.js project with Tailwind CSS and the
@/import alias. In its root folder, run this once. It createscomponents.jsonand thecn()helper insrc/lib/utils.ts:npx shadcn@latest init -d
Starting from nothing? The Quick Start walks through the whole thing.
- 2
Connect the registry
Run this once per project. It writes the Nexus UI entry into
components.jsonfor you:npx shadcn@latest registry add "@nexus-labs=https://www.nexus-ui.com/api/registry/{name}"Type it exactly as shown.
{name}is filled in by the CLI with the component you ask for, so you never replace it.
- 3
Add the component
Run this from your project root. It adds the component, the helpers it imports and its npm packages.
npx shadcn@latest add @nexus-labs/compare
- 4
Check what was added
With a project made as in the Quick Start (a
src/folder), these files land in it:components/compare/compare.tsxindex.ts - 5
Copying by hand? Skip the registry
Instead of steps 2 and 3 you can copy the files yourself, with no registry and no API key. Step 1 still applies, so that
cn()exists. Install the packages:npm install framer-motion
Then show the source and create each file at the path it gives:
- 6
Use it in a page
Installing only adds the files. Import the component in a page and render it. The Usage tab on the preview shows a fuller example with props:
// src/app/page.tsx import { Compare } from "@/components/compare"; export default function Page() { return ( <main> <Compare /> </main> ); }
Like Before / After Compare? 256 more are in Pro.
Pro unlocks all 256 Pro components, every template and block, and everything we release next. Before / After Compare stays free.
About Before / After Compare
The Before/After Compare component renders two images with a draggable divider the user controls to reveal either side. Mouse drag, touch, and keyboard input are all supported via Framer Motion gesture recognition. The divider handle has a spring-snapping interaction that stays under the cursor precisely. Use it to demonstrate product transformations — design tool output, image processing, code generation before/after — or any visual comparison.
Good for
- Design tool output demonstrations
- Image processing and AI generation showcases
- Before/after product feature displays
- Visual comparison and A/B test illustrations
Before / After Compare questions
How do I install the compare slider?
Run `npx shadcn@latest add @nexus-labs/compare`. Its only dependency is lucide-react for the handle icon; there is no animation library.
How does the divider move?
On desktop the divider follows the mouse across the image and returns to `initialPosition` (default 50) when the cursor leaves. On touch screens you drag with a finger.
Can I compare something other than two images?
Yes. `beforeContent` and `afterContent` accept any React node and take priority over `beforeImage` and `afterImage`, so you can compare two UI states, code renders or designs.
How do I set the size and labels?
Set the height or aspect ratio through `className` (for example `aspect-video`). `beforeLabel` and `afterLabel` add small pills in the top corners.