{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "number-field",
  "title": "Number field",
  "description": "scrub the label (pointer lock), type, step or drag the track · Shift fine, Alt coarse · off-default mark and Reset",
  "dependencies": [
    "cn@^0.5.2",
    "lucide-react@^1.52.0"
  ],
  "registryDependencies": [
    "@halftone-web/control-row",
    "@halftone-web/kit-setup",
    "@halftone-web/lib-controls-scrub"
  ],
  "files": [
    {
      "path": "components/controls/number-field.tsx",
      "content": "'use client';\nimport { cn } from 'cn';\nimport { MinusIcon, PlusIcon } from 'lucide-react';\nimport * as React from 'react';\nimport { ControlRow } from '@/components/controls/control-row';\nimport { clamp, createScrub, formatNumber, keyAction, modifierOf, nudge, parseNumber, roundTo, startDragLock, stepFor, type Range } from '@/lib/controls/scrub';\n\ntype Props = {\n  label: string;\n  value: number;\n  onChange: (value: number) => void;\n  min?: number;\n  max?: number;\n  step?: number;\n  /** Shown after the value: `px`, `ms`, `%`. */\n  unit?: string;\n  /** The value Reset goes back to. With it, the field marks itself while it's off it. */\n  defaultValue?: number;\n  /** Off its default, when the caller decides that (a system's own edits) rather than `defaultValue`. */\n  edited?: boolean;\n  /** Reset; defaults to `onChange(defaultValue)`. */\n  onReset?: () => void;\n  /** How the value reads (\"Full\" for a pill's corner). Also its aria-valuetext. */\n  format?: (value: number) => string;\n  /** A track under the row. On by default when there's a min and a max. */\n  track?: boolean;\n  /** − and + buttons beside the value. */\n  stepper?: boolean;\n  hint?: React.ReactNode;\n  id?: string;\n  className?: string;\n};\n\nconst fieldClass =\n  'border-border bg-background focus-visible:ring-ring/50 h-7 min-w-0 rounded-md border px-1.5 text-right text-xs tabular-nums outline-none focus-visible:ring-[3px] aria-invalid:border-destructive';\nconst stepClass =\n  'text-muted-foreground hover:text-foreground hover:bg-accent focus-visible:ring-ring/50 inline-flex size-6 shrink-0 items-center justify-center rounded-md outline-none focus-visible:ring-[3px] disabled:pointer-events-none disabled:opacity-50';\n\n/**\n * A number to tune: type it, step it, drag it, or scrub its label.\n * - **Scrub:** drag the label sideways; the pointer locks, so it keeps going past the screen's edge. A click without a\n *   drag puts you in the field.\n * - **Keys** (in the field or on the track): arrows step, Page Up/Down step by ten, Home/End go to the ends.\n * - **Shift** for fine steps (a tenth), **Alt** for coarse ones (ten), on the scrub and the keys alike.\n * - **Off its default**, a mark shows by the label, the label goes to full strength and a Reset button appears; a\n *   double-click on the label resets too.\n */\nexport function NumberField({\n  label,\n  value,\n  onChange,\n  min,\n  max,\n  step = 1,\n  unit = '',\n  defaultValue,\n  edited,\n  onReset,\n  format,\n  track = min !== undefined && max !== undefined,\n  stepper = true,\n  hint,\n  id,\n  className,\n}: Props) {\n  const ownId = React.useId();\n  const fieldId = id ?? ownId;\n  const range: Range = React.useMemo(() => ({ min, max, step }), [min, max, step]);\n  const [draft, setDraft] = React.useState<string | null>(null);\n  const [dragging, setDragging] = React.useState(false);\n  const stopDrag = React.useRef<(() => void) | null>(null);\n  const inputRef = React.useRef<HTMLInputElement>(null);\n  React.useEffect(() => () => stopDrag.current?.(), []);\n\n  const show = (n: number) => (format ? format(n) : `${formatNumber(n, stepFor(range, 'fine'))}${unit}`);\n  const off = edited ?? (defaultValue !== undefined && value !== defaultValue);\n  const reset = onReset ?? (defaultValue !== undefined ? () => onChange(defaultValue) : undefined);\n  const set = (n: number) => {\n    const next = clamp(roundTo(n, stepFor(range, 'fine')), min, max);\n    if (next !== value) onChange(next);\n  };\n  const parsed = draft === null ? value : parseNumber(draft);\n  const invalid = draft !== null && parsed === null;\n\n  const commit = () => {\n    if (draft !== null && parsed !== null) set(parsed);\n    setDraft(null);\n  };\n\n  const onKey = (e: React.KeyboardEvent<HTMLInputElement>) => {\n    if (e.key === 'Enter') {\n      commit();\n      return;\n    }\n    if (e.key === 'Escape' && draft !== null) {\n      e.preventDefault();\n      setDraft(null);\n      return;\n    }\n    const next = keyAction(e.key, parsed ?? value, modifierOf(e), range);\n    // Home/End in a text field move the caret unless the range has ends.\n    if (next === null) return;\n    e.preventDefault();\n    setDraft(null);\n    set(next);\n  };\n\n  const onScrubStart = (e: React.MouseEvent<HTMLElement>) => {\n    if (e.button !== 0) return;\n    e.preventDefault();\n    const scrub = createScrub(value, range);\n    let last = value;\n    setDragging(true);\n    stopDrag.current = startDragLock(\n      e.currentTarget,\n      e.clientX,\n      (dx, ev) => {\n        const next = scrub.move(dx, modifierOf(ev));\n        if (next !== last) {\n          last = next;\n          onChange(next);\n        }\n      },\n      (moved) => {\n        stopDrag.current = null;\n        setDragging(false);\n        if (!moved) inputRef.current?.focus();\n      }\n    );\n  };\n\n  const shown = draft ?? show(value);\n  const valueText = show(value);\n\n  return (\n    <ControlRow\n      label={label}\n      edited={off}\n      onReset={reset}\n      resetLabel={`Reset ${label.toLowerCase()}${defaultValue !== undefined ? ` to ${show(defaultValue)}` : ''}`}\n      hint={hint}\n      className={className}\n      labelProps={{\n        onMouseDown: onScrubStart,\n        onDoubleClick: reset && off ? reset : undefined,\n        title: 'Drag to change · Shift for fine steps, Alt for coarse',\n        'data-dragging': dragging || undefined,\n        className: cn('cursor-ew-resize touch-none select-none hover:text-foreground', dragging && 'text-foreground'),\n      }}\n      trailing={\n        <div className=\"flex shrink-0 items-center gap-0.5\">\n          <input\n            ref={inputRef}\n            id={fieldId}\n            role=\"spinbutton\"\n            inputMode=\"decimal\"\n            autoComplete=\"off\"\n            spellCheck={false}\n            aria-label={label}\n            aria-valuenow={value}\n            aria-valuemin={min}\n            aria-valuemax={max}\n            aria-valuetext={valueText}\n            aria-invalid={invalid || undefined}\n            value={shown}\n            onFocus={(e) => {\n              setDraft(format ? formatNumber(value, stepFor(range, 'fine')) : show(value));\n              const el = e.currentTarget;\n              requestAnimationFrame(() => el.select());\n            }}\n            onChange={(e) => setDraft(e.target.value)}\n            onBlur={commit}\n            onKeyDown={onKey}\n            className={cn(fieldClass, 'w-16')}\n          />\n          {stepper ? (\n            <>\n              <button type=\"button\" tabIndex={-1} aria-label={`Decrease ${label.toLowerCase()}`} disabled={min !== undefined && value <= min} onClick={(e) => set(nudge(value, -1, modifierOf(e), range))} className={stepClass}>\n                <MinusIcon className=\"size-3.5\" aria-hidden />\n              </button>\n              <button type=\"button\" tabIndex={-1} aria-label={`Increase ${label.toLowerCase()}`} disabled={max !== undefined && value >= max} onClick={(e) => set(nudge(value, 1, modifierOf(e), range))} className={stepClass}>\n                <PlusIcon className=\"size-3.5\" aria-hidden />\n              </button>\n            </>\n          ) : null}\n        </div>\n      }>\n      {track && min !== undefined && max !== undefined ? (\n        <input\n          type=\"range\"\n          min={min}\n          max={max}\n          step=\"any\"\n          value={value}\n          tabIndex={-1}\n          aria-hidden\n          onChange={(e) => set(min + Math.round((Number(e.target.value) - min) / step) * step)}\n          onKeyDown={(e) => {\n            const next = keyAction(e.key, value, modifierOf(e), range);\n            if (next === null) return;\n            e.preventDefault();\n            set(next);\n          }}\n          className=\"accent-foreground h-5 w-full cursor-pointer\"\n        />\n      ) : null}\n    </ControlRow>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/controls/number-field.tsx"
    }
  ],
  "meta": {
    "exports": [
      "NumberField"
    ],
    "api": "<NumberField label value onChange min? max? step? unit? defaultValue? format? track? stepper? />"
  },
  "categories": [
    "controls"
  ],
  "type": "registry:component"
}