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

fix(cdk/menu): avoid re-opening the menu on enter #30263

Merged
merged 1 commit into from
Jan 2, 2025
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
8 changes: 8 additions & 0 deletions src/cdk/menu/menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ export class CdkMenuItem implements FocusableOption, FocusableElement, Toggler,
case ENTER:
// Skip events that will trigger clicks so the handler doesn't get triggered twice.
if (!hasModifierKey(event) && !eventDispatchesNativeClick(this._elementRef, event)) {
const nodeName = this._elementRef.nativeElement.nodeName;

// Avoid repeat events on non-native elements (see #30250). Note that we don't do this
// on the native elements so we don't interfere with their behavior (see #26296).
if (nodeName !== 'A' && nodeName !== 'BUTTON') {
event.preventDefault();
}

this.trigger({keepOpen: event.keyCode === SPACE && !this.closeOnSpacebarTrigger});
}
break;
Expand Down
23 changes: 22 additions & 1 deletion src/cdk/menu/menu-trigger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,22 @@ describe('MenuTrigger', () => {
expect(secondEvent.defaultPrevented).toBe(false);
});

it('should prevent the default action on enter presses on non-button/non-link triggers', () => {
fixture.componentInstance.useButtonTrigger = false;
fixture.changeDetectorRef.markForCheck();
detectChanges();

const firstEvent = dispatchKeyboardEvent(nativeTrigger, 'keydown', ENTER);
detectChanges();
expect(firstEvent.defaultPrevented).toBe(true);
expect(nativeMenus.length).toBe(2);

const secondEvent = dispatchKeyboardEvent(nativeTrigger, 'keydown', ENTER);
detectChanges();
expect(nativeMenus.length).toBe(1);
expect(secondEvent.defaultPrevented).toBe(true);
});

it('should close the open menu on background click', () => {
nativeTrigger.click();
detectChanges();
Expand Down Expand Up @@ -674,7 +690,11 @@ class TriggerOpensItsMenu {

@Component({
template: `
<button cdkMenuItem [cdkMenuTriggerFor]="sub1">First</button>
@if (useButtonTrigger) {
<button cdkMenuItem [cdkMenuTriggerFor]="sub1">First</button>
} @else {
<div cdkMenuItem [cdkMenuTriggerFor]="sub1">First</div>
}

<ng-template #sub1>
<div cdkMenu>
Expand All @@ -693,6 +713,7 @@ class StandaloneTriggerWithInlineMenu {
@ViewChild('submenu_item', {read: ElementRef}) submenuItem?: ElementRef<HTMLElement>;
@ViewChild('inline_item', {read: ElementRef}) nativeInlineItem: ElementRef<HTMLElement>;
@ViewChildren(CdkMenu, {read: ElementRef}) nativeMenus: QueryList<ElementRef>;
useButtonTrigger = true;
}

@Component({
Expand Down
Loading