$npx @dinachi/cli@latest add compare-slider
After
Before
dragdefaultPositionstepdisabledrevisions.tsx
import { CompareSlider } from "@/components/ui/compare-slider"
export function Revisions() {
return (
<CompareSlider
label="Compare the two revisions"
before={<div className="h-56 bg-muted p-4">Before</div>}
after={<div className="h-56 bg-primary p-4 text-primary-foreground">After</div>}
/>
)
}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" />}
/>
);
}After
Before
Press anywhere to jump the divider there.
import { CompareSlider } from '@/components/ui/compare-slider';
export function Example() {
// Both sides are images, so there is nothing to click and a 2px handle is a
// needlessly small target.
return (
<CompareSlider
drag="panel"
label="Compare the two revisions"
before={<img src="/before.jpg" alt="Before retouching" />}
after={<img src="/after.jpg" alt="After retouching" />}
/>
);
}Proposed plan
Current plan
Deploys are queued.
Both layers stay interactive while the handle drags.
import { CompareSlider } from '@/components/ui/compare-slider';
import { Button } from '@/components/ui/button';
export function Example() {
return (
<CompareSlider
label="Compare the two plans"
before={<CurrentPlan />}
after={
<ProposedPlan>
<Button onClick={upgrade}>Upgrade</Button>
</ProposedPlan>
}
/>
);
}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
panelmode the divider jumps to the press. - A second pointer mid-drag is ignored until the first lifts.
onPositionChangeruns 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
Runs on release, each key step and for agents. agent
(position, details) => voiddetails.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
Accessible name for the handle. Also the name the agent reads.agent
string = "Compare position"stickyHandleboolean = false
Keeps the knob mid-viewport. Use when the panel is taller than the screen.disabled
Ignores drags and keys, dims the panel and blocks agents.agent
boolean = falseAgent
Inside an AgentProvider, it appears in agent.snapshot() as slider.
Reads
labelThe label prop, the handle's aria-label.
valueThe divider position, a whole number from 0 to 100.
min / maxAlways 0 and 100.
disabledTrue 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
labelnaming 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.
Keyboard
←↓Moves the divider left by one step
→↑Moves the divider right by one step
ShiftArrowMoves five steps at once
HomeEndJumps to either end
←↓Moves the divider left by one step
→↑Moves the divider right by one step
ShiftArrowMoves five steps at once
HomeEndJumps to either end