primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Popup-Menu doesn't work in positioned divs

Open Pranen opened this issue 10 years ago • 11 comments

Hi,

I try currently to migrate to the newest version of primeng but I run into problems in the popup-menu.

Normally, the popup-menu runs excellently, but If you put it into a div that is positioned the absolute positioning of the element fails because it's always positioned to the top of the page and not of its correct parent.

If you look into the old version (before the refactoring of the menu), the menus were created on root-level in the dom-tree and the positioning worked. But now, the menu is created at the place where the tag is written and so the computation of the offset isn't correct anymore.

Example to reproduce:

DOES WORK:

<button type="button" pButton icon="fa fa-list" label="Show" (click)="menu3.toggle($event)"></button>
<p-menu #menu3 popup="popup" [model]="menuItems"></p-menu>

DOESN'T WORK:

<div style="position: relative; border: 1px dashed green;">
    <button type="button" pButton icon="fa fa-list" label="Show" (click)="menu.toggle($event)"></button>
    <p-menu #menu popup="popup" [model]="menuItems"></p-menu>
</div>

Any clue how to solve this problem?

Regards, Peter

Pranen avatar Jul 18 '16 09:07 Pranen

Hi , Thats because of our menu contains position:absolute when you wrap it with another positioned div it still works but menu is visible somewhere else on the screen how is your usecase ?

Mrtcndkn avatar Jul 21 '16 09:07 Mrtcndkn

This is expected as menu is absolute positioned against document but having a div with relative positioning makes it relative to this div not the body causing an offset.

One solution could be adding an option like appendToBody="true", we'll try to see if this breaks anything inside the context of Angular2.

Can you avoid position:relative by the way?

cagataycivici avatar Jul 21 '16 09:07 cagataycivici

Hi,

thanks for the fast answer.

Unfortunately, we have to create a dashboard and the library that we are using needs to position the divs by css.

At the moment, we are stick on beta.5 because in that version, the popup-position is working correct.

Regards, Peter

Pranen avatar Jul 21 '16 11:07 Pranen

https://github.com/primefaces/primeng/issues/639 i think this solution will work for you.

Mrtcndkn avatar Jul 21 '16 21:07 Mrtcndkn

appendTo="body" should do it.

cagataycivici avatar Jul 25 '16 11:07 cagataycivici

cool man, it is working

huangzhenhong avatar Mar 07 '18 16:03 huangzhenhong

For me, appendTo="body" also working but still not showing items! I need to mention that I'm using serenity primeng template

medimohammadise avatar Mar 14 '18 11:03 medimohammadise

we are having he same problem in React. though in react i cannot use the appendTo="body"

AlpineVibrations avatar Dec 20 '18 00:12 AlpineVibrations

appendTo="body" is not working . Any other solutions?

dkolasani9 avatar Sep 27 '19 03:09 dkolasani9

append to body ruins the tab accessibility and makes it untargetable,

liamsmithaha avatar Jun 18 '21 04:06 liamsmithaha

appendTo="body" is not working . Any other solutions?

Try to use this -> [appendTo]=id it will work in all cases.

#id means try to pass id in ng-templates, div or ng-container

  • for example - <ng-template #id></ng-template> <ng-container #id></ng-container> <div #id></div>

amitesh786 avatar Jun 23 '22 17:06 amitesh786