Compare Slider

Animated

Two aligned layers split by a movable divider.

Source
$npx @dinachi/cli@latest add compare-slider
After
Before
drag
defaultPosition
step
disabled

Usage

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

export function Retouch() {
  return (
    <CompareSlider
      label="Compare the two revisions"
      before={<img src="/before.jpg" alt="Before retouching" />}
      after={<img src="/after.jpg" alt="After retouching" />}
    />
  )
}

Examples

After
Before
Drag the divider, or focus it and use the arrow keys.
import { CompareSlider } from '@/components/ui/compare-slider';

export function Example() {
  return (
    <CompareSlider
      label="Compare the two revisions"
      before={<img src="/before.jpg" alt="Before retouching" />}
      after={<img src="/after.jpg" alt="After retouching" />}
    />
  );
}

Notes

Motion tier

Needs motion. add --all skips it; install with add --motion or by name.

Drag modes

  • handle (default): only the divider starts a drag; both layers stay interactive. Use when either side is real UI.
  • panel: pressing anywhere jumps the divider there and makes both layers inert. Use when both sides are images.

When to use it

  • Before and after photos, design diffs, a map with a layer toggled.
  • Both layers must share the same frame, position and size.
  • For two unaligned things, use side-by-side panels.

Behaviour

  • A pointer drag tracks 1:1 with no easing.
  • A keyboard step animates over 100ms; a held arrow key moves continuously.
  • Grabbing the handle keeps the grab offset. In panel mode the divider jumps to the press.
  • A second pointer mid-drag is ignored until the first lifts.
  • onPositionChange runs only when the rounded position changes.

API

beforeReactNode
The layer left of the divider.
afterReactNode
The layer right of the divider.
defaultPositionnumber = 50
Starting divider position, 0 to 100.
onPositionChange
agent
(position, details) => void
Runs on release, each key step and for agents. details.cancel() puts the divider back.
stepnumber = 2
Percentage points per arrow key. Shift moves five times as far.
drag"handle" | "panel" = "handle"
What starts a drag. handle keeps both layers interactive.
label
agent
string = "Compare position"
Accessible name for the handle. Also the name the agent reads.
stickyHandleboolean = false
Keeps the knob mid-viewport. Use when the panel is taller than the screen.
disabled
agent
boolean = false
Ignores drags and keys, dims the panel and blocks agents.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as slider.

Reads

label

The label prop, the handle's aria-label.

value

The divider position, a whole number from 0 to 100.

min / max

Always 0 and 100.

disabled

True when the slider is disabled.

Actions

setValue(70)

Moves the divider. Rounds the input; refuses non-numbers and out-of-range values.

An agent's move runs through onPositionChange with details.reason set to imperative-action. isAgentChange(details) is true for it, and details.cancel() refuses it; the divider stays and the agent is told the app refused. A disabled slider refuses on its own.

await agent.act({
  steps: [{ ref: "Compare the two revisions", action: "setValue", args: [70] }],
})

Accessibility

  • The handle is a slider with min, max, live valuenow and a percentage valuetext.
  • The announced value updates on key press and stays current during a drag.
  • Give it a label naming what is compared; the default describes only the control.
  • Disabled, the handle leaves the tab order and gets aria-disabled.
  • Reduced motion applies each keyboard step immediately.