Skip to content

Commit 6bc6dc0

Browse files
committed
addressed comments
1 parent e3c96e4 commit 6bc6dc0

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

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

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
Component,
3-
ViewEncapsulation,
4-
ViewChild,
5-
ElementRef,
6-
Input,
7-
AfterContentInit,
8-
NgZone
9-
} from '@angular/core';
1+
import {Component, ViewEncapsulation, ViewChild, ElementRef, Input, NgZone} from '@angular/core';
102
import {InteractivityChecker} from './interactivity-checker';
113
import {coerceBooleanProperty} from '../coersion/boolean-property';
124

@@ -25,29 +17,17 @@ import {coerceBooleanProperty} from '../coersion/boolean-property';
2517
templateUrl: 'focus-trap.html',
2618
encapsulation: ViewEncapsulation.None,
2719
})
28-
export class FocusTrap implements AfterContentInit {
20+
export class FocusTrap {
2921
@ViewChild('trappedContent') trappedContent: ElementRef;
3022

31-
@Input()
32-
get active(): boolean { return this._active; }
33-
set active(val: boolean) { this._active = coerceBooleanProperty(val); }
34-
35-
/** Whether the DOM content is ready. */
36-
private _contentReady: boolean = false;
37-
3823
/** Whether the focus trap is active. */
39-
private _active: boolean = true;
24+
@Input()
25+
get disabled(): boolean { return this._disabled; }
26+
set disabled(val: boolean) { this._disabled = coerceBooleanProperty(val); }
27+
private _disabled: boolean = false;
4028

4129
constructor(private _checker: InteractivityChecker, private _ngZone: NgZone) { }
4230

43-
ngAfterContentInit() {
44-
this._contentReady = true;
45-
// Trigger setter behavior.
46-
if (this.active) {
47-
48-
}
49-
}
50-
5131
/**
5232
* Waits for microtask queue to empty, then focuses the first tabbable element within the focus
5333
* trap region.

src/lib/sidenav/sidenav.spec.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,49 +256,49 @@ describe('MdSidenav', () => {
256256
let fixture: ComponentFixture<SidenavWitFocusableElements>;
257257
let testComponent: SidenavWitFocusableElements;
258258
let sidenav: MdSidenav;
259-
let link1Element: HTMLElement;
260-
let link2Element: HTMLElement;
259+
let firstFocusableElement: HTMLElement;
260+
let lastFocusableElement: HTMLElement;
261261

262262
beforeEach(() => {
263263
fixture = TestBed.createComponent(SidenavWitFocusableElements);
264264
testComponent = fixture.debugElement.componentInstance;
265265
sidenav = fixture.debugElement.query(By.directive(MdSidenav)).componentInstance;
266-
link1Element = fixture.debugElement.query(By.css('.link1')).nativeElement;
267-
link2Element = fixture.debugElement.query(By.css('.link1')).nativeElement;
268-
link2Element.focus();
266+
firstFocusableElement = fixture.debugElement.query(By.css('.link1')).nativeElement;
267+
lastFocusableElement = fixture.debugElement.query(By.css('.link1')).nativeElement;
268+
lastFocusableElement.focus();
269269
});
270270

271-
it('should trp focus when opened in "over" mode', fakeAsync(() => {
271+
it('should trap focus when opened in "over" mode', fakeAsync(() => {
272272
testComponent.mode = 'over';
273-
link2Element.focus();
273+
lastFocusableElement.focus();
274274

275275
sidenav.open();
276276
endSidenavTransition(fixture);
277277
tick();
278278

279-
expect(document.activeElement).toBe(link1Element);
279+
expect(document.activeElement).toBe(firstFocusableElement);
280280
}));
281281

282-
it('should trap tabs when opened in "push" mode', fakeAsync(() => {
282+
it('should trap focus when opened in "push" mode', fakeAsync(() => {
283283
testComponent.mode = 'push';
284-
link2Element.focus();
284+
lastFocusableElement.focus();
285285

286286
sidenav.open();
287287
endSidenavTransition(fixture);
288288
tick();
289289

290-
expect(document.activeElement).toBe(link1Element);
290+
expect(document.activeElement).toBe(firstFocusableElement);
291291
}));
292292

293-
it('should not trap tabs when opened in "side" mode', fakeAsync(() => {
293+
it('should not trap focus when opened in "side" mode', fakeAsync(() => {
294294
testComponent.mode = 'side';
295-
link2Element.focus();
295+
lastFocusableElement.focus();
296296

297297
sidenav.open();
298298
endSidenavTransition(fixture);
299299
tick();
300300

301-
expect(document.activeElement).toBe(link2Element);
301+
expect(document.activeElement).toBe(lastFocusableElement);
302302
}));
303303
});
304304
});

src/lib/sidenav/sidenav.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class MdDuplicatedSidenavError extends MdError {
3838
@Component({
3939
moduleId: module.id,
4040
selector: 'md-sidenav',
41-
template: '<focus-trap [active]="focusTrapActive"><ng-content></ng-content></focus-trap>',
41+
template: '<focus-trap [disabled]="isFocusTrapDisabled"><ng-content></ng-content></focus-trap>',
4242
host: {
4343
'(transitionend)': '_onTransitionEnd($event)',
4444
// must prevent the browser from aligning text based on value
@@ -111,8 +111,9 @@ export class MdSidenav implements AfterContentInit {
111111
/** Event emitted when the sidenav alignment changes. */
112112
@Output('align-changed') onAlignChanged = new EventEmitter<void>();
113113

114-
get focusTrapActive() {
115-
return this.opened && this.mode != 'side';
114+
get isFocusTrapDisabled() {
115+
// The focus trap is only enabled when the sidenav is open in any mode other than side.
116+
return !this.opened || this.mode == 'side';
116117
}
117118

118119
/**
@@ -181,7 +182,7 @@ export class MdSidenav implements AfterContentInit {
181182
this.onCloseStart.emit();
182183
}
183184

184-
if (this.focusTrapActive) {
185+
if (!this.isFocusTrapDisabled) {
185186
this._focusTrap.focusFirstTabbableElementWhenReady();
186187
}
187188

0 commit comments

Comments
 (0)