Skip to content

Commit ca26b6c

Browse files
committed
fix(menu): inconsistent behavior when clicking on a disabled item
With our current setup any click inside a menu will close it, however browsers don't trigger mouse events when clicking on disabled buttons. This is somewhat consistent, however browsers **do** trigger events for non-disabled child nodes of a button (e.g. an icon). This means that there will be regions inside of a disabled item that will behave differently from others. These changes make the behavior consistent by disabling pointer events on disabled items, allowing the user to click through and have the menu act as if it was a click anywhere else. Fixes #16694.
1 parent 8e321ae commit ca26b6c

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('menu', () => {
5757

5858
it('should run not run click handlers on disabled menu items', async () => {
5959
await page.trigger().click();
60-
await page.items(2).click();
60+
await browser.actions().mouseMove(page.items(2)).click();
6161
expect(await page.getResultText()).toEqual('');
6262
});
6363

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
text-decoration: none;
4141

4242
&[disabled] {
43+
// Usually every click inside the menu closes it, however some browsers will stop events
44+
// when the user clicks on a disabled item, **except** when the user clicks on a non-disabled
45+
// child node of the disabled button. This is inconsistent because some regions of a disabled
46+
// button will still cause the menu to close and some won't (see #16694). We make the behavior
47+
// more consistent by disabling pointer events and allowing the user to click through.
48+
pointer-events: none;
4349
cursor: default;
4450
}
4551

src/material/menu/menu.e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('menu', () => {
5757

5858
it('should run not run click handlers on disabled menu items', async () => {
5959
await page.trigger().click();
60-
await page.items(2).click();
60+
await browser.actions().mouseMove(page.items(2)).click();
6161
expect(await page.getResultText()).toEqual('');
6262
});
6363

src/material/menu/menu.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ $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+
4756
@include cdk-high-contrast {
4857
&.cdk-program-focused,
4958
&.cdk-keyboard-focused,

0 commit comments

Comments
 (0)