"use client";
import { Sun, Thermometer } from "lucide-react";
import { motion, useMotionValue, useReducedMotion, useSpring, useTransform, useVelocity } from "motion/react";
import { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from "react";
import { cn } from "@/lib/utils";
type SpringSliderProps = {
value?: number;
defaultValue?: number;
onChange?: (value: number) => void;
min?: number;
max?: number;
step?: number;
/** "always" keeps the bubble visible; "active" shows it on hover, focus and drag. */
tooltip?: "always" | "active";
format?: (value: number) => string;
label: string;
className?: string;
};
export function SpringSlider({
value,
defaultValue = 50,
onChange,
min = 0,
max = 100,
step = 1,
tooltip = "active",
format = String,
label,
className,
}: SpringSliderProps) {
const [inner, setInner] = useState(defaultValue);
const [dragging, setDragging] = useState(false);
const [hot, setHot] = useState(false);
const track = useRef<HTMLDivElement>(null);
const thumb = useRef<HTMLDivElement>(null);
const reduce = useReducedMotion();
const current = value ?? inner;
const pct = ((current - min) / (max - min)) * 100;
// The bubble chases this target on a spring; its velocity drives the lean.
const target = useMotionValue(pct);
const trailing = useSpring(target, { stiffness: 170, damping: 13, mass: 0.7 });
const source = reduce ? target : trailing;
const left = useTransform(source, (v) => `${v}%`);
const velocity = useVelocity(trailing);
const rotate = useTransform(velocity, [-260, 0, 260], reduce ? [0, 0, 0] : [26, 0, -26]);
useEffect(() => {
target.set(pct);
}, [pct, target]);
function set(next: number) {
const snapped = Math.min(max, Math.max(min, Math.round((next - min) / step) * step + min));
if (snapped === current) return;
if (value === undefined) setInner(snapped);
onChange?.(snapped);
}
function fromPointer(e: PointerEvent<HTMLDivElement>) {
const rect = track.current?.getBoundingClientRect();
if (rect) set(min + ((e.clientX - rect.left) / rect.width) * (max - min));
}
function onKeyDown(e: KeyboardEvent<HTMLDivElement>) {
const big = Math.max(step, (max - min) / 10);
const moves: Record<string, number> = {
ArrowRight: current + step,
ArrowUp: current + step,
ArrowLeft: current - step,
ArrowDown: current - step,
PageUp: current + big,
PageDown: current - big,
Home: min,
End: max,
};
if (e.key in moves) {
e.preventDefault();
set(moves[e.key]);
}
}
const visible = tooltip === "always" || dragging || hot;
return (
<div
className={cn("relative flex h-6 w-full cursor-pointer touch-none items-center select-none", className)}
onPointerDown={(e) => {
e.currentTarget.setPointerCapture(e.pointerId);
setDragging(true);
fromPointer(e);
thumb.current?.focus();
}}
onPointerMove={(e) => dragging && fromPointer(e)}
onPointerUp={() => setDragging(false)}
onPointerCancel={() => setDragging(false)}
onPointerEnter={() => setHot(true)}
onPointerLeave={() => setHot(false)}
>
<div ref={track} className="relative h-1.5 w-full rounded-full bg-muted">
<div className="absolute inset-y-0 left-0 rounded-full bg-primary" style={{ width: `${pct}%` }} />
<motion.div
aria-hidden
style={{ left, rotate, x: "-50%", transformOrigin: "50% 140%" }}
animate={{ opacity: visible ? 1 : 0, scale: visible ? 1 : 0.6, y: visible ? 0 : 6 }}
transition={{ type: "spring", stiffness: 420, damping: 26 }}
className="pointer-events-none absolute bottom-full mb-4 rounded-lg bg-foreground px-2 py-1 text-xs font-semibold whitespace-nowrap text-background tabular-nums shadow-lg"
>
{format(current)}
<span className="absolute top-full left-1/2 -mt-1 size-2 -translate-x-1/2 rotate-45 rounded-[2px] bg-foreground" />
</motion.div>
<div className="absolute top-1/2" style={{ left: `${pct}%` }}>
<motion.div
ref={thumb}
role="slider"
tabIndex={0}
aria-label={label}
aria-valuemin={min}
aria-valuemax={max}
aria-valuenow={current}
aria-valuetext={format(current)}
onKeyDown={onKeyDown}
onFocus={() => setHot(true)}
onBlur={() => setHot(false)}
animate={{ scale: dragging ? 1.25 : 1 }}
transition={{ type: "spring", stiffness: 500, damping: 22 }}
className="-mt-2.5 -ml-2.5 size-5 rounded-full border-2 border-primary bg-background shadow-md outline-offset-4"
/>
</div>
</div>
</div>
);
}
export default function SliderSpringThumbDemo() {
const [brightness, setBrightness] = useState(64);
const [warmth, setWarmth] = useState(4200);
return (
<div className="w-full max-w-md rounded-2xl border border-border bg-card p-6 shadow-sm">
<h3 className="text-lg font-semibold tracking-tight text-card-foreground">Display</h3>
<p className="mt-1 text-sm text-muted-foreground">Drag quickly and watch the label catch up.</p>
<div className="mt-12 flex items-center gap-3">
<Sun className="size-4 shrink-0 text-muted-foreground" />
<SpringSlider label="Brightness" tooltip="always" value={brightness} onChange={setBrightness} format={(v) => `${v}%`} />
</div>
<div className="mt-2 flex justify-between pl-7 text-xs text-muted-foreground">
<span>Brightness</span>
<span>Auto-dims after 2 min</span>
</div>
<div className="mt-12 flex items-center gap-3">
<Thermometer className="size-4 shrink-0 text-muted-foreground" />
<SpringSlider
label="Colour temperature"
tooltip="always"
min={2700}
max={6500}
step={100}
value={warmth}
onChange={setWarmth}
format={(v) => `${v.toLocaleString("en-US")} K`}
/>
</div>
<div className="mt-2 flex justify-between pl-7 text-xs text-muted-foreground">
<span>Warm</span>
<span>Cool</span>
</div>
</div>
);
}