Skip to content

[lexical][lexical-code-core][lexical-playground] Bug Fix: reveal the caret in code blocks and other containers that scroll sideways - #9215

Merged
etrepum merged 5 commits into
meta:mainfrom
Om-singhaI:fix/code-block-caret-scroll
Sep 25, 2026
Merged

etrepum merged 5 commits into
meta:mainfrom
Om-singhaI:fix/code-block-caret-scroll

Conversation

@Om-singhaI

@Om-singhaI Om-singhaI commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part of #8459. This is the scrolling half of that issue. The word wrap half is a separate PR stacked on this one, so the core change can be reviewed on its own.

In a code block with a line wider than the block, End and Home (Cmd+Right and Cmd+Left on macOS) and Enter move the caret, but the block doesn't scroll, so the caret ends up out of view. Lexical handles those moves itself and prevents the browser's own caret scroll, and scrollIntoViewIfNeeded only scrolled vertically.

[lexical] $updateDOMSelection now passes the caret's DOM node to scrollIntoViewIfNeeded. Before the vertical pass, it walks from that node up to and including the root, and scrolls each overflow-x: auto or scroll container on the way sideways so the caret is inside it, less its scroll-padding. The page and the root's ancestors are never scrolled sideways, and the vertical pass is unchanged. It handles right to left containers, CSS zoom and transform: scale, and WebKit giving no rect for a caret at the logical end of right to left text (getCaretRect measures the character next to it instead). It uses isHTMLElement and getParentElement, not instanceof.

When a container scrolls back toward the start and the caret would also fit at scrollLeft 0, it goes all the way to 0. That covers the case @etrepum found on #8495, where Enter scrolled left "but not all the way". Home and Enter now show the whole start of the line, including the indentation they leave the caret after.

On #8495 the suggestion was a single walk that starts at the caret. I kept the vertical walk as it is and added a separate horizontal one, because starting the vertical walk inside the editor without an overflow-y check would also write scrollTop on overflow: hidden elements. Moving the vertical walk can be its own change.

[lexical-code-core][lexical-playground] The data-gutter float covers the first 52px of a code block, so .PlaygroundEditorTheme__code gets scroll-padding-left: 52px to keep the caret out from under it, and padding-inline-end: 8px so the caret after the last character of the longest line isn't drawn just past the edge. The @lexical/code-core README gets a Caret scrolling section for theme authors.

This builds on #8495 by @Frank20021 and #8468 by @KesavSundarM.

Things to know:

  • Moves Lexical makes inside a table scroll wrapper, like Tab between cells, paste and undo, now reveal the caret sideways there too, and scroll all the way back when the caret fits at the start.
  • In a highlighted code block typing goes through this too, because the highlighter retokenizes the line and Lexical puts the caret back. Plain arrow keys and clicks keep the browser's own scrolling, and extending a selection with Shift doesn't scroll sideways.
  • SKIP_SCROLL_INTO_VIEW_TAG opts out of both passes. Neither pass handles scroll-behavior: smooth.
  • Firefox leaves the end padding out of the scroll range while a sticky box like the gutter is inside, so at the far end of the longest line the caret can be drawn a fraction of a pixel past the block's edge: 0.33px at the left end of a right to left line on macOS, and 0.25px at the right end of a left to right line on Windows. The tests allow 0.5px at those two steps, in Firefox only. Every other check has no slack.

Test plan

Before

The new tests with LexicalUtils.ts, LexicalSelection.ts and PlaygroundEditorTheme.css from main.

$ pnpm vitest run --project unit packages/lexical/src/__tests__/unit/LexicalUtils.test.ts -t scrollIntoViewIfNeeded
 × scrolls to reveal a caret past the right edge
   AssertionError: expected +0 to be 701 // Object.is equality
 × scrolls all the way back when the caret fits at scrollLeft 0
   AssertionError: expected 400 to be +0 // Object.is equality
 × scrolls right to left containers with negative scrollLeft
   AssertionError: expected -400 to be +0 // Object.is equality
 ...
      Tests  15 failed | 9 passed | 50 skipped (74)

$ VITEST_BROWSER=chromium pnpm vitest run --project browser packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts
 × caret scrolling in a code block with the data-gutter float > End reveals the end of a long line, and Home scrolls all the way back
   AssertionError: {"caretBottom":33,"caretLeft":5179.40625,"caretRight":5179.40625,"caretTop":18,"lineStartX":52,"scrollLeft":0,"scrollportLeft":0,"scrollportRight":360}: expected 0 to be greater than 0
 ...
      Tests  9 failed (9)

All 9 fail in Firefox and WebKit too. The e2e spec fails in all three browsers. In Chromium, after the long lines are typed the block hasn't scrolled and the caret is about 2,240px past its right edge:

$ pnpm run test-e2e-chromium --retries=0 packages/lexical-playground/__tests__/e2e/CodeBlockCaretScroll.spec.mjs
  ✘  1 Code block caret scrolling › Home, End and Enter keep the caret visible in long lines
    Received: "{\"caretLeft\":3384.140625,\"caretRight\":3384.140625,\"lineStartX\":159,\"scrollLeft\":0,\"scrollportLeft\":107,\"scrollportRight\":1143}"
  1 failed

After

$ pnpm vitest run --project unit packages/lexical/src/__tests__/unit/LexicalUtils.test.ts -t scrollIntoViewIfNeeded
      Tests  24 passed | 50 skipped (74)

$ VITEST_BROWSER=chromium pnpm vitest run --project browser packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts
      Tests  9 passed (9)

$ pnpm run test-e2e-chromium --retries=0 packages/lexical-playground/__tests__/e2e/CodeBlockCaretScroll.spec.mjs
  1 passed

The browser test and the e2e spec also pass in Firefox and WebKit. tsc and flow pass, and prettier and eslint are clean on the changed files.

Since the core change reaches tables too, I also ran the existing tests near it in Chromium, Firefox and WebKit, on the stacked word wrap branch, which includes this one. They all pass: the StickyScrollbar, TableTrailingCaret, Issue6822BlockCursorArrowKeys and ShadowRootSelection browser tests, and the CodeBlock, CodeActionMenu, AutoScroll and Tables e2e specs. The only skips are the specs' own.

All of this ran headless on macOS with Node 25, so Linux, Windows and collab aren't covered. Could you add the extended-tests label?

…ditor to reveal the caret

## Description

scrollIntoViewIfNeeded only scrolled vertically, from the editor root up to the window. When Lexical moved the caret inside an element that scrolls sideways, like a code block with a long line, that element never scrolled. After MOVE_TO_END, MOVE_TO_START or Enter in a code block, which all prevent the browser's own caret scroll, the caret could be left out of view.

$updateDOMSelection now passes the caret's DOM node. Before the vertical pass, scrollIntoViewIfNeeded walks from that node up to and including the editor root. Each overflow-x auto or scroll container on the way is scrolled sideways so the caret is inside its scrollport, less its scroll-padding. The page and the editor's ancestors are never scrolled sideways, and the vertical pass is unchanged.

When a container scrolls back toward the start, and the caret would also fit at scrollLeft 0, it goes all the way to 0. So Home and Enter show the whole start of the line, including the indentation they leave the caret after, whatever the width of a gutter. Right to left containers use negative scrollLeft.

The target scrollLeft is rounded in the direction the container scrolls. Browsers round a fractional scrollLeft to a whole pixel, so rounding the other way could leave the caret up to half a pixel outside the view. In Chromium and WebKit a right to left caret revealed at the left edge of a code block stopped 0.14px and 0.13px outside it.

The caret rect and the container's rect are in viewport px, but clientWidth, scroll-padding and scrollLeft are in the container's own px. The scale between them is the rect's width over offsetWidth, so the reveal is also right under CSS zoom or a transform: scale. offsetWidth is rounded, so a difference of 1px or less counts as no scale.

WebKit gives a collapsed range at the logical end of right to left text no rect at all. getBoundingClientRect returns zeros and getClientRects is empty. A rect of zeros lies above the editor, so scrollIntoViewIfNeeded returned early and neither pass ran for that caret. $updateDOMSelection now measures a text caret with getCaretRect. When a collapsed range in a Text node has no rect, it measures the character next to the caret instead. The caret is at one of that character's edges, so revealing the character reveals the caret.

This only runs for caret moves that Lexical applies to the DOM. That includes the caret a code highlighter puts back after it retokenizes the line on each keystroke. Plain arrow keys and clicks keep the browser's own scrolling.

This is the scrolling part of meta#8459. The word wrap that the issue also asks for is a separate change.

## Test plan

### Before

New tests, with LexicalUtils.ts and LexicalSelection.ts from the parent commit:

pnpm vitest run --project unit packages/lexical/src/__tests__/unit/LexicalUtils.test.ts -t scrollIntoViewIfNeeded

 × scrolls to reveal a caret past the right edge
   AssertionError: expected +0 to be 701 // Object.is equality
 × scrolls all the way back when the caret fits at scrollLeft 0
   AssertionError: expected 400 to be +0 // Object.is equality
 × keeps the caret out of the scroll-padding when it does not fit at scrollLeft 0
   AssertionError: expected 1000 to be 448 // Object.is equality
 × scrolls right to left containers with negative scrollLeft
   AssertionError: expected -400 to be +0 // Object.is equality
 × rounds the scroll position toward the caret
   AssertionError: expected -1000 to be -1001 // Object.is equality
 × reveals the inline start of a rect wider than the view
   AssertionError: expected +0 to be 91 // Object.is equality
 × scrolls nested scroll containers from the caret outward
   AssertionError: expected +0 to be 701 // Object.is equality
 × reveals the caret in a zoomed or scaled scroll container
   AssertionError: expected +0 to be 201 // Object.is equality
 × does not scroll the ancestors of the root sideways
   AssertionError: expected +0 to be 701 // Object.is equality
 × scrolls the editor root itself sideways
   AssertionError: expected +0 to be 211 // Object.is equality
 × still scrolls the window vertically, and only vertically
   AssertionError: expected +0 to be 701 // Object.is equality
 × returns the rect of a caret that has one
 × measures the character before a caret that has no rect
 × measures the first character for a caret at the start that has no rect
 × keeps the empty rect when there is no character to measure
   TypeError: getCaretRect is not a function
      Tests  15 failed | 9 passed | 50 skipped (74)

Without the rounding, the tests that scroll by a fraction of a pixel fail:

pnpm vitest run --project unit packages/lexical/src/__tests__/unit/LexicalUtils.test.ts -t "rounds the scroll position|zoomed or scaled"

 × rounds the scroll position toward the caret
   AssertionError: expected -1000.14 to be -1001 // Object.is equality
 × reveals the caret in a zoomed or scaled scroll container
   AssertionError: expected 200.5 to be 201 // Object.is equality
      Tests  2 failed | 72 skipped (74)

The WebKit case, measured in headless WebKit from Playwright with a 650 character right to left Text node in a code element:

 offset 0    rect left 359.0, height 15.1, 1 client rect
 offset 649  rect left -4707.0, height 15.1, 1 client rect
 offset 650  rect left 0.0, top 0.0, height 0.0, 0 client rects

Chromium and Firefox have a rect at offset 650 (left -4710.8 and -4714.3). In WebKit, without getCaretRect, the right to left case of the browser mode test in the commit on top of this one fails because the block never scrolls:

VITEST_BROWSER=webkit pnpm vitest run --project browser packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts

 × caret scrolling in a code block with the data-gutter float > right to left lines scroll the other way and keep the caret visible
   AssertionError: expected 0 to be less than 0
      Tests  1 failed | 8 passed (9)

### After

pnpm vitest run --project unit packages/lexical/src/__tests__/unit/LexicalUtils.test.ts -t scrollIntoViewIfNeeded

 ✓ scrollIntoViewIfNeeded respects scroll-padding on document element
 ✓ scrollIntoViewIfNeeded ignores a selection rect that lies entirely above the editor
 ✓ scrolls to reveal a caret past the right edge
 ✓ scrolls all the way back when the caret fits at scrollLeft 0
 ✓ keeps the caret out of the scroll-padding when it does not fit at scrollLeft 0
 ✓ scrolls right to left containers with negative scrollLeft
 ✓ rounds the scroll position toward the caret
 ✓ reveals the inline start of a rect wider than the view
 ✓ leaves a visible caret alone and skips styles when nothing overflows
 ✓ ignores an element with overflow-x: visible
 ✓ ignores an element with overflow-x: hidden
 ✓ ignores an element with overflow-x: clip
 ✓ ignores a scroller that does not contain the caret vertically
 ✓ scrolls nested scroll containers from the caret outward
 ✓ reveals the caret in a zoomed or scaled scroll container
 ✓ does not scroll the ancestors of the root sideways
 ✓ scrolls the editor root itself sideways
 ✓ ignores a rect above the editor and an empty rect
 ✓ still scrolls the window vertically, and only vertically
 ✓ only scrolls vertically without a selection node
 ✓ returns the rect of a caret that has one
 ✓ measures the character before a caret that has no rect
 ✓ measures the first character for a caret at the start that has no rect
 ✓ keeps the empty rect when there is no character to measure
      Tests  24 passed | 50 skipped (74)

pnpm vitest run --project unit with LexicalUtils, LexicalUpdateTags, LexicalSelection, LexicalSelectionChangeRefCount, LexicalSelectionResolveLeafPosition, LexicalDOMCapturedSelection, LexicalSlotSelection and LexicalRootSelectionPhantomParagraph

 Test Files  8 passed (8)
      Tests  214 passed (214)

pnpm vitest run --project unit packages/lexical/src

 Test Files  76 passed (76)
      Tests  1765 passed (1765)

pnpm vitest run --project scripts-unit scripts/__tests__/unit/treeShakingSource.test.ts -t 'LexicalUtils.ts|LexicalSelection.ts'

      Tests  2 passed | 333 skipped (335)

pnpm run tsc and pnpm run flow pass, and prettier and eslint pass on the changed files. The browser mode test and the e2e spec that run this code in Chromium, Firefox and WebKit are in the commit on top of this one.
… of the code gutter and the block's edge when a code block scrolls sideways

## Description

Lexical now scrolls a code block sideways to reveal the caret, but nothing told it about the playground's data-gutter float. The float stays over the first 52px of the block while the block scrolls. So when the block scrolled back only part of the way, the caret stopped under the gutter. And at the end of the longest line the block is scrolled as far as it goes, and the caret could be drawn just past its edge: past the right edge of a left to right line in Chromium, and past the left edge of a right to left line in all three browsers.

The .PlaygroundEditorTheme__code rule now has scroll-padding-left: 52px, which Lexical keeps the caret out of, and padding-inline-end: 8px of room for the caret after the last character of the longest line. In a right to left block that padding is on the left, where the float is, so the float gets a margin and a sticky offset of -8px there. In Chromium and Firefox the float then stays flush with the left edge while the block is scrolled all the way back. WebKit draws the float of a right to left block about 10px from the right edge instead, at x 307 to 349 in a 360px block, over the first characters of the line. It does the same with the CSS from main, so this change does not cause it.

Firefox leaves that padding out of the scroll range while the block has a sticky box in it, like the float. At the far left end of a right to left line, scrollLeft stops at -4714 with or without the padding, and at -4722 with the float made position: static. So in Firefox the caret there is still drawn 0.33px left of the block's edge.

The @lexical/code-core README gets a Caret scrolling section. It says which caret moves scroll a code block sideways, when the block scrolls all the way back, and how a theme keeps the caret clear of a gutter and of the block's edge.

CodeBlockCaretScroll.test.ts builds an editor with the playground theme and its CSS in a 360px container. With the data-gutter float it checks:
- End scrolls the block and leaves the caret clear of the gutter, and Home scrolls it all the way back.
- Moving from the end of the long line to the end of a shorter one scrolls back only part of the way, and the caret stops at the gutter's edge.
- Enter after typing at the end of a long line scrolls back to the start of the new line.
- Smart Home on a line indented by 12 spaces, and Enter on it, scroll all the way back with the indentation in view.
- A right to left line scrolls with negative scrollLeft and keeps the caret visible.

The caret must be inside the scrollport, with no slack at either edge. The one exception is the caret at the far left end of the right to left line in Firefox, which may be up to 0.5px past the left edge, for the reason above. The gutter setups are a list with one entry, so another gutter can run the same cases. The float also runs with the editor scaled by 0.5 and by 2 with a CSS transform, and by 2 with CSS zoom. There End and smart Home must keep the caret visible and clear of the gutter, measured in viewport px. A number drawn over the caret is found by hit testing with only the code block's pseudo elements as targets. WebKit hit tests the float of a right to left block where it draws it, near the right edge. WebKit gives a caret at the logical end of right to left text no rect, so there the test measures the character before the caret.

CodeBlockCaretScroll.spec.mjs types the long lines into the playground and uses real Home, End, Enter and Backspace keys.

## Test plan

### Before

The new tests with packages/lexical/src/LexicalUtils.ts, packages/lexical/src/LexicalSelection.ts and packages/lexical-playground/src/themes/PlaygroundEditorTheme.css from main:

VITEST_BROWSER=chromium pnpm vitest run --project browser packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts

 × caret scrolling in a code block with the data-gutter float > End reveals the end of a long line, and Home scrolls all the way back
   AssertionError: {"caretBottom":33,"caretLeft":5179.40625,"caretRight":5179.40625,"caretTop":18,"lineStartX":52,"scrollLeft":0,"scrollportLeft":0,"scrollportRight":360}: expected 0 to be greater than 0
 × caret scrolling in a code block with the data-gutter float > moving back to the end of a shorter line stops the caret at the gutter, not under it
   AssertionError: expected 0 to be greater than 0
 × caret scrolling in a code block with the data-gutter float > Enter after typing at the end of a long line scrolls back to the start of the new line
   AssertionError: expected 0 to be greater than 0
 × caret scrolling in a code block with the data-gutter float > smart Home on an indented line scrolls all the way back and shows the indentation
   AssertionError: expected 0 to be greater than 0
 × caret scrolling in a code block with the data-gutter float > Enter on an indented line scrolls all the way back and shows the copied indentation
   AssertionError: expected 0 to be greater than 0
 × caret scrolling in a code block with the data-gutter float > right to left lines scroll the other way and keep the caret visible
   AssertionError: expected 0 to be less than 0
 × caret scrolling in a code block scaled by 0.5 with CSS transform > End and smart Home keep the caret visible and clear of the gutter
   AssertionError: {"caretBottom":22.4375,"caretLeft":1638.5703125,"caretRight":1638.5703125,"caretTop":14.9375,"lineStartX":26,"scrollLeft":0,"scrollportLeft":0,"scrollportRight":360}: expected 0 to be greater than 0
 × caret scrolling in a code block scaled by 2 with CSS transform > End and smart Home keep the caret visible and clear of the gutter
   AssertionError: {"caretBottom":89.75,"caretLeft":6554.28125,"caretRight":6554.28125,"caretTop":59.75,"lineStartX":104,"scrollLeft":0,"scrollportLeft":0,"scrollportRight":360}: expected 0 to be greater than 0
 × caret scrolling in a code block scaled by 2 with CSS zoom > End and smart Home keep the caret visible and clear of the gutter
   AssertionError: {"caretBottom":89.765625,"caretLeft":6554.125,"caretRight":6554.125,"caretTop":59.765625,"lineStartX":104,"scrollLeft":0,"scrollportLeft":0,"scrollportRight":360}: expected 0 to be greater than 0
      Tests  9 failed (9)

With VITEST_BROWSER=firefox and VITEST_BROWSER=webkit the same 9 tests fail the same way.

The e2e runs need the playground dev server on port 3000 (pnpm run dev).

pnpm run test-e2e-chromium --retries=0 packages/lexical-playground/__tests__/e2e/CodeBlockCaretScroll.spec.mjs

  ✘  1 Code block caret scrolling › Home, End and Enter keep the caret visible in long lines
    Expected: "ok"
    Received: "{\"caretLeft\":3384.140625,\"caretRight\":3384.140625,\"lineStartX\":159,\"scrollLeft\":0,\"scrollportLeft\":107,\"scrollportRight\":1143}"
  1 failed

It fails in WebKit at the same step. Firefox scrolls the block itself while the text is inserted, so there it fails at the next step, where smart Home leaves the block scrolled:

    Received: "{\"caretLeft\":-3026,\"caretRight\":-3026,\"lineStartX\":159,\"scrollLeft\":3279,\"scrollportLeft\":107,\"scrollportRight\":1143}"

With only PlaygroundEditorTheme.css from main, and the core fix in place:

VITEST_BROWSER=chromium pnpm vitest run --project browser packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts

 × caret scrolling in a code block with the data-gutter float > End reveals the end of a long line, and Home scrolls all the way back
   AssertionError: {"caretBottom":33,"caretLeft":360.40625,"caretRight":360.40625,"caretTop":18,"lineStartX":52,"scrollLeft":4819,"scrollportLeft":0,"scrollportRight":360}: expected 360.40625 to be less than or equal to 360
 × caret scrolling in a code block with the data-gutter float > moving back to the end of a shorter line stops the caret at the gutter, not under it
   AssertionError: {"caretBottom":44.875,"caretLeft":0.140625,"caretRight":0.140625,"caretTop":29.875,"lineStartX":52,"scrollLeft":3277,"scrollportLeft":0,"scrollportRight":360}: expected 0.140625 to be greater than or equal to 51
 × caret scrolling in a code block with the data-gutter float > right to left lines scroll the other way and keep the caret visible
   AssertionError: {"caretBottom":25,"caretLeft":-0.140625,"caretRight":-0.140625,"caretTop":10,"lineStartX":359.984375,"scrollLeft":-4714,"scrollportLeft":0,"scrollportRight":360}: expected -0.140625 to be greater than or equal to 0
      Tests  3 failed | 6 passed (9)

In WebKit the second and the third fail, with the caret at x 0, under the gutter, and at x -0.13. In Firefox only the second fails, with the caret at x 0.33. At the end of the longest left to right line, Firefox and WebKit keep the caret inside the block even without the end padding. The e2e spec only has left to right lines. It fails the same way in Chromium, after End on the longest line, and passes in Firefox and WebKit:

    Received: "{\"caretLeft\":1143.40625,\"caretRight\":1143.40625,\"lineStartX\":159,\"scrollLeft\":4143,\"scrollportLeft\":107,\"scrollportRight\":1143}"

### After

VITEST_BROWSER=chromium pnpm vitest run --project browser --reporter=verbose packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts

 ✓ caret scrolling in a code block with the data-gutter float > End reveals the end of a long line, and Home scrolls all the way back
 ✓ caret scrolling in a code block with the data-gutter float > moving back to the end of a shorter line stops the caret at the gutter, not under it
 ✓ caret scrolling in a code block with the data-gutter float > Enter after typing at the end of a long line scrolls back to the start of the new line
 ✓ caret scrolling in a code block with the data-gutter float > smart Home on an indented line scrolls all the way back and shows the indentation
 ✓ caret scrolling in a code block with the data-gutter float > Enter on an indented line scrolls all the way back and shows the copied indentation
 ✓ caret scrolling in a code block with the data-gutter float > right to left lines scroll the other way and keep the caret visible
 ✓ caret scrolling in a code block scaled by 0.5 with CSS transform > End and smart Home keep the caret visible and clear of the gutter
 ✓ caret scrolling in a code block scaled by 2 with CSS transform > End and smart Home keep the caret visible and clear of the gutter
 ✓ caret scrolling in a code block scaled by 2 with CSS zoom > End and smart Home keep the caret visible and clear of the gutter
      Tests  9 passed (9)

pnpm run test-e2e-chromium --retries=0 packages/lexical-playground/__tests__/e2e/CodeBlockCaretScroll.spec.mjs

  ✓  1 Code block caret scrolling › Home, End and Enter keep the caret visible in long lines (6.2s)
  1 passed (7.8s)

Both pass the same way with firefox and webkit. Each was run twice in headless Chromium, Firefox and WebKit on macOS, green every time. On macOS the e2e line moves are Meta+ArrowLeft and Meta+ArrowRight. Linux, Windows and collab were not run. pnpm run tsc and pnpm run flow pass, and prettier and eslint pass on the changed files.
@vercel

vercel Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 24, 2026 6:36pm UTC
lexical-playground Ready Ready Preview Sep 24, 2026 6:36pm UTC

Request Review

Comment thread packages/lexical/src/LexicalUtils.ts Outdated
Comment on lines +1696 to +1702
const rect = element.getBoundingClientRect();
// Only a caret level with the container is revealed sideways. A caret above
// or below it is left to the vertical pass, so a container that scrolls both
// ways with the caret out of view vertically is only scrolled vertically.
if (bottom <= rect.top || top >= rect.bottom) {
return 0;
}

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.

codex noted an issue here:

horizontal scrolling skips carets outside the container vertically.

In a 300×100 editor root with overflow: auto, revealing a caret at (800, 500) changes scrollTop to 419 but leaves scrollLeft at zero. The caret remains horizontally invisible. The new horizontal pass returns early because the caret is below the container; the subsequent vertical pass never retries horizontal scrolling.

Remove that vertical prerequisite, or rerun horizontal scrolling after revealing the caret vertically.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks. The check that skipped a scroller when the caret was above or below it had no real case behind it, and with a root that scrolls both ways it meant the sideways scroll never happened.

I removed the check instead of running the horizontal pass again. scrollLeft only moves the caret sideways and scrollTop only moves it up and down, so the two passes can't undo each other. A rerun would also only have fixed the root, since the vertical pass never scrolls a container inside it, like a code block with a max-height.

Fixed in ce839c7. The new unit test in LexicalUtils.test.ts uses your case, a 300 by 100 root with overflow: auto and a caret at (800, 500), and checks scrollTop 419 and scrollLeft 501. It failed with scrollLeft 0 before. The old test that pinned the skip now expects the reveal.

… below the scroll container

## Description

The horizontal pass in scrollIntoViewIfNeeded skipped any scroll container that the caret was above or below. That check was a safety net, with no case that needed it. But a container that scrolls both ways, like an editor root with overflow: auto or a code block with a max-height, often has the caret out of view vertically when a reveal starts. There the sideways scroll was skipped, and the vertical pass that runs next never came back for it. In a 300 by 100 root with overflow: auto, a caret at (800, 500) got scrollTop 419 but kept scrollLeft 0, so it stayed out of view.

The check is removed, so every horizontal scroll container from the caret up to the root is revealed sideways, wherever the caret is vertically. scrollLeft only moves the caret sideways and scrollTop only moves it up and down, so each pass reveals its own axis and the vertical pass can't undo the horizontal one. The vertical pass is unchanged.

Running the horizontal pass again after the vertical one was the other option. It would only have fixed the root, because the vertical pass never scrolls containers inside the root, so a code block with a max-height would still have been skipped. It would also have measured the caret again.

The unit test that pinned the skip now expects the reveal, for a caret below the code element and for one above it.

## Test plan

### Before

The new tests with LexicalUtils.ts from the parent commit:

pnpm vitest run --project unit packages/lexical/src/__tests__/unit/LexicalUtils.test.ts -t scrollIntoViewIfNeeded

 × reveals a caret above or below a scroller sideways too
   AssertionError: expected +0 to be 701 // Object.is equality
 × reveals a caret below and right of a root that scrolls both ways
   AssertionError: expected +0 to be 501 // Object.is equality
      Tests  2 failed | 23 passed | 50 skipped (75)

In the second test the scrollTop check before it passes, with 419.

### After

pnpm vitest run --project unit --reporter=verbose packages/lexical/src/__tests__/unit/LexicalUtils.test.ts -t scrollIntoViewIfNeeded

 ✓ scrollIntoViewIfNeeded respects scroll-padding on document element
 ✓ scrollIntoViewIfNeeded ignores a selection rect that lies entirely above the editor
 ✓ scrolls to reveal a caret past the right edge
 ✓ scrolls all the way back when the caret fits at scrollLeft 0
 ✓ keeps the caret out of the scroll-padding when it does not fit at scrollLeft 0
 ✓ scrolls right to left containers with negative scrollLeft
 ✓ rounds the scroll position toward the caret
 ✓ reveals the inline start of a rect wider than the view
 ✓ leaves a visible caret alone and skips styles when nothing overflows
 ✓ ignores an element with overflow-x: visible
 ✓ ignores an element with overflow-x: hidden
 ✓ ignores an element with overflow-x: clip
 ✓ reveals a caret above or below a scroller sideways too
 ✓ reveals a caret below and right of a root that scrolls both ways
 ✓ scrolls nested scroll containers from the caret outward
 ✓ reveals the caret in a zoomed or scaled scroll container
 ✓ does not scroll the ancestors of the root sideways
 ✓ scrolls the editor root itself sideways
 ✓ ignores a rect above the editor and an empty rect
 ✓ still scrolls the window vertically, and only vertically
 ✓ only scrolls vertically without a selection node
 ✓ returns the rect of a caret that has one
 ✓ measures the character before a caret that has no rect
 ✓ measures the first character for a caret at the start that has no rect
 ✓ keeps the empty rect when there is no character to measure
      Tests  25 passed | 50 skipped (75)

pnpm vitest run --project unit with LexicalUtils, LexicalUpdateTags, LexicalSelection, LexicalSelectionChangeRefCount, LexicalSelectionResolveLeafPosition, LexicalDOMCapturedSelection, LexicalSlotSelection and LexicalRootSelectionPhantomParagraph

 Test Files  8 passed (8)
      Tests  215 passed (215)

pnpm vitest run --project scripts-unit scripts/__tests__/unit/treeShakingSource.test.ts -t 'LexicalUtils.ts'

      Tests  1 passed | 334 skipped (335)

VITEST_BROWSER=chromium pnpm vitest run --project browser packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts

      Tests  9 passed (9)

The same with firefox and webkit, headless on macOS. pnpm run tsc passes, and prettier and eslint pass on the changed files.
…DOM slot element

## Description

To scroll a caret at an element point into view, $updateDOMSelection measures the DOM child the caret is just before. When that child is the keyed DOM of a leaf node whose $getDOMSlot points at an element inside it, the caret is next to that inner element, not the whole child. A DOMRenderExtension override can wrap the <br> of a LineBreakNode in a <span> this way, and draw the next line's number with the span's ::after. Then the span's rect runs from the end of the line to the start of the next line. So after insertLineBreak(true), the macOS Ctrl+O, at the end of a long line in a block that scrolls sideways, the reveal took the start of the next line as the caret and scrolled the block all the way back, leaving the real caret far out of view.

$updateDOMSelection now resolves that child through $getDOMSlot when it is the keyed DOM of a node that is not an ElementNode, and measures the slot's element. Nothing changes when the slot's element is the keyed DOM itself, which is the default. Element children are still measured whole, because an ElementDOMSlot points at where the children go, not at where a caret before the element is.

## Test plan

### Before

The new test with LexicalSelection.ts from the parent commit:

pnpm vitest run --project unit packages/lexical/src/__tests__/unit/LexicalSelectionResolveLeafPosition.test.ts

 × a caret just before the wrapped line break is scrolled into view by the inner <br>, not the wrapper
   AssertionError: expected +0 to be 1101 // Object.is equality
      Tests  1 failed | 3 passed (4)

The <br> ends the line 100px past the view, but the wrapper's rect starts 1000px before it, so the paragraph scrolled all the way back to 0.

### After

pnpm vitest run --project unit --reporter=verbose packages/lexical/src/__tests__/unit/LexicalSelectionResolveLeafPosition.test.ts

 ✓ DOM caret directly on a bare <br> at offset 0 resolves to "after" the LineBreakNode
 ✓ wrap pattern via DOMRenderExtension override > DOM caret inside the wrap <span> at offset 0 (before the inner <br>) resolves to "before" the LineBreakNode
 ✓ wrap pattern via DOMRenderExtension override > DOM caret inside the wrap <span> at offset 1 (after the inner <br>) resolves to "after" the LineBreakNode
 ✓ wrap pattern via DOMRenderExtension override > a caret just before the wrapped line break is scrolled into view by the inner <br>, not the wrapper
      Tests  4 passed (4)

pnpm vitest run --project unit with LexicalUtils, LexicalUpdateTags, LexicalSelection, LexicalSelectionChangeRefCount, LexicalSelectionResolveLeafPosition, LexicalDOMCapturedSelection, LexicalSlotSelection and LexicalRootSelectionPhantomParagraph

 Test Files  8 passed (8)
      Tests  216 passed (216)

pnpm vitest run --project scripts-unit scripts/__tests__/unit/treeShakingSource.test.ts -t 'LexicalUtils.ts|LexicalSelection.ts'

      Tests  2 passed | 333 skipped (335)

VITEST_BROWSER=chromium pnpm vitest run --project browser packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts

      Tests  9 passed (9)

The same with firefox and webkit, headless on macOS. pnpm run tsc and pnpm run flow pass, and prettier and eslint pass on the changed files.
…t the end of the longest code line

## Description

At the end of the longest line the code block is scrolled as far as it goes, and the tests check that the caret is still inside it. Firefox leaves the theme's inline end padding out of the scroll range while a sticky box is in the block, like the data-gutter float. The browser test already allows the caret 0.5px past the left edge in Firefox for that reason, at the far left end of a right to left line. On the Windows CI runners Firefox also draws the caret 0.25px past the right edge at the end of the longest left to right line. Every other check passed there, and on macOS Firefox keeps the caret inside.

The browser test and the e2e spec now allow the same 0.5px at that one step, only in Firefox. The browser test uses one constant, FIREFOX_LINE_END_SLACK, for both steps. Nothing else is loosened.

## Test plan

### Before

On CI, browser-tests (windows-2022, firefox):

 FAIL  browser (firefox) packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts > caret scrolling in a code block with the data-gutter float > End reveals the end of a long line, and Home scrolls all the way back
AssertionError: {"caretBottom":33.43333435058594,"caretLeft":360.25,"caretRight":360.25,"caretTop":18.433334350585938,"lineStartX":52,"scrollLeft":4375,"scrollportLeft":0,"scrollportRight":360}: expected 360.25 to be less than or equal to 360
      Tests  1 failed | 157 passed | 4 skipped (162)

On CI, e2e-tests / windows (24.x, firefox, rich-text), at the End on the longest line (CodeBlockCaretScroll.spec.mjs:181):

    Expected: "ok"
    Received: "{\"caretLeft\":1143.25,\"caretRight\":1143.25,\"lineStartX\":159,\"scrollLeft\":3699,\"scrollportLeft\":107,\"scrollportRight\":1143}"
  1 failed
  823 passed (16.0m)

### After

VITEST_BROWSER=firefox pnpm vitest run --project browser packages/lexical-playground/src/__tests__/browser/CodeBlockCaretScroll.test.ts

 Test Files  1 passed (1)
      Tests  9 passed (9)

It passes the same way with chromium and webkit.

E2E_BROWSER=firefox pnpm exec playwright test --project=firefox packages/lexical-playground/__tests__/e2e/CodeBlockCaretScroll.spec.mjs

  ✓  1 [firefox] › packages/lexical-playground/__tests__/e2e/CodeBlockCaretScroll.spec.mjs:130:3 › Code block caret scrolling › Home, End and Enter keep the caret visible in long lines (5.4s)
  1 passed (8.6s)

It passes the same way in chromium and webkit. All of these ran headless on macOS, where the caret stays inside in Firefox too, so the new slack isn't exercised here. Windows and Linux weren't run locally. prettier and eslint pass on the changed files, and pnpm run tsc passes.
@etrepum
etrepum added this pull request to the merge queue Sep 25, 2026
Merged via the queue into meta:main with commit bab2560 Sep 25, 2026
50 checks passed
@etrepum etrepum mentioned this pull request Sep 28, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 3a1d54f8 Deployed Sep 24, 2026 by vercel[bot]
Preview – lexical-playground — 3a1d54f8 Deployed Sep 24, 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