Compare Slider
Drag a divider across two images or panels to compare before and after.
Overview
Compare Slider stacks two versions of the same view and reveals the first up to a divider. Drag the divider, press anywhere to glide it there, or focus the handle and use the keyboard. Use it for photo edits, redesigns, light and dark themes, or any change that is easier to see than to describe.
before sits on the start side of the divider, left or top, and after on the end side. Both layers fill the slider, so give it a size or an aspect ratio. Images placed directly inside a layer cover it like object-fit: cover.
Installation
Usage
import { CompareSlider } from "@/components/animated/compare-slider"
<CompareSlider
className="aspect-video w-full"
beforeLabel="Original"
afterLabel="Graded"
before={<img src="/photo-raw.jpg" alt="The photo before editing" />}
after={<img src="/photo-graded.jpg" alt="The photo after color grading" />}
/>Any content
Each side can be any element, such as the same card in two themes. With hover, the divider follows a mouse without pressing; touch and pen still drag.
<CompareSlider
hover
label="Theme divider"
beforeLabel="Light"
afterLabel="Dark"
className="h-80 w-full"
before={<InvoiceCard tone="light" />}
after={<InvoiceCard tone="dark" />}
/>Controlled
Pass position and onPositionChange to drive the divider from your own state, for example to link it to a separate control.
const [position, setPosition] = useState(50)
<CompareSlider position={position} onPositionChange={setPosition} before={before} after={after} />Accessibility
The handle is a slider. It is named "Comparison divider" by default (change it with label), and its value is read as how much of each side shows, such as "60% Original, 40% Graded", using the labels when they are strings. Pass getValueText to word it differently.
Both layers stay in the page for assistive technology, so describe each one: give images alt text that says which version they are. For panels that repeat the same text, consider hiding one copy from screen readers and describing the difference in nearby text.
The corner labels are hidden from screen readers because the slider's value already names both sides. On touch screens, the page still scrolls across the slider in the direction it does not split.
With reduced motion enabled, the divider moves straight to a pressed point or key step instead of gliding.