Skip to content

[lexical-playground] Bug Fix: Reset the cached selection format on clear formatting - #8889

Merged
etrepum merged 3 commits into
meta:mainfrom
Yjason-K:fix/8881-clear-formatting-toolbar-state
Aug 1, 2026
Merged

etrepum merged 3 commits into
meta:mainfrom
Yjason-K:fix/8881-clear-formatting-toolbar-state

Conversation

@Yjason-K

Copy link
Copy Markdown
Contributor

Description

Clicking Clear Formatting removes the formatting from the text, but the toolbar keeps drawing the format buttons in their active state. Because the button still looks toggled on, re-applying the format takes two clicks — one to "turn off" the stale state and one to actually apply it.

hasFormat() reads the format bitmask that a RangeSelection caches on itself, not the format of the nodes:

https://github.com/facebook/lexical/blob/main/packages/lexical/src/LexicalSelection.ts#L872-L875

clearFormatting cleared format/style on the selected nodes but never updated that cache, so both toolbars kept reporting the old formats:

                        nodes                       selection.format
after bolding    ["Hel",0] ["lo Wo",1] ["rld",0]           1
after clearing   ["Hello World",0]            ✅           1        ❌ stale

Core already treats these as a pair — $formatText assigns selection.format right after mutating the nodes. This change does the same at the end of clearFormatting. TableSelection.hasFormat() derives its value from its nodes, so it needs no equivalent reset.

The collapsed-selection path returns early (it only clears block formatting), so the pending format a caret carries is untouched and #7383 keeps working.

Why the issue says it only reproduces mid-node

The stale cache happens for any non-collapsed selection — the added unit test fails for a whole-node selection too, before this change. The difference is only whether the browser happens to fire a selectionchange afterwards, which makes onSelectionChange recompute the format from the nodes and paper over the bug:

https://github.com/facebook/lexical/blob/main/packages/lexical/src/LexicalEvents.ts#L458-L485

Closes #8881

Test plan

Both added tests fail without the change in utils.ts and pass with it.

Before

ClearFormatting.spec.mjs — the HTML assertion passes (the formatting is cleared), only the toolbar state is wrong:

Error: expect(locator).not.toHaveClass(expected) failed

Expected pattern: not /active/
Received string:  "toolbar-item spaced active"

  14 × locator resolved to <button type="button" title="Bold (⌘+B)"
       class="toolbar-item spaced active"
       aria-label="Format text as bold. Shortcut: ⌘+B">…</button>

> 366 |     await expect(boldButton).not.toHaveClass(/active/);

ToolbarPluginClearFormatting.test.ts:

AssertionError: expected true to be false // Object.is equality

- Expected
+ Received

- false
+ true

> 259 |       expect(selection.hasFormat('bold')).toBe(false);

 Tests  1 failed | 8 passed (9)

After

$ pnpm run test-unit
  Test Files  226 passed (226)
       Tests  4929 passed | 1 skipped (4930)

$ pnpm run test-e2e-chromium
  822 passed (5.2m)
   22 skipped

$ pnpm run tsc
  (no errors)
after

Added coverage:

…ear formatting

clearFormatting cleared the format on the selected nodes but left the
format cached on the RangeSelection, which is what hasFormat() reads. The
toolbars kept rendering the cleared formats as active, so a format button
needed an extra click to re-apply. Core's $formatText already keeps the
two in sync; this does the same after clearing.

Fixes meta#8881
@vercel

vercel Bot commented Jul 29, 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 Jul 31, 2026 12:19am
lexical-playground Ready Ready Preview Jul 31, 2026 12:19am

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 Jul 29, 2026
@etrepum etrepum added the extended-tests Run extended e2e tests on a PR label Jul 30, 2026

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

Looks like the e2e tests don't work in collab mode

…ollab mode

The new meta#8881 test looked the bold button up with page.locator(), which only
searches the top-level document. In collab mode the playground runs in a
two-iframe split view, so the toolbar is not there and the assertion failed
with "element(s) not found". Use the locate() helper, which resolves through
getPageOrFrame() into the left frame like the rest of the e2e utils do.
@Yjason-K

Copy link
Copy Markdown
Contributor Author

Thanks — fixed in 2b37e7b.

The test used page.locator(), which misses the toolbar in collab's two-iframe split view. Switched to the locate() helper so it resolves into the left frame. Verified locally in collab, collab-v2, and rich-text modes.

@etrepum
etrepum added this pull request to the merge queue Aug 1, 2026
Merged via the queue into meta:main with commit f5c8297 Aug 1, 2026
44 checks passed
etrepum added a commit to mayrang/lexical that referenced this pull request Aug 2, 2026
Our branch extracted the body of clearFormatting into the $clearFormatting
$function; main (meta#8889) appended a reset of the format/style cached on the
RangeSelection. Kept both: the reset now lives at the end of
$clearFormatting, still inside the range/table selection branch and after
the collapsed-selection early return, so clearFormatting remains the thin
editor.update wrapper.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 2b37e7b9 Deployed Jul 31, 2026 by vercel[bot]
Preview – lexical-playground — 2b37e7b9 Deployed Jul 31, 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.

Bug: selection.hasFormat is giving wrong info when clear formatting is clicked from dropdown/button

2 participants