{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scrub-slider-base",
  "type": "registry:component",
  "title": "Scrub Slider (base)",
  "description": "Theme-ready base variant of An animated scrub slider that responds smoothly to drag and scrub interactions..",
  "dependencies": [
    "motion"
  ],
  "files": [
    {
      "path": "components/watermelon/scrub-slider.tsx",
      "type": "registry:component",
      "content": "'use client';\n\nimport { useRef, useState, useEffect, useCallback, type FC } from 'react';\nimport {\n  motion,\n  useMotionValue,\n  useSpring,\n  animate,\n  type Transition,\n} from 'motion/react';\n\ninterface ScrubSliderProps {\n  initialValue?: number;\n  tickCount?: number;\n}\n\ninterface AnimatedNumberProps {\n  value: number;\n}\n\nconst SPRING: Transition = {\n  stiffness: 200,\n  damping: 25,\n};\n\nconst AnimatedNumber: FC<AnimatedNumberProps> = ({ value }) => {\n  const [display, setDisplay] = useState(value);\n\n  useEffect(() => {\n    const controls = animate(display, value, {\n      duration: 0.2,\n      onUpdate(latest) {\n        setDisplay(Math.round(latest));\n      },\n    });\n\n    return controls.stop;\n  }, [value, display]);\n\n  return <>{display}</>;\n};\n\nexport const ScrubSlider: FC<ScrubSliderProps> = ({\n  initialValue = 0,\n  tickCount = 32,\n}) => {\n  const sliderRef = useRef<HTMLDivElement>(null);\n\n  const x = useMotionValue(0);\n  const smoothX = useSpring(x, SPRING);\n\n  const [value, setValue] = useState(initialValue);\n  const [isDragging, setIsDragging] = useState(false);\n  const [step, setStep] = useState(0);\n  const [sliderLeft, setSliderLeft] = useState(0);\n  const [sliderWidth, setSliderWidth] = useState(0);\n\n  const padding = 16;\n\n  useEffect(() => {\n    if (!sliderRef.current) return;\n\n    const measure = () => {\n      const rect = sliderRef.current!.getBoundingClientRect();\n\n      const width = rect.width - padding * 2;\n      const newStep = width / (tickCount - 1);\n\n      setSliderLeft(rect.left);\n      setSliderWidth(width);\n      setStep(newStep);\n\n      x.set(initialValue * newStep + padding);\n    };\n\n    measure();\n\n    const resizeObserver = new ResizeObserver(measure);\n    resizeObserver.observe(sliderRef.current);\n\n    return () => resizeObserver.disconnect();\n  }, [tickCount, initialValue, x]);\n\n  const updateValue = useCallback(\n    (clientX: number) => {\n      if (!step) return;\n\n      let posX = clientX - sliderLeft - padding;\n\n      posX = Math.max(0, Math.min(posX, sliderWidth));\n\n      const snappedIndex = Math.round(posX / step);\n      const snappedX = snappedIndex * step;\n\n      setValue(snappedIndex);\n      x.set(snappedX + padding);\n    },\n    [step, sliderLeft, sliderWidth, x],\n  );\n\n  useEffect(() => {\n    const move = (e: MouseEvent) => {\n      if (!isDragging) return;\n      updateValue(e.clientX);\n    };\n\n    const up = () => setIsDragging(false);\n\n    window.addEventListener('mousemove', move);\n    window.addEventListener('mouseup', up);\n\n    return () => {\n      window.removeEventListener('mousemove', move);\n      window.removeEventListener('mouseup', up);\n    };\n  }, [isDragging, updateValue]);\n\n  useEffect(() => {\n    const move = (e: TouchEvent) => {\n      if (!isDragging) return;\n      updateValue(e.touches[0].clientX);\n    };\n\n    const end = () => setIsDragging(false);\n\n    window.addEventListener('touchmove', move);\n    window.addEventListener('touchend', end);\n\n    return () => {\n      window.removeEventListener('touchmove', move);\n      window.removeEventListener('touchend', end);\n    };\n  }, [isDragging, updateValue]);\n\n  return (\n    <div className=\"theme-injected relative w-full max-w-md select-none\">\n      <motion.div\n        style={{ left: smoothX }}\n        className=\"pointer-events-none absolute -top-12 z-30 -translate-x-1/2\"\n      >\n        <motion.div\n          animate={{\n            y: isDragging ? -4 : 0,\n            scale: isDragging ? 1.05 : 1,\n          }}\n          transition={SPRING}\n          className=\"bg-foreground text-background rounded-lg px-3 py-1.5 text-2xl font-semibold shadow-sm\"\n        >\n          <AnimatedNumber value={value} />\n          °C\n        </motion.div>\n      </motion.div>\n\n      <div\n        ref={sliderRef}\n        onMouseDown={(e) => {\n          setIsDragging(true);\n          updateValue(e.clientX);\n        }}\n        onTouchStart={(e) => {\n          setIsDragging(true);\n          updateValue(e.touches[0].clientX);\n        }}\n        className=\"border-border bg-background relative h-24 cursor-pointer touch-none overflow-hidden rounded-lg border shadow-md\"\n      >\n        <div className=\"absolute inset-4\">\n          {Array.from({ length: tickCount }).map((_, i) => (\n            <div\n              key={i}\n              className=\"bg-muted-foreground/50 absolute top-0 bottom-0 w-1 -translate-x-1/2 rounded-lg\"\n              style={{\n                left: i * step,\n              }}\n            />\n          ))}\n        </div>\n\n        <motion.div\n          style={{ left: smoothX }}\n          animate={{\n            scaleY: isDragging ? 1.15 : 1,\n          }}\n          transition={SPRING}\n          className=\"bg-foreground absolute top-4 bottom-4 w-1 -translate-x-1/2 rounded-lg\"\n        />\n      </div>\n    </div>\n  );\n};\n"
    }
  ]
}
