Skip to content

[lexical-extension][lexical-playground] Feature: Insert paragraph on click after the last block - #8549

Merged
etrepum merged 1 commit into
meta:mainfrom
mayrang:fix/click-after-last-block-creates-paragraph
May 25, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
mayrang:fix/click-after-last-block-creates-paragraph

Conversation

@mayrang

@mayrang mayrang commented May 23, 2026

Copy link
Copy Markdown
Collaborator

Description

Closes #8544. When a DecoratorNode, a shadow-root ElementNode like TableNode, or a CodeNode sits at the end of the document, clicking the empty area below it currently puts the selection somewhere unexpected — null for a bare decorator, the end of a cell for a table, the end of the last line for a code block. Most editors (Notion, etc.) interpret that gesture as "make a paragraph here and let me type", and that's what this PR ships.

ClickAfterLastBlockExtension intercepts the click — both mousedown (to cancel the browser's native caret placement before it picks the previous text block) and click (to mark _lexicalHandled so lexical core skips its default onClick, then run editor.update to insert and select an empty paragraph). The two-phase shape was the only way to eliminate a one-frame cursor flicker on the previous text block; comments at the listener spot out the side effects to be aware of — focus transition ownership now lives with the extension, and lexical core's isSelectionChangeFromMouseDown flag never gets set for these clicks.

The trigger condition lives in a $shouldInsertAfter config callback, defaulting to $defaultShouldInsertAfter which matches DecoratorNode and shadow-root ElementNode. The default is exported so consumers extending the behavior — the playground composes it with $isCodeNode to cover the third type listed in the issue — don't re-derive the check. @lexical/code-core is intentionally kept out of @lexical/extension's dependencies; the composition lives at the call site.

Test plan

  • pnpm vitest run --project unit -t ClickAfterLastBlock — 5 unit tests: positive insertion, predicate-false skip, disabled: true, read-only mode, empty-root no-op.
  • pnpm tsc --noEmit -p tsconfig.json clean.
  • pnpm flow clean.
  • prettier --check / eslint clean on changed files.
  • Manual on Chrome and Safari playground:
    • Equation (block) / Table / Code / PageBreak at end-of-doc — clicking below inserts a paragraph and the caret lands there with no flicker on the previous block.
    • Plain paragraph / heading / list at end-of-doc — extension stays out of the way, native caret placement runs.
    • Read-only mode — no insertion.
    • Click directly on the last block or its content area — no insertion (target check).
    • After the extension inserts a paragraph, clicking below it again does nothing (last child is a paragraph, predicate rejects).
    • Typing in the inserted paragraph works normally.

@vercel

vercel Bot commented May 23, 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 24, 2026 8:47pm
lexical-playground Ready Ready Preview, Comment May 24, 2026 8:47pm

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 23, 2026
Comment on lines +28 to +51
const editor = buildEditorFromExtensions({
$initialEditorState: () => {
const root = $getRoot();
// Block-level TestDecoratorNode at the end of the document, to
// exercise the same path Equation / PageBreak hit in the playground.
const decorator = $createTestDecoratorNode().setIsInline(false);
root.append(
$createParagraphNode().append($createTextNode('hello')),
decorator,
);
},
dependencies: [
RichTextExtension,
configExtension(ClickAfterLastBlockExtension, {
disabled: options.disabled ?? false,
}),
],
name: 'click-after-last-block-test',
nodes: [TestDecoratorNode],
});
const root = document.createElement('div');
document.body.appendChild(root);
editor.setRootElement(root);
return {editor, root};

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.

All of the try/finally stuff in these tests is kinda ugly, the easiest thing to clean that up would be to do this setup in register and only return the editor.

Suggested change
const editor = buildEditorFromExtensions({
$initialEditorState: () => {
const root = $getRoot();
// Block-level TestDecoratorNode at the end of the document, to
// exercise the same path Equation / PageBreak hit in the playground.
const decorator = $createTestDecoratorNode().setIsInline(false);
root.append(
$createParagraphNode().append($createTextNode('hello')),
decorator,
);
},
dependencies: [
RichTextExtension,
configExtension(ClickAfterLastBlockExtension, {
disabled: options.disabled ?? false,
}),
],
name: 'click-after-last-block-test',
nodes: [TestDecoratorNode],
});
const root = document.createElement('div');
document.body.appendChild(root);
editor.setRootElement(root);
return {editor, root};
return buildEditorFromExtensions({
$initialEditorState: () => {
const root = $getRoot();
// Block-level TestDecoratorNode at the end of the document, to
// exercise the same path Equation / PageBreak hit in the playground.
const decorator = $createTestDecoratorNode().setIsInline(false);
root.append(
$createParagraphNode().append($createTextNode('hello')),
decorator,
);
},
dependencies: [
RichTextExtension,
configExtension(ClickAfterLastBlockExtension, {
disabled: options.disabled ?? false,
}),
],
name: 'click-after-last-block-test',
nodes: [TestDecoratorNode],
register() {
const root = document.createElement('div');
document.body.appendChild(root);
editor.setRootElement(root);
return () => root.remove();
},
});

The call sites would use const root = editor.getRootElement(); (and assert it's non-null with ! or a proper assert) and using editor = setUpEditor();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done. Folded the two inline-buildEditorFromExtensions tests into setUpEditor too — only thing they varied was the initial state, exposed as a withInitialChildren option.

Comment on lines +34 to +35
function markLexicalHandled(event: Event): void {
(event as Event & LexicalHandledEvent)._lexicalHandled = true;

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.

Suggested change
function markLexicalHandled(event: Event): void {
(event as Event & LexicalHandledEvent)._lexicalHandled = true;
function markLexicalHandled(event: Event & LexicalHandledEvent): void {
event._lexicalHandled = true;

It would be better to just do an @internal export of stopLexicalPropagation though and use that. Will be easier to refactor later if we aren't directly manipulating untyped private API from here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Went with the @internal export — added stopLexicalPropagation to lexical core's index.ts and Lexical.js.flow, dropped the LexicalHandledEvent interface and markLexicalHandled wrapper from the extension.

Comment on lines +109 to +132
let result = false;
editor.getEditorState().read(
() => {
const lastChild = $getRoot().getLastChild();
if (lastChild === null) {
return;
}
const lastChildDOM = editor.getElementByKey(lastChild.getKey());
if (lastChildDOM === null) {
return;
}
// Exclusive lower edge — clicks at exactly the bottom pixel fall
// through to native handling, which is what users expect when
// they click on a block's visible bottom border.
if (event.clientY <= lastChildDOM.getBoundingClientRect().bottom) {
return;
}
if ($shouldInsertAfter(lastChild)) {
result = true;
}
},
{editor},
);
return result;

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.

Suggested change
let result = false;
editor.getEditorState().read(
() => {
const lastChild = $getRoot().getLastChild();
if (lastChild === null) {
return;
}
const lastChildDOM = editor.getElementByKey(lastChild.getKey());
if (lastChildDOM === null) {
return;
}
// Exclusive lower edge — clicks at exactly the bottom pixel fall
// through to native handling, which is what users expect when
// they click on a block's visible bottom border.
if (event.clientY <= lastChildDOM.getBoundingClientRect().bottom) {
return;
}
if ($shouldInsertAfter(lastChild)) {
result = true;
}
},
{editor},
);
return result;
return editor.getEditorState().read(
() => {
const lastChild = $getRoot().getLastChild();
if (lastChild === null) {
return false;
}
const lastChildDOM = editor.getElementByKey(lastChild.getKey());
if (lastChildDOM === null) {
return false;
}
// Exclusive lower edge — clicks at exactly the bottom pixel fall
// through to native handling, which is what users expect when
// they click on a block's visible bottom border.
if (event.clientY <= lastChildDOM.getBoundingClientRect().bottom) {
return false;
}
if ($shouldInsertAfter(lastChild)) {
return true;
}
},
{editor},
);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done — shouldClaimClick returns the read result directly, and the trailing if ($shouldInsertAfter(lastChild)) { return true; } is now return $shouldInsertAfter(lastChild) so the function stays typed as boolean.

Comment on lines +54 to +56
: () => {
$getRoot().clear();
},

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.

Suggested change
: () => {
$getRoot().clear();
},
: () => {},

There's nothing here to clear in this case

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

…click after the last block

Adds ClickAfterLastBlockExtension that intercepts clicks below the
last child of the root and inserts a new empty paragraph when that
child is a non-editable block. Defaults match DecoratorNode and
shadow-root ElementNodes; the playground extends the predicate to
include CodeNode so all three node types listed in meta#8544 are
covered without pulling lexical-code-core into the core extension.

Closes meta#8544
@mayrang
mayrang force-pushed the fix/click-after-last-block-creates-paragraph branch from ccb78d3 to 973e46e Compare May 24, 2026 20:46
@etrepum
etrepum added this pull request to the merge queue May 25, 2026
Merged via the queue into meta:main with commit 89f18e5 May 25, 2026
40 checks passed
@etrepum etrepum mentioned this pull request May 28, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 973e46ed Deployed May 24, 2026 by vercel[bot]
Preview – lexical-playground — 973e46ed Deployed May 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.

Feature: Improve accessibility with a mouse click

2 participants