Before After Slider
Preview
"use client";
import { BeforeAfterSlider } from "@/components/motion/before-after-slider";
const PHOTO =
"https://images.unsplash.com/photo-1470770841072-f978cf4d019e?auto=format&fit=crop&w=1200&q=80";
export function BeforeAfterSliderPreview() {
return (
<div className="mx-auto w-full max-w-xl min-w-0 px-4 py-6">
<BeforeAfterSlider
before={
<img
src={PHOTO}
alt="Lake house at dawn, unedited"
draggable={false}
className="h-full w-full object-cover brightness-90 contrast-75 grayscale"
/>
}
after={
<img
src={PHOTO}
alt="Lake house at dawn, color graded"
draggable={false}
className="h-full w-full object-cover"
/>
}
/>
</div>
);
}
"use client";
// beui.dev/components/motion/before-after-slider
import { ChevronLeft, ChevronRight } from "lucide-react";
import {
type AnimationPlaybackControls,
animate,
motion,
useInView,
useMotionValue,
useReducedMotion,
useSpring,
useTransform,
useVelocity,
} from "motion/react";
import {
type KeyboardEvent,
type PointerEvent,
type ReactNode,
useEffect,
useLayoutEffect,
useRef,
useState,
} from "react";
import { EASE_IN_OUT, SPRING_GLIDE, SPRING_MOUSE, SPRING_PRESS } from "@/lib/ease";
import { useHoverCapable } from "@/lib/hooks/use-hover-capable";
import { useMessages } from "@/lib/i18n";
import { capturePointer, releasePointer, TOUCH_GESTURE_CLASS } from "@/lib/touch";
import { cn } from "@/lib/utils";
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const beforeAfterSliderMessages = {
label: "Before and after comparison",
before: "Before",
after: "After",
valueText: "{value}% before",
};
export type BeforeAfterSliderMessages = typeof beforeAfterSliderMessages;
export interface BeforeAfterSliderProps {
/** Shown on the left of the divider, usually an image. */
before: ReactNode;
/** Shown on the right of the divider, usually an image of the same size. */
after: ReactNode;
/** Divider position as a percent of the width, 0 to 100 (controlled). */
value?: number;
/** Starting position when uncontrolled. */
defaultValue?: number;
onValueChange?: (value: number) => void;
/** Show the Before and After labels in the top corners. */
showLabels?: boolean;
/** Sweep the divider once when the slider first scrolls into view, to show it moves. */
hint?: boolean;
disabled?: boolean;
labels?: Partial<BeforeAfterSliderMessages>;
/** Size the frame here; it defaults to a 3:2 box at full width. */
className?: string;
}
const clamp = (value: number) => Math.min(100, Math.max(0, value));
// Two decimals keep a drag smooth on wide images without a noisy value.
const round = (value: number) => Math.round(value * 100) / 100;
// Movement before a touch counts as a horizontal drag rather than a scroll.
const TOUCH_SLOP = 6;
// Half the handle, so grabbing it keeps the grab point instead of jumping.
const HANDLE_RADIUS = 20;
// How far, in percent, the first-view hint sweeps each way, and how long.
const HINT_REACH = 10;
const HINT_DURATION = 1.6;
// Divider speed (percent of the width per second) that reaches the full bend.
const BEND_SPEED = 250;
// How far the line's ends trail its middle at full bend, as a share of the height.
const BEND_MAX = 0.12;
// How far each chevron moves out from the center, in pixels.
const CHEVRON_SPLIT = { rest: 0, hover: 1.5, drag: 3 };
type Gesture = {
id: number;
left: number;
width: number;
startX: number;
startY: number;
/** Pointer distance from the divider when the handle was grabbed. */
offset: number;
/** False while a touch has not yet proved to be a horizontal drag. */
active: boolean;
};
/**
* Compare two images (or any two layers) with a draggable divider. Drag the
* handle or anywhere on the frame, click to glide the divider there, or use
* the arrow keys. On touch, a vertical swipe still scrolls the page.
*/
export function BeforeAfterSlider({
before,
after,
value,
defaultValue = 50,
onValueChange,
showLabels = true,
hint = true,
disabled = false,
labels,
className,
}: BeforeAfterSliderProps) {
const t = useMessages("beforeAfterSlider", beforeAfterSliderMessages, labels);
const reduce = useReducedMotion();
const [internal, setInternal] = useState(() => clamp(defaultValue));
const controlled = value !== undefined;
const current = clamp(controlled ? value : internal);
const [dragging, setDragging] = useState(false);
const [hovered, setHovered] = useState(false);
const hoverCapable = useHoverCapable();
const gesture = useRef<Gesture | null>(null);
const frameRef = useRef<HTMLDivElement>(null);
const handleRef = useRef<HTMLDivElement>(null);
const inView = useInView(frameRef, { once: true, amount: 0.6 });
// The hint plays once per mount and never after the user has touched it.
const hintState = useRef<"pending" | "done">(hint ? "pending" : "done");
const hintRun = useRef<AnimationPlaybackControls | null>(null);
// The next position change glides (a click or a key) instead of following
// the pointer exactly.
const glide = useRef(false);
const position = useMotionValue(current);
const width = useMotionValue(0);
const height = useMotionValue(0);
// The line bends like a rope held at the handle: its middle follows the
// divider and its ends trail behind, more the faster it moves, then it
// wobbles straight when it stops. Velocity covers drags, glides and the hint.
const velocity = useVelocity(position);
const pull = useTransform(velocity, [-BEND_SPEED, 0, BEND_SPEED], [-1, 0, 1]);
const bend = useSpring(useTransform(pull, (amount) => (reduce ? 0 : amount)), SPRING_MOUSE);
// clip-path, not a resize: the before layer keeps its full size, so the
// two layers stay aligned pixel for pixel at every position. The cut follows
// the line's curve. Until the frame is measured it is a plain straight cut.
const clipPath = useTransform([position, bend, width, height], ([p, amount, w, h]: number[]) => {
if (!w || !h) return `inset(0 ${100 - p}% 0 0)`;
const middle = (p / 100) * w;
const trail = amount * BEND_MAX * h;
// The control point sits past the middle by as much as the ends trail,
// so the curve passes exactly through the divider at half height.
return `path("M0 0 L${middle - trail} 0 Q${middle + trail} ${h / 2} ${middle - trail} ${h} L0 ${h} Z")`;
});
// The same curve for the drawn line, in a 100 by 100 box stretched over the
// frame, so it needs no measuring for its straight resting shape.
const lineD = useTransform([position, bend, width, height], ([p, amount, w, h]: number[]) => {
const trail = w ? ((amount * BEND_MAX * h) / w) * 100 : 0;
return `M${p - trail} 0 Q${p + trail} 50 ${p - trail} 100`;
});
const dividerX = useTransform(position, (p) => `${p}%`);
// Each label fades as the divider comes close, so neither is cut in half.
const beforeOpacity = useTransform(position, [8, 22], [0, 1]);
const afterOpacity = useTransform(position, [78, 92], [1, 0]);
useLayoutEffect(() => {
if (reduce || !glide.current) {
position.stop();
// set, not jump: jump zeroes the velocity the drag bend is read from.
if (reduce) position.jump(current);
else position.set(current);
return;
}
glide.current = false;
animate(position, current, { type: "spring", ...SPRING_GLIDE });
}, [current, position, reduce]);
useEffect(() => () => position.stop(), [position]);
// The curved cut is drawn in pixels, so it needs the frame's size.
useLayoutEffect(() => {
const frame = frameRef.current;
if (!frame) return;
const measure = () => {
const rect = frame.getBoundingClientRect();
width.set(rect.width);
height.set(rect.height);
};
measure();
const observer = new ResizeObserver(measure);
observer.observe(frame);
return () => observer.disconnect();
}, [width, height]);
// The hint moves only the picture, never the value, so assistive tech hears
// nothing and a controlled parent sees no change.
useEffect(() => {
if (!inView || reduce || disabled || hintState.current !== "pending") return;
const from = position.get();
const right = Math.min(100, from + HINT_REACH);
const left = Math.max(0, from - HINT_REACH);
const run = animate(position, [from, right, left, from], {
duration: HINT_DURATION,
ease: EASE_IN_OUT,
times: [0, 0.3, 0.7, 1],
delay: 0.25,
});
hintRun.current = run;
// Done only once it finishes or the user cuts in (stopHint), so a cleanup
// that stopped it early lets the next run play it from the start.
run.then(() => {
if (hintRun.current !== run) return;
hintRun.current = null;
hintState.current = "done";
});
return () => {
if (hintRun.current !== run) return;
run.stop();
hintRun.current = null;
position.jump(from);
};
}, [inView, reduce, disabled, position]);
// Any input ends the hint where the value says the divider is.
const stopHint = () => {
hintState.current = "done";
if (!hintRun.current) return;
hintRun.current.stop();
hintRun.current = null;
position.jump(current);
};
const commit = (next: number, smooth: boolean) => {
const clean = round(clamp(next));
if (clean === current) return;
glide.current = smooth;
if (!controlled) setInternal(clean);
onValueChange?.(clean);
};
const fromPointer = (active: Gesture, clientX: number) =>
((clientX - active.left - active.offset) / active.width) * 100;
const start = (event: PointerEvent<HTMLDivElement>, active: Gesture) => {
active.active = true;
setDragging(true);
capturePointer(event.currentTarget, event.pointerId);
handleRef.current?.focus({ preventScroll: true });
};
const onPointerDown = (event: PointerEvent<HTMLDivElement>) => {
if (disabled || event.button !== 0 || gesture.current) return;
stopHint();
const rect = event.currentTarget.getBoundingClientRect();
if (!rect.width) return;
const dividerPx = (position.get() / 100) * rect.width;
const pointerPx = event.clientX - rect.left;
const onHandle = Math.abs(pointerPx - dividerPx) <= HANDLE_RADIUS;
const active: Gesture = {
id: event.pointerId,
left: rect.left,
width: rect.width,
startX: event.clientX,
startY: event.clientY,
offset: onHandle ? pointerPx - dividerPx : 0,
active: false,
};
gesture.current = active;
// A touch waits to see which way it moves; a mouse or pen drags at once.
if (event.pointerType === "touch") return;
event.preventDefault();
start(event, active);
if (!onHandle) commit(fromPointer(active, event.clientX), true);
};
const onPointerMove = (event: PointerEvent<HTMLDivElement>) => {
const active = gesture.current;
if (!active || active.id !== event.pointerId || disabled) return;
if (!active.active) {
const dx = Math.abs(event.clientX - active.startX);
const dy = Math.abs(event.clientY - active.startY);
if (dx < TOUCH_SLOP && dy < TOUCH_SLOP) return;
// A mostly vertical swipe belongs to the page.
if (dy >= dx) {
gesture.current = null;
return;
}
start(event, active);
}
commit(fromPointer(active, event.clientX), false);
};
const end = (event: PointerEvent<HTMLDivElement>) => {
const active = gesture.current;
if (!active || active.id !== event.pointerId) return;
// Clear first: releasing capture fires lostpointercapture, which lands here again.
gesture.current = null;
// A tap that never became a drag glides the divider to the tap.
if (!active.active && event.type === "pointerup" && !disabled) {
commit(fromPointer(active, event.clientX), true);
}
setDragging(false);
releasePointer(event.currentTarget, event.pointerId);
};
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
if (disabled) return;
const step = event.shiftKey ? 10 : 1;
const next = {
ArrowRight: current + step,
ArrowUp: current + step,
ArrowLeft: current - step,
ArrowDown: current - step,
PageUp: current + 10,
PageDown: current - 10,
Home: 0,
End: 100,
}[event.key];
if (next === undefined) return;
event.preventDefault();
stopHint();
commit(next, true);
};
const chip =
"pointer-events-none absolute top-3 rounded-full bg-black/45 px-2.5 py-1 text-xs font-medium text-white backdrop-blur-sm";
return (
// biome-ignore lint/a11y/noStaticElementInteractions: a pointer surface for the slider below, which owns focus, keys and the accessible value.
<div
ref={frameRef}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={end}
onPointerCancel={end}
onLostPointerCapture={(event) => {
// A touch is implicitly captured by the child it landed on; taking
// capture for the frame makes that child lose it, and the event
// bubbles here. Only the frame's own capture ends the drag.
if (event.target === event.currentTarget) end(event);
}}
// Images inside are the consumer's; stop the browser dragging them out.
onDragStart={(event) => event.preventDefault()}
className={cn(
"relative isolate aspect-[3/2] w-full touch-pan-y overflow-hidden rounded-2xl bg-muted",
TOUCH_GESTURE_CLASS,
disabled ? "opacity-60" : dragging ? "cursor-grabbing" : "cursor-ew-resize",
className,
)}
>
<div className="absolute inset-0">{after}</div>
<motion.div className="absolute inset-0" style={{ clipPath }}>
{before}
</motion.div>
{showLabels ? (
<div aria-hidden="true">
<motion.span className={cn(chip, "left-3")} style={{ opacity: beforeOpacity }}>
{t("before")}
</motion.span>
<motion.span className={cn(chip, "right-3")} style={{ opacity: afterOpacity }}>
{t("after")}
</motion.span>
</div>
) : null}
{/* The stroke stays its pixel width however the box is stretched. */}
<svg
aria-hidden="true"
viewBox="0 0 100 100"
preserveAspectRatio="none"
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
>
<motion.path
d={lineD}
fill="none"
stroke="rgb(0 0 0 / 0.14)"
strokeLinecap="square"
vectorEffect="non-scaling-stroke"
initial={false}
animate={{ strokeWidth: dragging && !reduce ? 5.5 : 4 }}
transition={SPRING_PRESS}
/>
<motion.path
d={lineD}
fill="none"
stroke="white"
strokeLinecap="square"
vectorEffect="non-scaling-stroke"
initial={false}
animate={{ strokeWidth: dragging && !reduce ? 3.5 : 2 }}
transition={SPRING_PRESS}
/>
</svg>
{/* Full width so a percent translate moves the handle by a percent of the frame. */}
<motion.div className="pointer-events-none absolute inset-0" style={{ x: dividerX }}>
<div className="absolute top-1/2 left-0 -translate-x-1/2 -translate-y-1/2">
<motion.div
ref={handleRef}
role="slider"
tabIndex={disabled ? -1 : 0}
aria-label={t("label")}
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(current)}
aria-valuetext={t("valueText", { value: Math.round(current) })}
aria-orientation="horizontal"
aria-disabled={disabled || undefined}
onKeyDown={onKeyDown}
onPointerEnter={(event) => {
if (hoverCapable && event.pointerType !== "touch") setHovered(true);
}}
onPointerLeave={() => setHovered(false)}
animate={{ scale: reduce ? 1 : dragging ? 1.08 : hovered ? 1.04 : 1 }}
transition={SPRING_PRESS}
className="pointer-events-auto flex size-10 items-center justify-center rounded-full bg-white text-neutral-900 shadow-[0_0_0_1px_rgb(0_0_0/0.12)] outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-black/20"
>
{/* The chevrons part as the handle is hovered and grabbed. */}
{([ChevronLeft, ChevronRight] as const).map((Icon, index) => {
const split = reduce
? 0
: CHEVRON_SPLIT[dragging ? "drag" : hovered ? "hover" : "rest"];
return (
<motion.span
key={index === 0 ? "left" : "right"}
aria-hidden="true"
animate={{ x: index === 0 ? -split : split }}
transition={SPRING_PRESS}
className={cn("flex", index === 0 ? "-mr-1" : "-ml-1")}
>
<Icon className="size-3.5" strokeWidth={2.5} />
</motion.span>
);
})}
</motion.div>
</div>
</motion.div>
</div>
);
}
Install
Add it with the shadcn CLI, or copy the source manually.
shadcn init? You are set. Theme setupInstall dependencies
npm i clsx lucide-react motion tailwind-mergeAdd util files
// Shared motion tokens. Easing curves mirror the CSS custom properties in
// globals.css; springs are the canonical physics used across components.
// Strong custom variants — defaults like `ease-in`/`ease-out` feel weak.
export const EASE_OUT = [0.16, 1, 0.3, 1] as const;
/** Accelerating curve for things that fall (a dropped ball, a toppled block). Not for UI entrances. */
export const EASE_IN = [0.55, 0, 1, 0.45] as const;
export const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;
export const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;
/** CSS string form of EASE_OUT for inline style transitions. */
export const EASE_OUT_CSS = "cubic-bezier(0.16, 1, 0.3, 1)";
/** Press feedback on buttons and other tappable surfaces. */
export const SPRING_PRESS = {
type: "spring",
stiffness: 500,
damping: 30,
mass: 0.6,
} as const;
/** Content swaps — label/icon slots trading places inside a control. */
export const SPRING_SWAP = {
type: "spring",
stiffness: 460,
damping: 30,
mass: 0.55,
} as const;
/** Overlay panel entrances — modals and sheets summoned by pointer. */
export const SPRING_PANEL = {
type: "spring",
stiffness: 420,
damping: 40,
mass: 0.5,
} as const;
/** Shared-layout glides — pills, indicators and panels morphing between positions. */
export const SPRING_LAYOUT = {
type: "spring",
stiffness: 360,
damping: 32,
mass: 0.6,
} as const;
/** Cursor-follow physics for decorative mouse tracking (magnetic, tilt, dock). */
export const SPRING_MOUSE = {
stiffness: 200,
damping: 15,
mass: 0.3,
} as const;
/** Dragged handles and fills (sliders) — critically damped `useSpring` config,
* so the value follows the pointer butterily and never rebounds off an end. */
export const SPRING_GLIDE = {
stiffness: 700,
damping: 50,
mass: 0.5,
} as const;
"use client";
import { useEffect, useState } from "react";
/**
* Returns true only on devices that have a true hover (mouse / trackpad).
* Touch devices fire phantom `:hover` on tap that sticks until tap-elsewhere
* — gate hover-only effects (scale lifts, magnetic pulls) behind this.
*/
export function useHoverCapable() {
const [canHover, setCanHover] = useState(false);
useEffect(() => {
if (typeof window === "undefined" || !window.matchMedia) return;
const mq = window.matchMedia("(hover: hover) and (pointer: fine)");
const update = () => setCanHover(mq.matches);
update();
mq.addEventListener?.("change", update);
return () => mq.removeEventListener?.("change", update);
}, []);
return canHover;
}
"use client";
import {
createContext,
type ReactNode,
useCallback,
useContext,
useMemo,
} from "react";
/**
* A component's messages. Plain strings with `{name}` placeholders, so a
* dictionary stays JSON-serializable and can be passed from a server layout.
* There are no plural forms: write messages that read correctly for any count.
*/
export type Messages = Record<string, string>;
/** A language file: component namespace → any subset of that component's messages. */
export type LocaleMessages = Record<string, Partial<Messages>>;
type LocaleContextValue = {
locale?: string;
messages: LocaleMessages;
};
const NO_MESSAGES: LocaleMessages = {};
const LocaleContext = createContext<LocaleContextValue>({ messages: NO_MESSAGES });
/** Without a locale, digits stay plain so server and client render the same text. */
export function formatNumber(value: number, locale?: string) {
return locale ? new Intl.NumberFormat(locale).format(value) : String(value);
}
export interface LocaleProviderProps {
/** BCP 47 tag, e.g. `de`, `pt-BR`. Drives `Intl` number formatting. */
locale: string;
/**
* Component namespace → translated strings. Missing keys fall back to English.
* Pass a stable object (a module constant, imported JSON or a memoized value):
* a new object each render gives every translated component a new `t`.
*/
messages?: LocaleMessages;
children: ReactNode;
}
export function LocaleProvider({ locale, messages = NO_MESSAGES, children }: LocaleProviderProps) {
const value = useMemo(() => ({ locale, messages }), [locale, messages]);
return <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>;
}
export function useLocale(): { locale?: string } {
return { locale: useContext(LocaleContext).locale };
}
/**
* Resolves a component's strings: `labels` prop, then the provider's
* messages for `namespace`, then the component's English `defaults`.
* Numbers passed as values are formatted for the provider's locale.
*/
export function useMessages<T extends Messages>(
namespace: string,
defaults: T,
labels?: Partial<T>,
) {
const { locale, messages } = useContext(LocaleContext);
const provided = messages[namespace];
// Inline `labels={{ … }}` is a new object every render. Keying it by content
// keeps `t` one identity until a label changes, so effects and memos that
// depend on `t` don't re-run per render, and render-time reads never lag.
const labelsKey = labels ? JSON.stringify(labels) : "";
const stableLabels = useMemo(
() => (labelsKey ? (JSON.parse(labelsKey) as Partial<T>) : undefined),
[labelsKey],
);
return useCallback(
(key: keyof T & string, values?: Record<string, string | number>) => {
const template = stableLabels?.[key] ?? provided?.[key] ?? defaults[key];
if (!values) return template;
return template.replace(/\{(\w+)\}/g, (match, name: string) => {
const value = values[name];
if (value === undefined) return match;
return typeof value === "number" ? formatNumber(value, locale) : value;
});
},
[defaults, stableLabels, locale, provided],
);
}
// Shared touch primitives. iOS and iPadOS run their own gestures on top of the
// page — the long-press selection callout and the selection it drags in with
// it — and they win: once the platform claims a touch it cancels ours
// mid-gesture, so a press-and-hold or a drag simply dies. Surfaces that own
// their gesture have to opt out.
//
// What the two classes below cover, precisely:
// - `-webkit-touch-callout: none` stops iOS's long-press callout. WebKit-only:
// it is not a property other engines have, so it is inert everywhere else.
// - `user-select: none` stops the long-press selection on every engine,
// Android included, and stops a drag from painting a selection under the
// cursor. It is inherited, so it reaches every descendant — which is why the
// two classes differ only in whether they apply it unconditionally.
// What neither covers:
// - Chrome for Android's long-press menu on a link or an image. No CSS
// suppresses it; a gesture surface that wraps one needs its own
// `onContextMenu` with `preventDefault()`.
// - The native drag of an `<img>` or `<a>` descendant. `-webkit-user-drag` is
// not inherited and plain divs and buttons are not drag sources, so setting
// it on the surface does nothing — the child itself needs `draggable={false}`.
/**
* Classes for a surface that *is* the control: a thumb, a drum, a stage, a
* handle, a hold button. Selection is suppressed on every input, because a
* drag that highlights the control's own label is wrong on a mouse too.
* Compose with `touch-none` when the surface also owns the scroll axis — leave
* it off when the page must still scroll from there.
*/
export const TOUCH_GESTURE_CLASS = "select-none [-webkit-touch-callout:none]";
/**
* The same opt-out for a gesture surface that wraps content the consumer owns:
* a scroller, a context-menu trigger, a sheet header, a list row. Selection is
* suppressed only where the platform runs its own press gestures — a coarse
* pointer — so a mouse user can still select and copy that content. If the
* gesture itself would paint a selection under the cursor, add `select-none`
* for the duration of the gesture rather than reaching for
* `TOUCH_GESTURE_CLASS`.
*
* `pointer: coarse` describes the *primary* pointer and nothing else, so a
* hybrid machine reads it wrong in both directions: a tablet with a mouse
* plugged in keeps touch as primary and loses mouse selection, and a laptop
* with a touchscreen keeps the mouse as primary and leaves selection live
* under a finger. No media query can answer per interaction — the query is
* about the device, and the question is about the gesture in progress. The
* default stays here because it is right on the machines that are one thing or
* the other, and losing a selection is a nuisance; where the miss costs a
* *gesture* instead, the surface pairs it with `holdSelection` on the press.
*/
export const TOUCH_GESTURE_CONTENT_CLASS =
"[-webkit-touch-callout:none] pointer-coarse:select-none";
/**
* Suppress selection on `element` for as long as a gesture is running on it,
* whatever the primary pointer of the machine happens to be. Returns the
* release. Inline, so it wins over the class above and is gone again the
* moment the gesture ends.
*
* For the press gestures a native selection would otherwise steal — a
* long-press that opens a menu. Elsewhere prefer the classes: a surface that
* takes selection away for the whole session is a surface whose text nobody
* can copy.
*/
export function holdSelection(element: HTMLElement) {
element.style.setProperty("user-select", "none");
element.style.setProperty("-webkit-user-select", "none");
return () => {
element.style.removeProperty("user-select");
element.style.removeProperty("-webkit-user-select");
};
}
/**
* Pointer capture, best effort. WebKit throws `NotFoundError` when the pointer
* is already gone by the time the handler runs — routine on iOS, where the
* system can claim the touch first — and an uncaught throw takes the rest of
* the handler, the gesture included, down with it. Touch pointers carry
* implicit capture anyway, so losing it is never fatal.
*/
export function capturePointer(element: Element, pointerId: number) {
try {
element.setPointerCapture(pointerId);
} catch {
// Pointer is no longer active — implicit capture still applies on touch.
}
}
/** Release a capture taken with `capturePointer`, ignoring a stale pointer. */
export function releasePointer(element: Element, pointerId: number) {
try {
if (element.hasPointerCapture(pointerId)) {
element.releasePointerCapture(pointerId);
}
} catch {
// Capture was already dropped by the browser.
}
}
/**
* Whether this event came from a pointer that is *hovering*: not a touch, and
* not currently pressed. Which input the user is holding right now is not
* something a device capability can answer — a touchscreen laptop hovers and
* taps, and iPadOS reports a fine hovering pointer for a finger — so both
* paths stay live and each handler branches on the event it was given.
*
* A pen resting on the glass is making contact, not hovering: `buttons` is the
* tell, and it sends a pen tap down the same route a finger takes.
*
* This answers what an *enter* asks. A leave is the other half of a pair and
* has to be read against the enter that started it — `useHoverGesture` in
* `lib/hooks/use-hover-gesture` does that, and hover surfaces should use it
* rather than asking this question twice.
*/
export const isHoveringPointer = (event: {
pointerType: string;
buttons: number;
}) => event.pointerType !== "touch" && event.buttons === 0;
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Copy the source code
"use client";
// beui.dev/components/motion/before-after-slider
import { ChevronLeft, ChevronRight } from "lucide-react";
import {
type AnimationPlaybackControls,
animate,
motion,
useInView,
useMotionValue,
useReducedMotion,
useSpring,
useTransform,
useVelocity,
} from "motion/react";
import {
type KeyboardEvent,
type PointerEvent,
type ReactNode,
useEffect,
useLayoutEffect,
useRef,
useState,
} from "react";
import { EASE_IN_OUT, SPRING_GLIDE, SPRING_MOUSE, SPRING_PRESS } from "@/lib/ease";
import { useHoverCapable } from "@/lib/hooks/use-hover-capable";
import { useMessages } from "@/lib/i18n";
import { capturePointer, releasePointer, TOUCH_GESTURE_CLASS } from "@/lib/touch";
import { cn } from "@/lib/utils";
/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */
export const beforeAfterSliderMessages = {
label: "Before and after comparison",
before: "Before",
after: "After",
valueText: "{value}% before",
};
export type BeforeAfterSliderMessages = typeof beforeAfterSliderMessages;
export interface BeforeAfterSliderProps {
/** Shown on the left of the divider, usually an image. */
before: ReactNode;
/** Shown on the right of the divider, usually an image of the same size. */
after: ReactNode;
/** Divider position as a percent of the width, 0 to 100 (controlled). */
value?: number;
/** Starting position when uncontrolled. */
defaultValue?: number;
onValueChange?: (value: number) => void;
/** Show the Before and After labels in the top corners. */
showLabels?: boolean;
/** Sweep the divider once when the slider first scrolls into view, to show it moves. */
hint?: boolean;
disabled?: boolean;
labels?: Partial<BeforeAfterSliderMessages>;
/** Size the frame here; it defaults to a 3:2 box at full width. */
className?: string;
}
const clamp = (value: number) => Math.min(100, Math.max(0, value));
// Two decimals keep a drag smooth on wide images without a noisy value.
const round = (value: number) => Math.round(value * 100) / 100;
// Movement before a touch counts as a horizontal drag rather than a scroll.
const TOUCH_SLOP = 6;
// Half the handle, so grabbing it keeps the grab point instead of jumping.
const HANDLE_RADIUS = 20;
// How far, in percent, the first-view hint sweeps each way, and how long.
const HINT_REACH = 10;
const HINT_DURATION = 1.6;
// Divider speed (percent of the width per second) that reaches the full bend.
const BEND_SPEED = 250;
// How far the line's ends trail its middle at full bend, as a share of the height.
const BEND_MAX = 0.12;
// How far each chevron moves out from the center, in pixels.
const CHEVRON_SPLIT = { rest: 0, hover: 1.5, drag: 3 };
type Gesture = {
id: number;
left: number;
width: number;
startX: number;
startY: number;
/** Pointer distance from the divider when the handle was grabbed. */
offset: number;
/** False while a touch has not yet proved to be a horizontal drag. */
active: boolean;
};
/**
* Compare two images (or any two layers) with a draggable divider. Drag the
* handle or anywhere on the frame, click to glide the divider there, or use
* the arrow keys. On touch, a vertical swipe still scrolls the page.
*/
export function BeforeAfterSlider({
before,
after,
value,
defaultValue = 50,
onValueChange,
showLabels = true,
hint = true,
disabled = false,
labels,
className,
}: BeforeAfterSliderProps) {
const t = useMessages("beforeAfterSlider", beforeAfterSliderMessages, labels);
const reduce = useReducedMotion();
const [internal, setInternal] = useState(() => clamp(defaultValue));
const controlled = value !== undefined;
const current = clamp(controlled ? value : internal);
const [dragging, setDragging] = useState(false);
const [hovered, setHovered] = useState(false);
const hoverCapable = useHoverCapable();
const gesture = useRef<Gesture | null>(null);
const frameRef = useRef<HTMLDivElement>(null);
const handleRef = useRef<HTMLDivElement>(null);
const inView = useInView(frameRef, { once: true, amount: 0.6 });
// The hint plays once per mount and never after the user has touched it.
const hintState = useRef<"pending" | "done">(hint ? "pending" : "done");
const hintRun = useRef<AnimationPlaybackControls | null>(null);
// The next position change glides (a click or a key) instead of following
// the pointer exactly.
const glide = useRef(false);
const position = useMotionValue(current);
const width = useMotionValue(0);
const height = useMotionValue(0);
// The line bends like a rope held at the handle: its middle follows the
// divider and its ends trail behind, more the faster it moves, then it
// wobbles straight when it stops. Velocity covers drags, glides and the hint.
const velocity = useVelocity(position);
const pull = useTransform(velocity, [-BEND_SPEED, 0, BEND_SPEED], [-1, 0, 1]);
const bend = useSpring(useTransform(pull, (amount) => (reduce ? 0 : amount)), SPRING_MOUSE);
// clip-path, not a resize: the before layer keeps its full size, so the
// two layers stay aligned pixel for pixel at every position. The cut follows
// the line's curve. Until the frame is measured it is a plain straight cut.
const clipPath = useTransform([position, bend, width, height], ([p, amount, w, h]: number[]) => {
if (!w || !h) return `inset(0 ${100 - p}% 0 0)`;
const middle = (p / 100) * w;
const trail = amount * BEND_MAX * h;
// The control point sits past the middle by as much as the ends trail,
// so the curve passes exactly through the divider at half height.
return `path("M0 0 L${middle - trail} 0 Q${middle + trail} ${h / 2} ${middle - trail} ${h} L0 ${h} Z")`;
});
// The same curve for the drawn line, in a 100 by 100 box stretched over the
// frame, so it needs no measuring for its straight resting shape.
const lineD = useTransform([position, bend, width, height], ([p, amount, w, h]: number[]) => {
const trail = w ? ((amount * BEND_MAX * h) / w) * 100 : 0;
return `M${p - trail} 0 Q${p + trail} 50 ${p - trail} 100`;
});
const dividerX = useTransform(position, (p) => `${p}%`);
// Each label fades as the divider comes close, so neither is cut in half.
const beforeOpacity = useTransform(position, [8, 22], [0, 1]);
const afterOpacity = useTransform(position, [78, 92], [1, 0]);
useLayoutEffect(() => {
if (reduce || !glide.current) {
position.stop();
// set, not jump: jump zeroes the velocity the drag bend is read from.
if (reduce) position.jump(current);
else position.set(current);
return;
}
glide.current = false;
animate(position, current, { type: "spring", ...SPRING_GLIDE });
}, [current, position, reduce]);
useEffect(() => () => position.stop(), [position]);
// The curved cut is drawn in pixels, so it needs the frame's size.
useLayoutEffect(() => {
const frame = frameRef.current;
if (!frame) return;
const measure = () => {
const rect = frame.getBoundingClientRect();
width.set(rect.width);
height.set(rect.height);
};
measure();
const observer = new ResizeObserver(measure);
observer.observe(frame);
return () => observer.disconnect();
}, [width, height]);
// The hint moves only the picture, never the value, so assistive tech hears
// nothing and a controlled parent sees no change.
useEffect(() => {
if (!inView || reduce || disabled || hintState.current !== "pending") return;
const from = position.get();
const right = Math.min(100, from + HINT_REACH);
const left = Math.max(0, from - HINT_REACH);
const run = animate(position, [from, right, left, from], {
duration: HINT_DURATION,
ease: EASE_IN_OUT,
times: [0, 0.3, 0.7, 1],
delay: 0.25,
});
hintRun.current = run;
// Done only once it finishes or the user cuts in (stopHint), so a cleanup
// that stopped it early lets the next run play it from the start.
run.then(() => {
if (hintRun.current !== run) return;
hintRun.current = null;
hintState.current = "done";
});
return () => {
if (hintRun.current !== run) return;
run.stop();
hintRun.current = null;
position.jump(from);
};
}, [inView, reduce, disabled, position]);
// Any input ends the hint where the value says the divider is.
const stopHint = () => {
hintState.current = "done";
if (!hintRun.current) return;
hintRun.current.stop();
hintRun.current = null;
position.jump(current);
};
const commit = (next: number, smooth: boolean) => {
const clean = round(clamp(next));
if (clean === current) return;
glide.current = smooth;
if (!controlled) setInternal(clean);
onValueChange?.(clean);
};
const fromPointer = (active: Gesture, clientX: number) =>
((clientX - active.left - active.offset) / active.width) * 100;
const start = (event: PointerEvent<HTMLDivElement>, active: Gesture) => {
active.active = true;
setDragging(true);
capturePointer(event.currentTarget, event.pointerId);
handleRef.current?.focus({ preventScroll: true });
};
const onPointerDown = (event: PointerEvent<HTMLDivElement>) => {
if (disabled || event.button !== 0 || gesture.current) return;
stopHint();
const rect = event.currentTarget.getBoundingClientRect();
if (!rect.width) return;
const dividerPx = (position.get() / 100) * rect.width;
const pointerPx = event.clientX - rect.left;
const onHandle = Math.abs(pointerPx - dividerPx) <= HANDLE_RADIUS;
const active: Gesture = {
id: event.pointerId,
left: rect.left,
width: rect.width,
startX: event.clientX,
startY: event.clientY,
offset: onHandle ? pointerPx - dividerPx : 0,
active: false,
};
gesture.current = active;
// A touch waits to see which way it moves; a mouse or pen drags at once.
if (event.pointerType === "touch") return;
event.preventDefault();
start(event, active);
if (!onHandle) commit(fromPointer(active, event.clientX), true);
};
const onPointerMove = (event: PointerEvent<HTMLDivElement>) => {
const active = gesture.current;
if (!active || active.id !== event.pointerId || disabled) return;
if (!active.active) {
const dx = Math.abs(event.clientX - active.startX);
const dy = Math.abs(event.clientY - active.startY);
if (dx < TOUCH_SLOP && dy < TOUCH_SLOP) return;
// A mostly vertical swipe belongs to the page.
if (dy >= dx) {
gesture.current = null;
return;
}
start(event, active);
}
commit(fromPointer(active, event.clientX), false);
};
const end = (event: PointerEvent<HTMLDivElement>) => {
const active = gesture.current;
if (!active || active.id !== event.pointerId) return;
// Clear first: releasing capture fires lostpointercapture, which lands here again.
gesture.current = null;
// A tap that never became a drag glides the divider to the tap.
if (!active.active && event.type === "pointerup" && !disabled) {
commit(fromPointer(active, event.clientX), true);
}
setDragging(false);
releasePointer(event.currentTarget, event.pointerId);
};
const onKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
if (disabled) return;
const step = event.shiftKey ? 10 : 1;
const next = {
ArrowRight: current + step,
ArrowUp: current + step,
ArrowLeft: current - step,
ArrowDown: current - step,
PageUp: current + 10,
PageDown: current - 10,
Home: 0,
End: 100,
}[event.key];
if (next === undefined) return;
event.preventDefault();
stopHint();
commit(next, true);
};
const chip =
"pointer-events-none absolute top-3 rounded-full bg-black/45 px-2.5 py-1 text-xs font-medium text-white backdrop-blur-sm";
return (
// biome-ignore lint/a11y/noStaticElementInteractions: a pointer surface for the slider below, which owns focus, keys and the accessible value.
<div
ref={frameRef}
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={end}
onPointerCancel={end}
onLostPointerCapture={(event) => {
// A touch is implicitly captured by the child it landed on; taking
// capture for the frame makes that child lose it, and the event
// bubbles here. Only the frame's own capture ends the drag.
if (event.target === event.currentTarget) end(event);
}}
// Images inside are the consumer's; stop the browser dragging them out.
onDragStart={(event) => event.preventDefault()}
className={cn(
"relative isolate aspect-[3/2] w-full touch-pan-y overflow-hidden rounded-2xl bg-muted",
TOUCH_GESTURE_CLASS,
disabled ? "opacity-60" : dragging ? "cursor-grabbing" : "cursor-ew-resize",
className,
)}
>
<div className="absolute inset-0">{after}</div>
<motion.div className="absolute inset-0" style={{ clipPath }}>
{before}
</motion.div>
{showLabels ? (
<div aria-hidden="true">
<motion.span className={cn(chip, "left-3")} style={{ opacity: beforeOpacity }}>
{t("before")}
</motion.span>
<motion.span className={cn(chip, "right-3")} style={{ opacity: afterOpacity }}>
{t("after")}
</motion.span>
</div>
) : null}
{/* The stroke stays its pixel width however the box is stretched. */}
<svg
aria-hidden="true"
viewBox="0 0 100 100"
preserveAspectRatio="none"
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
>
<motion.path
d={lineD}
fill="none"
stroke="rgb(0 0 0 / 0.14)"
strokeLinecap="square"
vectorEffect="non-scaling-stroke"
initial={false}
animate={{ strokeWidth: dragging && !reduce ? 5.5 : 4 }}
transition={SPRING_PRESS}
/>
<motion.path
d={lineD}
fill="none"
stroke="white"
strokeLinecap="square"
vectorEffect="non-scaling-stroke"
initial={false}
animate={{ strokeWidth: dragging && !reduce ? 3.5 : 2 }}
transition={SPRING_PRESS}
/>
</svg>
{/* Full width so a percent translate moves the handle by a percent of the frame. */}
<motion.div className="pointer-events-none absolute inset-0" style={{ x: dividerX }}>
<div className="absolute top-1/2 left-0 -translate-x-1/2 -translate-y-1/2">
<motion.div
ref={handleRef}
role="slider"
tabIndex={disabled ? -1 : 0}
aria-label={t("label")}
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(current)}
aria-valuetext={t("valueText", { value: Math.round(current) })}
aria-orientation="horizontal"
aria-disabled={disabled || undefined}
onKeyDown={onKeyDown}
onPointerEnter={(event) => {
if (hoverCapable && event.pointerType !== "touch") setHovered(true);
}}
onPointerLeave={() => setHovered(false)}
animate={{ scale: reduce ? 1 : dragging ? 1.08 : hovered ? 1.04 : 1 }}
transition={SPRING_PRESS}
className="pointer-events-auto flex size-10 items-center justify-center rounded-full bg-white text-neutral-900 shadow-[0_0_0_1px_rgb(0_0_0/0.12)] outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-black/20"
>
{/* The chevrons part as the handle is hovered and grabbed. */}
{([ChevronLeft, ChevronRight] as const).map((Icon, index) => {
const split = reduce
? 0
: CHEVRON_SPLIT[dragging ? "drag" : hovered ? "hover" : "rest"];
return (
<motion.span
key={index === 0 ? "left" : "right"}
aria-hidden="true"
animate={{ x: index === 0 ? -split : split }}
transition={SPRING_PRESS}
className={cn("flex", index === 0 ? "-mr-1" : "-ml-1")}
>
<Icon className="size-3.5" strokeWidth={2.5} />
</motion.span>
);
})}
</motion.div>
</div>
</motion.div>
</div>
);
}
API Reference
beforeReactNodeShown on the left of the divider, usually an image.
—afterReactNodeShown on the right of the divider, usually an image of the same size.
—value?numberDivider position as a percent of the width, 0 to 100 (controlled).
—defaultValue?numberStarting position when uncontrolled.
50onValueChange?((value: number) => void)—showLabels?booleanShow the Before and After labels in the top corners.
truehint?booleanSweep the divider once when the slider first scrolls into view, to show it moves.
truedisabled?booleanfalselabels?Partial<{ label: string; before: string; after: string; valueText: string; }>—className?stringSize the frame here; it defaults to a 3:2 box at full width.
—Related components
Cylinder Carousel
A carousel whose items line the inside of a cylinder, receding into the center and growing toward the edges. Drag, scroll or arrow-key to roll it, with a springy glide and snap. Reduced-motion drops the glide.
Arc Picker
Radial text picker with top, bottom, left or right curves, smooth wheel movement and live selection on each scroll step. One persistent bracket frame grows and squeezes around each label. Supports drag momentum, keyboard input, disabled choices, controlled or uncontrolled values, forms and reduced motion.
Range Slider
Slider with tick dots and a vertical-bar thumb that bounces as it lands on each step. Drag or keyboard, reduced-motion safe.
Updated