{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "number-input",
  "title": "Number field",
  "description": "A number field with a stepper on each side, whose digits roll up or down when the value is stepped. Typing never rolls.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/input.json",
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/roll.json",
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/number-input.tsx",
      "content": "'use client'\n\nimport {\n  InputInvalidIcon,\n  inputBoxClassName,\n  inputControlClassName,\n} from '@/registry/components/kino/input'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { mergeRefs } from '@/registry/lib/refs'\nimport { runningTransitions } from '@/registry/lib/resize'\nimport { Roll } from '@/registry/lib/roll'\nimport {\n  NumberInputControl as NumberInputControlPrimitive,\n  NumberInputDecrement as NumberInputDecrementPrimitive,\n  NumberInputGroup as NumberInputGroupPrimitive,\n  NumberInputIncrement as NumberInputIncrementPrimitive,\n  NumberInput as NumberInputPrimitive,\n  NumberInputScrubAreaCursor as NumberInputScrubAreaCursorPrimitive,\n  NumberInputScrubArea as NumberInputScrubAreaPrimitive,\n  type NumberInputValueChangeReason,\n} from '@ui-sdk/react/number-input'\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  type TransitionEvent,\n  use,\n  useLayoutEffect,\n  useMemo,\n  useRef,\n  useState,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A number input whose digits roll when the value is stepped: by the\n * buttons, the arrow keys, the wheel, a scrub or an agent. Typing never rolls.\n */\n\n/** The digits are still, or rolling up (`1`) or down (`-1`). */\ntype RollState = { phase: 'still' } | { phase: 'rolling'; trend: 1 | -1 }\n\nconst STILL: RollState = { phase: 'still' }\n\nconst STEP_REASONS = new Set<NumberInputValueChangeReason>([\n  'increment-press',\n  'decrement-press',\n  'step-key',\n  'wheel',\n  'scrub',\n  'imperative',\n])\n\n/** Whether a change of this reason, from `prev` to `next`, rolls the digits, and which way. */\nfunction rollOf(\n  reason: NumberInputValueChangeReason,\n  next: number | null,\n  prev: number | null,\n): RollState {\n  return isStep(reason) ? rollTo(next, prev) : STILL\n}\n\n/**\n * Whether a change of this reason stepped the value, by a button, a key, the\n * wheel, a scrub, or an agent or a controller setting it, rather than typing,\n * pasting or a form's reset.\n */\nfunction isStep(reason: NumberInputValueChangeReason) {\n  return STEP_REASONS.has(reason)\n}\n\n/** The roll from `prev` to `next`: none when either is empty or they are the same. */\nfunction rollTo(next: number | null, prev: number | null): RollState {\n  if (next === null || prev === null || next === prev) return STILL\n  return { phase: 'rolling', trend: next > prev ? 1 : -1 }\n}\n\n/** The roll, the input's text the copy shows (null until it has been read), and how the copy says it has landed. */\nconst RollContext = createContext<{\n  roll: RollState\n  text: string | null\n  settle: () => void\n}>({\n  roll: STILL,\n  text: null,\n  settle: () => {},\n})\n\nfunction NumberInput({\n  className,\n  onValueChange,\n  onInput,\n  onBlur,\n  onKeyUp,\n  ref,\n  ...props\n}: Omit<ComponentProps<typeof NumberInputPrimitive>, 'className'> & {\n  className?: string\n}) {\n  const [roll, setRoll] = useState<RollState>(STILL)\n  // The value it showed last. A parent that owns the value may change it on its own, as a pager's Next does: that rolls too.\n  const [shown, setShown] = useState<number | null>(\n    props.value === undefined ? (props.defaultValue ?? null) : props.value,\n  )\n  if (props.value !== undefined && props.value !== shown) {\n    setShown(props.value)\n    setRoll(rollTo(props.value, shown))\n  }\n\n  // The input's text, which ui-sdk writes itself, read once it has: after\n  // ui-sdk's own effects on every render, and after a keystroke or a blur\n  // reaches the root. The copy that rolls shows it.\n  const root = useRef<HTMLDivElement>(null)\n  const refs = useMemo(() => mergeRefs(root, ref), [ref])\n  // Null until the first read, so the copy starts with the text and nothing rolls in on load.\n  const [text, setText] = useState<string | null>(null)\n  const readText = () => {\n    const input = root.current?.querySelector<HTMLInputElement>(\n      '[data-slot=number-input-control]',\n    )\n    if (input && input.value !== text) setText(input.value)\n  }\n  useLayoutEffect(readText)\n\n  return (\n    <RollContext value={{ roll, text, settle: () => setRoll(STILL) }}>\n      <NumberInputPrimitive\n        ref={refs}\n        className={cn('flex min-w-0 flex-col', className)}\n        onValueChange={(value, details) => {\n          onValueChange?.(value, details)\n          if (details.canceled) return\n          setShown(value)\n          setRoll(rollOf(details.reason, value, shown))\n        }}\n        onInput={(event) => {\n          onInput?.(event)\n          readText()\n        }}\n        onBlur={(event) => {\n          onBlur?.(event)\n          readText()\n        }}\n        onKeyUp={(event) => {\n          onKeyUp?.(event)\n          readText()\n        }}\n        {...props}\n      />\n    </RollContext>\n  )\n}\n\nfunction NumberInputGroup({\n  className,\n  ...props\n}: Omit<ComponentProps<typeof NumberInputGroupPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <NumberInputGroupPrimitive\n      className={cn(inputBoxClassName, 'w-full overflow-hidden', className)}\n      {...props}\n    />\n  )\n}\n\nconst stepperClassName =\n  'flex size-(--field-height) shrink-0 items-center justify-center text-muted-foreground transition-[color,background-color] duration-(--motion-press-duration) ease-(--motion-press-fade-easing) select-none hover:bg-muted hover:text-foreground data-disabled:opacity-50 data-disabled:hover:bg-transparent'\n\nfunction NumberInputDecrement({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof NumberInputDecrementPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <NumberInputDecrementPrimitive\n      aria-label='Decrease'\n      className={cn(stepperClassName, 'border-e border-input', className)}\n      {...props}\n    >\n      {children ?? (\n        <IconPlaceholder\n          lucide='MinusIcon'\n          tabler='IconMinus'\n          hugeicons='MinusSignIcon'\n          phosphor='MinusIcon'\n          remixicon='RiSubtractLine'\n          aria-hidden\n        />\n      )}\n    </NumberInputDecrementPrimitive>\n  )\n}\n\nfunction NumberInputIncrement({\n  className,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof NumberInputIncrementPrimitive>, 'className'> & {\n  className?: string\n}) {\n  return (\n    <NumberInputIncrementPrimitive\n      aria-label='Increase'\n      className={cn(stepperClassName, 'border-s border-input', className)}\n      {...props}\n    >\n      {children ?? (\n        <IconPlaceholder\n          lucide='PlusIcon'\n          tabler='IconPlus'\n          hugeicons='Add01Icon'\n          phosphor='PlusIcon'\n          remixicon='RiAddLine'\n          aria-hidden\n        />\n      )}\n    </NumberInputIncrementPrimitive>\n  )\n}\n\ntype NumberInputControlProps = Omit<\n  ComponentProps<typeof NumberInputControlPrimitive>,\n  'className'\n> & { className?: string }\n\n/**\n * ui-sdk's number `<input>`, with a copy of its text drawn over it that\n * rolls while the value is stepped. `className` sets where the text sits and\n * its padding, on the input and the copy alike. For a field of your own\n * around it, such as the Money input.\n */\nfunction RollingInput({\n  decimal,\n  className,\n  ...props\n}: NumberInputControlProps & {\n  /** The locale's decimal point, so digits line up on it when a step adds decimals: 1,280 to 1,281.00 rolls one digit. */\n  decimal?: string\n}) {\n  const { roll, text, settle } = use(RollContext)\n  const rolling = roll.phase === 'rolling'\n  const settleWhenStill = (event: TransitionEvent<HTMLSpanElement>) => {\n    const moving = runningTransitions(event.currentTarget, { subtree: true }).some(\n      (transition) => transition.playState === 'running',\n    )\n    if (!moving) settle()\n  }\n  return (\n    // While the digits roll, the input's own text hides under a copy that rolls.\n    <span className='relative flex min-w-0 flex-1'>\n      <NumberInputControlPrimitive\n        className={cn(\n          inputControlClassName,\n          'tabular-nums',\n          className,\n          rolling && 'text-transparent caret-transparent',\n        )}\n        {...props}\n      />\n      <span\n        aria-hidden\n        className={cn(\n          'pointer-events-none absolute inset-0 flex items-center overflow-hidden px-(--control-padding) text-base tabular-nums md:text-sm',\n          className,\n          !rolling && 'invisible',\n        )}\n        // The input shows again once every digit has landed, however long the feel takes.\n        onTransitionEnd={settleWhenStill}\n        onTransitionCancel={settleWhenStill}\n      >\n        {/* A full-width line, so the copy sits where the input's text-align puts its text. */}\n        <span className='w-full'>\n          {text === null ? null : (\n            <Roll\n              value={text}\n              tail={rollTail(text, decimal)}\n              trend={rolling ? roll.trend : 1}\n            />\n          )}\n        </span>\n      </span>\n    </span>\n  )\n}\n\n/** How many characters come after the decimal point, the place a value's digits line up on; 0 without one. */\nfunction rollTail(value: string, decimal: string | undefined) {\n  const at = decimal ? value.lastIndexOf(decimal) : -1\n  return at === -1 ? 0 : value.length - at\n}\n\n/** The number input's text box, centred, with the invalid mark at its end. `className` goes to the box around them. */\nfunction NumberInputControl({ className, ...props }: NumberInputControlProps) {\n  return (\n    <span className={cn('flex min-w-0 flex-1 items-center', className)}>\n      <RollingInput className='text-center' {...props} />\n      <InputInvalidIcon />\n    </span>\n  )\n}\n\n/**\n * An area, such as the field's label, that changes the value when dragged\n * sideways, or up and down with `orientation`. While it drags, the pointer is\n * locked and hidden, so the area draws its own cursor where the pointer would be.\n */\nfunction NumberInputScrubArea({\n  className,\n  children,\n  orientation = 'horizontal',\n  ...props\n}: Omit<\n  ComponentProps<typeof NumberInputScrubAreaPrimitive>,\n  'className' | 'children'\n> & {\n  className?: string\n  children?: ReactNode\n}) {\n  const vertical = orientation === 'vertical'\n  return (\n    <NumberInputScrubAreaPrimitive\n      orientation={orientation}\n      className={cn(\n        'w-fit',\n        vertical ? 'cursor-ns-resize' : 'cursor-ew-resize',\n        className,\n      )}\n      {...props}\n    >\n      {children}\n      <NumberInputScrubAreaCursorPrimitive\n        // ui-sdk places it at the pointer from its corner, so the icon is centred on it. An outline in the background colour keeps it readable over any content.\n        className='text-foreground z-50 -ms-2.5 -mt-2.5 drop-shadow-[0_0_1px_var(--background)] [&_svg]:size-5'\n      >\n        {vertical ? (\n          <IconPlaceholder\n            lucide='MoveVerticalIcon'\n            tabler='IconArrowsVertical'\n            hugeicons='MoveVerticalIcon'\n            phosphor='ArrowsVerticalIcon'\n            remixicon='RiExpandUpDownLine'\n            aria-hidden\n          />\n        ) : (\n          <IconPlaceholder\n            lucide='MoveHorizontalIcon'\n            tabler='IconArrowsHorizontal'\n            hugeicons='MoveHorizontalIcon'\n            phosphor='ArrowsHorizontalIcon'\n            remixicon='RiExpandLeftRightLine'\n            aria-hidden\n          />\n        )}\n      </NumberInputScrubAreaCursorPrimitive>\n    </NumberInputScrubAreaPrimitive>\n  )\n}\n\nexport {\n  isStep,\n  NumberInput,\n  NumberInputControl,\n  NumberInputDecrement,\n  NumberInputGroup,\n  NumberInputIncrement,\n  NumberInputScrubArea,\n  RollingInput,\n  rollOf,\n  rollTail,\n}\n",
      "type": "registry:component",
      "target": "@components/kino/number-input.tsx"
    }
  ],
  "type": "registry:component"
}