Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dialog): add ariaLabel and focusOnOpen config options #6558

Merged
merged 1 commit into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export class MatDialogConfig<D = any> {
/** ID of the element that describes the dialog. */
ariaDescribedBy?: string | null = null;

/** Aria label to assign to the dialog element */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A thought occurred to me (for a follow-up PR if you agree): to make the focus go to the root dialog element, we can just expose a tabIndex property on the dialog config. If it's 0, then it's the first focusable element. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that would work since the interactivity checker starts looking from the children. Also we always focus the dialog anyway in order to prevent the user from opening multiple dialogs while animating.

ariaLabel?: string | null = null;

/** Whether the dialog should focus the first focusable element on open. */
autoFocus?: boolean = true;

// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
}
7 changes: 5 additions & 2 deletions src/lib/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export function throwMatDialogContentAlreadyAttachedError() {
'class': 'mat-dialog-container',
'tabindex': '-1',
'[attr.role]': '_config?.role',
'[attr.aria-labelledby]': '_ariaLabelledBy',
'[attr.aria-labelledby]': '_config?.ariaLabel ? null : _ariaLabelledBy',
'[attr.aria-label]': '_config?.ariaLabel',
'[attr.aria-describedby]': '_config?.ariaDescribedBy || null',
'[@slideDialog]': '_state',
'(@slideDialog.start)': '_onAnimationStart($event)',
Expand Down Expand Up @@ -144,7 +145,9 @@ export class MatDialogContainer extends BasePortalOutlet {
// If were to attempt to focus immediately, then the content of the dialog would not yet be
// ready in instances where change detection has to run first. To deal with this, we simply
// wait for the microtask queue to be empty.
this._focusTrap.focusInitialElementWhenReady();
if (this._config.autoFocus) {
this._focusTrap.focusInitialElementWhenReady();
}
}

/** Restores focus to the element that was focused before the dialog opened. */
Expand Down
38 changes: 38 additions & 0 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,18 @@ describe('MatDialog', () => {
.toBe('INPUT', 'Expected first tabbable element (input) in the dialog to be focused.');
}));

it('should allow disabling focus of the first tabbable element', fakeAsync(() => {
dialog.open(PizzaMsg, {
viewContainerRef: testViewContainerRef,
autoFocus: false
});

viewContainerFixture.detectChanges();
flushMicrotasks();

expect(document.activeElement.tagName).not.toBe('INPUT');
}));

it('should re-focus trigger element when dialog closes', fakeAsync(() => {
// Create a element that has focus before the dialog is opened.
let button = document.createElement('button');
Expand Down Expand Up @@ -930,6 +942,32 @@ describe('MatDialog', () => {
}));

});

describe('aria-label', () => {
it('should be able to set a custom aria-label', () => {
dialog.open(PizzaMsg, {
ariaLabel: 'Hello there',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();

const container = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(container.getAttribute('aria-label')).toBe('Hello there');
});

it('should not set the aria-labelledby automatically if it has an aria-label', fakeAsync(() => {
dialog.open(ContentElementDialog, {
ariaLabel: 'Hello there',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
tick();
viewContainerFixture.detectChanges();

const container = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(container.hasAttribute('aria-labelledby')).toBe(false);
}));
});
});

describe('MatDialog with a parent MatDialog', () => {
Expand Down