Skip to content

[lexical-react] Bug Fix: Use the current query when a typeahead option is picked - #9953

Open
GiorgioPorgio wants to merge 1 commit into
meta:mainfrom
GiorgioPorgio:fix/typeahead-stale-match-split
Open

GiorgioPorgio wants to merge 1 commit into
meta:mainfrom
GiorgioPorgio:fix/typeahead-stale-match-split

Conversation

@GiorgioPorgio

Copy link
Copy Markdown

Description

LexicalTypeaheadMenuPlugin stores the trigger match in resolution inside startTransition. An option picked before that transition renders is split off with the previous query's length.

  • Typed more, then picked: the start of the query stays in front of the node. On the playground @Luk, e, Enter gives @ + mention.
  • Backspace, then picked: the text before the query is cut. hello @Skywalker, 3 Backspaces, Enter gives hel + mention.

It happens when keys queue up behind a busy main thread. On playground.lexical.dev, keys 40 ms apart behind a 150 ms long task reproduce it every time.

Same cause as #9332. openTypeahead now keeps the resolution it was last given in a ref. The pick splits with it. The ref replaces openGenerationRef, since a new object per open tells opens apart like the counter did. The tests from #9332 pass unchanged.

Out of scope: Enter while a close is still pending picks with the last open match, as before.

Changeset

[fix] for @lexical/react, with a Verify: paragraph.

Test plan

Before

typeahead-before.mp4
$ pnpm exec vitest run --project unit packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.test.tsx -t "picking an option"
       × replaces the whole query after more was typed
+   "text": "hello /a[Alpha]",
       × keeps the text before the query after characters were deleted
+   "text": "hel[Alpha]",

Playground (pnpm run dev, Playwright, headless Chromium): wrong in 60 of 60 runs, both cases, with keys 40 ms apart behind a 150 ms long task and back to back at 1x and 4x CPU.

After

typeahead-after.mp4
$ pnpm exec vitest run --project unit packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.test.tsx
      Tests  23 passed (23)
$ pnpm run ci-check
Found 0 errors
$ pnpm run test-unit
      Tests  40241 passed | 423 expected fail | 174 skipped (40838)

Playground, same runs: correct in 60 of 60. Only Chromium was tested. Firefox and WebKit were not.

…option is picked before the menu re-renders

## Description

`LexicalTypeaheadMenuPlugin` stores the trigger match in `resolution` inside `startTransition`. An option picked before that transition renders is split off with the previous query's length.

- Typed more, then picked: the start of the query stays in front of the node. On the playground `@Lu`, `ke`, Enter gives `@L` + mention.
- Backspace, then picked: the text before the query is cut. `hello @Luke`, 3 Backspaces, Enter gives `hel` + mention.

It happens when keys queue up behind a busy main thread. On playground.lexical.dev, keys 40 ms apart behind a 150 ms long task reproduce it every time.

Same cause as meta#9332. `openTypeahead` now keeps the resolution it was last given in a ref. The pick splits with it. The ref replaces `openGenerationRef`, since a new object per open tells opens apart like the counter did. The tests from meta#9332 pass unchanged.

Out of scope: Enter while a close is still pending picks with the last open match, as before.

## Changeset

`[fix]` for `@lexical/react`, with a `Verify:` paragraph.

## Test plan

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.test.tsx -t "picking an option"
       × replaces the whole query after more was typed
+   "text": "hello /a[Alpha]",
       × keeps the text before the query after characters were deleted
+   "text": "hel[Alpha]",
```

Playground (`pnpm run dev`, Playwright, headless Chromium): wrong in 60 of 60 runs, both cases, with keys 40 ms apart behind a 150 ms long task and back to back at 1x and 4x CPU.

### After

```
$ pnpm exec vitest run --project unit packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.test.tsx
      Tests  23 passed (23)
$ pnpm run ci-check
Found 0 errors
$ pnpm run test-unit
      Tests  40241 passed | 423 expected fail | 174 skipped (40838)
```

Playground, same runs: correct in 60 of 60. Only Chromium was tested. Firefox and WebKit were not.
@vercel

vercel Bot commented Nov 19, 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 19, 2026 5:59pm UTC
lexical-playground Ready Ready Preview Nov 19, 2026 5:59pm UTC

Request Review

@meta-cla

meta-cla Bot commented Nov 19, 2026

Copy link
Copy Markdown

Hi @GiorgioPorgio!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@rootnode-bot

rootnode-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 19, 2026, 6:16 PM UTC

@meta-cla

meta-cla Bot commented Nov 19, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@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 Nov 19, 2026

@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 for the clear write-up. This change looks good to me.

The root cause is well identified. LexicalTypeaheadMenuPlugin sets resolution inside startTransition, so a pick that lands before that render split the text with the previous query's match. Keeping the latest resolution in latestResolutionRef and reading it in selectOptionAndCleanUp (packages/lexical-react/src/shared/LexicalMenu.tsx, lines 360-371) fixes both symptoms:

  • The two new cases in LexicalTypeaheadMenuPlugin.test.tsx fail against main with exactly the output in your Before section (hello /a[Alpha], hel[Alpha]) and pass with this change, so they guard the fix.
  • The same race on the playground's mentions menu (keys queued behind a long task in Chromium) leaves hello @ + mention and hel + mention on main, and hello + mention with this change.
  • Replacing openGenerationRef with object identity keeps the stale-close ordering from #9332: every open passes a new resolution object, and those tests pass unchanged.
  • LexicalNodeMenuPlugin passes no ref and keeps its current behavior.
  • The changeset follows CONTRIBUTING.md: @lexical/react: patch, a [fix] line, and the Verify: paragraph a behavior-changing fix needs, including the matchingString note.

Agreed that a pick made while a close is still pending is a separate case, and it behaves as it did before.

Automated review (maintainer link)

sehnzucht added a commit to sehnzucht/lexical that referenced this pull request Nov 26, 2026
…a transition

## Description

`LexicalTypeaheadMenuPlugin` sets the menu `resolution` inside `startTransition`, on open and on each query change. In a large document where the host keeps rendering after each edit, that transition can wait behind higher-priority work for seconds (median ~12s reported in meta#9976, vs ~1.3s as a normal update).

Add an `openInTransition` prop (default `true`). With `openInTransition={false}`, the resolution is set as a normal React update, so the menu appears with the trigger key without forking the plugin. The default keeps today's behavior.

Same underlying lag as meta#9332 / meta#9953.

Closes meta#9976

## Changeset

```md
---
'@lexical/react': patch
---

[feature] `LexicalTypeaheadMenuPlugin` accepts `openInTransition` (default `true`). Set it to `false` to set the menu resolution, on open and on each query change, as a normal React update instead of inside `startTransition`, so a busy host no longer delays the menu for seconds after the trigger key
```

## Test plan

### Before

New test file run against the plugin from `main`:

```
$ pnpm exec vitest run --project unit packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.openInTransition.test.tsx
 ❯ |unit| packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.openInTransition.test.tsx (2 tests | 1 failed) 94ms
   ❯ LexicalTypeaheadMenuPlugin openInTransition (2)
     × opens without startTransition when openInTransition is false 9ms
AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times
 ❯ packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.openInTransition.test.tsx:158:33
    158|     expect(startTransition).not.toHaveBeenCalled();
 Test Files  1 failed (1)
      Tests  1 failed | 1 passed (2)
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.openInTransition.test.tsx packages/lexical-react/src/__tests__/unit/LexicalTypeaheadMenuPlugin.test.tsx
 Test Files  2 passed (2)
      Tests  23 passed (23)
```
@pkg-pr-new

pkg-pr-new Bot commented Nov 28, 2026

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 9557f80

This branch was successfully deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant