Skip to content

[lexical] Bug Fix: keep caret above the on-screen keyboard after Enter - #8486

Merged
etrepum merged 3 commits into
meta:mainfrom
jWA86:fix/auto-scroll-visual-viewport-mobile
May 10, 2026
Merged

etrepum merged 3 commits into
meta:mainfrom
jWA86:fix/auto-scroll-visual-viewport-mobile

Conversation

@jWA86

@jWA86 jWA86 commented May 9, 2026

Copy link
Copy Markdown
Contributor

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: Chrome / Android, long bullet list. Enter -> new item is hidden behind the keyboard until you type 2 characters.
Before.mp4
  • After: same scenario, scroll is immediate on Enter, no double-Enter, the new item appears above the keyboard.
after.mp4

Root cause

scrollIntoViewIfNeeded(LexicalUtils.ts) compares the selection rect against window.innerHeight in 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.visualViewport when available; fall back to innerHeight for environments without it.

const visualViewport = defaultView.visualViewport;
if (visualViewport) {
  const offsetTop = visualViewport.offsetTop;
  targetTop = offsetTop;
  targetBottom = offsetTop + visualViewport.height;
} else {
  targetTop = 0;
  targetBottom = getWindow(editor).innerHeight;
}

getBoundingClientRect already reports in layout-viewport coordinates, and visualViewport.offsetTop + height is the visible-area bottom in those same coordinates, so the surrounding math (currentBottom > targetBottom -> scrollBy(0, diff)) stays consistent.
The CSS scroll-padding accounting 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

  • New e2e test Auto scroll respects mobile visual viewport > Pressing Enter scrolls new caret above the on-screen keyboard (in packages/lexical-playground/__tests__/e2e/AutoScroll.spec.mjs):
    • Sets a 400*600 viewport, fills a bullet list past overflow, then stubs window.visualViewport via a real EventTarget that reports height: 300 and dispatches the resize event to mirror the real "keyboard appears" sequence.
    • Reads the caret position read-only via the element-anchor child rect (no DOM mutation, so Lexical's mutation observer / reconciler is not triggered).
    • Asserts caretRect.bottom <= visualViewport.offsetTop + visualViewport.height (so iOS-style non-zero offsetTop is also covered).
    • Fails before the fix with caretRect.bottom ~ 600 (sat at innerHeight, behind the simulated keyboard);
    • passes after with caretRect.bottom ~ 280.
  • Existing "Auto scroll while typing" suite still passes. The fix only changes the body-scroll branch; tests that scroll the editor container itself (addScroll(...)) hit the unchanged else branch.
  • Manual repro: Chrome on Android (real device), long bullet list at the bottom of the editor; before -> keyboard hides the new item until the second character; after -> immediate scroll on Enter.

Known limitations of the test setup

  • The stub doesn't simulate the timing of a real keyboard animation (focus -> keyboard slides up -> relayout -> IME events). The synchronous read in scrollIntoViewIfNeeded doesn't depend on that timing, but a future iOS-specific test might want to.
  • iOS WebKit offsetTop > 0 cases (visual viewport offset when the URL bar collapses) are exercised by the assertion math but not by the stub values

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.
@vercel

vercel Bot commented May 9, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment May 9, 2026 8:27pm
lexical-playground Ready Ready Preview, Comment May 9, 2026 8:27pm

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label May 9, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label May 9, 2026
etrepum
etrepum previously approved these changes May 9, 2026
@etrepum
etrepum dismissed their stale review May 9, 2026 19:25

Test failures

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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).
@jWA86

jWA86 commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

need to re-run the affected jobs.
I tested locally, verified the change against chromium / firefox / webkit, both rich-text and rich-text-with-collab, and the new test passes deterministically across all matrices.

@etrepum
etrepum added this pull request to the merge queue May 10, 2026
Merged via the queue into meta:main with commit 19a034b May 10, 2026
39 checks passed
@etrepum etrepum mentioned this pull request May 28, 2026
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

2 active deployments
Preview – lexical — 46f8fdc4 Deployed May 9, 2026 by vercel[bot]
Preview – lexical-playground — 46f8fdc4 Deployed May 9, 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 Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants