Skip to content

refactor(core): consolidate the IME composition guard into utils/ime - #4907

Merged
nynexman4464 merged 1 commit into
mainfrom
nynexman4464/refactor/ime-guard-shared-base
Aug 17, 2026
Merged

nynexman4464 merged 1 commit into
mainfrom
nynexman4464/refactor/ime-guard-shared-base

Conversation

@nynexman4464

Copy link
Copy Markdown
Contributor

Why

The IME composition guard — the check that a keydown is committing/cancelling a Korean/Japanese/Chinese composition and must not be treated as a command — was implemented three different ways across the codebase: a shared isImeKeyEvent predicate (whose canonical definition oddly lived inside hooks/useFocusTrap.ts), hand-inlined e.isComposing || e.keyCode === 229 checks, and one ref-tracking variant. That scattering is why the same bug keeps getting rediscovered one component at a time (#4860, and now #4892).

This is the groundwork: one canonical primitive, in a discoverable home, that everything points at — including the new lint rule stacked on top of this.

What

  • New utils/ime.ts exporting isImeKeyEvent, with the single authoritative doc comment explaining the keydown-before-compositionend ordering and why the legacy keyCode === 229 fallback is still load-bearing. Re-exported from the utils barrel.
  • useFocusTrap.ts keeps a deprecated re-export for back-compat; the three relative importers and the three inline sites now all call the shared predicate.

Risk

None intended — pure consolidation, no behavior change. The predicate is byte-for-byte the same logic; call sites are unchanged in what they guard.

Testing

  • New utils/ime.test.ts (both signals: isComposing and keyCode 229).
  • All affected component suites pass (Typeahead, PowerSearch, Dialog, ContextMenu, Tooltip, Chat composer, focus-trap).
  • tsc clean; eslint clean on changed files.

Design notes and the full site inventory in the follow-up issue. First of a 3-PR stack (this → editable-field fixes → lint rule).

@cixzhang

@vercel

vercel Bot commented Aug 11, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 17, 2026 4:21pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 11, 2026
@github-actions

github-actions Bot commented Aug 11, 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

Chat (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 5383 -
Complexity N/A Very High (438) -
ContextMenu (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 389 -
Complexity N/A Very High (43) -
Dialog (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 621 -
Complexity N/A Very High (79) -
PowerSearch (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 3667 -
Complexity N/A Very High (336) -
Tooltip (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 468 -
Complexity N/A Very High (54) -
Typeahead (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 1121 -
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: 2 accessibility violation(s) found — 2 serious.

Chat - 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/9 stories · Learn more
    • WCAG: 1.4.3 (Level AA)
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 11, 2026
@nynexman4464
nynexman4464 force-pushed the nynexman4464/refactor/ime-guard-shared-base branch from 88a8d84 to d41ca93 Compare August 11, 2026 22:23
github-actions Bot added a commit that referenced this pull request Aug 11, 2026
@nynexman4464
nynexman4464 force-pushed the nynexman4464/refactor/ime-guard-shared-base branch from d41ca93 to a26d166 Compare August 11, 2026 23:47
@cixzhang

Copy link
Copy Markdown
Contributor

Looks good. We should fix build

@nynexman4464
nynexman4464 force-pushed the nynexman4464/refactor/ime-guard-shared-base branch from a26d166 to 1b932c8 Compare August 17, 2026 15:21
The IME composition guard — the check that a keydown is committing or
cancelling a Korean/Japanese/Chinese composition and must not be treated as a
command — was implemented three different ways: a shared isImeKeyEvent
predicate whose canonical definition lived inside hooks/useFocusTrap.ts, several
hand-inlined isComposing/keyCode checks, and one ref-tracking variant. That
scattering is why the same bug keeps being rediscovered one component at a time.

Move the canonical predicate to a discoverable utils/ime.ts with the single
authoritative doc comment (why the composing keydown precedes compositionend,
and why the legacy keyCode fallback is still load-bearing). Name that fallback
value as IME_PROCESSING_KEY_CODE rather than a bare 229. Re-export from the
utils barrel and migrate the inline sites to it.

isImeKeyEvent is not part of the public API (not in src/index.ts) and every
consumer now imports it from utils, so no back-compat shim is needed.

No behavior change — the predicate logic is identical.
@nynexman4464
nynexman4464 force-pushed the nynexman4464/refactor/ime-guard-shared-base branch from 1b932c8 to cb94552 Compare August 17, 2026 15:44
github-actions Bot added a commit that referenced this pull request Aug 17, 2026
@nynexman4464
nynexman4464 merged commit a7b6045 into main Aug 17, 2026
27 of 29 checks passed
cixzhang added a commit that referenced this pull request Aug 18, 2026
All three conflicts are the same one: #4907 moved the IME composition
guard to `utils/ime` while this branch was removing the call sites that
used it.

- `Dialog` and `useTooltip`: main added `isImeKeyEvent(...)` inside the
  per-component Escape listeners this branch deletes. The guard is not
  lost — `layerStack` runs the same check once, before it routes a press
  to any layer, so every layer on the stack is covered by one call.
- `useFocusTrap`: kept this branch's `hasOpenLayer()` body and main's
  import path.

Also drops this branch's `hooks/isImeKeyEvent.ts`, a duplicate of the
predicate main now owns in `utils/ime`; `layerStack` imports from there.
The `hooks` barrel keeps re-exporting it from its new home, so the
public surface is unchanged.

`useLayerDismissal`'s doc no longer claims the stack orders "whatever
mix of layers is open": Lightbox, MobileNav and InfoTip do not register
with it yet, and the doc now says so.
@github-actions
github-actions Bot deleted the nynexman4464/refactor/ime-guard-shared-base branch August 18, 2026 06:11
cixzhang added a commit that referenced this pull request Aug 24, 2026
All three conflicts are the same one: #4907 moved the IME composition
guard to `utils/ime` while this branch was removing the call sites that
used it.

- `Dialog` and `useTooltip`: main added `isImeKeyEvent(...)` inside the
  per-component Escape listeners this branch deletes. The guard is not
  lost — `layerStack` runs the same check once, before it routes a press
  to any layer, so every layer on the stack is covered by one call.
- `useFocusTrap`: kept this branch's `hasOpenLayer()` body and main's
  import path.

Also drops this branch's `hooks/isImeKeyEvent.ts`, a duplicate of the
predicate main now owns in `utils/ime`; `layerStack` imports from there.
The `hooks` barrel keeps re-exporting it from its new home, so the
public surface is unchanged.

`useLayerDismissal`'s doc no longer claims the stack orders "whatever
mix of layers is open": Lightbox, MobileNav and InfoTip do not register
with it yet, and the doc now says so.

This branch was successfully deployed

1 active deployment
Preview — cb94552a Deployed Aug 17, 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants