GooseUI

Components

Compare Slider

New

Two layers stacked on top of each other: the top one is clipped with clip-path by the --compare-slider-pos variable. An invisible native <input type="range"> covers the whole frame, so dragging from any point, touch and the keyboard work out of the box. The markup is rendered on the server; the only client part is a small island that writes the range value into the variable. A registered custom property lets the divider glide after the pointer with a CSS transition. Without JavaScript the frame shows the comparison at defaultPosition. Both slots accept any ReactNode: images, SVG, video or regular markup.

Browser Support

Registered custom properties

Animates registered CSS properties

Your browser:
Vertical form controls

Displays a vertical range control

Your browser:
:has()

Styles elements based on their contents

Your browser:
color-mix()

Colors the handle focus ring

Your browser:

Preview

Drag anywhere in the frame or press Tab and use the arrow keys.

BeforeAfter

Installation

npx shadcn@latest add @gooseui/compare-slider
pnpm dlx shadcn@latest add @gooseui/compare-slider
yarn dlx shadcn@latest add @gooseui/compare-slider
bunx --bun shadcn@latest add @gooseui/compare-slider

Usage

import { CompareSlider } from "@/components/ui/compare-slider"

export default function Example() {
  return (
    <CompareSlider
      before={<img src="/photo-before.jpg" alt="Before retouch" />}
      after={<img src="/photo-after.jpg" alt="After retouch" />}
      beforeLabel="Before"
      afterLabel="After"
    />
  )
}

Examples

Transparent images

checkerboard draws a checker pattern under both layers, so transparent areas of a cutout are visible.

OriginalCutout
<CompareSlider
  className="aspect-[4/3]"
  checkerboard
  defaultPosition={60}
  before={<img src="/product.jpg" alt="Original" />}
  after={<img src="/product-cutout.png" alt="Background removed" />}
  beforeLabel="Original"
  afterLabel="Cutout"
/>

Vertical, with markup

orientation="vertical" moves the divider up and down. Here both layers are plain components instead of images.

Team plan

$24per month

  • Unlimited projects
  • Shared workspaces
  • Priority support
Start trial

Team plan

$24

per month

  • Unlimited projects
  • Shared workspaces
  • Priority support
Start trial
Old layoutNew layout
<CompareSlider
  orientation="vertical"
  className="aspect-[3/4] max-w-xs"
  before={<PricingCard variant="old" />}
  after={<PricingCard variant="new" />}
  beforeLabel="Old layout"
  afterLabel="New layout"
/>

Controlled position

Pass position and onPositionChange to drive the divider from outside.

Position: 30%
const [position, setPosition] = useState(30)

<CompareSlider
  position={position}
  onPositionChange={setPosition}
  before={<img src="/before.jpg" alt="" />}
  after={<img src="/after.jpg" alt="" />}
/>
<Button onClick={() => setPosition(50)}>50%</Button>

Props

PropTypeDefaultDescription
beforeReactNode-Layer on the start side of the divider (left or top)
afterReactNode-Layer on the end side of the divider (right or bottom)
defaultPositionnumber50Initial divider position in percent
positionnumber-Divider position in percent for controlled mode
onPositionChange(position: number) => void-Called with the new position while dragging or on keys
orientation"horizontal" | "vertical""horizontal"Direction of the divider
checkerboardbooleanfalseChecker pattern under the layers for transparent images
beforeLabelReactNode-Badge over the before layer
afterLabelReactNode-Badge over the after layer
stepnumber5Arrow key step in percent
sliderLabelstring"Comparison divider"Accessible name of the range input
instantbooleanfalseFollow the pointer without the smoothing transition
namestring-Form field name: the position is submitted with the form
classNamestring-Additional CSS classes; set the aspect ratio here

Features

  • Native range: a click anywhere in the frame jumps there, the drag goes on outside the frame
  • Smooth handle: a 160 ms CSS transition on @property; reduced motion and instant turn it off
  • Keyboard: arrows move by step, Page Up/Down by two steps, Home/End jump to the edges
  • Forms: with name the position goes to FormData
  • Touch friendly: the page still scrolls across the drag axis on phones