Repository navigation
[otp field] Fix Safari IME composition over-filling slots - #5098
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,058.89 ms -62.27 ms(-5.6%) | Renders: 76 (+0) | Paint: 1,714.04 ms -109.26 ms(-6.0%)
14 tests within noise — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
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.
61b7ac2 to
ad4a3cb
Compare
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
compositionendthen trailingchange(old WebKit/Chrome ordering): the trailingonChangere-enterscommitInputValuewithcomposingValue == null, but it carries the same final value, andsetValuededupes it — no extraonValueChange, focus stays correct. - State-vs-ref for the composing branch: unlike Combobox, this uses only
composingValuestate (noisComposingRef) to branch inonChange. This is safe because React classifies composition events as discrete and flushes theonCompositionStartupdate synchronously before the nextinput/change, so the first intermediate value is buffered, not committed. - Android path:
onCompositionStartreturns early, leavingcomposingValuenull, soonChangecommits as before andonCompositionEndno-ops — no regression versus current behavior. composingValue === '': a composition that starts and ends with nochangestill commits, because'' != nullkeeps 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
Summary
OTPField.Inputcommitted every IMEonChangevalue 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 withddddddinstead of the finalddd. Chrome does not reproduce the same over-fill in the reported flow.Changes
This makes OTP input composition-aware, following the Combobox pattern:
onChangeupdate only that temporary text.compositionend, commit the final value once through the normal normalization/replacement path.Fixes #5091