Skip to content

fix(Typeahead): guard Enter/Escape/arrow keys against IME composition - #4860

Merged
cixzhang merged 1 commit into
meta:mainfrom
HelloOjasMutreja:fix/typeahead-ime-composition-enter
Aug 11, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
HelloOjasMutreja:fix/typeahead-ime-composition-enter

Conversation

@HelloOjasMutreja

Copy link
Copy Markdown
Contributor

Summary

Closes #4828.

Typing with an IME (Korean/Japanese/Chinese) in BaseTypeahead (and everything built on it: Typeahead, Tokenizer, PowerSearch's free-text content-search field) and pressing Enter to commit the composition is misinterpreted as "accept the highlighted suggestion". The pending syllable is then re-committed into the freshly-cleared input, becoming its own separate selection.

Root cause

BaseTypeahead.handleKeyDown's case 'Enter' had no composition guard. The browser event order when an IME commits on Enter is: keydown (isComposing: true, or legacy keyCode: 229) → compositionend (writes the pending syllable) → input. The keydown reaches handleSelect, which fires onChange and setQuery(''), clearing the controlled input — but the composition is still live on that DOM node, so compositionend then commits the pending syllable into the now-empty field.

This is the same class of bug already fixed for ChatComposerInput (and documented for overlays generally via isImeKeyEvent in useFocusTrap.ts) — BaseTypeahead never got the same guard.

Fix

Added an early-return guard using the existing isImeKeyEvent helper (useFocusTrap.ts) at the top of BaseTypeahead.handleKeyDown, before the ArrowDown-opens-popover branch and the Enter/Escape/ArrowUp/ArrowDown/Home/End switch — an IME candidate window uses all of those keys for its own navigation, not just Enter.

Also guarded the same gap in PowerSearchEditPopover's Enter-to-save handler, which would otherwise close the popover mid-composition while typing a CJK filter value (flagged in the issue as a second unguarded site).

Test notes

Added regression tests in both Typeahead.test.tsx and PowerSearchEditPopover.test.tsx: a composing Enter (both isComposing: true and legacy keyCode: 229) does not fire selection/save, and a real Enter afterward still works normally. Verified both fail against the pre-fix code and pass with the fix.

Test plan

  • vitest run packages/core/src/Typeahead — 50/50 passing
  • vitest run packages/core/src/PowerSearch — 150/150 passing
  • vitest run packages/core/src/Tokenizer — 61/61 passing (built on BaseTypeahead)
  • Verified both new tests fail against the pre-fix code and pass against the fix
  • eslint clean (pre-existing, unrelated wrapper-div warnings elsewhere in PowerSearchEditPopover.tsx)

BaseTypeahead's keydown handler treated every Enter as "accept the
highlighted suggestion" with no composition guard. The browser fires
keydown (isComposing: true, or legacy keyCode 229) before
compositionend commits the pending IME syllable -- so the Enter that
a CJK IME user presses to confirm a composition both selected the
highlighted result and cleared the input via handleSelect's
setQuery(''), leaving the still-composing syllable to land in the
freshly-cleared field and become its own spurious second selection.

Everything built on BaseTypeahead (Typeahead, Tokenizer, PowerSearch's
content-search field) inherits the fix. Also guarded the same gap in
PowerSearchEditPopover's Enter-to-save handler, which had the same
issue typing a CJK filter value. Both use the isImeKeyEvent helper
already established for this exact purpose in useFocusTrap.ts and
already applied in ChatComposerInput.

Closes meta#4828.
@vercel

vercel Bot commented Aug 10, 2026

Copy link
Copy Markdown

@HelloOjasMutreja is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 10, 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 10, 2026
@github-actions

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

PowerSearch (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 3668 -
Complexity N/A Very High (337) -
Typeahead (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1120 -
Complexity N/A Very High (161) -

Bundle Size Summary

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

Accessibility Audit

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

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

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

github-actions Bot added a commit that referenced this pull request Aug 10, 2026
@HelloOjasMutreja
HelloOjasMutreja marked this pull request as ready for review August 10, 2026 19:38

@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, good check for i18n.

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.

[Bug] PowerSearch: Enter during IME composition splits the last character into a separate filter

2 participants