How it works
The after layer sizes the component; the before layer sits on top and is clipped on the right with clip-path. The divider and the clip both read one CSS variable, so dragging changes a single value and triggers no layout. Pointer Events with pointer capture keep the drag alive outside the component. On touch, a drag starts only after 6px of sideways movement, so vertical swipes still scroll the page. Labels are small ink-glass pills and the handle is a white knob with a soft glass ring. The demo is a photo-restore card: the comparison sits between a title row and the file with an ink Save button.
Use it for
- Showing the effect of photo restoration, enhancement or filters.
- Comparing an old and a new design of a page.
- Before and after results for renovation, repair or cleaning.
Look elsewhere when
- The two images differ in framing: the divider will not line up, so show them side by side.
- More than two versions: use tabs or a grid.
- Pixel-level analysis: use a difference overlay.
Usage
import { ImageCompare } from './ImageCompare.jsx'
export function RestorePreview({ rawUrl, fixedUrl }) {
return (
<ImageCompare
style={{ width: 480, aspectRatio: '16 / 10' }}
before={<img src={rawUrl} alt="Original photo" />}
after={<img src={fixedUrl} alt="Restored photo" />}
beforeLabel="Before"
afterLabel="After"
aria-label="Comparison position"
/>
)
}Props
| Prop | Type | Default | What it does |
|---|---|---|---|
before | ReactNode | — | Content shown left of the divider (the original): image, SVG, video or any node. |
after | ReactNode | — | Content shown right of the divider (the result). It sizes the component in the document flow. |
value | number | — | Controlled position from 0 to 100, as a percentage from the left edge. |
defaultValue | number | 50 | Initial position when uncontrolled. |
onChange | (value: number) => void | — | Called when the position changes, continuously while dragging. |
beforeLabel | ReactNode | 'Before' | Label in the top-left corner. |
afterLabel | ReactNode | 'After' | Label in the top-right corner. |
showLabels | boolean | true | Whether to show the labels. |
step | number | 1 | Percentage moved per arrow key press; multiplied by 10 with Shift. |
hint | boolean | true | The first time the slider is half in view, the divider glides 12% toward the larger side and back to show it can be dragged. Only the picture moves; value and onChange are untouched, and any press or key cancels it. |
aria-label | string | 'Comparison position' | Accessible name of the handle (the slider). |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root element; use style for width and aspect-ratio. |
Accessibility
- The handle is a role="slider" with aria-valuenow, aria-valuemin and aria-valuemax; aria-valuetext reads like “Before 50%, After 50%”.
- Arrow keys move 1% (10% with Shift), PageUp and PageDown 10%, Home and End go to the ends.
- Pressing anywhere on the component focuses the handle so arrow keys can fine-tune; a focus ring is shown.
- The handle has a 48px touch target, and vertical swipes on the image still scroll the page.
- With reduced motion there is no first-view hint, and clicks jump straight to the position without gliding.
Motion
First view: the divider glides 12% out and back (720ms each way, ease-in-out) to hint that it can be dragged. Dragging follows the pointer with no transition. Click, Home and End glide over 360ms ease-out; the handle scales to 1.06 on hover and 1.1 while dragging (160ms).
Add it to your project
Pro members copy a prompt and their coding agent installs the source, the same way as the free components.
ImageCompare.jsximage-compare.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.





