Image Compare
Before and after slider split by a lit tube. Drag, press, keys or hover to scrub; horizontal or vertical.
Installation
npx shadcn@latest add https://neonkit.heyaadi.com/r/image-compare.jsonUsage
import { ImageCompare } from "@/components/ui/image-compare"
<ImageCompare
className="aspect-[3/2]"
beforeLabel="Raw"
afterLabel="Graded"
before={<img src="/raw.jpg" alt="" />}
after={<img src="/graded.jpg" alt="Shibuya at night" />}
/>Examples
Vertical
Any children work, here a grain overlay drawn in SVG. Labels fade as the divider covers them.
Scrub on hover
With hover, a mouse moves the divider without pressing. Touch still drags.
Controlled
value and onValueChange, with presets driving it from outside.
API reference
Give the root a size or an aspect ratio, or let the after layer set it. Images inside fill and cover their layer.
| Prop | Type | Default |
|---|---|---|
before | ReactNodeShown on the left (or top), up to the divider. | None |
after | ReactNodeShown on the right (or bottom). Its alt text is the one read; leave the before image's alt empty. | None |
beforeLabel | ReactNodeCaption on the before side. Strings also name the slider. | None |
afterLabel | ReactNodeCaption on the after side. | None |
value | numberControlled divider position, 0 to 100. | None |
defaultValue | numberInitial position when uncontrolled. | 50 |
onValueChange | (value: number) => voidCalled on every move. | None |
onValueCommit | (value: number) => voidCalled once a drag or key press ends. | None |
orientation | "horizontal"Vertical splits top from bottom. | "horizontal" |
hover | booleanA mouse moves the divider without pressing. | false |
step | numberPercent per arrow key. Page Up and Page Down move ten steps. | 1 |
disabled | booleanBlocks input and takes the handle out of the tab order. | false |
color | NeonColorPalette name or hex. Falls back to --neon-accent, then cyan. | inherited |
Accessibility
- The handle is a slider with aria-valuetext like "42% Raw, 58% Graded".
- Arrows move by step (Right and Down increase in both orientations), Page Up and Page Down by ten steps, Home and End go to the edges.
- Horizontal compares keep vertical touch scrolling for the page, and vertical ones keep horizontal.
- With reduced motion the divider never glides; it goes straight to the press.