An open source design system that's fully customizable and agent ready
TypeScript
fix(core): defer clear focus restoration out of tap to prevent touch page scroll jump (#5440)
Geervane83410a9126d
diff --git a/.changeset/defer-clear-focus-tap.md b/.changeset/defer-clear-focus-tap.mdnew file mode 100644index 0000000000..9b454604db--- /dev/null+++ b/.changeset/defer-clear-focus-tap.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] defer clear focus restoration for pointer/touch taps to prevent page scroll jumps while preserving synchronous focus restoration on keyboard activation and properly composing `onPointerDown` in `InputClearButton`+@Geervandiff --git a/packages/core/src/DateInput/DateInput.tsx b/packages/core/src/DateInput/DateInput.tsxindex 2692ef2f99..bc270de7ed 100644--- a/packages/core/src/DateInput/DateInput.tsx+++ b/packages/core/src/DateInput/DateInput.tsx@@ -619,10 +619,21 @@ function PointerDateField({);// Handle clear button click- const handleClear = useCallback(() => {- fireChange(undefined);- inputRef.current?.focus();- }, [fireChange]);+ const handleClear = useCallback(+ (e?: React.MouseEvent<HTMLButtonElement>) => {+ fireChange(undefined);+ if (!e || e.detail === 0) {+ inputRef.current?.focus();+ } else {+ // Defer focus restoration past the button's unmount task so iOS Safari+ // and touch browsers don't jump the page scroll to 0 on tap.+ requestAnimationFrame(() => {+ inputRef.current?.focus({preventScroll: true});+ });+ }+ },+ [fireChange],+ );// Handle date selection from calendarconst handleDateSelect = useCallback(diff --git a/packages/core/src/DateTimeInput/DateTimeInput.tsx b/packages/core/src/DateTimeInput/DateTimeInput.tsxindex 9d2c0fe9f5..23b69ae64e 100644--- a/packages/core/src/DateTimeInput/DateTimeInput.tsx+++ b/packages/core/src/DateTimeInput/DateTimeInput.tsx@@ -1520,13 +1520,24 @@ function PointerDateTimeField({);// --- Clear ---- const handleClear = useCallback(() => {- setNativeTimeDraft(undefined);- fireChange(undefined);- if (!usesNativePicker) {- dateInputRef.current?.focus();- }- }, [fireChange, usesNativePicker]);+ const handleClear = useCallback(+ (e?: React.MouseEvent<HTMLButtonElement>) => {+ setNativeTimeDraft(undefined);+ fireChange(undefined);+ if (!usesNativePicker) {+ if (!e || e.detail === 0) {+ dateInputRef.current?.focus();+ } else {+ // Defer focus restoration past the button's unmount task so iOS Safari+ // and touch browsers don't jump the page scroll to 0 on tap.+ requestAnimationFrame(() => {+ dateInputRef.current?.focus({preventScroll: true});+ });+ }+ }+ },+ [fireChange, usesNativePicker],+ );// Focus time input when clicking wrapper padding/iconconst {onClick: handleTimeWrapperClick, onMouseUp: handleTimeWrapperMouseUp} =diff --git a/packages/core/src/Field/InputClearButton.test.tsx b/packages/core/src/Field/InputClearButton.test.tsxindex 0f95dcca77..82f9ebfc2d 100644--- a/packages/core/src/Field/InputClearButton.test.tsx+++ b/packages/core/src/Field/InputClearButton.test.tsx@@ -19,7 +19,11 @@ import path from 'node:path';import {describe, it, expect, vi, afterEach} from 'vitest';import {render, screen, fireEvent} from '@testing-library/react';import * as stylex from '@stylexjs/stylex';-import {InputClearButton, type InputClearButtonProps} from './InputClearButton';+import {+ InputClearButton,+ InternalInputClearButton,+ type InputClearButtonProps,+} from './InputClearButton';import {Icon, registerIcons, resetIcons} from '../Icon';import {defineTheme} from '../theme/defineTheme';import {generateThemeCSS} from '../theme/generateThemeRules';@@ -323,3 +327,34 @@ describe('InputClearButton glyph source', () => {expect(getGlyph()).toContainElement(screen.getByTestId('custom-close'));});});++describe('InputClearButton pointer and focus interactions', () => {+ it('prevents default on pointerdown and mousedown to keep focus on input', () => {+ render(<InputClearButton label="Clear" onClick={() => {}} />);+ const button = screen.getByRole('button', {name: 'Clear'});++ const pointerDownEvent = fireEvent.pointerDown(button);+ expect(pointerDownEvent).toBe(false);++ const mouseDownEvent = fireEvent.mouseDown(button);+ expect(mouseDownEvent).toBe(false);+ });++ it('composes custom onPointerDown while preventing default in InternalInputClearButton', () => {+ const handlePointerDown = vi.fn();+ render(+ <InternalInputClearButton+ label="Clear"+ onClick={() => {}}+ onPointerDown={handlePointerDown}+ onClickCapture={() => {}}+ />,+ );+ const button = screen.getByRole('button', {name: 'Clear'});+ const pointerDownEvent = fireEvent.pointerDown(button);++ expect(handlePointerDown).toHaveBeenCalledTimes(1);+ expect(pointerDownEvent).toBe(false);+ });+});+diff --git a/packages/core/src/Field/InputClearButton.tsx b/packages/core/src/Field/InputClearButton.tsxindex 04560bd8ed..ab921acb7e 100644--- a/packages/core/src/Field/InputClearButton.tsx+++ b/packages/core/src/Field/InputClearButton.tsx@@ -117,8 +117,12 @@ function renderInputClearButton({}/>}+ onPointerDown={e => {+ e.preventDefault();+ onPointerDown?.(e);+ }}+ onMouseDown={e => e.preventDefault()}onClick={onClick}- onPointerDown={onPointerDown}onClickCapture={onClickCapture}isIconOnlyxstyle={[styles.button, xstyle]}diff --git a/packages/core/src/Field/PanelSearchInput.tsx b/packages/core/src/Field/PanelSearchInput.tsxindex 4f436aad99..6d88682065 100644--- a/packages/core/src/Field/PanelSearchInput.tsx+++ b/packages/core/src/Field/PanelSearchInput.tsx@@ -225,14 +225,27 @@ export function PanelSearchInput({[onBlur],);- const handleClear = useCallback(() => {- onValueChange('');- // Clearing puts the caret back where the user was typing, matching- // TextInput's built-in clear.- if (typeof ref === 'object' && ref?.current) {- ref.current.focus();- }- }, [onValueChange, ref]);+ const handleClear = useCallback(+ (e?: React.MouseEvent<HTMLButtonElement>) => {+ onValueChange('');+ // Clearing puts the caret back where the user was typing, matching+ // TextInput's built-in clear. Defer focus restoration past the button's+ // unmount task so touch browsers don't jump page scroll on tap, while+ // preserving synchronous focus for keyboard users.+ if (!e || e.detail === 0) {+ if (typeof ref === 'object' && ref?.current) {+ ref.current.focus();+ }+ } else {+ requestAnimationFrame(() => {+ if (typeof ref === 'object' && ref?.current) {+ ref.current.focus({preventScroll: true});+ }+ });+ }+ },+ [onValueChange, ref],+ );return (<divdiff --git a/packages/core/src/FileInput/FileInput.tsx b/packages/core/src/FileInput/FileInput.tsxindex 527f19cb86..35c66d4760 100644--- a/packages/core/src/FileInput/FileInput.tsx+++ b/packages/core/src/FileInput/FileInput.tsx@@ -589,7 +589,16 @@ export function FileInput({onChange(null);if (inputRef.current) {inputRef.current.value = '';- inputRef.current.focus();+ const targetInput = inputRef.current;+ if (e.detail === 0) {+ targetInput.focus();+ } else {+ // Defer focus restoration past the button's unmount task so iOS Safari+ // and touch browsers don't jump the page scroll to 0 on tap.+ requestAnimationFrame(() => {+ targetInput.focus({preventScroll: true});+ });+ }}},[onChange],diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsxindex be976c59bc..61dfccc559 100644--- a/packages/core/src/NumberInput/NumberInput.tsx+++ b/packages/core/src/NumberInput/NumberInput.tsx@@ -829,13 +829,24 @@ export function NumberInput({const canDecrement = getNextValue(-1) !== valueForStepping;// Handle clear button click- const handleClear = useCallback(() => {- if (hasClear) {- onChange(null);- }- setPendingInput(null);- inputRef.current?.focus();- }, [hasClear, onChange]);+ const handleClear = useCallback(+ (e?: React.MouseEvent<HTMLButtonElement>) => {+ if (hasClear) {+ onChange(null);+ }+ setPendingInput(null);+ if (!e || e.detail === 0) {+ inputRef.current?.focus();+ } else {+ // Defer focus restoration past the button's unmount task so iOS Safari+ // and touch browsers don't jump the page scroll to 0 on tap.+ requestAnimationFrame(() => {+ inputRef.current?.focus({preventScroll: true});+ });+ }+ },+ [hasClear, onChange],+ );// Focus input when clicking anywhere on the wrapper (icons, padding, etc.)const {onClick: handleWrapperClick, onMouseUp: handleWrapperMouseUp} =diff --git a/packages/core/src/TextInput/TextInput.test.tsx b/packages/core/src/TextInput/TextInput.test.tsxindex 0e0778159d..45fb970e94 100644--- a/packages/core/src/TextInput/TextInput.test.tsx+++ b/packages/core/src/TextInput/TextInput.test.tsx@@ -1064,3 +1064,53 @@ describe('TextInput readonly theme state', () => {expect(root).not.toHaveAttribute('data-readonly');});});++describe('TextInput clear button focus behavior', () => {+ it('synchronously restores focus to the input on keyboard activation (detail === 0)', () => {+ const handleChange = vi.fn();+ render(+ <TextInput+ label="Search"+ value="test"+ hasClear+ onChange={handleChange}+ />,+ );++ const input = screen.getByRole('textbox');+ const clearButton = screen.getByRole('button', {name: /clear/i});++ clearButton.focus();+ expect(document.activeElement).toBe(clearButton);++ // Keyboard activation (e.g. Enter / Space on focused button yields detail 0)+ fireEvent.click(clearButton, {detail: 0});++ expect(handleChange).toHaveBeenCalledWith('', expect.any(Object));+ // Must be synchronously focused without waiting for animation frames+ expect(document.activeElement).toBe(input);+ });++ it('defers focus restoration via requestAnimationFrame on pointer activation', () => {+ const handleChange = vi.fn();+ const rafSpy = vi.spyOn(window, 'requestAnimationFrame');+ render(+ <TextInput+ label="Search"+ value="test"+ hasClear+ onChange={handleChange}+ />,+ );++ const clearButton = screen.getByRole('button', {name: /clear/i});++ // Pointer activation (detail > 0)+ fireEvent.click(clearButton, {detail: 1});++ expect(handleChange).toHaveBeenCalledWith('', expect.any(Object));+ expect(rafSpy).toHaveBeenCalled();+ rafSpy.mockRestore();+ });+});+diff --git a/packages/core/src/TextInput/TextInput.tsx b/packages/core/src/TextInput/TextInput.tsxindex f38c5692c4..4714a0a85a 100644--- a/packages/core/src/TextInput/TextInput.tsx+++ b/packages/core/src/TextInput/TextInput.tsx@@ -381,10 +381,21 @@ export function TextInput({};// Handle clear button click- const handleClear = useCallback(() => {- onChange?.('', null as unknown as ChangeEvent<HTMLInputElement>);- inputRef.current?.focus();- }, [onChange]);+ const handleClear = useCallback(+ (e?: React.MouseEvent<HTMLButtonElement>) => {+ onChange?.('', null as unknown as ChangeEvent<HTMLInputElement>);+ if (!e || e.detail === 0) {+ inputRef.current?.focus();+ } else {+ // Defer focus restoration past the button's unmount task so iOS Safari+ // and touch browsers don't jump the page scroll to 0 on tap.+ requestAnimationFrame(() => {+ inputRef.current?.focus({preventScroll: true});+ });+ }+ },+ [onChange],+ );// Focus input when clicking anywhere on the wrapper (icons, padding, etc.)const {onClick: handleWrapperClick, onMouseUp: handleWrapperMouseUp} =diff --git a/packages/core/src/TimeInput/TimeInput.tsx b/packages/core/src/TimeInput/TimeInput.tsxindex dc41adf599..0ba855340e 100644--- a/packages/core/src/TimeInput/TimeInput.tsx+++ b/packages/core/src/TimeInput/TimeInput.tsx@@ -622,13 +622,24 @@ export function TimeInput({);// Handle clear button click- const handleClear = useCallback(() => {- fireChange(undefined);- // Focusing a native time control reopens the OS picker on iOS.- if (!usesNativeTimePicker) {- inputRef.current?.focus();- }- }, [fireChange, usesNativeTimePicker]);+ const handleClear = useCallback(+ (e?: React.MouseEvent<HTMLButtonElement>) => {+ fireChange(undefined);+ // Focusing a native time control reopens the OS picker on iOS.+ if (!usesNativeTimePicker) {+ if (!e || e.detail === 0) {+ inputRef.current?.focus();+ } else {+ // Defer focus restoration past the button's unmount task so iOS Safari+ // and touch browsers don't jump the page scroll to 0 on tap.+ requestAnimationFrame(() => {+ inputRef.current?.focus({preventScroll: true});+ });+ }+ }+ },+ [fireChange, usesNativeTimePicker],+ );// Focus input when clicking anywhere on the wrapper (icons, padding, etc.)const {onClick: handleWrapperClick, onMouseUp: handleWrapperMouseUp} =