Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Menu: Not Positioned Properly Relative to Fixed Element #6351

Open
peconomou929 opened this issue Apr 9, 2024 · 1 comment
Open

Menu: Not Positioned Properly Relative to Fixed Element #6351

peconomou929 opened this issue Apr 9, 2024 · 1 comment
Labels
Type: Bug Issue contains a defect related to a specific component.

Comments

@peconomou929
Copy link
Contributor

Describe the bug

Consider the code below. The important thing is that

  1. we have an element with a fixed position (i.e. CSS position: fixed;), and
  2. this contains an element which triggers a PrimeReact Menu to popup.
const menuRef = useRef<Menu>(null);

<FixedNavBar style={{position: 'fixed', top: '0'}}>
   <a onClick={(event) => menuRef.current?.toggle(event)}> Click Me! </a>
   <Menu
      model={ myMenuModel }
      ref={ menuRef }
      popup
   />
</FixedNavBar>

The expectation is that whenever the Menu is opened, it always (if there is enough room) appears right below the item that triggered it, in this case, the anchor element. To achieve this, PrimeReact sets inline styles on the menu which absolutely position it relative to the document body. The issue is that these computed positions fail when the menu should be positioned relative to a fixed element like a navigation bar. In fact, they fail no matter how you set the appendTo property of the menu.

  1. On the one hand, say I set appendTo={document.body} (the default), so that the menu appears outside of the fixed element. Then, if I scroll the window while the menu is open, the menu will move relative to the viewport while the navigation bar stays fixed (undesirable).
  2. On the other hand, say I set appendTo='self', so that the menu appears inside the fixed element. Then the position of the menu will depend on the window's vertical scroll position at the moment that the menu was opened.

This issue has actually already been raised, and, in my opinion, unjustifiably closed. It can be solved externally only with undesirable workarounds such as setting !important styles which override PrimeReact's inline styles.

Possible Solutions:

  1. add an attribute called popupPosition which can take the value absolute or fixed. If set to absolute use the current implementation. If set to fixed, compute the position of the targetRef relative to the viewport, and then set the position of the menu relative to the viewport as well, with position: fixed.
  2. allow external customisation of the style property (for position, top, left, etc.) so that they don't automatically get overridden by PrimeReact.

Reproducer

No response

PrimeReact version

10.6.2

React version

17.x

Language

TypeScript

Build / Runtime

Create React App (CRA)

Browser(s)

No response

Steps to reproduce the behavior

Take a look at the bug description.

Expected behavior

The menu should always appear right below the targetRef, even when working with fixed-position navigation bars.

@peconomou929 peconomou929 added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Apr 9, 2024
@melloware melloware added Type: Bug Issue contains a defect related to a specific component. and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Apr 9, 2024
@melloware
Copy link
Member

PR is welcome if you want to study the code how it opens the menu?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a defect related to a specific component.
Projects
None yet
Development

No branches or pull requests

2 participants