Repository navigation
[lexical-react] Bug Fix: Use the current query when a typeahead option is picked - #9953
GiorgioPorgio wants to merge 1 commit into
Conversation
…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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Hi @GiorgioPorgio! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
AI review status for this pull request.
|
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
There was a problem hiding this comment.
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.tsxfail againstmainwith 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 andhel+ mention onmain, andhello+ mention with this change. - Replacing
openGenerationRefwith object identity keeps the stale-close ordering from #9332: every open passes a new resolution object, and those tests pass unchanged. LexicalNodeMenuPluginpasses no ref and keeps its current behavior.- The changeset follows CONTRIBUTING.md:
@lexical/react: patch, a[fix]line, and theVerify:paragraph a behavior-changing fix needs, including thematchingStringnote.
Agreed that a pick made while a close is still pending is a separate case, and it behaves as it did before.
…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) ```
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
Description
LexicalTypeaheadMenuPluginstores the trigger match inresolutioninsidestartTransition. An option picked before that transition renders is split off with the previous query's length.@Luk,e, Enter gives@+ mention.hello @Skywalker, 3 Backspaces, Enter giveshel+ 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.
openTypeaheadnow keeps the resolution it was last given in a ref. The pick splits with it. The ref replacesopenGenerationRef, 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 aVerify:paragraph.Test plan
Before
typeahead-before.mp4
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
Playground, same runs: correct in 60 of 60. Only Chromium was tested. Firefox and WebKit were not.