Skip to content

[lexical-rich-text] Bug Fix: Text typed after selecting a horizontal rule no longer jumps to the top of the document - #9845

Merged
potatowagon merged 1 commit into
meta:mainfrom
Electro-Jam-Instruments:a11y-hr-typing
Nov 11, 2026
Merged

potatowagon merged 1 commit into
meta:mainfrom
Electro-Jam-Instruments:a11y-hr-typing

Conversation

@Electro-Jam

Copy link
Copy Markdown
Contributor

Description

Letters typed while a horizontal rule, or any other block decorator, is selected land at the very top of the document instead of next to it.

Why

Type --- and a space to make a horizontal rule. Put the caret at the end of the paragraph above it and press the down arrow. The rule is now selected and gets its highlight, and the caret is gone. While a rule is selected, the editor takes the browser's own selection away.

Now type the letter q. It does not go next to the rule. With no caret to follow, the browser puts it in the very first position in the document, which can be a long way off screen.

The person typing sees the rule highlighted and reasonably expects the letter to follow it. Nothing says it went elsewhere. The only way back is to notice, undo, and move the caret to the right place by hand.

The arrow keys and Enter already step off a selected rule. Typed text did not.

What

When one block-level decorator, such as a horizontal rule, is selected and a letter is typed, the caret first moves to the start of the block after it. If nothing block-level follows, it goes into a new paragraph after the rule. Then the letter is typed as usual.

Shape

This copies what Enter already does for a selected decorator in the same file. It is one handler on BEFORE_INPUT_COMMAND, registered at low priority so it runs before the default handler. Nothing is exported.

What this does not change

Plain text editors. #9282 reports the same cause there, and #9286 and #9710 cover the arrow keys. Arrow keys, Enter and Backspace on a selected rule are as before. Inline decorators and selections of more than one node are left alone.

Related: #9282

Changeset

.changeset/rich-text-type-after-selected-block-decorator.md

Test plan

New file: packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts.

Before

pnpm exec vitest run --project unit packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts without the change to index.ts:

 × types at the start of the block after it
 × types into a new paragraph when nothing follows
 Tests  2 failed | 1 passed (3)

In Chromium on the playground: type first, Enter, before, Enter, --- , after; put the caret at the end of before; press ArrowDown; type q:

["qfirst","before","<HR>","after"]

The same steps on playground.lexical.dev put the q at the top of the document: qWelcome to the playground.

After

 Test Files  21 passed (21)
      Tests  175 passed (175)

Chromium, same steps:

["first","before","<HR>","qafter"]

Full unit suite: 29,809 passed, 4 failed. The 4 are in lexical-eslint-plugin, lexical-html, lexical-y and lexical (docs, README and generated-JSON checks) and fail the same way without this change.

E2E, Chromium, rich text: 620 passed. 15 did not start (Composition and Share launch their own browser, which is not installed here) and 2 DateTime tests failed under load and pass on re-run. E2E, Chromium, plain text: 80 passed, 12 did not start for the same reason.

Not exercised: Firefox, WebKit, the collab runs, the browser-mode tests and the production build.

…rule no longer jumps to the top of the document

## Description

Letters typed while a horizontal rule, or any other block decorator, is selected land at the very top of the document instead of next to it.

### Why

Type `---` and a space to make a horizontal rule. Put the caret at the end of the paragraph above it and press the down arrow. The rule is now selected and gets its highlight, and the caret is gone. While a rule is selected, the editor takes the browser's own selection away.

Now type the letter `q`. It does not go next to the rule. With no caret to follow, the browser puts it in the very first position in the document, which can be a long way off screen.

The person typing sees the rule highlighted and reasonably expects the letter to follow it. Nothing says it went elsewhere. The only way back is to notice, undo, and move the caret to the right place by hand.

The arrow keys and Enter already step off a selected rule. Typed text did not.

### What

When one block-level decorator, such as a horizontal rule, is selected and a letter is typed, the caret first moves to the start of the block after it. If nothing block-level follows, it goes into a new paragraph after the rule. Then the letter is typed as usual.

### Shape

This copies what Enter already does for a selected decorator in the same file. It is one handler on `BEFORE_INPUT_COMMAND`, registered at low priority so it runs before the default handler. Nothing is exported.

### What this does not change

Plain text editors. meta#9282 reports the same cause there, and meta#9286 and meta#9710 cover the arrow keys. Arrow keys, Enter and Backspace on a selected rule are as before. Inline decorators and selections of more than one node are left alone.

Related: meta#9282

## Changeset

`.changeset/rich-text-type-after-selected-block-decorator.md`

## Test plan

New file: `packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts`.

### Before

`pnpm exec vitest run --project unit packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts` without the change to `index.ts`:

```
 × types at the start of the block after it
 × types into a new paragraph when nothing follows
 Tests  2 failed | 1 passed (3)
```

In Chromium on the playground: type `first`, Enter, `before`, Enter, `--- `, `after`; put the caret at the end of `before`; press ArrowDown; type `q`:

```
["qfirst","before","<HR>","after"]
```

The same steps on playground.lexical.dev put the `q` at the top of the document: `qWelcome to the playground`.

### After

```
 Test Files  21 passed (21)
      Tests  175 passed (175)
```

Chromium, same steps:

```
["first","before","<HR>","qafter"]
```

Full unit suite: 29,809 passed, 4 failed. The 4 are in `lexical-eslint-plugin`, `lexical-html`, `lexical-y` and `lexical` (docs, README and generated-JSON checks) and fail the same way without this change.

E2E, Chromium, rich text: 620 passed. 15 did not start (Composition and Share launch their own browser, which is not installed here) and 2 DateTime tests failed under load and pass on re-run. E2E, Chromium, plain text: 80 passed, 12 did not start for the same reason.

Not exercised: Firefox, WebKit, the collab runs, the browser-mode tests and the production build.
@vercel

vercel Bot commented Nov 8, 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 Nov 8, 2026 3:37am UTC
lexical-playground Ready Ready Preview Nov 8, 2026 3:37am UTC

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Nov 8, 2026

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9845

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9845

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9845

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9845

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9845

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9845

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9845

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9845

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9845

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9845

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9845

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9845

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9845

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9845

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9845

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9845

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9845

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9845

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9845

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9845

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9845

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9845

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9845

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9845

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9845

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9845

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9845

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9845

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9845

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9845

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9845

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9845

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9845

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9845

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9845

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9845

commit: bb2afbe

@rootnode-bot

rootnode-bot Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 9, 2026, 7:13 PM UTC

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks, this looks good. The new handler runs ahead of core's default beforeinput handler, so the browser's own insertion is cancelled and the letter goes through the normal controlled insertion in the same update. In the playground on Chromium, your steps give qfirst on main and qafter on this branch, and one undo removes both the letter and the paragraph the handler inserted. In Firefox, typing with a selected rule inserts nothing, both before and after this change.

A few optional, non-blocking suggestions:

  1. Keep the formatting of the text the letter lands in front of (packages/lexical-rich-text/src/index.ts:1474). next.selectStart() returns a selection with no format, so with a bold after you get a plain q followed by a bold after. Clicking at the start of that text and typing gives a bold q, because the selection-change handler copies the text node's format. Copying it here keeps the two paths consistent:
    const selection = next.selectStart();
    const anchorNode = selection.anchor.getNode();
    if ($isTextNode(anchorNode)) {
      selection.setFormat(anchorNode.getFormat());
      selection.setStyle(anchorNode.getStyle());
    }
  2. Assert that the browser default is prevented (packages/lexical-rich-text/src/__tests__/unit/RichTextNodeSelectionTyping.test.ts, type() at line 64). The bug is the browser inserting at the top of the document. In jsdom, the signal for that is event.defaultPrevented, which is true with this change and false on main. Returning the event from type() and asserting it in the first two cases would pin the fix. AGENTS.md's testing section leaves real input and selection behaviour to browser or E2E tests, so a case next to the ArrowDown cases in packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs would also be welcome.
  3. Nit: the third case ('leaves a range selection where it was') passes only because jsdom performs no native insertion. In a browser, the q would be typed at the caret. Asserting event.defaultPrevented === false plus the selection would say what the test means.

IME composition (insertCompositionText) still never reaches this handler, because core returns before dispatching for it. That would be a natural follow-up rather than something for this PR.

) {
const next = nodes[0].getNextSibling();
if ($isElementNode(next) && !next.isInline()) {
next.selectStart();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: selectStart() returns a selection with format 0 and no style, so typing before bold text gives a plain q followed by a bold after. Clicking at the start of that text and typing gives a bold q, because the selection-change handler copies the text node's format. Copying it keeps the two paths consistent:

const selection = next.selectStart();
const anchorNode = selection.anchor.getNode();
if ($isTextNode(anchorNode)) {
  selection.setFormat(anchorNode.getFormat());
  selection.setStyle(anchorNode.getStyle());
}

data,
inputType: 'insertText',
});
editor.update(() => editor.dispatchCommand(BEFORE_INPUT_COMMAND, event), {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: the bug is the browser inserting at the top of the document, and in jsdom the signal for that is event.defaultPrevented: true with this change, false on main. Returning the event from type() and asserting expect(event.defaultPrevented).toBe(true) in the first two cases would pin the fix. In the third case, false would replace asserting that no q appeared, which only holds because jsdom performs no native insertion.

@potatowagon potatowagon added the extended-tests Run extended e2e tests on a PR label Nov 11, 2026
@potatowagon
potatowagon added this pull request to the merge queue Nov 11, 2026
Merged via the queue into meta:main with commit d952196 Nov 11, 2026
62 of 63 checks passed
@potatowagon potatowagon mentioned this pull request Dec 2, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — bb2afbe9 Deployed Nov 8, 2026 by vercel[bot]
Preview – lexical-playground — bb2afbe9 Deployed Nov 8, 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