Skip to content

Commit 4ddbc32

Browse files
committed
fix(material/dialog): ignore clicks on aria-disabled close buttons
For compatibility with the `disabledInteractive` input that we have in other components, these changes add some logic that will skip click events on `MatDialogClose` if it's `aria-disabled`. I went with this approach, as opposed to introducing another input, because it would also involve implementing `disabledInteractive` for the close button and may introduce conflicts with its host bindings. Fixes #33366.
1 parent 0fe6d61 commit 4ddbc32

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/material/dialog/dialog-content-directives.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export class MatDialogClose implements OnInit, OnChanges {
7070
}
7171

7272
_onButtonClick(event: MouseEvent) {
73+
// For compatibility with `disabledInteractive`. We don't need to handle plain `disabled`,
74+
// because disabled buttons don't dispatch click events by default. See #33366.
75+
if (this._elementRef.nativeElement.getAttribute('aria-disabled') === 'true') {
76+
return;
77+
}
78+
7379
// Determinate the focus origin using the click event, because using the FocusMonitor will
7480
// result in incorrect origins. Most of the time, close buttons will be auto focused in the
7581
// dialog, and therefore clicking the button won't result in a focus change. This means that

src/material/dialog/dialog.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,25 @@ describe('MatDialog', () => {
18351835
);
18361836
});
18371837

1838+
it('should not close when clicking on an aria-disabled close button', async () => {
1839+
expect(overlayContainerElement.querySelectorAll('.mat-mdc-dialog-container').length).toBe(
1840+
1,
1841+
);
1842+
1843+
const closeButton = overlayContainerElement.querySelector(
1844+
'button[mat-dialog-close]',
1845+
) as HTMLElement;
1846+
1847+
closeButton.setAttribute('aria-disabled', 'true');
1848+
closeButton.click();
1849+
viewContainerFixture.detectChanges();
1850+
await viewContainerFixture.whenStable();
1851+
1852+
expect(overlayContainerElement.querySelectorAll('.mat-mdc-dialog-container').length).toBe(
1853+
1,
1854+
);
1855+
});
1856+
18381857
it('should not close if [mat-dialog-close] is applied on a non-button node', () => {
18391858
expect(overlayContainerElement.querySelectorAll('.mat-mdc-dialog-container').length).toBe(
18401859
1,

0 commit comments

Comments
 (0)