Skip to content

Commit db4d1eb

Browse files
committed
fix(menu): clicks on disabled item closing the menu
We have some logic to prevent clicks on disabled items from closing the menu. The problem is that browsers swallow clicks and don't fire their event listeners for disabled `button` nodes, but not any non-disabled child nodes. In #16696 we made it consistent so clicks don't land on any of the button's elements, but these changes fix the underlying issue by binding the event both on the root `button`, and a wrapper `span` that's around the content. This way we're guaranteed to hit at least one of the listeners. These changes also move the event outside the `NgZone` since it doesn't trigger any changes in the view. Fixes #19173.
1 parent 2a97418 commit db4d1eb

File tree

8 files changed

+86
-36
lines changed

8 files changed

+86
-36
lines changed

src/material-experimental/mdc-menu/menu-item.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ng-content></ng-content>
1+
<span class="mat-mdc-menu-item-content" #content><ng-content></ng-content></span>
22
<div class="mat-mdc-menu-ripple" matRipple
33
[matRippleDisabled]="disableRipple || disabled"
44
[matRippleTrigger]="_getHostElement()">

src/material-experimental/mdc-menu/menu.scss

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@
5555
text-decoration: none;
5656

5757
&[disabled] {
58-
// Usually every click inside the menu closes it, however some browsers will stop events
59-
// when the user clicks on a disabled item, **except** when the user clicks on a non-disabled
60-
// child node of the disabled button. This is inconsistent because some regions of a disabled
61-
// button will still cause the menu to close and some won't (see #16694). We make the behavior
62-
// more consistent by disabling pointer events and allowing the user to click through.
63-
pointer-events: none;
6458
cursor: default;
6559
}
6660

@@ -86,6 +80,11 @@
8680
}
8781
}
8882

83+
.mat-mdc-menu-item-content {
84+
display: flex;
85+
align-items: center;
86+
}
87+
8988
// Renders out a chevron on menu items that trigger a sub-menu.
9089
.mat-mdc-menu-item-submenu-trigger {
9190
@include mat-menu-item-submenu-trigger($mdc-list-side-padding);

src/material-experimental/mdc-menu/menu.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,23 @@ describe('MDC-based MatMenu', () => {
569569
expect(items.every(item => item.getAttribute('role') === 'menuitem')).toBe(true);
570570
});
571571

572+
it('should prevent the default action when clicking on a disabled item', () => {
573+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
574+
fixture.detectChanges();
575+
fixture.componentInstance.trigger.openMenu();
576+
fixture.detectChanges();
577+
578+
const item = overlayContainerElement.querySelector('.mat-mdc-menu-item[disabled]')!;
579+
const itemEvent = dispatchFakeEvent(item, 'click');
580+
fixture.detectChanges();
581+
expect(itemEvent.defaultPrevented).toBe(true);
582+
583+
const contentWrapper = item.querySelector('span')!;
584+
const wrapperEvent = dispatchFakeEvent(contentWrapper, 'click');
585+
fixture.detectChanges();
586+
expect(wrapperEvent.defaultPrevented).toBe(true);
587+
});
588+
572589
it('should be able to set an alternate role on the menu items', () => {
573590
const fixture = createComponent(MenuWithCheckboxItems);
574591
fixture.detectChanges();

src/material/menu/menu-item.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ng-content></ng-content>
1+
<span #content><ng-content></ng-content></span>
22
<div class="mat-menu-ripple" matRipple
33
[matRippleDisabled]="disableRipple || disabled"
44
[matRippleTrigger]="_getHostElement()">

src/material/menu/menu-item.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import {
1818
Optional,
1919
Input,
2020
HostListener,
21+
NgZone,
2122
AfterViewInit,
23+
ViewChild,
2224
} from '@angular/core';
2325
import {
2426
CanDisable, CanDisableCtor,
@@ -63,6 +65,9 @@ export class MatMenuItem extends _MatMenuItemMixinBase
6365
/** ARIA role for the menu item. */
6466
@Input() role: 'menuitem' | 'menuitemradio' | 'menuitemcheckbox' = 'menuitem';
6567

68+
/** Reference to the element wrapping the projected content. */
69+
@ViewChild('content') _content: ElementRef<HTMLElement> | undefined;
70+
6671
private _document: Document;
6772

6873
/** Stream that emits when the menu item is hovered. */
@@ -81,9 +86,11 @@ export class MatMenuItem extends _MatMenuItemMixinBase
8186
private _elementRef: ElementRef<HTMLElement>,
8287
@Inject(DOCUMENT) document?: any,
8388
private _focusMonitor?: FocusMonitor,
84-
@Inject(MAT_MENU_PANEL) @Optional() public _parentMenu?: MatMenuPanel<MatMenuItem>) {
89+
@Inject(MAT_MENU_PANEL) @Optional() public _parentMenu?: MatMenuPanel<MatMenuItem>,
90+
private _ngZone?: NgZone) {
8591

8692
// @breaking-change 8.0.0 make `_focusMonitor` and `document` required params.
93+
// @breaking-change 11.0.0 make `_ngZone` a required parameter.
8794
super();
8895

8996
if (_parentMenu && _parentMenu.addItem) {
@@ -111,6 +118,13 @@ export class MatMenuItem extends _MatMenuItemMixinBase
111118
// mouse or touch interaction.
112119
this._focusMonitor.monitor(this._elementRef, false);
113120
}
121+
122+
// @breaking-change 11.0.0 Remove null check for `_ngZone`.
123+
if (this._ngZone) {
124+
this._ngZone.runOutsideAngular(() => this._bindDisabledClickEvents());
125+
} else {
126+
this._bindDisabledClickEvents();
127+
}
114128
}
115129

116130
ngOnDestroy() {
@@ -122,6 +136,11 @@ export class MatMenuItem extends _MatMenuItemMixinBase
122136
this._parentMenu.removeItem(this);
123137
}
124138

139+
this._elementRef.nativeElement.removeEventListener('click', this._preventDisabledClicks);
140+
if (this._content) {
141+
this._content.nativeElement.removeEventListener('click', this._preventDisabledClicks);
142+
}
143+
125144
this._hovered.complete();
126145
this._focused.complete();
127146
}
@@ -136,20 +155,6 @@ export class MatMenuItem extends _MatMenuItemMixinBase
136155
return this._elementRef.nativeElement;
137156
}
138157

139-
/** Prevents the default element actions if it is disabled. */
140-
// We have to use a `HostListener` here in order to support both Ivy and ViewEngine.
141-
// In Ivy the `host` bindings will be merged when this class is extended, whereas in
142-
// ViewEngine they're overwritten.
143-
// TODO(crisbeto): we move this back into `host` once Ivy is turned on by default.
144-
// tslint:disable-next-line:no-host-decorator-in-concrete
145-
@HostListener('click', ['$event'])
146-
_checkDisabled(event: Event): void {
147-
if (this.disabled) {
148-
event.preventDefault();
149-
event.stopPropagation();
150-
}
151-
}
152-
153158
/** Emits to the hover stream. */
154159
// We have to use a `HostListener` here in order to support both Ivy and ViewEngine.
155160
// In Ivy the `host` bindings will be merged when this class is extended, whereas in
@@ -163,7 +168,8 @@ export class MatMenuItem extends _MatMenuItemMixinBase
163168

164169
/** Gets the label to be used when determining whether the option should be focused. */
165170
getLabel(): string {
166-
const element: HTMLElement = this._elementRef.nativeElement;
171+
const element: HTMLElement = this._content ?
172+
this._content.nativeElement : this._elementRef.nativeElement;
167173
const textNodeType = this._document ? this._document.TEXT_NODE : 3;
168174
let output = '';
169175

@@ -183,6 +189,26 @@ export class MatMenuItem extends _MatMenuItemMixinBase
183189
return output.trim();
184190
}
185191

192+
/** Binds the click events that prevent the default actions while disabled. */
193+
private _bindDisabledClickEvents() {
194+
// We need to bind this event both on the root node and the content wrapper, because browsers
195+
// won't dispatch events on disabled `button` nodes, but they'll still be dispatched if the
196+
// user interacts with a non-disabled child of the button. This means that can get regions
197+
// inside a disabled menu item where clicks land and others where they don't.
198+
this._elementRef.nativeElement.addEventListener('click', this._preventDisabledClicks);
199+
if (this._content) {
200+
this._content.nativeElement.addEventListener('click', this._preventDisabledClicks);
201+
}
202+
}
203+
204+
/** Prevents the default click action if the menu item is disabled. */
205+
private _preventDisabledClicks = (event: Event) => {
206+
if (this.disabled) {
207+
event.preventDefault();
208+
event.stopPropagation();
209+
}
210+
}
211+
186212
static ngAcceptInputType_disabled: BooleanInput;
187213
static ngAcceptInputType_disableRipple: BooleanInput;
188214
}

src/material/menu/menu.scss

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ $mat-menu-submenu-indicator-size: 10px !default;
4444
@include mat-menu-item-base();
4545
position: relative;
4646

47-
&[disabled] {
48-
// Usually every click inside the menu closes it, however some browsers will stop events
49-
// when the user clicks on a disabled item, **except** when the user clicks on a non-disabled
50-
// child node of the disabled button. This is inconsistent because some regions of a disabled
51-
// button will still cause the menu to close and some won't (see #16694). We make the behavior
52-
// more consistent by disabling pointer events and allowing the user to click through.
53-
pointer-events: none;
54-
}
55-
5647
@include cdk-high-contrast(active, off) {
5748
&.cdk-program-focused,
5849
&.cdk-keyboard-focused,

src/material/menu/menu.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,23 @@ describe('MatMenu', () => {
604604
expect(items.every(item => item.getAttribute('role') === 'menuitem')).toBe(true);
605605
});
606606

607+
it('should prevent the default action when clicking on a disabled item', () => {
608+
const fixture = createComponent(SimpleMenu, [], [FakeIcon]);
609+
fixture.detectChanges();
610+
fixture.componentInstance.trigger.openMenu();
611+
fixture.detectChanges();
612+
613+
const item = overlayContainerElement.querySelector('.mat-menu-item[disabled]')!;
614+
const itemEvent = dispatchFakeEvent(item, 'click');
615+
fixture.detectChanges();
616+
expect(itemEvent.defaultPrevented).toBe(true);
617+
618+
const contentWrapper = item.querySelector('span')!;
619+
const wrapperEvent = dispatchFakeEvent(contentWrapper, 'click');
620+
fixture.detectChanges();
621+
expect(wrapperEvent.defaultPrevented).toBe(true);
622+
});
623+
607624
it('should be able to set an alternate role on the menu items', () => {
608625
const fixture = createComponent(MenuWithCheckboxItems);
609626
fixture.detectChanges();

tools/public_api_guard/material/menu.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ export interface MatMenuDefaultOptions {
101101
}
102102

103103
export declare class MatMenuItem extends _MatMenuItemMixinBase implements FocusableOption, CanDisable, CanDisableRipple, AfterViewInit, OnDestroy {
104+
_content: ElementRef<HTMLElement> | undefined;
104105
readonly _focused: Subject<MatMenuItem>;
105106
_highlighted: boolean;
106107
readonly _hovered: Subject<MatMenuItem>;
107108
_parentMenu?: MatMenuPanel<MatMenuItem> | undefined;
108109
_triggersSubmenu: boolean;
109110
role: 'menuitem' | 'menuitemradio' | 'menuitemcheckbox';
110-
constructor(_elementRef: ElementRef<HTMLElement>, document?: any, _focusMonitor?: FocusMonitor | undefined, _parentMenu?: MatMenuPanel<MatMenuItem> | undefined);
111-
_checkDisabled(event: Event): void;
111+
constructor(_elementRef: ElementRef<HTMLElement>, document?: any, _focusMonitor?: FocusMonitor | undefined, _parentMenu?: MatMenuPanel<MatMenuItem> | undefined, _ngZone?: NgZone | undefined);
112112
_getHostElement(): HTMLElement;
113113
_getTabIndex(): string;
114114
_handleMouseEnter(): void;
@@ -119,7 +119,7 @@ export declare class MatMenuItem extends _MatMenuItemMixinBase implements Focusa
119119
static ngAcceptInputType_disableRipple: BooleanInput;
120120
static ngAcceptInputType_disabled: BooleanInput;
121121
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatMenuItem, "[mat-menu-item]", ["matMenuItem"], { "disabled": "disabled"; "disableRipple": "disableRipple"; "role": "role"; }, {}, never, ["*"]>;
122-
static ɵfac: i0.ɵɵFactoryDef<MatMenuItem, [null, null, null, { optional: true; }]>;
122+
static ɵfac: i0.ɵɵFactoryDef<MatMenuItem, [null, null, null, { optional: true; }, null]>;
123123
}
124124

125125
export declare class MatMenuModule {

0 commit comments

Comments
 (0)