Image Compare
A before/after slider with a lit-key handle, drag or hover modes, keyboard control, vertical orientation and side labels.
Preview
Billing
November · 312 clients
Revenue
$48,211
+12.4%
Outstanding
$6,380
4 due
Paid on time
96%
+3 pts
Collected Last 10 weeks
BILLING ADMIN v2Home | Invoices | Clients | Reports | Settings | Help
Revenue (MTD)
48211.2
Outstanding
6380
Clients
312
IDCLIENTAMOUNTSTATUS
INV-2041Northline$1,284.50PAID
INV-2042Pebble notes$342.00OVERDUE
INV-2043Pebble$5,120.00PAID
INV-2044Pebble Studio$96.40DRAFT
INV-2045Kiln Room$730.15PAID
46% legacy · drag, or focus the handle and use arrow keys
Weekly signups
2,310
Jul
Aug
Sep
Oct
Nov
Dec
Weekly signups
1,600
Jul
Aug
Sep
Oct
Nov
Dec
Install
Add the editable source with the shadcn CLI. Your project also needs the shared theme tokens this component uses. Setup and requirements. The Pebble installer is not published yet.
npx shadcn@latest add https://pebbleui.com/r/image-compare.jsonProps
| Prop | Type | Default | Description |
|---|---|---|---|
| before | ReactNode | — | Layer on the start side (left, or top when vertical). |
| after | ReactNode | — | Layer on the end side. |
| beforeLabel / afterLabel | string | "Before" / "After" | Chip text for each side; an empty string hides the chip. |
| defaultValue | number | 50 | Divider position in percent. |
| value / onValueChange | number / (value: number) => void | — | Controlled position in percent. |
| orientation | "horizontal" | "vertical" | "horizontal" | Vertical moves the divider up and down. |
| mode | "drag" | "hover" | "drag" | drag follows a press; hover follows the mouse without pressing. |
| labelPosition | "top" | "bottom" | "top" | Edge the chips sit on (horizontal). |
| label | string | "Comparison position" | Accessible name of the slider handle. |
Build notes
- The frame writes `--pos: <value>%`. The before layer uses `clip-path: inset(0 calc(100 - v)% 0 0)` (vertical: `inset(0 0 (100 - v)% 0)`), so both layers keep identical layout and nothing reflows while dragging. The divider is a 2px `bg-card` bar at `left: var(--pos)` with a hairline ring and `--elevation-sm`; it turns ember while dragging or focused (`group-has-focus-visible`). - Pointer: `pointerdown` captures the pointer on the frame and maps `clientX` (or `clientY`) through the frame's rect to percent; `touch-none` keeps touch drags from scrolling the page. Hover mode updates on every non-touch `pointermove` and makes the knob `pointer-events-none` so it never blocks tracking. - Knob: 36px circle with the lit-key recipe (`from-key-top to-key-bottom`, `ring-key-edge`, `--key-shine`), a two-chevron grip icon rotated 90° when vertical. Scale transitions at `--motion-fast` with `ease-enter`. - Chips: `bg-card/85` + `backdrop-blur-sm` raised pills; opacity 0 when the value is within 18% (22% vertical) of their edge. <!-- PEBBLE-LICENSE -->
View source
registry/media/image-compare/image-compare.tsx
"use client";
/* PEBBLE-LICENSE
Built by Pebble. Component: image-compare.
First-party license: MIT
The following first-party terms apply only to original Pebble material.
MIT License
Copyright (c) 2026 Praxys Solutions LLC (Pebble)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
END PEBBLE-LICENSE */
import * as React from "react";
import { cn } from "@/lib/utils";
export type ImageCompareProps = Omit<React.ComponentProps<"div">, "onChange" | "defaultValue"> & {
/** Layer shown on the start side (left, or top when vertical). */
before: React.ReactNode;
/** Layer shown on the end side (right, or bottom when vertical). */
after: React.ReactNode;
/** Chip text over the before side. Empty hides it. */
beforeLabel?: string;
/** Chip text over the after side. Empty hides it. */
afterLabel?: string;
/** Handle position in percent when uncontrolled. */
defaultValue?: number;
/** Handle position in percent (controlled). */
value?: number;
/** Called with the new position in percent. */
onValueChange?: (value: number) => void;
/** Divider direction: a vertical line moving left/right, or a horizontal line moving up/down. */
orientation?: "horizontal" | "vertical";
/** `drag` follows a press; `hover` follows the pointer without pressing. */
mode?: "drag" | "hover";
/** Where the chips sit along the divider (horizontal only). */
labelPosition?: "top" | "bottom";
/** Accessible name of the slider. */
label?: string;
};
const clamp = (n: number) => Math.min(100, Math.max(0, n));
export function ImageCompare({
before,
after,
beforeLabel = "Before",
afterLabel = "After",
defaultValue = 50,
value: valueProp,
onValueChange,
orientation = "horizontal",
mode = "drag",
labelPosition = "top",
label = "Comparison position",
className,
style,
...props
}: ImageCompareProps) {
const [inner, setInner] = React.useState(clamp(defaultValue));
const value = clamp(valueProp ?? inner);
const [dragging, setDragging] = React.useState(false);
const rootRef = React.useRef<HTMLDivElement>(null);
const handleRef = React.useRef<HTMLDivElement>(null);
const horizontal = orientation === "horizontal";
const commit = (next: number) => {
const v = Math.round(clamp(next) * 10) / 10;
if (v === value) return;
if (valueProp === undefined) setInner(v);
onValueChange?.(v);
};
const fromPointer = (event: React.PointerEvent) => {
const rect = rootRef.current?.getBoundingClientRect();
if (!rect || !rect.width || !rect.height) return;
commit(
horizontal
? ((event.clientX - rect.left) / rect.width) * 100
: ((event.clientY - rect.top) / rect.height) * 100,
);
};
// The before layer is clipped to the start side of the line.
const clip = horizontal ? `inset(0 ${100 - value}% 0 0)` : `inset(0 0 ${100 - value}% 0)`;
const valueText = `${Math.round(value)}% ${beforeLabel || "before"}, ${Math.round(100 - value)}% ${afterLabel || "after"}`;
return (
<div
ref={rootRef}
data-slot="image-compare"
data-orientation={orientation}
data-mode={mode}
data-dragging={dragging || undefined}
className={cn(
"group/compare relative isolate touch-none overflow-hidden rounded-xl bg-muted select-none",
"shadow-raised-md ring-1 inset-shadow-highlight ring-hairline",
mode === "drag" && (horizontal ? "cursor-ew-resize" : "cursor-ns-resize"),
className,
)}
style={{ "--pos": `${value}%`, ...style } as React.CSSProperties}
onPointerDown={(event) => {
if (mode !== "drag" || event.button !== 0) return;
event.preventDefault();
event.currentTarget.setPointerCapture(event.pointerId);
setDragging(true);
fromPointer(event);
handleRef.current?.focus({ preventScroll: true });
}}
onPointerMove={(event) => {
if (mode === "hover" ? event.pointerType !== "touch" || event.buttons > 0 : dragging)
fromPointer(event);
}}
onPointerUp={(event) => {
if (event.currentTarget.hasPointerCapture(event.pointerId))
event.currentTarget.releasePointerCapture(event.pointerId);
setDragging(false);
}}
onPointerCancel={() => setDragging(false)}
{...props}
>
<div data-slot="image-compare-after" className="absolute inset-0 *:size-full">
{after}
</div>
<div
data-slot="image-compare-before"
className="absolute inset-0 *:size-full"
style={{ clipPath: clip }}
>
{before}
</div>
{beforeLabel && (
<Chip
className={cn("left-3", horizontal && labelPosition === "bottom" ? "bottom-3" : "top-3")}
hidden={value < (horizontal ? 18 : 22)}
>
{beforeLabel}
</Chip>
)}
{afterLabel && (
<Chip
className={cn(
horizontal ? "right-3" : "left-3",
horizontal && labelPosition === "top" ? "top-3" : "bottom-3",
)}
hidden={value > (horizontal ? 82 : 78)}
>
{afterLabel}
</Chip>
)}
{/* Divider: a hairline of light with a soft shadow either side. */}
<div
aria-hidden
className={cn(
"pointer-events-none absolute z-10 bg-card shadow-[0_0_0_1px_var(--hairline),var(--elevation-sm)]",
"transition-[background-color,box-shadow] duration-(--motion-fast)",
"group-has-focus-visible/compare:bg-accent group-data-dragging/compare:bg-accent",
horizontal
? "inset-y-0 left-(--pos) w-0.5 -translate-x-1/2"
: "inset-x-0 top-(--pos) h-0.5 -translate-y-1/2",
)}
/>
<div
ref={handleRef}
role="slider"
tabIndex={0}
aria-label={label}
aria-orientation={orientation}
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(value)}
aria-valuetext={valueText}
data-slot="image-compare-handle"
onKeyDown={(event) => {
const big = event.shiftKey ? 10 : 1;
const map: Record<string, number> = {
ArrowLeft: value - big,
ArrowDown: horizontal ? value - big : value + big,
ArrowRight: value + big,
ArrowUp: horizontal ? value + big : value - big,
PageDown: value - 10,
PageUp: value + 10,
Home: 0,
End: 100,
};
if (!(event.key in map)) return;
event.preventDefault();
commit(map[event.key]);
}}
className={cn(
"absolute z-20 grid size-9 place-items-center rounded-full outline-none",
horizontal ? "top-1/2 left-(--pos)" : "top-(--pos) left-1/2",
"-translate-x-1/2 -translate-y-1/2",
// The lit key: gradient, edge ring, top shine.
"bg-linear-to-b from-key-top to-key-bottom text-primary-foreground shadow-[var(--key-shine),var(--elevation-md)] ring-1 ring-key-edge",
"cursor-grab transition-[scale,box-shadow] duration-(--motion-fast) ease-enter group-data-dragging/compare:cursor-grabbing",
"group-data-dragging/compare:scale-[1.08] group-data-dragging/compare:shadow-[var(--key-shine),var(--elevation-lg)] hover:scale-105",
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card",
"motion-reduce:transition-none motion-reduce:group-data-dragging/compare:scale-100 motion-reduce:hover:scale-100",
mode === "hover" && "pointer-events-none",
)}
>
<svg
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth={1.5}
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden
className={cn("size-4", !horizontal && "rotate-90")}
>
<path d="M5.5 4.5 2 8l3.5 3.5M10.5 4.5 14 8l-3.5 3.5" />
</svg>
</div>
</div>
);
}
function Chip({
hidden,
className,
children,
}: {
hidden: boolean;
className?: string;
children: React.ReactNode;
}) {
return (
<span
aria-hidden
className={cn(
"pointer-events-none absolute z-10 rounded-full bg-card/85 px-2.5 py-1 text-xs font-medium text-foreground backdrop-blur-sm",
"shadow-raised-xs ring-1 inset-shadow-highlight ring-hairline",
"transition-opacity duration-(--motion-base) ease-enter motion-reduce:transition-none",
hidden ? "opacity-0" : "opacity-100",
className,
)}
>
{children}
</span>
);
}
