Before / After, animated React component
An image comparison slider with a springy handle, fading corner labels, arrow-key control and a segmented Before / Split / After jump.
- Pro
- Motion Effects
- Spring
Props
| Prop | Type | Default | What it does |
|---|---|---|---|
| before | ReactNode | — | What shows on the left of the handle, usually an <img>. |
| after | ReactNode | — | What shows on the right of the handle. |
| beforeLabel | string | "Before" | Top-left label. |
| afterLabel | string | "After" | Top-right label. |
| autoplay | boolean | false | Sweeps the handle every few seconds until touched. |
| className | string | — | Extra classes on the wrapper. |
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 with your API key
Before / After is a Pro item, so the CLI has to send your API key. Do this once per project:
- Create a key at nexus-ui.com/dashboard/api-key. Copy it straight away: it starts with
nxs_and is shown only once. - In your project root, next to
package.json, create a file named.env.localwith this line, using your own key:NEXUS_API_KEY=nxs_your_key_here
- Open
components.jsonand add this entry inside the outermost braces, next to"aliases". Leave{name}and${NEXUS_API_KEY}exactly as written; the CLI fills them in:"registries": { "@nexus-labs": { "url": "https://www.nexus-ui.com/api/registry/{name}", "headers": { "Authorization": "Bearer ${NEXUS_API_KEY}" } } }
- Create a key at nexus-ui.com/dashboard/api-key. Copy it straight away: it starts with
Before / After questions
How do I add my own images to the Before / After slider?
Run `npx shadcn@latest add @nexus-labs/before-after`, then pass `before` and `after` as React nodes, usually an `<img>` with `className="h-full w-full object-cover"`. The frame is a fixed 16:9 box, so `object-cover` keeps differently sized images from distorting. It is a Pro component, so it needs a Pro plan and an API key; key setup is on the CLI docs page (/docs/cli).
How do I change the labels?
Use `beforeLabel` and `afterLabel`, which default to "Before" and "After". Each label fades out as the handle moves close to its side of the frame.
How does the comparison effect work?
The after layer sits underneath and the before layer sits on top, cropped with a CSS `clip-path` driven by a Framer Motion spring. It is CSS plus Framer Motion with no canvas, and you can click or drag anywhere on the image to move the handle.
Does it support keyboard control and reduced motion?
The handle is a focusable `role="slider"`, and the Left and Right arrow keys move it 5% at a time. The Before, Split and After buttons jump to 100%, 50% and 0%. The component does not read prefers-reduced-motion, so leave `autoplay` off (the default) if you need that.