question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug(Menu): Wrapping Menu in your own component breaks focusFirstItem

See original GitHub issue

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

No response

Description

In the example below…

  1. focus on the menu trigger (the button) then hit Enter on keyboard…
  2. the menu.js code runs the focusFirstItem method…
  3. this._directDescendantItems is empty… and thus keyboard navigation breaks on the dropdown breaks

BROKEN EXAMPLE (pass the values of dropdown from parent component):

@Component({
  selector: 'app-dropdown',
  template: `
    <button [matMenuTriggerFor]="dd">{{label}}</button>
    <mat-menu #dd="matMenu">
      <ng-content></ng-content>
    </mat-menu>
  `,
  ...

parent component:

<app-dropdown label="My Dropdown">
   <button mat-menu-item>Settings</button>
   <button mat-menu-item>Log off</button>
</app-dropdown>

Hard-coding things there works fine (obviously):

@Component({
  selector: 'app-dropdown',
  template: `
    <button [matMenuTriggerFor]="dd">{{label}}</button>
    <mat-menu #dd="matMenu">
       <button mat-menu-item>Settings</button>
       <button mat-menu-item>Log off</button>
    </mat-menu>
  `,

this works

Reproduction

Steps to reproduce:

  1. wrapper menu with component using slots (content projection)
  2. use keyboard to open menu

Expected Behavior

Opening menu with keyboard would focus first menu-item

Actual Behavior

Opening menu with keyboard fails to focus menu items

Environment

  • Angular: 14.0.4
  • CDK/Material: 14.0.4
  • Browser(s): Chrome Latest (103)
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows 10

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rothsandrocommented, Sep 12, 2022

@marcel-goldammer Ah, the menu items are in a third component. Yes, that doesn’t work. menu + menu item must be in the same component, only the button can be extracted and reused 😦

0reactions
angular-automatic-lock-bot[bot]commented, Oct 13, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrapping Menu Component of Angular material 6
I am wrapping the angular material menu component. I am extending the MatMenuTrigger, MatMenuItem, making them as separate directives and ...
Read more >
@material/menu-surface | Yarn - Package Manager
NOTE: Material Components Web tends to release breaking changes on a monthly basis, but follows semver so you can control when you incorporate...
Read more >
Menu - Angular Material
aria-labelledby for the menu panel. @Input(). backdropClass: string. Class to be added to the backdrop element.
Read more >
Context Menu wrap item name without taking full width - Telerik
Ever since we upgraded to Kendo release 2016.3.1118, the context menu wrapping is broken, and is still broken with latest version.
Read more >
bug(Menu): Wrapping Menu in your own component breaks ...
focus on the menu trigger (the button) then hit Enter on keyboard... the menu.js code runs the focusFirstItem method.. this.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found