// // Copyright 2026 DXOS.org // import { NumberInput as NumberInputPrimitive } from '@ark-ui/react/number-input'; import React, { type FocusEvent, type KeyboardEventHandler, type MouseEvent, forwardRef, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { mx } from '@dxos/ui-theme'; import { type ThemedClassName } from '@dxos/ui-types'; import { translationKey } from '#translations'; import { recipes } from '../../recipes.ts'; import { type Size } from '../../sizes.ts'; import { Button } from '../Button/Button.tsx'; import * as Popover from '../Popover/Popover.tsx'; import { Slider } from '../Slider/Slider.tsx'; /** Keys Ark steps the value by, rather than typing into it. */ const STEP_KEYS = new Set(['ArrowUp', 'ArrowDown', 'PageUp', 'PageDown', 'Home', 'End']); /** What changed a number input's value: a stepper press or arrow key, or typing. */ export type NumberInputChangeSource = 'step' | 'type'; /** * Where the decrement and increment buttons go: `end` (or `true`) trails the input with the pair, `start` leads it * with the pair, `split` puts decrement at the start and increment at the end, and `false` drops them (the arrow keys * still step). */ export type NumberInputStepper = boolean | 'start' | 'end' | 'split'; export type NumberInputProps = ThemedClassName< Pick< NumberInputPrimitive.RootProps, | 'min' | 'max' | 'step' | 'formatOptions' | 'locale' | 'allowMouseWheel' | 'clampValueOnBlur' | 'inputMode' | 'disabled' | 'readOnly' | 'required' | 'invalid' | 'name' | 'form' > > & { /** The formatted text; `valueAsNumber` in `onValueChange` is the number (NaN when empty). */ 'value'?: string; 'defaultValue'?: string; /** `source` tells a stepper press or arrow key (`step`) from typing (`type`). */ 'onValueChange'?: (value: string, valueAsNumber: number, source: NumberInputChangeSource) => void; /** The value is committed: the input blurred, or Enter was pressed. */ 'onValueCommit'?: (value: string, valueAsNumber: number) => void; 'onKeyDown'?: KeyboardEventHandler; 'placeholder'?: string; 'autoFocus'?: boolean; 'stepper'?: NumberInputStepper; /** * Shows the stepper only while the row is hovered or focused, on the side away from the number (`start` when * `align='end'`, `end` otherwise) in place of `stepper`'s placement; it keeps its place, so the number never moves. */ 'stepperOnHover'?: boolean; /** Sizes just this input (`data-size` scopes `theme/size.css`'s metrics to it), whatever its enclosing scope. */ 'size'?: Size; /** Text at the input's start, inside the row (e.g. a vector axis's letter). */ 'prefix'?: string; /** Aligns the number to the row's end, as figures in a column do. */ 'align'?: 'start' | 'end'; /** Adds an end button that opens a slider over `min`..`max` (0..100 when unset). */ 'slider'?: boolean; /** Overrides the translated `number-input.increment.label`. */ 'incrementLabel'?: string; /** Overrides the translated `number-input.decrement.label`. */ 'decrementLabel'?: string; /** Overrides the translated `number-input.slider.label`. */ 'sliderLabel'?: string; 'aria-label'?: string; 'data-testid'?: string; }; const SLIDER_RANGE = { min: 0, max: 100 }; /** * Ark number input in a control row: a text input that parses and formats by locale, steps by `step` with ArrowUp/Down * (and PageUp/Down), clamps to `min`/`max` on blur, with compact decrement and increment buttons placed by `stepper` * and an optional slider button. Inside a `Field.Root` the input takes the field's id, label, description and state. * `data-testid` goes to the row, the ref to the input. */ export const NumberInput = forwardRef( ( { classNames, value, defaultValue, onValueChange, onValueCommit, onKeyDown, placeholder, autoFocus, stepper = true, stepperOnHover, align = 'start', size, prefix, slider, incrementLabel, decrementLabel, sliderLabel, 'aria-label': ariaLabel, 'data-testid': testId, ...props }, forwardedRef, ) => { const { t } = useTranslation(translationKey); // Ark reports no cause with a change, so a press on a stepper or a stepping key marks the changes it makes. const stepping = useRef(false); const startStep = () => { stepping.current = true; }; const endStep = () => { stepping.current = false; }; const stepperProps = { onPointerDown: startStep, onPointerUp: endStep, onPointerLeave: endStep }; const caretAtEnd = useCaretAtEnd(); const placement = stepper && stepperOnHover ? (align === 'end' ? 'start' : 'end') : stepper === true ? 'end' : stepper; const reveal = stepperOnHover ? '' : undefined; const decrement = (