Skip to content

bug(CdkMenuItem): Using CdkMenuItem as hostDirective not working properly #28094

Open
@johnnyelwailer

Description

@johnnyelwailer

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

I want to create a custom menu component, that utilizes CdkMenu:

@Component({
  selector: 'custom-menu',
  standalone: true,
  template: '<ng-content></ng-content>',
  hostDirectives: [CdkMenu],
})
export class CustomMenu {}

@Directive({
  selector: 'customMenuItem',
  standalone: true,
  hostDirectives: [CdkMenuItem],
})
export class CustomMenuItem {}
<button [cdkMenuTriggerFor]="customMenuTmpl">Open CUSTOM menu</button>

<ng-template #customMenuTmpl>
  <custom-menu>
    <button customMenuItem>Item 1</button>
    <button customMenuItem>Item 2</button>
    <button customMenuItem>Item 3</button>
  </custom-menu>
</ng-template>

However, the issue is, that the menu item does not behave as expected.

Reproduction

StackBlitz link: https://stackblitz.com/edit/qau9h1-invtxe?file=src%2Fexample%2Fexample.html
Steps to reproduce:

  1. For each button:
  2. Focus with keyboard
  3. Use arrows to navigate

Expected Behavior

Both menus should behave the same. The keyboard navigation should work.

Actual Behavior

The custom menu attaching a CdkMenuItem using hostDirectives does not have the same behavior as when using plain cdkMenuItem directives directly. Keyboard navigation does not work, and clicking an item does not close the menu.

Environment

  • Angular: 17
  • CDK/Material: 17
  • Browser(s): Chrome, Edge
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: cdk/menuneeds: clarificationThe issue does not contain enough information for the team to determine if it is a real bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions