Skip to content

Commit

Permalink
fix(modal): fall back to modal id for dialog aria-labelledby attrib…
Browse files Browse the repository at this point in the history
…ute (#1403)

## What is the current behavior?

If you set `[clrModalLabelledById]` to an empty string, the
`aria-labelledby` attribute of the modal dialog element is set to an
empty value.

This behavior can be seen in the modal stories since the input is
defaulted to an empty string. We can't default it to the modal id
because that is not known until the component is instantiated.

Issue Number: CDE-2050

## What is the new behavior?

If you set `[clrModalLabelledById]` to an empty string, the
`aria-labelledby` attribute of the modal dialog element is set to the
modal id.
  • Loading branch information
kevinbuhmann authored May 28, 2024
1 parent ec04af7 commit 6493dc3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion projects/angular/src/modal/modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
role="dialog"
aria-modal="true"
[attr.aria-hidden]="!_open"
[attr.aria-labelledby]="labelledBy"
[attr.aria-labelledby]="labelledBy || modalId"
>
<div class="clr-sr-only">{{commonStrings.keys.modalContentStart}}</div>
<div class="modal-content-wrapper">
Expand Down
23 changes: 21 additions & 2 deletions projects/angular/src/modal/modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,29 @@ describe('Modal', () => {
expect(compiled.querySelector('.close').getAttribute('aria-label')).toBe('custom close label');
});

it('should add expected aria-labelledby', () => {
// open modal
it('should use modal id for aria-labelledby by default', () => {
modal.open();
fixture.detectChanges();

expect(compiled.querySelector('.modal-dialog').getAttribute('aria-labelledby')).toBe(modal.modalId);
});

it('should allow a custom aria-labelledby attribute value', () => {
modal.labelledBy = 'custom-id';

modal.open();
fixture.detectChanges();

expect(compiled.querySelector('.modal-dialog').getAttribute('aria-labelledby')).toBe('custom-id');
});

it('should fall back to the modal id for the aria-labelledby attribute value', () => {
// set to a falsy value
modal.labelledBy = '';

modal.open();
fixture.detectChanges();

expect(compiled.querySelector('.modal-dialog').getAttribute('aria-labelledby')).toBe(modal.modalId);
});

Expand Down
2 changes: 1 addition & 1 deletion projects/angular/src/modal/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ClrModal implements OnChanges, OnDestroy {
@Input('clrModalPreventClose') stopClose = false;
@Output('clrModalAlternateClose') altClose = new EventEmitter<boolean>(false);

@Input('clrModalLabelledById') labelledBy = this.modalId;
@Input('clrModalLabelledById') labelledBy: string;

// presently this is only used by inline wizards
@Input('clrModalOverrideScrollService') bypassScrollService = false;
Expand Down

0 comments on commit 6493dc3

Please sign in to comment.