Skip to content

feat(cdk-experimental/menu): enable keyboard handling for context menu #20171

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/cdk-experimental/menu/context-menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ describe('CdkContextMenuTrigger', () => {

expect(getContextMenu()).toBeDefined();
});

it('should focus the first menuitem when the context menu is opened', () => {
openContextMenu();
expect(document.querySelector(':focus')!.id).toEqual('first_menu_item');
});
});

describe('nested context menu triggers', () => {
Expand Down Expand Up @@ -250,7 +255,7 @@ describe('CdkContextMenuTrigger', () => {

<ng-template cdkMenuPanel #context="cdkMenuPanel">
<div cdkMenu [cdkMenuPanel]="context">
<button cdkMenuItem></button>
<button id="first_menu_item" cdkMenuItem></button>
</div>
</ng-template>
`,
Expand Down
9 changes: 9 additions & 0 deletions src/cdk-experimental/menu/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ export class CdkContextMenuTrigger implements OnDestroy {

this._contextMenuTracker.update(this);
this.open({x: event.clientX, y: event.clientY});

// A context menu can be triggered via a mouse right click or a keyboard shortcut.
if (event.button === 2) {
this._menuPanel._menu?.focusFirstItem('mouse');
} else if (event.button === 0) {
this._menuPanel._menu?.focusFirstItem('keyboard');
} else {
this._menuPanel._menu?.focusFirstItem('program');
}
}
}

Expand Down