You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Typeahead menu rendering off-screen when cursor near edges #3834
Move cursor over to the edge of the editor area. Cursor should be close to the right-edge of the browser window -- at least this is how it's demonstrated in the linked repro video.
Use @-mention feature to trigger the typeahead menu from LexicalTypeaheadMenuPlugin.
Menu will render outside of visible area.
Note: This is easier to reproduce if the browser is resized to a smaller width and/or the browser's responsive design mode is enabled.
Link to code example:
Peek.2023-02-06.11-39.mp4
The current behavior
Currently the typeahead menu from LexicalTypeaheadMenuPlugin renders outside of the user's viewable area when triggered near the edge of the browser window. The cursor position where this happens can depend based on the width of the content that's inside of the menu. This is likely also an issue if the cursor position is near the bottom and bottom right of the window.
The expected behavior
The typeahead menu is expected to possibly have a maximum location based on the window's height and width and/or maybe run developer-defined function to change the positioning depending on the situation's needs.
Reacted by Saravana Vijaya Kumar A, shuirong, Dennis Soehnen, candidia, Edouard, Jingkang Zhang and balibabu
Unfortunately no that doesn't work -- although I think it resolves another issue I've seen that was similar.
@svk-assembly and for those that are looking for a workaround for now: You can provide a different element ref for LexicalTypeaheadMenuPlugin's menuRenderFn prop then maybe use some CSS trickery to position it a little better. Doing this, however, makes the menu element "unaware" of the current cursor position.
I'm dealing with the same issue. The overflow is not only happening on the right side but also at the bottom if Lexical is placed far at the bottom of the page.
In addition to @aalmazan findings, I would like to add that there is an anchorClassName prop on the LexicalTypeaheadMenuPlugin component that passes all classes to the anchor element ref. So it's possible to apply styles to the anchor element while keeping the automatic position feature with anchorElementRef.
However, that doesn't solve the problem. I was thinking about adding some collision detection myself and "mirror the direction" of the typeahead (right -> left and bottom -> top) if there is any collision detected. In my use case, I'm fine with screen collision detection. I imagine others may prefer to have a collision detection with the Lexical instance so the typeahead component would never overflow the editor.
Reacted by rettoSea, Allan Almazan, shuirong and Iqra
I wrote this plugin, and unfortunately there is a fine line between building a decent typeahead plugin for a text editor and building an entire popover framework with robust positioning heuristics (which there are already plenty of).
I think a good compromise would be allowing the consumer of this plugin to have more control of the positioning, thoughts? Or within menuRenderFn you can render a component that handles all of it's own positioning, that's what we do internally.
@tylerjbainbridge I completely agree this should be more composable with other libraries whose purposes are positioning popovers like floating-ui and ariakit
Thanks @eashish93. I've got a little improved version below. positionReference is not available according to my types and latest version of floating-ui. Instead, use refs.setPositionReference.
Below is my version. I'm using flip() (docs) which is a "no-space" fallback strategy, meaning, it will only re-position when there's no space.
const{ x, y, refs, strategy }=useFloating({placement: 'bottom-start',middleware: [offset(6),flip(),shift()],});
Another annoying bad UX is that the menu changes its positioning - above or under the caret - depending on the number of items.
Steps to reproduce:
open the playground
delete everything there
press enter 5 times
type / - the menu appears under the caret
type pa - the menu changes its position
type r - the menu changes its position again
From a user's perspective, this jumping is distracting and doesn't make sense.
IMHO the menu shouldn't take into account the boundaries of the editor element, only the window's boundaries.
This is still an issue for me for some reason but not sure why
EDIT: I was working on it for a day and solved it 15 minutes after I wrote here. Needed to make sure that the width of the first child of the portal is correctly set.
Activity
svk-assembly commentedon Feb 7, 2023
I face the same issue. I see packages/lexical-react/src/LexicalTypeaheadMenuPlugin.tsx has a useDynamicPositioning hook but it doesn't work.
thegreatercurve commentedon Feb 7, 2023
Does #3826 fix the issue for either of you?
aalmazan commentedon Feb 7, 2023
Unfortunately no that doesn't work -- although I think it resolves another issue I've seen that was similar.
@svk-assembly and for those that are looking for a workaround for now: You can provide a different element ref for
LexicalTypeaheadMenuPlugin'smenuRenderFnprop then maybe use some CSS trickery to position it a little better. Doing this, however, makes the menu element "unaware" of the current cursor position.https://github.com/facebook/lexical/blob/14cd2a6ce853996c1f3afe8b3aa14b52f29c5ba4/packages/lexical-playground/src/plugins/MentionsPlugin/index.tsx#L684-L720
In the playground example, you would change line 715 to an element ref you provide (maybe from a parent component) instead.
blacktoast commentedon Feb 9, 2023
if this issue is happening because of #3826, could I try to fix it?
davidwlhlm commentedon Feb 9, 2023
I'm dealing with the same issue. The overflow is not only happening on the right side but also at the bottom if Lexical is placed far at the bottom of the page.
In addition to @aalmazan findings, I would like to add that there is an
anchorClassNameprop on theLexicalTypeaheadMenuPlugincomponent that passes all classes to the anchor element ref. So it's possible to apply styles to the anchor element while keeping the automatic position feature withanchorElementRef.However, that doesn't solve the problem. I was thinking about adding some collision detection myself and "mirror the direction" of the typeahead (right -> left and bottom -> top) if there is any collision detected. In my use case, I'm fine with screen collision detection. I imagine others may prefer to have a collision detection with the Lexical instance so the typeahead component would never overflow the editor.
tylerjbainbridge commentedon Mar 1, 2023
I wrote this plugin, and unfortunately there is a fine line between building a decent typeahead plugin for a text editor and building an entire popover framework with robust positioning heuristics (which there are already plenty of).
I think a good compromise would be allowing the consumer of this plugin to have more control of the positioning, thoughts? Or within
menuRenderFnyou can render a component that handles all of it's own positioning, that's what we do internally.eashish93 commentedon Mar 21, 2023
Until this is fixed, there is an alternative solution using @floating-ui lib combined with this plugin.
Here's the sample implementation:
candidia commentedon Jun 19, 2023
@tylerjbainbridge I completely agree this should be more composable with other libraries whose purposes are positioning popovers like floating-ui and ariakit
jvandenaardweg commentedon Jun 19, 2023
Thanks @eashish93. I've got a little improved version below.
positionReferenceis not available according to my types and latest version of floating-ui. Instead, userefs.setPositionReference.Below is my version. I'm using
flip()(docs) which is a "no-space" fallback strategy, meaning, it will only re-position when there's no space.thorn0 commentedon Sep 18, 2023
Another annoying bad UX is that the menu changes its positioning - above or under the caret - depending on the number of items.
Steps to reproduce:
/- the menu appears under the caretpa- the menu changes its positionr- the menu changes its position againFrom a user's perspective, this jumping is distracting and doesn't make sense.
IMHO the menu shouldn't take into account the boundaries of the editor element, only the window's boundaries.
etrepum commentedon Dec 9, 2024
Can't reproduce
cemremengu commentedon Dec 20, 2024
This is still an issue for me for some reason but not sure whyEDIT: I was working on it for a day and solved it 15 minutes after I wrote here. Needed to make sure that the width of the first child of the portal is correctly set.
1111mp commentedon Aug 27, 2025
https://github.com/facebook/lexical/blob/7b16879e0a7cf550e1ac9a9ffb8c3499624f0796/packages/lexical-react/src/shared/LexicalMenu.ts#L557
I don't understand why this line of code is necessary, but it's definitely where the problem lies.