Bug: Regressions in typing experience on Android
Lexical version: 0.32.1 (reproducible on latest playground deployment)
Steps To Reproduce
- Use Samsung Keyboard
- Type something
- Turn on a format like bold or italic
- Type a word and turn off the format
- Type a letter
Try the same thing with another keyboard (e.g FUTO Keyboard)
Link to code example:
The current behavior
In the following video I test the steps mentioned above using Samsung Keyboard. When the letter is typed after turning off the format, the letter still gets added with that format to the node and then the word gets duplicated with the format turned off.
https://github.com/user-attachments/assets/44de1859-c727-4fcd-bebb-a0d530bb8aa5
https://github.com/user-attachments/assets/a819ddad-7c62-4eff-8a4a-81e7654512a6
In the following video I try to replicate the same thing using FUTO Keyboard. I wasn't able to replicate the same exact issue but it has some of its own quirks, especially when trying to backspace.
https://github.com/user-attachments/assets/eac59675-1110-494f-9f34-e388865f4791
https://github.com/user-attachments/assets/e91c098a-4f97-43b8-b21d-fcf33ad6f663
My guess is that this has something to do with composition (see the underline below the "active" word)
The expected behavior
Turning off a format and typing should work as expected without duplication. Same for backspacing.
Impact of fix
We've seen a fair amount of complaints from users on Android that their typing experience is very bad, and almost every time this seems to be on non-Gboard keyboards like Samsung Keyboard, SwiftKey and open-source keyboards.
Samsung keyboard with Firefox is even worse it seems
https://github.com/user-attachments/assets/c2f9b3e4-4f54-4b6e-ac1c-3b11503e0631
Regression is in the issue title, do you know when it did work?
Regression is in the issue title, do you know when it did work?
Not off the top unfortunately. I'm pretty sure this did used to work at some point given that formatting is a common thing and we have a decent amount of users who use these alternative keyboards, but I could be wrong. I can change the wording of that if you want.
I think it's fine to leave it in the title, would just be useful for whoever is debugging this to know. My guess is that it boils down to something in the onBeforeInput or onInput handlers in LexicalEvents, there's a gnarly implicit state machine based on global variables and timers that handles input. could be https://github.com/facebook/lexical/pull/7024
I did some testing on the weekend with a bunch of older versions of Lexical and the issue still happened so it might not be a regression with Lexical itself, its possible that either something changed with the keyboard's implementation or that we just didn't realize this was an issue all along.
Here's some logs when using Samsung Keyboard, turning on bold before typing anything then typing Hel and then turning bold off and typing l which causes Hell to be duplicated:
I tried another open-source keyboard called AnySoftKeyboard which doesn't seem to have the duplication issue and im able to type Hello completely with Hel being bold and lo being non-bold:
One thing I can see is that with AnySoftKeyboard, it only sends the character to be added in insertCompositionText whereas Samsung Keyboard sends the whole word that is being composed with the new character added to it.
Although I'm not sure how much of a difference the insertCompositionText inconsistency makes considering for insertCompositonText beforeinput events we let the browser handle it instead of handling it ourselves.
I also found a bug with Yandex keyboard with latest lexical (0.33.1) - double-tap instead of selecting just delete the word. Gboard works fine.
upd: I found that Yandex keyboard glitches with tip-tap too. e.g: https://github.com/ueberdosis/tiptap/issues/6104 https://github.com/streetwriters/notesnook/issues/6879
upd2: I see CONTROLLED_TEXT_INSERTION_COMMAND with empty payload after SELECTION_CHANGE_COMMAND
upd3: I found that yandex keyboard actively uses a chain of calls compositionupdate<-compositionstart<-compositionend even with just tap/cursor change (always send compositionend), which somehow breaks
the logic inside onCompositionStart to call dispatchCommand(editor, CONTROLLED_TEXT_INSERTION_COMMAND, COMPOSITION_START_CHAR) which causes replacing selection range with \u200b. :\ I tried to patch-package to comment this if, and editing looks much better (but I didn't try how it might broke FF/safari).
I investigated this on a Galaxy device with Samsung Keyboard + Chrome.
What happens when you toggle Bold mid-composition
When you're typing and tap Bold, the selection's format changes to bold but the current text node is still plain. On the next keystroke, compositionstart fires and $handleCompositionStart (in LexicalEvents.ts) checks if the node's format matches the selection's format. After tapping Bold, they don't match — the node is plain, the selection is bold.
When this mismatch is detected, Lexical inserts a zero-width space () into a new bold text node and moves the cursor there. This prepares a landing spot for the upcoming composition. This is the same CONTROLLED_TEXT_INSERTION_COMMAND with COMPOSITION_START_CHAR identified in the Yandex keyboard investigation above.
Why Samsung Keyboard duplicates text
Samsung Keyboard caches the text it's composing and the DOM region where it lives. When Lexical's DOM changes (ZWSP insertion, new nodes, cursor move) reach Android's input system, Android tells the keyboard to restart via restartInput(). Samsung Keyboard then restores its cached text at the new cursor position — but the original text in the original node is still there. Result: text appears twice.
This matches the insertCompositionText observation earlier in this thread — Samsung sends the full composed word (e.g. Hel), not just the new character. When Samsung restores Hel at the ZWSP node while the original Hel stays in the plain node, you get HelHel.
AnySoftKeyboard doesn't have this problem because it only sends the incremental character and doesn't cache/restore the full composing region the same way.
Not a Lexical regression
This ZWSP format-mismatch code has existed since v0.8 (PR #3863, Feb 2023). COMPOSITION_START_CHAR itself dates back to PR #2185 (May 2022). Testing on older Lexical versions confirms the issue appears there too, so the trigger is on the keyboard/browser side.
The same composing-region-cache behavior is documented in Flutter (flutter/flutter#31512, #42273, #51893), React Native, and Slate — Samsung Keyboard duplicating text when the DOM mutates during composition is a cross-platform problem, not specific to Lexical.
Why it may not reproduce on every device
After the Bold tap, $updateDOMSelection calls rootElement.focus() because the toolbar button briefly took focus from the editor. Whether this focus() call ends the active composition depends on the Chrome and Samsung Keyboard version:
- If composition survives the focus change, Samsung's composing range stays in the original node — no duplication.
- If composition ends, Samsung starts a new composition at the ZWSP node — the old text stays in the original node, and you see duplication.
This is why the same Lexical code produces different results on different devices.
Korean input — jamo separation
Same root cause, different symptom. Korean composition cycles per jamo (ㅎ→하→한). Each time the ZWSP path fires during a format change, the cycling gets interrupted and produces separated jamo (ㅎㅏ instead of 하).
This analysis is based on code reading and local testing where I couldn't fully reproduce the duplication — if anyone can consistently reproduce it, sharing the device model, Chrome version, and Samsung Keyboard version would help narrow down the exact conditions. I'm planning to file a separate issue for the Korean jamo case and open a fix PR that defers the format application to compositionEnd instead of inserting the ZWSP on format mismatch.
Yandex keyboard — related but different trigger
The Yandex keyboard issue reported above hits the same ZWSP insertion code but through a different condition. Yandex fires compositionend → compositionstart on every cursor change/tap, so when text is selected, the !selection.isCollapsed() branch inserts a ZWSP that replaces the selected text — causing deletion. The Samsung/Korean fix (which targets the formatMismatch condition) won't cover this; Yandex needs its own fix on the isCollapsed path.