{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ruler-input","type":"registry:ui","title":"Ruler Input","description":"Design-tool number field: drag sideways to scrub the value over a sliding tick ruler, Shift for coarse, Alt for fine, and it stretches like rubber at the limits. Click to type, arrows to step.","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/ui/ruler-input.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useId, useRef, useState, type KeyboardEvent, type PointerEvent } from \"react\";\nimport { AnimatePresence, animate, motion, useMotionValue, useTransform } from \"motion/react\";\n\nexport interface RulerInputProps {\n  label?: string;\n  /** Controlled value. */\n  value?: number;\n  /** Initial value when uncontrolled. */\n  defaultValue?: number;\n  min?: number;\n  max?: number;\n  step?: number;\n  /** Suffix shown after the number. */\n  unit?: string;\n  /** Pointer px per step while scrubbing. */\n  pixelsPerStep?: number;\n  onChange?: (value: number) => void;\n  className?: string;\n}\n\nconst decimals = (n: number) => (String(n).split(\".\")[1] ?? \"\").length;\n// Soft limit: the further you pull past the end, the less it gives.\nconst rubber = (px: number) => Math.sign(px) * 28 * (1 - Math.exp(-Math.abs(px) / 90));\n\nexport function RulerInput({\n  label = \"Opacity\",\n  value,\n  defaultValue = 64,\n  min = 0,\n  max = 100,\n  step = 1,\n  unit = \"%\",\n  pixelsPerStep = 4,\n  onChange,\n  className,\n}: RulerInputProps) {\n  const id = useId();\n  const input = useRef<HTMLInputElement>(null);\n  const scrub = useRef<{ lastX: number; raw: number; moved: boolean } | null>(null);\n  const [inner, setInner] = useState(defaultValue);\n  const [draft, setDraft] = useState<string | null>(null);\n  const [mode, setMode] = useState<\"fine\" | \"coarse\" | null>(null);\n  const [active, setActive] = useState(false);\n  const current = value ?? inner;\n  const dec = decimals(step);\n\n  const over = useMotionValue(0);\n  const scaleX = useTransform(over, (o) => 1 + Math.abs(o) / 260);\n  const originX = useTransform(over, (o) => (o > 0 ? 0 : 1));\n  const x = useTransform(over, (o) => o * 0.25);\n\n  const commit = (v: number, precision = dec) => {\n    const next = Number(Math.min(max, Math.max(min, v)).toFixed(precision));\n    if (next === current) return;\n    if (value === undefined) setInner(next);\n    onChange?.(next);\n  };\n  const mult = (e: { shiftKey: boolean; altKey: boolean }) => (e.shiftKey ? 10 : e.altKey ? 0.1 : 1);\n\n  const onDown = (e: PointerEvent<HTMLDivElement>) => {\n    if (e.button !== 0 || (e.target === input.current && document.activeElement === input.current)) return;\n    e.preventDefault(); // don't focus the input yet: this might be a scrub\n    e.currentTarget.setPointerCapture(e.pointerId);\n    scrub.current = { lastX: e.clientX, raw: current, moved: false };\n  };\n  const onMove = (e: PointerEvent<HTMLDivElement>) => {\n    const s = scrub.current;\n    if (!s) return;\n    const dx = e.clientX - s.lastX;\n    if (!s.moved && Math.abs(dx) < 3) return;\n    if (!s.moved) {\n      s.moved = true;\n      setActive(true);\n    }\n    s.lastX = e.clientX;\n    const m = mult(e);\n    const m2 = m === 10 ? \"coarse\" : m === 1 ? null : \"fine\";\n    if (m2 !== mode) setMode(m2);\n    const give = (60 / pixelsPerStep) * step; // how far past the ends the pull can stretch\n    s.raw = Math.min(max + give, Math.max(min - give, s.raw + (dx / pixelsPerStep) * step * m));\n    const snapped = Math.round(s.raw / (step * Math.min(m, 1))) * step * Math.min(m, 1);\n    commit(snapped, m < 1 ? dec + 1 : dec);\n    const past = s.raw > max ? s.raw - max : s.raw < min ? s.raw - min : 0;\n    over.set(rubber((past / step) * pixelsPerStep));\n  };\n  const onUp = () => {\n    const s = scrub.current;\n    scrub.current = null;\n    setActive(false);\n    setMode(null);\n    animate(over, 0, { type: \"spring\", stiffness: 600, damping: 14 });\n    if (s && !s.moved) {\n      input.current?.focus();\n      input.current?.select();\n    }\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === \"ArrowUp\" || e.key === \"ArrowDown\") {\n      e.preventDefault();\n      const m = mult(e);\n      commit(current + (e.key === \"ArrowUp\" ? 1 : -1) * step * m, m < 1 ? dec + 1 : dec);\n      setDraft(null);\n    } else if (e.key === \"Home\" || e.key === \"End\") {\n      e.preventDefault();\n      commit(e.key === \"Home\" ? min : max);\n      setDraft(null);\n    } else if (e.key === \"Enter\") {\n      finishDraft();\n      input.current?.select();\n    } else if (e.key === \"Escape\") {\n      setDraft(null);\n      input.current?.blur();\n    }\n  };\n  const finishDraft = () => {\n    if (draft !== null) {\n      const n = parseFloat(draft);\n      if (Number.isFinite(n)) commit(n, dec + 1);\n    }\n    setDraft(null);\n  };\n\n  const shown = String(Number(current.toFixed(dec + 1)));\n  const pct = ((current - min) / (max - min || 1)) * 100;\n\n  return (\n    <div className={`w-64 ${className ?? \"\"}`}>\n      <motion.div\n        onPointerDown={onDown}\n        onPointerMove={onMove}\n        onPointerUp={onUp}\n        onPointerCancel={onUp}\n        onLostPointerCapture={() => scrub.current && onUp()}\n        style={{ scaleX, originX, x }}\n        className={`group relative flex h-11 cursor-ew-resize touch-none select-none items-center overflow-hidden rounded-xl border bg-card shadow-sm transition-[border-color,box-shadow] duration-200 focus-within:border-ring focus-within:ring-[3px] focus-within:ring-ring/40 ${\n          active ? \"border-ring\" : \"border-border hover:border-muted-foreground/50\"\n        }`}\n      >\n        <span aria-hidden className=\"absolute inset-y-0 left-0 bg-primary/12 transition-[width] duration-75\" style={{ width: `${pct}%` }} />\n        <span\n          aria-hidden\n          className={`absolute inset-x-0 bottom-0 h-2 text-muted-foreground transition-opacity duration-200 ${active ? \"opacity-70\" : \"opacity-25 group-hover:opacity-45\"}`}\n          style={{\n            backgroundImage: \"repeating-linear-gradient(90deg, currentColor 0 1px, transparent 1px 8px)\",\n            backgroundPositionX: -((current - min) / step) * pixelsPerStep,\n            maskImage: \"linear-gradient(90deg, transparent, #000 25%, #000 75%, transparent)\",\n          }}\n        />\n        <label htmlFor={id} className=\"relative cursor-ew-resize pl-3.5 text-sm text-muted-foreground\">\n          {label}\n        </label>\n        <input\n          ref={input}\n          id={id}\n          type=\"text\"\n          inputMode=\"decimal\"\n          role=\"spinbutton\"\n          aria-valuenow={current}\n          aria-valuemin={min}\n          aria-valuemax={max}\n          aria-valuetext={`${shown}${unit}`}\n          value={draft ?? shown}\n          onChange={(e) => setDraft(e.target.value)}\n          onKeyDown={onKey}\n          onBlur={finishDraft}\n          style={{ outline: \"none\" }} // the field shows the focus ring\n          className=\"relative min-w-0 flex-1 cursor-ew-resize bg-transparent text-right text-sm font-semibold tabular-nums text-foreground outline-none focus:cursor-text\"\n        />\n        <span aria-hidden className=\"relative pr-3.5 pl-0.5 text-sm text-muted-foreground\">\n          {unit}\n        </span>\n      </motion.div>\n      <div aria-hidden className=\"relative mt-2 flex h-5 justify-center\">\n        <AnimatePresence>\n          {mode && (\n            <motion.span\n              key={mode}\n              initial={{ opacity: 0, y: -6, scale: 0.8 }}\n              animate={{ opacity: 1, y: 0, scale: 1 }}\n              exit={{ opacity: 0, y: -6, scale: 0.8 }}\n              transition={{ type: \"spring\", stiffness: 600, damping: 22 }}\n              className=\"absolute rounded-full bg-muted px-2 py-0.5 text-[11px] font-medium text-foreground\"\n            >\n              {mode === \"fine\" ? \"Fine ×0.1\" : \"Coarse ×10\"}\n            </motion.span>\n          )}\n        </AnimatePresence>\n      </div>\n    </div>\n  );\n}\n"}]}