Two layers stacked on top of each other: the top one is clipped with clip-path by the --compare-slider-pos variable. An invisible native <input type="range"> covers the whole frame, so dragging from any point, touch and the keyboard work out of the box. The markup is rendered on the server; the only client part is a small island that writes the range value into the variable. A registered custom property lets the divider glide after the pointer with a CSS transition. Without JavaScript the frame shows the comparison at defaultPosition. Both slots accept any ReactNode: images, SVG, video or regular markup.
Browser Support
Animates registered CSS properties
Displays a vertical range control
Styles elements based on their contents
Colors the handle focus ring
Preview
Drag anywhere in the frame or press Tab and use the arrow keys.
Installation
Usage
import { CompareSlider } from "@/components/ui/compare-slider"
export default function Example() {
return (
<CompareSlider
before={<img src="/photo-before.jpg" alt="Before retouch" />}
after={<img src="/photo-after.jpg" alt="After retouch" />}
beforeLabel="Before"
afterLabel="After"
/>
)
}Examples
Transparent images
checkerboard draws a checker pattern under both layers, so transparent areas of a cutout are visible.
<CompareSlider
className="aspect-[4/3]"
checkerboard
defaultPosition={60}
before={<img src="/product.jpg" alt="Original" />}
after={<img src="/product-cutout.png" alt="Background removed" />}
beforeLabel="Original"
afterLabel="Cutout"
/>Vertical, with markup
orientation="vertical" moves the divider up and down. Here both layers are plain components instead of images.
<CompareSlider
orientation="vertical"
className="aspect-[3/4] max-w-xs"
before={<PricingCard variant="old" />}
after={<PricingCard variant="new" />}
beforeLabel="Old layout"
afterLabel="New layout"
/>Controlled position
Pass position and onPositionChange to drive the divider from outside.
const [position, setPosition] = useState(30)
<CompareSlider
position={position}
onPositionChange={setPosition}
before={<img src="/before.jpg" alt="" />}
after={<img src="/after.jpg" alt="" />}
/>
<Button onClick={() => setPosition(50)}>50%</Button>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| before | ReactNode | - | Layer on the start side of the divider (left or top) |
| after | ReactNode | - | Layer on the end side of the divider (right or bottom) |
| defaultPosition | number | 50 | Initial divider position in percent |
| position | number | - | Divider position in percent for controlled mode |
| onPositionChange | (position: number) => void | - | Called with the new position while dragging or on keys |
| orientation | "horizontal" | "vertical" | "horizontal" | Direction of the divider |
| checkerboard | boolean | false | Checker pattern under the layers for transparent images |
| beforeLabel | ReactNode | - | Badge over the before layer |
| afterLabel | ReactNode | - | Badge over the after layer |
| step | number | 5 | Arrow key step in percent |
| sliderLabel | string | "Comparison divider" | Accessible name of the range input |
| instant | boolean | false | Follow the pointer without the smoothing transition |
| name | string | - | Form field name: the position is submitted with the form |
| className | string | - | Additional CSS classes; set the aspect ratio here |
Features
- Native range: a click anywhere in the frame jumps there, the drag goes on outside the frame
- Smooth handle: a 160 ms CSS transition on @property; reduced motion and instant turn it off
- Keyboard: arrows move by step, Page Up/Down by two steps, Home/End jump to the edges
- Forms: with name the position goes to FormData
- Touch friendly: the page still scrolls across the drag axis on phones