Skip to content

feat(core): add onKeyDown seam to ChatComposerInput for platform-specific keys - #4280

Merged
cixzhang merged 2 commits into
mainfrom
feat/chat-composer-enter-behavior
Jul 24, 2026
Merged

cixzhang merged 2 commits into
mainfrom
feat/chat-composer-enter-behavior

Conversation

@cixzhang

@cixzhang cixzhang commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Addresses finding #9 from the Astryx adoption feedback (#4276): ChatComposerInput can't host a composer with platform-specific Enter behavior.

The key handler submitted unconditionally on Enter (without Shift):

if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); … onSubmit?.(text); }

So it sent on Enter even on a soft keyboard — where Return should insert a newline rather than strand a multi-line prompt — didn't guard IME composition (an Enter used to commit an input-method candidate submitted the message), and offered no seam to intercept keys. The reporter had to keep their own <textarea> composer.

Change

One general seam instead of a policy-specific prop:

  • onKeyDown?: (event) => void — runs before the built-in Enter/history handling (after any open trigger menu).
    • event.preventDefault() → suppress the default submit (e.g. newline on a touch keyboard).
    • act on the event without preventDefault() → add behavior (e.g. Cmd/Ctrl+Enter to send) while default handling still runs for other keys.

Plus an unconditional correctness fix: Enter no longer submits mid-IME-composition (nativeEvent.isComposing, plus the legacy keyCode === 229). This is a bug fix, not a preference, so it has no prop.

Risk / compatibility

Additive and backward-compatible — default behavior is unchanged. onKeyDown is opt-in; the only behavioral change with no opt-in is the IME guard, which only stops an incorrect submit during composition.

Design note: an earlier revision added a shouldSubmitOnEnter boolean/predicate prop (as the issue suggested); we dropped it. It overlaps onKeyDown, can't express pointer-conditional or shortcut cases without growing, and pre-names one policy in the API. onKeyDown + preventDefault() covers every case (touch newline, Cmd+Enter, Esc-to-blur, …) with one composable primitive.

Testing

New Enter submit behavior tests: IME isComposing and legacy keyCode 229 guards, the touch-newline preventDefault recipe, the Cmd/Ctrl+Enter add-behavior recipe, and onKeyDown ordering. Docs (.doc.mjs en/zh/dense) and the EnterBehavior Storybook story updated to the onKeyDown recipe. pnpm build, core typecheck, typecheck:docs, and pnpm lint clean; full Chat suite green.

The composer's key handler unconditionally submitted on Enter (without
Shift), so it sent on Enter even on a soft keyboard — where Return
should insert a newline rather than strand a multi-line prompt — and it
did not guard IME composition, so an Enter used to commit an
input-method candidate submitted the message instead. There was no seam
to override any of this.

Add two additive, backward-compatible props:

- shouldSubmitOnEnter?: boolean | ((event) => boolean) (default true) —
  a fixed policy or per-keystroke predicate; return false to let Enter
  fall through and insert a line break (desktop-send / touch-newline).
- onKeyDown?: (event) => void — runs before the built-in Enter/history
  handling (after an open trigger menu); preventDefault() to take over.

Independently, Enter no longer submits mid-IME-composition
(nativeEvent.isComposing, plus the legacy keyCode 229 signal),
regardless of shouldSubmitOnEnter.
@vercel

vercel Bot commented Jul 24, 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, Comment Jul 24, 2026 9:05am

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 24, 2026
github-actions Bot added a commit that referenced this pull request Jul 24, 2026
@github-actions

github-actions Bot commented Jul 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

Chat · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 5280 -
Complexity N/A Very High (433) -

Bundle Size Summary

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

Accessibility Audit

Status: 1 accessibility violation(s) found — 1 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/8 stories · Learn more
    • WCAG: 1.4.3 (Level AA)

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

The composer's key handler unconditionally submitted on Enter (without
Shift), so it sent on Enter even on a soft keyboard — where Return
should insert a newline rather than strand a multi-line prompt — and it
did not guard IME composition, so an Enter used to commit an
input-method candidate submitted the message instead. There was also no
seam to intercept keys.

Expose one general primitive instead of a policy-specific prop:

- onKeyDown?: (event) => void — runs before the built-in Enter/history
  handling (after an open trigger menu). preventDefault() to suppress
  the default submit (e.g. newline on a touch keyboard); or act on the
  event without preventDefault() to add behavior (e.g. Cmd/Ctrl+Enter
  submit) while the default handling still runs for other keys.

Independently, Enter no longer submits mid-IME-composition
(nativeEvent.isComposing, plus the legacy keyCode 229 signal) — an
unconditional correctness fix, not a preference, so it has no prop.
@cixzhang cixzhang changed the title feat(core): let ChatComposerInput host platform-specific Enter behavior feat(core): add onKeyDown seam to ChatComposerInput for platform-specific keys Jul 24, 2026
github-actions Bot added a commit that referenced this pull request Jul 24, 2026
@cixzhang
cixzhang merged commit 0c4d8d7 into main Jul 24, 2026
19 checks passed
@github-actions
github-actions Bot deleted the feat/chat-composer-enter-behavior branch July 25, 2026 06:50
AKnassa added a commit to AKnassa/astryx that referenced this pull request Sep 26, 2026
Resolved conflicts:
- packages/core/src/Chat/ChatComposerInput.tsx: kept the PR's
  handleRef.setValue alongside upstream's `focus: focusEditable`
  (meta#6419/meta#6398 keep an existing in-editor selection on focus); dropped the
  PR's local placeCaretAtEnd helper in favour of upstream's import from
  ./chatComposerSelection; handleKeyDown deps keep upstream's
  onKeyDownProp (meta#4280) and drop onChange, since submit-clear now reports
  through emitChange. The pending-emissions ledger auto-merged over
  upstream's one-shot pendingEchoValueRef, next to meta#6419's
  emitChangeVersionRef.
- packages/core/src/Chat/ChatComposerInput.test.tsx: took upstream's
  imports (waitFor, act, userEvent); the PR's controlled-value and
  trigger-menu tests auto-merged.
- packages/core/src/Chat/ChatComposerInput.doc.mjs: kept the PR's
  handleRef/value prose in upstream's prettier-wrapped layout, and recast
  the English/dense em dashes as punctuation per meta#4394.

Known fallout, fixed in the follow-up review commit (astracat F2): the
required `setValue` member breaks upstream's five-member handle literal in
useChatPasteAsToken.test.ts (TS2741 under the core typecheck).

This branch was successfully deployed

1 active deployment
Preview — d0c3e468 Deployed Jul 24, 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.

1 participant