{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "detent-slider",
  "title": "Detent Slider",
  "description": "Drags freely, then springs into the nearest step with overshoot.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://spotcheckui.com/r/spotcheck-base.json"
  ],
  "files": [
    {
      "path": "registry/spotcheck/components/spotcheck/detent-slider.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { animate, motion, useMotionValue, useReducedMotion, useTransform } from \"motion/react\"\n\nimport { cn } from \"@/lib/utils\"\n\nconst RELEASE_SPRING = { stiffness: 380, damping: 12 }\nconst PRESS_SPRING = { stiffness: 900, damping: 40 }\nconst THUMB_SPRING = { stiffness: 500, damping: 18 }\nconst MAX_TICKS = 41\n\nexport type DetentZone = { from: number; to: number; label: string }\n\ntype DetentSliderProps = {\n  /** Names the control for screen readers and prints above the value. */\n  label: string\n  /** The lowest value. */\n  min?: number\n  /** The highest value. */\n  max?: number\n  /** Distance between detents. */\n  step?: number\n  /** The current value, when you control the slider. */\n  value?: number\n  /** The starting value, when the slider manages itself. */\n  defaultValue?: number\n  /** Runs when the slider lands on a new step, not on every pointer move. */\n  onValueChange?: (value: number) => void\n  /** Named ranges, such as Deterministic, Balanced and Creative. */\n  zones?: DetentZone[]\n  /** Values to print under the track. Defaults to the two ends. */\n  marks?: number[]\n  /** Formats the readout and marks. Pass it from a client component. */\n  format?: (value: number) => string\n  /** The spring used when landing on a step. */\n  spring?: { stiffness: number; damping: number }\n  /** Added to the outer element. */\n  className?: string\n}\n\nfunction decimalsOf(step: number) {\n  return (String(step).split(\".\")[1] ?? \"\").length\n}\n\nexport function snapToStep(value: number, min: number, max: number, step: number) {\n  const steps = Math.round((Math.min(max, Math.max(min, value)) - min) / step)\n  return Math.min(max, Number((min + steps * step).toFixed(decimalsOf(step))))\n}\n\nexport function findZone(zones: DetentZone[], value: number) {\n  const distance = (zone: DetentZone) => Math.max(zone.from - value, value - zone.to, 0)\n  return zones.reduce<DetentZone | undefined>(\n    (best, zone) => (!best || distance(zone) < distance(best) ? zone : best),\n    undefined,\n  )\n}\n\nexport function DetentSlider({\n  label,\n  min = 0,\n  max = 100,\n  step = 1,\n  value,\n  defaultValue,\n  onValueChange,\n  zones = [],\n  marks = [min, max],\n  format = (v) => v.toFixed(decimalsOf(step)),\n  spring = RELEASE_SPRING,\n  className,\n}: DetentSliderProps) {\n  const reduced = useReducedMotion()\n  const [internal, setInternal] = React.useState(defaultValue ?? min)\n  const current = value ?? internal\n  const [drag, setDrag] = React.useState<number | null>(null)\n  const shown = drag ?? current\n\n  const track = React.useRef<HTMLDivElement>(null)\n  const fractionOf = (v: number) => (v - min) / (max - min)\n  const progress = useMotionValue(fractionOf(current))\n  const thumbLeft = useTransform(progress, (p) => `${p * 100}%`)\n\n  const moveTo = React.useCallback(\n    (fraction: number, options: { stiffness: number; damping: number } | null) => {\n      if (!options || reduced) progress.jump(fraction)\n      else animate(progress, fraction, { type: \"spring\", ...options })\n    },\n    [progress, reduced],\n  )\n\n  const commit = (next: number) => {\n    setInternal(next)\n    if (next !== current) onValueChange?.(next)\n    moveTo(fractionOf(next), spring)\n  }\n\n  // Follows a controlled value that changes from outside the slider.\n  const settled = React.useRef(current)\n  React.useEffect(() => {\n    if (settled.current !== current && drag === null) moveTo((current - min) / (max - min), spring)\n    settled.current = current\n  }, [current, drag, max, min, moveTo, spring])\n\n  const fromPointer = (clientX: number) => {\n    const rect = track.current?.getBoundingClientRect()\n    if (!rect || rect.width === 0) return min\n    const fraction = Math.min(1, Math.max(0, (clientX - rect.left) / rect.width))\n    return min + fraction * (max - min)\n  }\n\n  const onPointerDown = (event: React.PointerEvent<HTMLDivElement>) => {\n    event.currentTarget.setPointerCapture(event.pointerId)\n    const next = fromPointer(event.clientX)\n    setDrag(next)\n    moveTo(fractionOf(next), PRESS_SPRING)\n  }\n\n  const onPointerMove = (event: React.PointerEvent<HTMLDivElement>) => {\n    if (drag === null) return\n    const next = fromPointer(event.clientX)\n    setDrag(next)\n    moveTo(fractionOf(next), null)\n  }\n\n  const onPointerEnd = () => {\n    if (drag === null) return\n    setDrag(null)\n    commit(snapToStep(drag, min, max, step))\n  }\n\n  const onKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {\n    const moves: Record<string, number> = {\n      ArrowRight: current + step,\n      ArrowUp: current + step,\n      ArrowLeft: current - step,\n      ArrowDown: current - step,\n      Home: min,\n      End: max,\n    }\n    const next = moves[event.key]\n    if (next === undefined) return\n    event.preventDefault()\n    commit(snapToStep(next, min, max, step))\n  }\n\n  const count = Math.round((max - min) / step) + 1\n  const shownZone = findZone(zones, shown)\n  const currentZone = findZone(zones, current)\n  const reached = Math.round((shown - min) / step)\n\n  return (\n    <div className={cn(\"flex flex-col gap-3.5 font-(family-name:--f-body)\", className)}>\n      <div className=\"flex items-end justify-between gap-4\">\n        <div>\n          <div className=\"spotcheck-label\">{label}</div>\n          <div className=\"spotcheck-display text-[64px] leading-[.9] tabular-nums\">{format(shown)}</div>\n        </div>\n        {shownZone && (\n          <div className=\"border-(length:--rule) border-ink px-2 py-[3px] font-(family-name:--f-mono) text-[11px] tracking-[.12em] uppercase\">\n            {shownZone.label}\n          </div>\n        )}\n      </div>\n\n      <div\n        ref={track}\n        role=\"slider\"\n        tabIndex={0}\n        aria-label={label}\n        aria-valuemin={min}\n        aria-valuemax={max}\n        aria-valuenow={current}\n        aria-valuetext={currentZone ? `${format(current)}, ${currentZone.label}` : format(current)}\n        className=\"group relative mx-3 mt-2 h-14 cursor-pointer touch-pan-y outline-none\"\n        onPointerDown={onPointerDown}\n        onPointerMove={onPointerMove}\n        onPointerUp={onPointerEnd}\n        onPointerCancel={onPointerEnd}\n        onKeyDown={onKeyDown}\n      >\n        <div className=\"bg-ink absolute inset-x-0 top-[27px] h-0.5\" />\n        <motion.div\n          data-part=\"fill\"\n          className=\"bg-accent-ink absolute inset-x-0 top-6 h-2 origin-left\"\n          style={{ scaleX: progress }}\n        />\n        {count <= MAX_TICKS &&\n          Array.from({ length: count }, (_, tick) => (\n            <i\n              key={tick}\n              className={cn(\n                \"absolute top-3.5 -ml-px h-7 w-0.5\",\n                tick <= reached ? \"bg-ink\" : \"bg-[color-mix(in_srgb,var(--ink)_30%,transparent)]\",\n              )}\n              style={{ left: `${(tick / (count - 1)) * 100}%` }}\n            />\n          ))}\n        <motion.div className=\"pointer-events-none absolute inset-y-0 w-0\" style={{ left: thumbLeft }}>\n          <motion.span\n            data-part=\"thumb\"\n            className=\"bg-ink border-paper group-focus-visible:bg-accent-ink absolute top-3.5 -left-3.5 block size-7 border-(length:--rule) shadow-[0_0_0_var(--rule)_var(--ink)] group-focus-visible:outline-2 group-focus-visible:outline-offset-4 group-focus-visible:outline-(--accent-ink)\"\n            animate={drag === null ? { scale: 1, rotate: 0 } : { scale: 1.25, rotate: 45 }}\n            transition={reduced ? { duration: 0 } : { type: \"spring\", ...THUMB_SPRING }}\n          />\n        </motion.div>\n      </div>\n\n      <div className=\"relative mx-3 h-4 font-(family-name:--f-mono) text-[10.5px] text-mute tabular-nums\">\n        {marks.map((mark) => (\n          <span\n            key={mark}\n            className=\"absolute -translate-x-1/2 whitespace-nowrap\"\n            style={{ left: `${fractionOf(mark) * 100}%` }}\n          >\n            {format(mark)}\n          </span>\n        ))}\n      </div>\n    </div>\n  )\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "springs"
  ],
  "type": "registry:component"
}