Compare
A component for comparing two images side by side with hover, drag, or autoplay modes
Default
Hover or drag compare with optional autoplay
Drag the slider to compare
50% / 50%
default-example.tsx
1"use client";23import { Compare, type CompareProps } from "@/registry/ui";45export function CompareDefaultExample(props: CompareProps) {6 const {7 firstImage = "https://placehold.co/1600x900?text=Before",8 secondImage = "https://placehold.co/1600x900?text=After",9 className = "w-full max-w-5xl h-[450px]",10 firstImageClassName = "rounded-2xl",11 secondImageClassName = "rounded-2xl",12 initialSliderPercentage = 50,13 slideMode = "hover",14 showHandlebar = true,15 autoplay = false,16 autoplayDuration = 5000,17 showPercentage = true,18 showInstructions = true,19 instructionsText = "Drag the slider to compare",20 } = props;2122 return (23 <Compare24 firstImage={firstImage}25 secondImage={secondImage}26 className={className}27 firstImageClassName={firstImageClassName}28 secondImageClassName={secondImageClassName}29 initialSliderPercentage={initialSliderPercentage}30 slideMode={slideMode}31 showHandlebar={showHandlebar}32 autoplay={autoplay}33 autoplayDuration={autoplayDuration}34 showPercentage={showPercentage}35 showInstructions={showInstructions}36 instructionsText={instructionsText}37 />38 );39}
Controls
slideMode
initialSliderPercentage
showHandlebar
showPercentage
showInstructions
autoplay
secondImageClassName
autoplayDuration
Installation & source
Install via the shadcn CLI or copy the registry files manually.
bash
npx shadcn@latest add @tt-ui/compare
Props
| Name | Type | Default | Description |
|---|---|---|---|
| firstImage | string | https://placehold.co/600x400 | The first image to compare |
| secondImage | string | https://placehold.co/600x400 | The second image to compare |
| className | string | w-full max-w-5xl h-[450px] | Tailwind classes for the compare container |
| firstImageClassName | string | rounded-2xl | The class name of the first image |
| secondImageClassName | string | rounded-2xl | The class name of the second image |
| initialSliderPercentage | number | 50 | The initial percentage of the slider |
| slideMode | "hover" | "drag" | hover | The mode of the slider |
| showHandlebar | boolean | true | Whether to show the handlebar |
| autoplay | boolean | false | Whether to autoplay the slider |
| autoplayDuration | number | 5000 | The duration of the autoplay |
| showPercentage | boolean | false | Whether to show the slider percentage below the compare |
| showInstructions | boolean | false | Whether to show instructions below the compare |
| instructionsText | string | Drag the slider to compare | Custom instruction text when showInstructions is true |