Skip to content

Commit

Permalink
feat(modal): aria-labelledby attribute for modal window
Browse files Browse the repository at this point in the history
Closes #1477
Closes #2049
  • Loading branch information
ymeine authored and maxokorokov committed Jun 28, 2018
1 parent d48f5dc commit 3cc6fa7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/src/app/components/modal/demos/basic/modal-basic.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ng-template #content let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Profile update</h4>
<h4 class="modal-title" id="modal-basic-title">Profile update</h4>
<button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
<span aria-hidden="true">&times;</span>
</button>
Expand Down
2 changes: 1 addition & 1 deletion demo/src/app/components/modal/demos/basic/modal-basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class NgbdModalBasic {
constructor(private modalService: NgbModal) {}

open(content) {
this.modalService.open(content).result.then((result) => {
this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
Expand Down
2 changes: 1 addition & 1 deletion src/modal/modal-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {NgbActiveModal, NgbModalRef} from './modal-ref';
@Injectable()
export class NgbModalStack {
private _document: any;
private _windowAttributes = ['backdrop', 'centered', 'keyboard', 'size', 'windowClass'];
private _windowAttributes = ['ariaLabelledBy', 'backdrop', 'centered', 'keyboard', 'size', 'windowClass'];
private _backdropAttributes = ['backdropClass'];

constructor(
Expand Down
4 changes: 3 additions & 1 deletion src/modal/modal-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {ngbFocusTrap} from '../util/focus-trap';
'role': 'dialog',
'tabindex': '-1',
'(keyup.esc)': 'escKey($event)',
'(click)': 'backdropClick($event)'
'(click)': 'backdropClick($event)',
'[attr.aria-labelledby]': 'ariaLabelledBy',
},
template: `
<div [class]="'modal-dialog' + (size ? ' modal-' + size : '') + (centered ? ' modal-dialog-centered' : '')" role="document">
Expand All @@ -35,6 +36,7 @@ export class NgbModalWindow implements OnInit,
private _document: any;
private _elWithFocus: Element; // element that is focused prior to modal opening

@Input() ariaLabelledBy: string;
@Input() backdrop: boolean | string = true;
@Input() centered: string;
@Input() keyboard = true;
Expand Down
15 changes: 15 additions & 0 deletions src/modal/modal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,22 @@ describe('ngb-modal', () => {
fixture.detectChanges();
expect(fixture.nativeElement).not.toHaveModal();
});
});

describe('accessibility', () => {
it('should support aria-labelledby', () => {
const id = 'aria-labelledby-id';

const modalInstance = fixture.componentInstance.open('foo', {ariaLabelledBy: id});
fixture.detectChanges();

const modalElement = <HTMLElement>document.querySelector('ngb-modal-window');
expect(modalElement.getAttribute('aria-labelledby')).toBe(id);

modalInstance.close('some result');
fixture.detectChanges();
expect(fixture.nativeElement).not.toHaveModal();
});
});
});

Expand Down
5 changes: 5 additions & 0 deletions src/modal/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {NgbModalRef} from './modal-ref';
* Represent options available when opening new modal windows.
*/
export interface NgbModalOptions {
/**
* Sets the aria attribute aria-labelledby.
*/
ariaLabelledBy?: string;

/**
* Whether a backdrop element should be created for a given modal (true by default).
* Alternatively, specify 'static' for a backdrop which doesn't close the modal on click.
Expand Down

0 comments on commit 3cc6fa7

Please sign in to comment.