Skip to content

Commit f6c5ea4

Browse files
authored
Toggle aria-modal attribute in parent modal when autocomplete panel opens / closes (#27663)
* Toggle parent modal aria-owns attribute when autocomplete is closed * Add unit tests and update comments
1 parent 82844b3 commit f6c5ea4

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/material/autocomplete/autocomplete-trigger.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,11 @@ export class MatAutocompleteTrigger
278278
openPanel(): void {
279279
this._attachOverlay();
280280
this._floatLabel();
281+
// Add aria-owns attribute when the autocomplete becomes visible.
282+
if (this._trackedModal) {
283+
const panelId = this.autocomplete.id;
284+
addAriaReferencedId(this._trackedModal, 'aria-owns', panelId);
285+
}
281286
}
282287

283288
/** Closes the autocomplete suggestion panel. */
@@ -317,6 +322,12 @@ export class MatAutocompleteTrigger
317322
// user clicks outside.
318323
this._changeDetectorRef.detectChanges();
319324
}
325+
326+
// Remove aria-owns attribute when the autocomplete is no longer visible.
327+
if (this._trackedModal) {
328+
const panelId = this.autocomplete.id;
329+
removeAriaReferencedId(this._trackedModal, 'aria-owns', panelId);
330+
}
320331
}
321332

322333
/**

src/material/autocomplete/autocomplete.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3729,6 +3729,31 @@ describe('MDC-based MatAutocomplete', () => {
37293729
.withContext('expecting modal to own the autocommplete panel')
37303730
.toContain(panelId);
37313731
}));
3732+
3733+
it('should remove the aria-owns attribute of the modal when the autocomplete panel closes', fakeAsync(() => {
3734+
fixture.componentInstance.trigger.openPanel();
3735+
fixture.componentInstance.trigger.closePanel();
3736+
fixture.detectChanges();
3737+
3738+
const panelId = fixture.componentInstance.autocomplete.id;
3739+
const modalElement = fixture.componentInstance.modal.nativeElement;
3740+
3741+
expect(modalElement.getAttribute('aria-owns')).toBeFalsy();
3742+
}));
3743+
3744+
it('should readd the aria-owns attribute of the modal when the autocomplete panel opens again', fakeAsync(() => {
3745+
fixture.componentInstance.trigger.openPanel();
3746+
fixture.componentInstance.trigger.closePanel();
3747+
fixture.componentInstance.trigger.openPanel();
3748+
fixture.detectChanges();
3749+
3750+
const panelId = fixture.componentInstance.autocomplete.id;
3751+
const modalElement = fixture.componentInstance.modal.nativeElement;
3752+
3753+
expect(modalElement.getAttribute('aria-owns')?.split(' '))
3754+
.withContext('expecting modal to own the autocommplete panel')
3755+
.toContain(panelId);
3756+
}));
37323757
});
37333758
});
37343759

0 commit comments

Comments
 (0)