Skip to content
VenusUI
Get started

Image Compare

A before/after slider with a draggable divider that also works with the keyboard.

Preview

Installation

npx shadcn@latest add @venusui/image-compare

Run this in a project set up with shadcn. If you haven’t set it up yet, run npx shadcn@latest init first.

Files added to your project

Paths use the default shadcn aliases. The CLI follows the aliases in your components.json.

  • @/components/ui/image-compare.tsx

Usage

The code behind the preview above.

image-compare-demo.tsx

import { ImageCompare } from "@/components/ui/image-compare"

function Mock({ dark }: { dark?: boolean }) {
  return (
    <div className={dark ? "size-full bg-zinc-950 p-6 text-zinc-50" : "size-full bg-zinc-50 p-6 text-zinc-950"}>
      <div className="h-3 w-24 rounded bg-current opacity-80" />
      <div className="mt-4 grid grid-cols-3 gap-3">
        {[0, 1, 2, 3, 4, 5].map((i) => (
          <div key={i} className="h-16 rounded-md border border-current/15 bg-current/5" />
        ))}
      </div>
    </div>
  )
}

export default function ImageCompareDemo() {
  return (
    <ImageCompare
      className="max-w-xl"
      beforeLabel="Light"
      afterLabel="Dark"
      before={<Mock />}
      after={<Mock dark />}
    />
  )
}

API reference

PropTypeDefaultDescription
beforeReactNode—Left layer, e.g. an <img>. Required.
afterReactNode—Right layer. Required.
beforeLabelstring"Before"Label shown on the left and used in the slider’s name.
afterLabelstring"After"Label shown on the right.
defaultPositionnumber50Starting divider position, 0–100.
classNamestring—Classes for the frame, e.g. a different aspect ratio.

Only the props above.

Requirements

  • React 19 and Tailwind CSS v4, with a cn helper at @/lib/utils. No other dependencies.

Customization

  • Use two images of the same size with className="size-full object-cover" and meaningful alt text.
  • The frame is 16:9 by default; pass className="aspect-square" for square images.

Accessibility

  • The divider is a native range input with a descriptive label: arrows, Home and End move it, and screen readers announce the position.
  • A focus ring appears on the handle when the slider has keyboard focus.

Troubleshooting

Images look stretched.
Add object-cover and size-full to both images so they fill the frame the same way.
Unknown registry "@venusui"
Your shadcn CLI couldn’t find the namespace in the shadcn registry index. Use the latest CLI (npx shadcn@latest), install by URL, or register it with its address: npx shadcn@latest registry add @venusui=https://venusui.com/r/{name}.json.
No components.json found
Run npx shadcn@latest init in your project first.
Loading preview

Cards arranged in a ring that turns in 3D, with buttons, arrow keys and announcements.

Loading preview

Analog Clock

Data display

A station clock whose needle waits at twelve; drag the minute hand to set it by hand.

Loading preview

A card that tilts in 3D toward the pointer with spring physics.