Components

Image Compare

A before/after image comparison slider for React and Tailwind CSS: drag the divider with mouse, pen or touch, follow the cursor in hover mode, or step it with the keyboard to reveal one layer over another through a clip-path. Works horizontally or vertically with any two children — photos, SVGs, or whole components — and never re-renders while dragging.

  • Drag, touch & hover
  • Horizontal / vertical
  • Any two layers
  • Slider a11y
  • No re-renders
  • prefers-reduced-motion
Render
Sketch

Installation

$ npx shadcn@latest add https://duskui.com/r/image-compare.json

Usage

photo-edit.tsx
import { ImageCompare } from "@/components/dusk/image-compare/image-compare"

export function PhotoEdit() {
  return (
    <ImageCompare
      className="aspect-video max-w-2xl"
      beforeLabel="Original"
      afterLabel="Edited"
      before={<img src="/original.jpg" alt="Original photo" />}
      after={<img src="/edited.jpg" alt="Color-graded photo" />}
    />
  )
}

Examples

Vertical

orientation="vertical" moves the divider top to bottom. Size the frame with any aspect-ratio class; both layers stretch to fill it.

Dusk
Day

Hover to compare

With mode="hover" the divider follows the mouse without pressing. Touch screens still drag. The layers here are live components, not images.

Monthly revenue

$48,210

DuskUI

Monthly revenue

$48,210

Default

API Reference

Also accepts any div attribute. Give it a size with className (it defaults to aspect-[16/10] w-full).

PropTypeDefaultDescription
beforeReactNode—Layer shown on the left (or top). Stretched to fill; images get object-cover.
afterReactNode—Layer shown on the right (or bottom).
beforeLabel / afterLabelReactNode—Corner badges. Strings also name the layers in the slider's value text.
orientation"horizontal" | "vertical""horizontal"Direction the divider travels.
mode"drag" | "hover""drag"Press-and-drag, or follow the mouse. Touch always drags.
positionnumber—Controlled position in percent: how much of the before layer is visible.
defaultPositionnumber50Starting position when uncontrolled.
onPositionChange(position: number) => void—Called as the divider moves.
stepnumber2Percent per arrow key. Page Up / Page Down move 10.
labelstring"Comparison slider"Accessible name of the slider.

Accessibility

  • The handle is a focusable role="slider" with aria-valuenow, aria-orientation and value text such as “40% before, 60% after”.
  • Arrow keys move the divider by step, Page Up / Page Down by 10%, Home and End jump to either edge.
  • Pressing anywhere on the frame jumps the divider there and focuses the handle, so keyboard and pointer stay in sync.
  • Horizontal sliders keep vertical page scrolling on touch (touch-action: pan-y), vertical ones keep horizontal scrolling.
  • Give images meaningful alt text — both layers stay in the accessibility tree.
  • With prefers-reduced-motion, clicks and keyboard jumps snap instead of gliding.