Skip to content

NewMCP server for your coding agent. Set it up ›

Nexus UI
Get Pro

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

PropTypeDefaultWhat it does
before—What shows on the left of the handle, usually an <img>.
after—What shows on the right of the handle.
beforeLabel"Before"Top-left label.
afterLabel"After"Top-right label.
autoplayfalseSweeps the handle every few seconds until touched.
className—Extra classes on the wrapper.

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 with your API key

    Before / After is a Pro item, so the CLI has to send your API key. Do this once per project:

    1. Create a key at nexus-ui.com/dashboard/api-key. Copy it straight away: it starts with nxs_ and is shown only once.
    2. In your project root, next to package.json, create a file named .env.local with this line, using your own key:
      NEXUS_API_KEY=nxs_your_key_here
    3. Open components.json and 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}"
          }
        }
      }

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.