Skip to content

Commit f6f9e61

Browse files
committed
fix: address more feedback
1 parent d4984fd commit f6f9e61

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

src/lib/core/a11y/focus-trap.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class FocusTrap {
2929
this._enabled = val;
3030
this._startAnchor.tabIndex = this._endAnchor.tabIndex = this._enabled ? 0 : -1;
3131
}
32-
private _enabled: boolean = false;
32+
private _enabled: boolean = true;
3333

3434
constructor(
3535
private _element: HTMLElement,
@@ -42,9 +42,7 @@ export class FocusTrap {
4242
}
4343
}
4444

45-
/**
46-
* Destroys the focus trap by cleaning up the anchors.
47-
*/
45+
/** Destroys the focus trap by cleaning up the anchors. */
4846
destroy() {
4947
if (this._startAnchor.parentNode) {
5048
this._startAnchor.parentNode.removeChild(this._startAnchor);
@@ -74,24 +72,22 @@ export class FocusTrap {
7472
}
7573

7674
/**
77-
* Waits for microtask queue to empty, then focuses the first tabbable element within the focus
78-
* trap region.
75+
* Waits for microtask queue to empty, then focuses
76+
* the first tabbable element within the focus trap region.
7977
*/
8078
focusFirstTabbableElementWhenReady() {
8179
this._ngZone.onMicrotaskEmpty.first().subscribe(() => this.focusFirstTabbableElement());
8280
}
8381

8482
/**
85-
* Waits for microtask queue to empty, then focuses the last tabbable element within the focus
86-
* trap region.
83+
* Waits for microtask queue to empty, then focuses
84+
* the last tabbable element within the focus trap region.
8785
*/
8886
focusLastTabbableElementWhenReady() {
8987
this._ngZone.onMicrotaskEmpty.first().subscribe(() => this.focusLastTabbableElement());
9088
}
9189

92-
/**
93-
* Focuses the first tabbable element within the focus trap region.
94-
*/
90+
/** Focuses the first tabbable element within the focus trap region. */
9591
focusFirstTabbableElement() {
9692
let redirectToElement = this._element.querySelector('[cdk-focus-start]') as HTMLElement ||
9793
this._getFirstTabbableElement(this._element);
@@ -101,9 +97,7 @@ export class FocusTrap {
10197
}
10298
}
10399

104-
/**
105-
* Focuses the last tabbable element within the focus trap region.
106-
*/
100+
/** Focuses the last tabbable element within the focus trap region. */
107101
focusLastTabbableElement() {
108102
let focusTargets = this._element.querySelectorAll('[cdk-focus-end]');
109103
let redirectToElement: HTMLElement = null;
@@ -154,24 +148,23 @@ export class FocusTrap {
154148
return null;
155149
}
156150

151+
/** Creates an anchor element. */
157152
private _createAnchor(): HTMLElement {
158153
let anchor = document.createElement('div');
159-
anchor.tabIndex = 0;
154+
anchor.tabIndex = this._enabled ? 0 : -1;
160155
anchor.classList.add('cdk-visually-hidden');
161156
anchor.classList.add('cdk-focus-trap-anchor');
162157
return anchor;
163158
}
164159
}
165160

166161

167-
/**
168-
* Factory that allows easy instantiation of focus traps.
169-
*/
162+
/** Factory that allows easy instantiation of focus traps. */
170163
@Injectable()
171164
export class FocusTrapFactory {
172165
constructor(private _checker: InteractivityChecker, private _ngZone: NgZone) { }
173166

174-
attach(element: HTMLElement, deferAnchors = false): FocusTrap {
167+
create(element: HTMLElement, deferAnchors = false): FocusTrap {
175168
return new FocusTrap(element, this._checker, this._ngZone, deferAnchors);
176169
}
177170
}
@@ -195,7 +188,7 @@ export class FocusTrapDeprecatedDirective implements OnDestroy, AfterContentInit
195188
}
196189

197190
constructor(private _elementRef: ElementRef, private _focusTrapFactory: FocusTrapFactory) {
198-
this.focusTrap = this._focusTrapFactory.attach(this._elementRef.nativeElement, true);
191+
this.focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement, true);
199192
}
200193

201194
ngOnDestroy() {
@@ -208,9 +201,7 @@ export class FocusTrapDeprecatedDirective implements OnDestroy, AfterContentInit
208201
}
209202

210203

211-
/**
212-
* Directive for trapping focus within a region.
213-
*/
204+
/** Directive for trapping focus within a region. */
214205
@Directive({
215206
selector: '[cdkTrapFocus]'
216207
})
@@ -223,7 +214,7 @@ export class FocusTrapDirective implements OnDestroy, AfterContentInit {
223214
set enabled(val: boolean) { this.focusTrap.enabled = val; }
224215

225216
constructor(private _elementRef: ElementRef, private _focusTrapFactory: FocusTrapFactory) {
226-
this.focusTrap = this._focusTrapFactory.attach(this._elementRef.nativeElement, true);
217+
this.focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement, true);
227218
}
228219

229220
ngOnDestroy() {

src/lib/dialog/dialog-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class MdDialogContainer extends BasePortalHost implements OnDestroy {
9090
*/
9191
private _trapFocus() {
9292
if (!this._focusTrap) {
93-
this._focusTrap = this._focusTrapFactory.attach(this._elementRef.nativeElement);
93+
this._focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement);
9494
}
9595

9696
// If were to attempt to focus immediately, then the content of the dialog would not yet be

src/lib/sidenav/sidenav.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export class MdSidenav implements AfterContentInit, OnDestroy {
172172
}
173173

174174
ngAfterContentInit() {
175-
this._focusTrap = this._focusTrapFactory.attach(this._elementRef.nativeElement);
175+
this._focusTrap = this._focusTrapFactory.create(this._elementRef.nativeElement);
176176
this._focusTrap.enabled = this.isFocusTrapEnabled;
177177

178178
// This can happen when the sidenav is set to opened in

0 commit comments

Comments
 (0)