Compare slider
Before and after, side by side and labelled; works without JavaScript.
Preview
The panels overlap and a divider reveals one or the other. Drag anywhere or use the keyboard. A flick carries on a little and settles with a spring, and the divider nudges once when it scrolls into view. Dragging takes the pointer, so keep links out of the panels. With reduced motion turned on, it renders the plain layout.
Sample productKnow what your users loveSample copy for a made-up product.Start free
Sample productKnow what your users loveSample copy for a made-up product.Start free
Installation
Run this in a project that already uses shadcn/ui. It adds the component and anything it needs; the motion version also adds the plain one and the motion package.
Usage
import { CompareSlider } from "@/components/custom/compare-slider";
<CompareSlider
beforeLabel="Before"
afterLabel="After"
before={<img src={beforeUrl} alt="The old dashboard" />}
after={<img src={afterUrl} alt="The new dashboard" />}
/>import { CompareSliderMotion } from "@/components/custom/compare-slider/compare-slider-motion";
<CompareSliderMotion before={before} after={after} defaultPosition={40} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| before* (required) | ReactNode | – | Left panel. |
| after* (required) | ReactNode | – | Right panel. Give both the same size. |
| beforeLabel | string | "Before" | Chip on the before panel; also used in the accessible names. |
| afterLabel | string | "After" | Chip on the after panel. |
| ...props | React.ComponentProps<"div"> | – | Any other div attribute (id, aria-*, data-*, ref) goes to the root element. |
| Prop | Type | Default | Description |
|---|---|---|---|
| before* (required) | ReactNode | – | Left panel. |
| after* (required) | ReactNode | – | Right panel. Give both the same size. |
| beforeLabel | string | "Before" | Chip on the before panel; also used in the accessible names. |
| afterLabel | string | "After" | Chip on the after panel. |
| ...props | React.ComponentProps<"div"> | – | Any other div attribute (id, aria-*, data-*, ref) goes to the root element. |
| defaultPosition | number | 50 | Where the divider starts, 0 to 100 from the left. |
Accessibility
- Plain shows both panels side by side as labelled figures, so nothing depends on JavaScript or dragging.
- In the motion version the divider is a native range input: arrow keys, Page Up/Down, Home and End move it, and its value is announced as a share of each panel.
- The motion version follows the visitor's reduced-motion setting and never hides content before it animates.