Repository navigation
[lexical] Bug Fix: keep caret above the on-screen keyboard after Enter - #8486
Merged
Merged
Conversation
scrollIntoViewIfNeeded compared the selection rect against window.innerHeight (the layout viewport). On mobile the on-screen keyboard does not change innerHeight, only the visual viewport, so the new caret created by Enter often lands behind the keyboard. The browser's native auto-scroll only kicks in once the user types a character or two; in a list this leads users to press Enter again, which exits the list. Read window.visualViewport.offsetTop / .height when available so the caret lands in the keyboard-clear area; fall back to innerHeight when visualViewport is unavailable. getBoundingClientRect already reports in layout-viewport coords, so offsetTop + height is the visible bottom in those same coords and the surrounding scroll math is unchanged. Adds an e2e test that stubs window.visualViewport via a real EventTarget (dispatching a resize event to mirror the real keyboard-appears sequence), fills a long bullet list past overflow, presses Enter, and asserts the new caret stays within visualViewport.offsetTop + visualViewport.height. The test fails before the fix (caret around innerHeight) and passes after (caret within the visual viewport). The existing Auto scroll tests are unaffected: they exercise the in-editor overflow branch which is untouched.
jWA86
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon,
takuyakanbr and
zurfyx
as code owners
May 9, 2026 17:40
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
previously approved these changes
May 9, 2026
etrepum
reviewed
May 9, 2026
etrepum
left a comment
Collaborator
There was a problem hiding this comment.
It looks like this new e2e test does not work consistently
…against collab + sub-pixel rounding Two flake sources surfaced in CI on the previous commit: - Collab mode: clicking the .block-controls dropdown timed out because the dropdown sat outside the 400-px-wide viewport in the split collab layout. Switch from the local UI-click toggleBulletList to the shared keyboard-shortcut version (Ctrl/Cmd+Shift+8), which doesn't depend on toolbar position or viewport size. - Firefox layout rounding: the assertion compared a fractional caret bottom (e.g. 300.27) against an integer visualViewport bound (300), failing by 0.27 px even though Lexical scrolled correctly. Allow a 1-px tolerance — the original bug overshoots by ~280 px so this still catches the regression. Verified locally on chromium / firefox / webkit, both rich-text and rich-text-with-collab modes (10x / 5x / 3x repeats, 0 flakes).
Contributor
Author
|
need to re-run the affected jobs. |
etrepum
approved these changes
May 10, 2026
Merged
This was referenced Jun 21, 2026
Open
4 tasks
corinthionia
added a commit
to corinthionia/lexical
that referenced
this pull request
Aug 7, 2026
…ffsetTop ## Description `scrollIntoViewIfNeeded` compares `selectionRect`, which is in layout-viewport coordinates, against a target band that started at `visualViewport.offsetTop` (meta#8486). On iOS WebKit the layout viewport itself slides under the on-screen keyboard, so the visible area still starts at 0 in that frame: adding offsetTop shifts the band down by offsetTop, and the caret is scrolled to offsetTop px below where it should land — partially under the keyboard — or, when it already sits above the shifted band, dragged with a bogus `scrollBy` on every keystroke. Reproduces in the last paragraph with the page scrolled, because that is where offsetTop grows and where the document has no scroll room left. Two shapes on device: - Latin input: press Enter for a new empty line, type the first character, and the caret lands slightly below the keyboard edge (short by exactly offsetTop). Safari re-centers it on the following characters and it stays put after that. - Korean input: the caret jitters up and down on every character. The iOS Korean keyboard sends `deleteContentBackward` + `insertText` per keystroke with no composition events, so the caret is re-measured twice per character, including on the intermediate just-deleted state, which re-triggers the bad scroll instead of settling. Keep only the shrunken height, which is what actually keeps the caret above the keyboard. ## Test plan Before: on iPhone Safari, in a scrolled Lexical Playground page, the caret lands below the keyboard on the first character of a new last line (Latin) and jitters once per character (Korean). The e2e case below fails with the caret settling at 450px (= offsetTop 150 + height 300) when the visible band ends at 300px. After: neither shape reproduces on device, verified with both the English and the Korean keyboard; the caret still scrolls above the on-screen keyboard. - `LexicalUtils.test.ts`: new unit test — with `offsetTop: 200, height: 300`, a caret already inside `[0, 300]` triggers no `scrollBy` (before the fix it scrolled by -100), and a caret hidden behind the keyboard still scrolls by 120. - `AutoScroll.spec.mjs`: the keyboard e2e test now also runs with a non-zero `visualViewport.offsetTop`. The pre-existing case stubbed offsetTop as 0, so the offsetTop term was never exercised. - `packages/lexical` unit suite (833) green; AutoScroll e2e green on chromium. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
corinthionia
added a commit
to corinthionia/lexical
that referenced
this pull request
Aug 7, 2026
…fsetTop when zoomed ## Description `scrollIntoViewIfNeeded` compares `selectionRect`, which is in layout-viewport coordinates, against a target band that started at `visualViewport.offsetTop` (meta#8486). `offsetTop` only belongs in that band when it really is an offset within that frame, which `visualViewport.scale` tells apart: - `scale === 1`: offsetTop is either 0 (Blink shrinks the visual viewport for the on-screen keyboard without panning it) or the iOS WebKit keyboard slide, where the layout viewport itself moves under the keyboard so the visible area still starts at 0 in that frame. Adding it shifts the band down by offsetTop, and the caret is scrolled to offsetTop px below where it should land — partially under the keyboard — or, when it already sits above the shifted band, dragged with a bogus `scrollBy` on every keystroke. - `scale !== 1`: the visual viewport is panned over the layout viewport, so the visible band genuinely is [offsetTop, offsetTop + height] and must keep it. Gating on scale distinguishes the two causes directly, with no engine sniffing. Reproduces in the last paragraph with the page scrolled, because that is where offsetTop grows and where the document has no scroll room left. Two shapes on device: - Latin input: press Enter for a new empty line, type the first character, and the caret lands slightly below the keyboard edge (short by exactly offsetTop). Safari re-centers it on the following characters and it stays put after that. - Korean input: the caret jitters up and down on every character. The iOS Korean keyboard sends `deleteContentBackward` + `insertText` per keystroke with no composition events, so the caret is re-measured twice per character, including on the intermediate just-deleted state, which re-triggers the bad scroll instead of settling. Known limitation: on iOS with pinch zoom and the keyboard up at the same time, offsetTop conflates the zoom pan with the keyboard slide and the band is approximate. There is no API to separate them, and the combination is far rarer than the reported bug. ## Test plan Before: on iPhone Safari, in a scrolled Lexical Playground page, the caret lands below the keyboard on the first character of a new last line (Latin) and jitters once per character (Korean). The e2e case below fails with the caret settling at 450px (= offsetTop 150 + height 300) when the visible band ends at 300px. After: neither shape reproduces on device, verified with both the English and the Korean keyboard; the caret still scrolls above the on-screen keyboard. - `LexicalUtils.test.ts`: two new unit tests. Unzoomed (`offsetTop: 200, height: 300, scale: 1`), a caret already inside `[0, 300]` triggers no `scrollBy` — before the fix it scrolled by -100 — and a caret hidden behind the keyboard still scrolls by 120. Zoomed (`scale: 2`), a caret above the `[200, 500]` band still scrolls by -100; dropping the scale gate fails this one with zero calls. - `AutoScroll.spec.mjs`: the keyboard e2e test now also runs with a non-zero `visualViewport.offsetTop`. The pre-existing case stubbed offsetTop as 0, so the offsetTop term was never exercised since meta#8486 introduced it. - `packages/lexical` unit suite (834) green; AutoScroll e2e green on chromium. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
On mobile, after pressing Enter (in a list, paragraph, heading, table cell ...), the new caret often lands behind the on-screen keyboard. The browser's own auto-scroll only kicks in once the user starts typing, typically on the second character.
In a list this is particularly painful: users press Enter again, thinking nothing happened, which exits the list and destroys the item they were trying to add.
Before / After
Before.mp4
after.mp4
Root cause
scrollIntoViewIfNeeded(LexicalUtils.ts) compares the selection rect againstwindow.innerHeightin the body-scroll branch. On a desktop, layout viewport is more or less = visual viewport, so this is correct.On mobile with the keyboard open, the layout viewport stays full-screen while the visual viewport shrinks to the area above the keyboard. The function therefore concludes the caret is already in view (
currentBottom <= innerHeight) and does not scroll, even though visually the caret is behind the keyboard.When the user starts typing, the browser's native auto-scroll-into-visual-viewport heuristic eventually catches up, that's why "the second character fixes it."
Fix
In the body-scroll branch, read
window.visualViewportwhen available; fall back toinnerHeightfor environments without it.getBoundingClientRectalready reports in layout-viewport coordinates, andvisualViewport.offsetTop + heightis the visible-area bottom in those same coordinates, so the surrounding math (currentBottom > targetBottom->scrollBy(0, diff)) stays consistent.The CSS
scroll-paddingaccounting from #8218 is unchanged.The non-body branch (in-editor overflow) is untouched.
Scope
The bug affects every Enter/return key that creates a new block: paragraphs, list items (
<li>), headings, table cells, custom decorator nodes, and more generally any post-update selection change that hits the body-scroll branch.Lists make it particularly user-visible because the natural workaround (press Enter again) destroys data. Fixing the underlying scroll math fixes all of them at once.
Test Plan
Auto scroll respects mobile visual viewport > Pressing Enter scrolls new caret above the on-screen keyboard(inpackages/lexical-playground/__tests__/e2e/AutoScroll.spec.mjs):window.visualViewportvia a realEventTargetthat reportsheight: 300and dispatches theresizeevent to mirror the real "keyboard appears" sequence.caretRect.bottom <= visualViewport.offsetTop + visualViewport.height(so iOS-style non-zerooffsetTopis also covered).caretRect.bottom ~ 600(sat atinnerHeight, behind the simulated keyboard);caretRect.bottom ~ 280.addScroll(...)) hit the unchangedelsebranch.Known limitations of the test setup
scrollIntoViewIfNeededdoesn't depend on that timing, but a future iOS-specific test might want to.offsetTop > 0cases (visual viewport offset when the URL bar collapses) are exercised by the assertion math but not by the stub values