{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scrub-field",
  "type": "registry:component",
  "title": "Scrub Field",
  "description": "A numeric input you drag sideways to change, like the fields in design tools. Typing and arrow keys still work.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "input",
    "number",
    "drag",
    "scrub",
    "form"
  ],
  "docs": "https://hyperblur.dev/effects/scrub-field",
  "files": [
    {
      "path": "registry/hyperblur/scrub-field.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/scrub-field.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface ScrubFieldProps\n  extends Omit<\n    React.InputHTMLAttributes<HTMLInputElement>,\n    \"value\" | \"defaultValue\" | \"onChange\" | \"min\" | \"max\" | \"step\" | \"type\" | \"size\"\n  > {\n  value?: number;\n  defaultValue?: number;\n  onChange?: (value: number) => void;\n  min?: number;\n  max?: number;\n  step?: number;\n  sensitivity?: number;\n  label?: string;\n  suffix?: string;\n}\n\nconst css = `\n.hb-scf {\n  display: inline-flex;\n  align-items: stretch;\n  height: 36px;\n  box-sizing: border-box;\n  border-radius: 8px;\n  border: 1px solid rgba(255, 255, 255, .16);\n  background: rgba(255, 255, 255, .04);\n  color: #f5f5f5;\n  font: 500 14px/1 system-ui, -apple-system, \"Segoe UI\", sans-serif;\n  transition: border-color .15s ease, background-color .15s ease;\n}\n.hb-scf:hover { background: rgba(255, 255, 255, .07); }\n.hb-scf:focus-within,\n.hb-scf[data-dragging=\"true\"] {\n  border-color: #2f7bff;\n  background: rgba(47, 123, 255, .1);\n}\n.hb-scf:focus-within { box-shadow: 0 0 0 3px rgba(47, 123, 255, .28); }\n.hb-scf-l {\n  display: flex;\n  align-items: center;\n  padding: 0 10px;\n  border-right: 1px solid rgba(255, 255, 255, .12);\n  color: rgba(255, 255, 255, .55);\n  font-size: 11px;\n  font-weight: 600;\n  letter-spacing: .08em;\n  text-transform: uppercase;\n  cursor: ew-resize;\n  user-select: none;\n  -webkit-user-select: none;\n  touch-action: none;\n  transition: color .15s ease;\n}\n.hb-scf-l:hover,\n.hb-scf[data-dragging=\"true\"] .hb-scf-l { color: #fff; }\n.hb-scf-i {\n  width: 4.5em;\n  min-width: 0;\n  padding: 0 10px;\n  border: 0;\n  outline: 0;\n  background: transparent;\n  color: inherit;\n  font: inherit;\n  font-variant-numeric: tabular-nums;\n  appearance: textfield;\n  -moz-appearance: textfield;\n}\n.hb-scf-i::-webkit-inner-spin-button,\n.hb-scf-i::-webkit-outer-spin-button {\n  appearance: none;\n  -webkit-appearance: none;\n  margin: 0;\n}\n.hb-scf-s {\n  display: flex;\n  align-items: center;\n  padding: 0 10px 0 0;\n  color: rgba(255, 255, 255, .45);\n  font-size: 12px;\n  pointer-events: none;\n  user-select: none;\n  -webkit-user-select: none;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-scf, .hb-scf-l { transition: none; }\n}\n`;\n\nfunction places(n: number) {\n  const s = String(n);\n  const i = s.indexOf(\".\");\n  return i < 0 ? 0 : s.length - i - 1;\n}\n\nexport function ScrubField({\n  value,\n  defaultValue = 0,\n  onChange,\n  min,\n  max,\n  step = 1,\n  sensitivity = 0.5,\n  label = \"Value\",\n  suffix = \"\",\n  id: idProp,\n  className = \"\",\n  style,\n  onBlur,\n  onKeyDown,\n  ...rest\n}: ScrubFieldProps) {\n  const autoId = React.useId();\n  const id = idProp ?? autoId;\n  const suffixId = `${autoId}-s`;\n  const [inner, setInner] = React.useState(defaultValue);\n  const [draft, setDraft] = React.useState<string | null>(null);\n  const [dragging, setDragging] = React.useState(false);\n  const drag = React.useRef({ active: false, moved: false, x: 0, raw: 0 });\n  const last = React.useRef(defaultValue);\n  const current = value ?? inner;\n  const unit = step > 0 ? step : 1;\n\n  function clamp(n: number) {\n    let v = n;\n    if (min !== undefined) v = Math.max(min, v);\n    if (max !== undefined) v = Math.min(max, v);\n    return v;\n  }\n\n  function snap(n: number) {\n    const base = min ?? 0;\n    const v = Math.round((n - base) / unit) * unit + base;\n    return Number(v.toFixed(Math.max(places(unit), places(base))));\n  }\n\n  function commit(n: number) {\n    if (n === last.current) return;\n    last.current = n;\n    setInner(n);\n    onChange?.(n);\n  }\n\n  function handleInput(e: React.ChangeEvent<HTMLInputElement>) {\n    const raw = e.target.value;\n    setDraft(raw);\n    const n = Number(raw);\n    if (raw !== \"\" && Number.isFinite(n)) commit(n);\n  }\n\n  function settle() {\n    last.current = current;\n    commit(clamp(current));\n    setDraft(null);\n  }\n\n  function handleBlur(e: React.FocusEvent<HTMLInputElement>) {\n    onBlur?.(e);\n    settle();\n  }\n\n  function handleKey(e: React.KeyboardEvent<HTMLInputElement>) {\n    onKeyDown?.(e);\n    if (e.defaultPrevented) return;\n    if (e.key === \"Enter\") {\n      settle();\n      return;\n    }\n    if (e.key !== \"ArrowUp\" && e.key !== \"ArrowDown\") return;\n    e.preventDefault();\n    const dir = e.key === \"ArrowUp\" ? 1 : -1;\n    last.current = current;\n    commit(clamp(snap(current + dir * unit * (e.shiftKey ? 10 : 1))));\n    setDraft(null);\n  }\n\n  function handleDown(e: React.PointerEvent<HTMLLabelElement>) {\n    if (e.button !== 0) return;\n    e.currentTarget.setPointerCapture(e.pointerId);\n    drag.current = { active: true, moved: false, x: e.clientX, raw: current };\n    last.current = current;\n    setDragging(true);\n  }\n\n  function handleMove(e: React.PointerEvent<HTMLLabelElement>) {\n    const d = drag.current;\n    if (!d.active) return;\n    const dx = e.clientX - d.x;\n    d.x = e.clientX;\n    if (dx === 0) return;\n    d.moved = true;\n    d.raw = clamp(d.raw + dx * sensitivity * (e.shiftKey ? 10 : 1));\n    commit(clamp(snap(d.raw)));\n    setDraft(null);\n  }\n\n  function handleEnd(e: React.PointerEvent<HTMLLabelElement>) {\n    if (!drag.current.active) return;\n    drag.current.active = false;\n    if (e.currentTarget.hasPointerCapture(e.pointerId)) {\n      e.currentTarget.releasePointerCapture(e.pointerId);\n    }\n    setDragging(false);\n  }\n\n  function handleLabelClick(e: React.MouseEvent<HTMLLabelElement>) {\n    if (drag.current.moved) e.preventDefault();\n  }\n\n  return (\n    <div className={`hb-scf ${className}`} style={style} data-dragging={dragging}>\n      <style href=\"hb-scrub-field\" precedence=\"default\">{css}</style>\n      {label && (\n        <label\n          htmlFor={id}\n          className=\"hb-scf-l\"\n          onPointerDown={handleDown}\n          onPointerMove={handleMove}\n          onPointerUp={handleEnd}\n          onPointerCancel={handleEnd}\n          onLostPointerCapture={handleEnd}\n          onClick={handleLabelClick}\n        >\n          {label}\n        </label>\n      )}\n      <input\n        aria-describedby={suffix ? suffixId : undefined}\n        {...rest}\n        id={id}\n        className=\"hb-scf-i\"\n        type=\"number\"\n        inputMode=\"decimal\"\n        min={min}\n        max={max}\n        step={unit}\n        value={draft ?? String(current)}\n        onChange={handleInput}\n        onBlur={handleBlur}\n        onKeyDown={handleKey}\n      />\n      {suffix && (\n        <span id={suffixId} className=\"hb-scf-s\">\n          {suffix}\n        </span>\n      )}\n    </div>\n  );\n}\n"
    }
  ]
}