An open source design system that's fully customizable and agent ready
TypeScript
fix(NumberInput): hide native browser number spinners (#4715)
Cindy Zhang46433cf563f5
diff --git a/.changeset/numberinput-hide-native-spinners.md b/.changeset/numberinput-hide-native-spinners.mdnew file mode 100644index 0000000000..92c27f635f--- /dev/null+++ b/.changeset/numberinput-hide-native-spinners.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] NumberInput: hide the browser's native number spinners so the field matches the component's own visual treatment across browsers, and stop a focused wheel gesture (which steps the value) from also scrolling an ancestor container. Keyboard stepping and the `spinbutton` role are unchanged, so there is no accessibility impact.+@cixzhangdiff --git a/packages/core/src/NumberInput/NumberInput.doc.mjs b/packages/core/src/NumberInput/NumberInput.doc.mjsindex aeb333cf09..42b141edc1 100644--- a/packages/core/src/NumberInput/NumberInput.doc.mjs+++ b/packages/core/src/NumberInput/NumberInput.doc.mjs@@ -192,7 +192,6 @@ export const docs = {{name: 'Description', required: false, description: 'Additional description text below the label.'},{name: 'Icon', required: false, description: 'An optional icon within the input.'},{name: 'Placeholder', required: false, description: 'Placeholder text shown when the input is empty.'},- {name: 'Spinner', required: false, description: 'Increment and decrement controls for the value.'},],},};@@ -379,7 +378,6 @@ export const docsZh = {{name: 'Description', required: false, description: 'Additional description text below the label.'},{name: 'Icon', required: false, description: 'An optional icon within the input.'},{name: 'Placeholder', required: false, description: 'Placeholder text shown when the input is empty.'},- {name: 'Spinner', required: false, description: 'Increment and decrement controls for the value.'},],},};@@ -402,7 +400,6 @@ export const docsDense = {{name: 'Description', required: false, description: 'Additional description text below the label.'},{name: 'Icon', required: false, description: 'An optional icon within the input.'},{name: 'Placeholder', required: false, description: 'Placeholder text shown when the input is empty.'},- {name: 'Spinner', required: false, description: 'Increment and decrement controls for the value.'},],},propDescriptions: {diff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsxindex 10710a6b17..68e65106da 100644--- a/packages/core/src/NumberInput/NumberInput.test.tsx+++ b/packages/core/src/NumberInput/NumberInput.test.tsx@@ -965,11 +965,15 @@ describe('keyboard clearing with hasClear (#3599)', () => {});});-describe('NumberInput statusVariant forwarding', () => {it('defaults to attached (status renders with data-variant="attached")', () => {const {container} = render(- <NumberInput label="Amount" value={null} onChange={() => {}} status={{type: 'error', message: 'Must be positive'}} />,+ <NumberInput+ label="Amount"+ value={null}+ onChange={() => {}}+ status={{type: 'error', message: 'Must be positive'}}+ />,);expect(container.querySelector('.astryx-field-status')).toHaveAttribute('data-variant',@@ -979,11 +983,42 @@ describe('NumberInput statusVariant forwarding', () => {it('forwards statusVariant="detached" to the underlying Field status', () => {const {container} = render(- <NumberInput label="Amount" value={null} onChange={() => {}} status={{type: 'error', message: 'Must be positive'}} statusVariant="detached" />,+ <NumberInput+ label="Amount"+ value={null}+ onChange={() => {}}+ status={{type: 'error', message: 'Must be positive'}}+ statusVariant="detached"+ />,);expect(container.querySelector('.astryx-field-status')).toHaveAttribute('data-variant','detached',);});++ it('stops wheel propagation while focused so ancestor containers do not scroll', () => {+ const onScrollableWheel = vi.fn();+ render(+ <div onWheel={onScrollableWheel}>+ <NumberInput label="Amount" value={5} onChange={() => {}} />+ </div>,+ );+ const input = screen.getByRole('spinbutton');+ input.focus();+ fireEvent.wheel(input, {deltaY: 100});+ expect(onScrollableWheel).not.toHaveBeenCalled();+ });++ it('does not stop wheel propagation when the input is not focused', () => {+ const onScrollableWheel = vi.fn();+ render(+ <div onWheel={onScrollableWheel}>+ <NumberInput label="Amount" value={5} onChange={() => {}} />+ </div>,+ );+ const input = screen.getByRole('spinbutton');+ fireEvent.wheel(input, {deltaY: 100});+ expect(onScrollableWheel).toHaveBeenCalledTimes(1);+ });});diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsxindex 6f2bb7be02..14956b789b 100644--- a/packages/core/src/NumberInput/NumberInput.tsx+++ b/packages/core/src/NumberInput/NumberInput.tsx@@ -81,6 +81,18 @@ const styles = stylex.create({borderWidth: 0,borderStyle: 'none',padding: 0,+ // Hide the browser's native number spinners; the component provides its own+ // affordances (keyboard entry, optional clear button) and the spinners+ // clash with the input's visual treatment and sizing.+ MozAppearance: 'textfield',+ '::-webkit-inner-spin-button': {+ WebkitAppearance: 'none',+ margin: 0,+ },+ '::-webkit-outer-spin-button': {+ WebkitAppearance: 'none',+ margin: 0,+ },fontFamily: typographyVars['--font-family-body'],fontSize: {default: typeScaleVars['--text-body-size'],@@ -579,6 +591,16 @@ export function NumberInput({],);+ // While focused, a wheel gesture steps the value — keep that gesture from+ // also bubbling up and scrolling an ancestor container (page, Dialog,+ // ScrollArea). When the input isn't focused the wheel isn't stepping the+ // value, so normal scrolling is left alone.+ const handleWheel = useCallback((e: React.WheelEvent<HTMLInputElement>) => {+ if (document.activeElement === e.currentTarget) {+ e.stopPropagation();+ }+ }, []);+// Handle clear button clickconst handleClear = useCallback(() => {if (hasClear) {@@ -637,6 +659,7 @@ export function NumberInput({onFocus={handleFocus}onBlur={handleBlur}onKeyDown={handleKeyDown}+ onWheel={handleWheel}placeholder={placeholder}// With a disabledMessage the input keeps focusability via aria-disabled// so the reason is focus-discoverable; readOnly + the handler guards