Skip to content

Commit

Permalink
Toggle aria-modal attribute in parent modal when autocomplete panel o…
Browse files Browse the repository at this point in the history
…pens / closes (#27663)

* Toggle parent modal aria-owns attribute when autocomplete is closed

* Add unit tests and update comments

(cherry picked from commit f6c5ea4)
  • Loading branch information
eminguyen authored and mmalerba committed Oct 1, 2023
1 parent adbc4b4 commit d62cc12
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/material/autocomplete/autocomplete-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ export abstract class _MatAutocompleteTriggerBase
openPanel(): void {
this._attachOverlay();
this._floatLabel();
// Add aria-owns attribute when the autocomplete becomes visible.
if (this._trackedModal) {
const panelId = this.autocomplete.id;
addAriaReferencedId(this._trackedModal, 'aria-owns', panelId);
}
}

/** Closes the autocomplete suggestion panel. */
Expand Down Expand Up @@ -302,6 +307,12 @@ export abstract class _MatAutocompleteTriggerBase
// user clicks outside.
this._changeDetectorRef.detectChanges();
}

// Remove aria-owns attribute when the autocomplete is no longer visible.
if (this._trackedModal) {
const panelId = this.autocomplete.id;
removeAriaReferencedId(this._trackedModal, 'aria-owns', panelId);
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/material/autocomplete/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3729,6 +3729,31 @@ describe('MDC-based MatAutocomplete', () => {
.withContext('expecting modal to own the autocommplete panel')
.toContain(panelId);
}));

it('should remove the aria-owns attribute of the modal when the autocomplete panel closes', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.componentInstance.trigger.closePanel();
fixture.detectChanges();

const panelId = fixture.componentInstance.autocomplete.id;
const modalElement = fixture.componentInstance.modal.nativeElement;

expect(modalElement.getAttribute('aria-owns')).toBeFalsy();
}));

it('should readd the aria-owns attribute of the modal when the autocomplete panel opens again', fakeAsync(() => {
fixture.componentInstance.trigger.openPanel();
fixture.componentInstance.trigger.closePanel();
fixture.componentInstance.trigger.openPanel();
fixture.detectChanges();

const panelId = fixture.componentInstance.autocomplete.id;
const modalElement = fixture.componentInstance.modal.nativeElement;

expect(modalElement.getAttribute('aria-owns')?.split(' '))
.withContext('expecting modal to own the autocommplete panel')
.toContain(panelId);
}));
});
});

Expand Down

0 comments on commit d62cc12

Please sign in to comment.