Skip to content

NewMCP server for your coding agent. Set it up ›

Nexus UI
Get Pro

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.

OriginalGraded

Props

PropTypeDefaultWhat it does
beforeImage—Image for the left (before) side.
afterImage—Image for the right (after) side.
beforeAlt"Before"Alt text for beforeImage.
afterAlt"After"Alt text for afterImage.
beforeContent—Any content for the before side; overrides beforeImage.
afterContent—Any content for the after side; overrides afterImage.
beforeLabel—Label chip on the left.
afterLabel—Label chip on the right.
initialPosition50Starting divider position, 0-100.
mode"drag"Drag: press or drag anywhere. Hover: the divider follows the pointer and returns on leave.
onChange—Called with the divider position when the user moves it.
autoplayfalseSweeps the divider back and forth until someone touches it.
className—Extra classes; set the aspect ratio and max width here (defaults to 4:3, 480px).

Add it to your project

  1. 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 creates components.json and the cn() helper in src/lib/utils.ts:

    npx shadcn@latest init -d

    Starting from nothing? The Quick Start walks through the whole thing.

  2. 2

    Connect the registry

    Run this once per project. It writes the Nexus UI entry into components.json for 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.

  1. 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
  2. 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.tsx
    index.ts
  3. 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:

  4. 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.