Skip to content

Bug: Typeahead menu rendering off-screen when cursor near edges #3834

Closed

Description

@aalmazan

Lexical version: latest master @ f6a278e

Steps To Reproduce

  1. Open Lexical playground.
  2. 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.
  3. Use @-mention feature to trigger the typeahead menu from LexicalTypeaheadMenuPlugin.
  4. 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.

Activity

  1. svk-assembly commented on Feb 7, 2023

    @svk-assembly

    I face the same issue. I see packages/lexical-react/src/LexicalTypeaheadMenuPlugin.tsx has a useDynamicPositioning hook but it doesn't work.

  2. thegreatercurve commented on Feb 7, 2023

    @thegreatercurve
    Contributor

    Does #3826 fix the issue for either of you?

  3. aalmazan commented on Feb 7, 2023

    @aalmazan
    Author

    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.

    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.

  4. blacktoast commented on Feb 9, 2023

    @blacktoast
    Contributor

    if this issue is happening because of #3826, could I try to fix it?

  5. davidwlhlm commented on Feb 9, 2023

    @davidwlhlm

    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.

  6. tylerjbainbridge commented on Mar 1, 2023

    @tylerjbainbridge
    Contributor

    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.

  7. eashish93 commented on Mar 21, 2023

    @eashish93

    Until this is fixed, there is an alternative solution using @floating-ui lib combined with this plugin.
    Here's the sample implementation:

    import {
      useFloating,
      FloatingPortal,
      autoPlacement,
      offset,
      flip,
      shift,
    } from '@floating-ui/react';
    
    function ComponentPickerMenuPlugin({ anchorEl, options}) {
       const { x, y, positionReference, refs, strategy } = useFloating({
        placement: 'top',
        middleware: [offset(15), autoPlacement(), shift()],
      });
     ///..... other function as seen on playground.
    
       <LexicalTypeaheadMenuPlugin<ComponentPickerOption>
          onQueryChange={setQueryString}
          onSelectOption={onSelectOption}
          triggerFn={checkForTriggerMatch}
          onOpen={(r) => {
            positionReference({
              getBoundingClientRect: r.getRect
            });
          }}
          options={filteredOptions}
          anchorClassName="reset-anchor"
          menuRenderFn={(
            anchorElementRef,
            { selectedIndex, selectOptionAndCleanUp, setHighlightedIndex }
          ) =>
            anchorElementRef.current && filteredOptions.length && isMenuOpen ? (
              <FloatingPortal root={anchorEl}>
                <div
                  ref={refs.setFloating}
                  style={{
                    position: strategy,
                    top: y ?? 0,
                    left: x ?? 0,
                    width: 'max-content',
                  }}>
                  
                    <ul>
                      {filteredOptions.map((option, i: number) => (
                        <ComponentPickerMenuItem
                          index={i}
                          isSelected={selectedIndex === i}
                          onClick={() => {
                            setHighlightedIndex(i);
                            selectOptionAndCleanUp(option);
                          }}
                          onMouseEnter={() => {
                            setHighlightedIndex(i);
                          }}
                          key={option.key}
                          option={option}
                        />
                      ))}
                    </ul>
                </div>
              </FloatingPortal>
            ) : null
          }
        />
    }
    .reset-anchor {
     top: initial!important;
     left: initial!important;
    }
  8. added
    other
    and removed
    other
    on Apr 5, 2023
  9. candidia commented on Jun 19, 2023

    @candidia

    @tylerjbainbridge I completely agree this should be more composable with other libraries whose purposes are positioning popovers like floating-ui and ariakit

  10. jvandenaardweg commented on Jun 19, 2023

    @jvandenaardweg

    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()
      ],
    });
    onOpen={(r) => {
      refs.setPositionReference({
        getBoundingClientRect: r.getRect,
      });
    }}
  11. thorn0 commented on Sep 18, 2023

    @thorn0
    Contributor

    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:

    1. open the playground
    2. delete everything there
    3. press enter 5 times
    4. type / - the menu appears under the caret
    5. type pa - the menu changes its position
    6. 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.

  12. removed
    good first issue
    on Apr 5, 2024
  13. added
    popover
    on Dec 9, 2024
  14. etrepum commented on Dec 9, 2024

    @etrepum
    Collaborator

    Can't reproduce

  15. cemremengu commented on Dec 20, 2024

    @cemremengu

    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.

  16. 1111mp commented on Aug 27, 2025

    @1111mp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    all-platforms-bugpopovertypeahead

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions