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

bug(Menu): Incorrect position of submenu while using ng-template #26252

Open
1 task done
csisy opened this issue Dec 13, 2022 · 2 comments
Open
1 task done

bug(Menu): Incorrect position of submenu while using ng-template #26252

csisy opened this issue Dec 13, 2022 · 2 comments
Labels
area: material/menu P4 A relatively minor issue that is not relevant to core functions

Comments

@csisy
Copy link

csisy commented Dec 13, 2022

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

11.x

Description

After some digging, I found the commit that break the positioning of the submenu when the menu items are created by using ng-template

The first related issue is #16167 and the corresponding commit changed the way the padding is determined. Instead of using a fix value, it calculates the padding from the "offset of the first item".

Also, another issue (which is still open) #14842 clearly states that @ContentChildren does not find child elements created via ngTemplateOutlet.

These hand in hand break our code when we changed Angular version. Here is what happens:

  • We have a template for "no-child menu items"
  • The menu item is created by using ngTemplateOutlet when the menu item does not have any child element
  • Otherwise, a button[mat-menu-item] is created directly

This causes a problem, since the _items collection of the menu does not contain the elements created from the ng-template so the calculated "padding" is totally wrong (in our case 200px instead of 8px).

A possible fix would be that instead of using the first item offset, the style of the first item's parent could be calculated using window.getComputedStyle, for example.

Reproduction

Steps to reproduce (see stackblitz):

  1. Open the main menu
  2. Open a submenu (position is off)

The stackblitz is greatly simplified.

Expected Behavior

The appropriate padding should be calculated so the submenu can be shown in the right place.

Actual Behavior

The submenu is shown in the wrong place

Environment

  • Angular: 14.2.0
  • CDK/Material: 14.2.0
  • Browser(s): Google Chrome 108.0.5359.95
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows 10
@csisy csisy added the needs triage This issue needs to be triaged by the team label Dec 13, 2022
@SLKnutson
Copy link

As a work around, you can add a hidden disabled button as an item of the parent menu.

@wagnermaciel wagnermaciel added P4 A relatively minor issue that is not relevant to core functions area: material/menu and removed needs triage This issue needs to be triaged by the team labels May 3, 2023
@maertz
Copy link

maertz commented Apr 3, 2024

Still there, recreated the stackblitz sample from @csisy with the nested menu sample from the docs. Thanks @SLKnutson for the workaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/menu P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

No branches or pull requests

4 participants