Skip to content

fix(core): defer clear focus restoration out of tap to prevent touch page scroll jump - #5440

Merged
cixzhang merged 3 commits into
meta:mainfrom
Geervan:fix/input-clear-button-touch-focus
Sep 11, 2026
Merged

cixzhang merged 3 commits into
meta:mainfrom
Geervan:fix/input-clear-button-touch-focus

Conversation

@Geervan

@Geervan Geervan commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #5422
Following up on #5350 (which fixed TouchDateField), this PR extends the deferred focus restoration pattern across all clearable input controls (DateInput, DateTimeInput, PanelSearchInput, FileInput, TextInput, NumberInput, TimeInput) that consume InputClearButton.

Cause

When clearing an input value via InputClearButton on mobile WebKit/iOS Safari:

  1. Clearing the value synchronously unmounts <InputClearButton> (which is conditionally rendered when value is set).
  2. Calling .focus() synchronously on the target input within the same task while processing the touch tap causes iOS Safari to jump the document scroll position to 0 (scrollTop: 0).

The Fix

  • Focus Deferral: Wrapped .focus({preventScroll: true}) in requestAnimationFrame across all clearable input components (DateInput, DateTimeInput, PanelSearchInput, FileInput, TextInput, NumberInput, TimeInput).
  • Focus Protection: Added onPointerDown/onMouseDown with preventDefault() on InputClearButton to prevent pointer events from stealing focus from the input field prior to clearing.

Test Plan

  • Vitest Unit Tests: All tests passing (300/300 tests passed across InputClearButton, DateInput, DateTimeInput, FileInput, TextInput, NumberInput, TimeInput).
  • ESLint: Passed with 0 errors.
  • Changeset: Added .changeset/defer-clear-focus-tap.md (node scripts/check-changesets.mjs passed).
  • Visual Verification: Verified in Storybook with mobile touch emulation — tapping clear clears the value without page scroll displacement.

@vercel

vercel Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Sep 9, 2026 1:31pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 24, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Aug 24, 2026
@github-actions

github-actions Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

DateInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 3305 -
Complexity N/A Very High (359) -
DateTimeInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 3397 -
Complexity N/A Very High (535) -
Field (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 818 -
Complexity N/A Very High (56) -
FileInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 666 -
Complexity N/A Very High (103) -
NumberInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1078 -
Complexity N/A Very High (154) -
TextInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 341 -
Complexity N/A Very High (58) -
TimeInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 535 -
Complexity N/A Very High (94) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.8KB 1.2KB

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 serious.

FileInput - 1 issue(s)
  • 🟠 serious: Ensure the contrast between foreground and background colors meets WCAG 2 AA minimum contrast ratio thresholds
    • Rule: color-contrast · Affects 3/15 stories · Learn more
    • WCAG: 1.4.3 (Level AA)

Visual Regression

Status: No visual change across 16 compared shot(s).


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Aug 24, 2026

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Touch clearing keeps TextInput focused and scroll-stable in Chromium, but keyboard users now lose focus to the document for one frame before the input is restored. The base kept focus on the input before click bubbling and the first animation frame. Please preserve that keyboard path while deferring only the pointer/touch path, with focused regression coverage.

This head also no longer integrates with current main: the synthetic merge conflicts in DateTimeInput and TimeInput and produces duplicate onPointerDown props in InputClearButton, colliding with the current popup-aware handler. Please rebase and compose the current handler so touch clearing stays scroll-stable without dropping current popup behavior.

[Reviewed by Robohands]

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this fixes both prior issues. Chromium keeps keyboard focus and touch scroll stable, and Selector/MultiSelector popups stay open when cleared.

[Reviewed by Robohands]

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Sep 9, 2026
@cixzhang
cixzhang merged commit e83410a into meta:main Sep 11, 2026
30 checks passed
@github-actions
github-actions Bot deleted the fix/input-clear-button-touch-focus branch September 11, 2026 10:58
cixzhang pushed a commit that referenced this pull request Oct 24, 2026
…S keyboard (#8183)

## User impact

On an iPhone, tapping the clear button of a TextInput or NumberInput that is not focused empties the field and focuses it, but no keyboard comes up. The person has to tap the field again to type the replacement they cleared the field for.

## Problem and solution fit

Since #5440, the clear button's pointer path focuses the field a frame later (`requestAnimationFrame`). iOS Safari raises its keyboard only for a focus made while handling the touch, so the later focus lands with no keyboard. #5440 deferred the focus to avoid a page jump to the top. `preventScroll`, which that change also added, is what prevents the jump; the deferral only broke the keyboard. This change keeps `preventScroll` and focuses inside the tap.

## Expected behavior and authority

UIKit's clear button makes its field the first responder: in a UIKit app on iOS 26.2 the keyboard starts rising 75 ms after the lift. No current record says otherwise; #5440 is the regression history. Siblings: #8158 makes the same change in PanelSearchInput; #6425, #7690, #7910, #7940 (TextInput) and #5978, #8015 (NumberInput) touch the same files with other intents.

## Smallest restoration

Two lines in each of TextInput and NumberInput: the pointer path calls `focus({preventScroll: true})` directly instead of in the next frame. The keyboard and assistive-technology path (`detail === 0`) is unchanged, as is the clear button's `preventDefault` on pointerdown, which keeps a focused field focused. DateInput, TimeInput, DateTimeInput and FileInput have the same pattern; their lanes own them.

## Evidence

Mobile Safari, iOS 26.2 (simulator), page scrolled 254 px, field unfocused with a value, one tap on its clear button:

| | keyboard | page scroll |
| --- | --- | --- |
| TextInput before | none (viewport stays 714) | stays 254 |
| TextInput after | up (viewport 714 → 377) | stays 254 |
| NumberInput before | none | stays 254 |
| NumberInput after | number pad (714 → 404) | 254 → 266: Safari reveals the field the pad would cover |

![Clear tap before and after: TextInput, then NumberInput](https://raw.githubusercontent.com/vjeux/astryx/evidence/c08-clear-keyboard/clear-keyboard/clear-tap-keyboard.png)

- Regression tests: the pointer path focuses synchronously, with `preventScroll`, and without `requestAnimationFrame`. This replaces the test that pinned the deferral.

## Scope

- [x] One defect is restored.
- [x] Separable contradictory or unsettled tagalongs were removed or split.
- [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
- [x] Public text and artifacts contain no internal Meta context.

## Testing

- `pnpm vitest run` on TextInput, NumberInput and Field: 15 files, 600 tests.
- Core typecheck, eslint and prettier on touched files, `check:changesets`.
- Mobile Safari on the iOS 26.2 simulator (iPhone 17 Pro): the four runs above, with visual viewport, focus and scroll events read from the page.
cixzhang pushed a commit that referenced this pull request Oct 24, 2026
## User impact

On an iPhone, two taps that should start typing put a caret in the field but no keyboard, so a second tap is needed:

- a tap on a Typeahead's selected value, which turns it back into editable text (edit mode);
- a tap on the clear button of a Selector or MultiSelector search row after the keyboard was dismissed.

## Problem and solution fit

1. Why it occurs: both focus the input in an animation frame after the tap. iOS raises its keyboard only for a focus made while the browser is still handling the tap, or while the keyboard is already up.
2. Why it harms the task: the field looks ready for typing but there is nothing to type with.
3. Why it matters: the tap meant to start a correction or a new search seems to do nothing.

Both now focus inside the tap's own dispatch (the clear with `preventScroll` for a pointer tap).

## Expected behavior and authority

`component:Typeahead` FR1 makes the editing state an editable input. For the clear button the reference is UIKit: tapped while the field is not editing, a search field's clear button makes it first responder, keyboard included (iOS 26.2 simulator).

#5440 deferred the clear's refocus against a page scroll jump on iOS. With `preventScroll`, a page scrolled to 668 px stays there through the clear on iOS 26.2, keyboard down or up. Other clearable fields keep #5440's deferral (a separate fix).

## Smallest restoration

`Typeahead.tsx`: `handleEnterEditMode` runs its existing steps without the `requestAnimationFrame`. `PanelSearchInput.tsx`: `handleClear` drops the deferred branch. No API, style or layout change.

## Evidence

iOS 26.2 simulator, Mobile Safari, real taps, keyboard down before the tap; left: main, right: this branch.

Typeahead, tap on the selected value "Apple":

![Before: Apple selected, no keyboard. After: the keyboard is up](https://raw.githubusercontent.com/vjeux/astryx/83a02b6d816ed70a82b05bb5db1c427fa32ee40f/pr2-edit-mode.png)

Searchable Selector, "ap" typed, keyboard dismissed, tap on clear:

![Before: cleared, no keyboard. After: cleared, keyboard up](https://raw.githubusercontent.com/vjeux/astryx/83a02b6d816ed70a82b05bb5db1c427fa32ee40f/pr2-clear.png)

- Before: the input is focused, no keyboard (inset 0).
- After: the input is focused and the keyboard is up (inset 337 px); typing fills the field.
- Unchanged: a clear with the keyboard up keeps it up; the scroll position.
- Regression tests: three cases check that focus runs while the click is dispatched; all fail on the parent commit.

## Scope

- [x] One defect is restored.
- [x] Separable contradictory or unsettled tagalongs were removed or split.
- [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
- [x] Public text and artifacts contain no internal Meta context.

## Testing

```
pnpm -F @astryxdesign/core typecheck
pnpm exec vitest run packages/core/src/Typeahead packages/core/src/InputGroup packages/core/src/Selector packages/core/src/MultiSelector packages/core/src/Field
pnpm storybook:build
```

Real-browser states: iOS 26.2 simulator (iPhone 17 Pro), Mobile Safari, real taps and typing, on main and on this branch: the Typeahead default story (pick "Apple", blur, tap the value); the searchable Selector story (open, type, blur, tap clear; type again, tap clear with the keyboard up), once at the top of the page and once with the page scrolled to 668 px. Focus, the visual viewport and the scroll position read after each tap.

This branch was successfully deployed

1 active deployment
Preview — 2e6efb11 Deployed Sep 9, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InputClearButton focuses inside the tap for every input — #5350 fixed only TouchDateField

2 participants