Skip to content

[otp field] Fix Safari IME composition over-filling slots - #5098

Merged
atomiks merged 8 commits into
mui:masterfrom
atomiks:claude/bold-colden-797761
Sep 15, 2026
Merged

atomiks merged 8 commits into
mui:masterfrom
atomiks:claude/bold-colden-797761

Conversation

@atomiks

@atomiks atomiks commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

OTPField.Input committed every IME onChange value during composition. In Safari, the in-progress text can arrive as accumulating values (d, dd, ddd), so the field wrote each intermediate value into the slots and could end up with dddddd instead of the final ddd. Chrome does not reproduce the same over-fill in the reported flow.

Changes

This makes OTP input composition-aware, following the Combobox pattern:

  • Track active composition and keep the temporary text in local input state.
  • During composition, let onChange update only that temporary text.
  • On compositionend, commit the final value once through the normal normalization/replacement path.
  • Reuse that commit path for normal input and composition end, preserving paste, autofill, invalid-character reporting, cancellation, and focus movement.
  • Keep the Android guard used by Combobox for inconsistent keyboard composition events.

Fixes #5091

@atomiks atomiks added component: otp field type: bug It doesn't behave as expected. labels Jun 22, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

commit: d2b162d

@code-infra-dashboard

code-infra-dashboard Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+64B(+0.01%) 🔺+74B(+0.05%)

Details of bundle changes

Performance

Total duration: 1,058.89 ms -62.27 ms(-5.6%) | Renders: 76 (+0) | Paint: 1,714.04 ms -109.26 ms(-6.0%)

Test Duration Renders
Combobox open — 500 items 24.91 ms ▼-7.82 ms(-23.9%) 4 (+0)

14 tests within noise — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit d2b162d
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6aa8a37e4d20ca0008035b26
😎 Deploy Preview https://deploy-preview-5098--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks atomiks changed the title [otp field] Commit IME composition once to avoid over-filling slots [otp field] Fix Safari IME composition over-filling slots Jun 22, 2026
Safari can surface in-progress IME text through onChange as an accumulating
string (`d`, `dd`, `ddd`). OTPFieldInput treated each intermediate value as
committed bulk input and advanced focus by its length, over-filling slots.

Track composition with a ref and buffer the in-progress text in a temporary
`composingValue` for display. While composing, onChange no longer calls
setValue, reportValueInvalid, or queueFocusInput; the final value commits once
on compositionend through a shared commit helper reused by normal input.
@atomiks
atomiks force-pushed the claude/bold-colden-797761 branch from 61b7ac2 to ad4a3cb Compare June 22, 2026 11:35
@atomiks
atomiks marked this pull request as ready for review June 23, 2026 04:33

@mj12albert mj12albert left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The fix works for Safari and matches the behavior in Chrome now:

Screen.Recording.2026-06-23.at.5.19.30.PM.mov

Though the "correct" behavior still feels very strange to use, if I had to type the code it would be just as fast (if not faster) to switch the input method, type the code, and switch it back

@flaviendelangle flaviendelangle left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PR review

This is a clean, well-targeted fix for the Safari IME over-fill (#5091). It mirrors the established Combobox composition pattern, the extracted commitInputValue is a faithful move of the original onChange body, and the 62 existing + 1 new test pass. Nothing is merge-blocking. The notes below are minor test/comment observations only.

Bugs (0)

No findings. I specifically checked the highest-risk paths and they hold:

  • Double-commit on compositionend then trailing change (old WebKit/Chrome ordering): the trailing onChange re-enters commitInputValue with composingValue == null, but it carries the same final value, and setValue dedupes it — no extra onValueChange, focus stays correct.
  • State-vs-ref for the composing branch: unlike Combobox, this uses only composingValue state (no isComposingRef) to branch in onChange. This is safe because React classifies composition events as discrete and flushes the onCompositionStart update synchronously before the next input/change, so the first intermediate value is buffered, not committed.
  • Android path: onCompositionStart returns early, leaving composingValue null, so onChange commits as before and onCompositionEnd no-ops — no regression versus current behavior.
  • composingValue === '': a composition that starts and ends with no change still commits, because '' != null keeps the composition treated as active (?? only falls through on null/undefined).

Tests (2)

1. ℹ️ Android composition branch is untested

Location: packages/react/src/otp-field/input/OTPFieldInput.tsx:194 (PR version)

onCompositionStart(event) {
  if (platform.os.android) {
    return;
  }
  setComposingValue(event.currentTarget.value);
},

The new platform.os.android early-return is a distinct branch with no coverage. The added test exercises only the non-Android buffering path.

Failure scenario: A future refactor could break the Android passthrough (where each onChange must still commit immediately) without any test failing. This is the same constraint Combobox depends on (#2942).

Fix: If practical to mock the @base-ui/utils/platform module, add a case asserting that with platform.os.android === true, intermediate change events commit immediately (matching pre-PR behavior). Acceptable to skip if the platform module can't be mocked cleanly, as Combobox does.

2. ℹ️ No test for invalid characters surfaced during composition commit

Location: packages/react/src/otp-field/input/OTPFieldInput.tsx (PR version, commitInputValue → reportValueInvalid)

The composition commit routes through the same didRejectCharacters / reportValueInvalid path as normal input, but the new test only commits valid alphanumerics.

Failure scenario: A regression where invalid-character reporting is dropped specifically on the composition-commit path would go unnoticed.

Fix: Add a composition test where compositionend carries a value containing a rejected character and assert onValueChange/invalid reporting behaves as it does for direct input.

Simplifications (0)

No findings. Extracting the shared body into commitInputValue is itself the right de-duplication, and it's consistent with the file's existing inline-handler style (no useStableCallback needed since the handlers are recreated per render).

Docs (1)

1. ℹ️ Comment claims Gecko/Firefox reproduces, but the issue is Safari-only

Location: packages/react/src/otp-field/input/OTPFieldInput.tsx:75 (PR version)

// While an IME composition is active, Safari (and other WebKit/Gecko engines) can expose the
// in-progress text through `onChange` as an accumulating string (`d`, then `dd`, then `ddd`).

The linked issue reports the over-fill in Safari/iOS only and explicitly notes Chrome does not reproduce it; there's no evidence cited for Firefox (Gecko). The fix is correctly defensive regardless, but the comment slightly overclaims the reproduction surface.

Fix: Narrow to "Safari/WebKit" or add "(Chrome does not reproduce the reported flow)" to match the issue, so the comment doesn't mislead a future reader about which engines were actually observed.

Verdict

Approve — correct, well-scoped fix that matches the Combobox composition pattern and passes its tests; only optional test-coverage and comment-accuracy notes remain.


🤖 Review generated with Claude Code

@atomiks
atomiks merged commit 04dc5a1 into mui:master Sep 15, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: otp field type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[otp field] Safari IME input duplicates alphanumeric characters

3 participants