Skip to content

Commit e61530e

Browse files
committed
address comments
1 parent 40cb457 commit e61530e

File tree

3 files changed

+97
-5
lines changed

3 files changed

+97
-5
lines changed

src/lib/sidenav/drawer.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ export class MdDrawerToggleResult {
6565
encapsulation: ViewEncapsulation.None,
6666
})
6767
export class MdDrawerContent implements AfterContentInit {
68-
/** Margins to be applied to the content. */
68+
/**
69+
* Margins to be applied to the content. These are used to push / shrink the drawer content when a
70+
* drawer is open. We use margin rather than transform even for push mode because transform breaks
71+
* fixed position elements inside of the transformed element.
72+
*/
6973
_margins: {left: number, right: number} = {left: 0, right: 0};
7074

7175
constructor(
@@ -74,7 +78,7 @@ export class MdDrawerContent implements AfterContentInit {
7478
}
7579

7680
ngAfterContentInit() {
77-
this._container._contentMargins.subscribe((margins) => {
81+
this._container._contentMargins.subscribe(margins => {
7882
this._margins = margins;
7983
this._changeDetectorRef.markForCheck();
8084
});
@@ -196,6 +200,10 @@ export class MdDrawer implements AfterContentInit, OnDestroy {
196200
/** @deprecated */
197201
@Output('align-changed') onAlignChanged = new EventEmitter<void>();
198202

203+
/**
204+
* An observable that emits when the drawer mode changes. This is used by the drawer container to
205+
* to know when to when the mode changes so it can adapt the margins on the content.
206+
*/
199207
_modeChanged = new Subject();
200208

201209
get isFocusTrapEnabled() {
@@ -537,6 +545,13 @@ export class MdDrawerContainer implements AfterContentInit, OnDestroy {
537545
* sparingly, because it causes a reflow.
538546
*/
539547
private _updateContentMargins() {
548+
// 1. For drawers in `over` mode, they don't affect the content.
549+
// 2. For drawers in `side` mode they should shrink the content. We do this by adding to the
550+
// left margin (for left drawer) or right margin (for right the drawer).
551+
// 3. For drawers in `push` mode the should shift the content without resizing it. We do this by
552+
// adding to the left or right margin and simultaneously subtracting the same amount of
553+
// margin from the other side.
554+
540555
let left = 0;
541556
let right = 0;
542557

src/lib/sidenav/sidenav.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {Component} from '@angular/core';
2+
import {async, TestBed, ComponentFixture} from '@angular/core/testing';
3+
import {MdSidenav, MdSidenavModule} from './index';
4+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
5+
import {By} from '@angular/platform-browser';
6+
7+
8+
describe('MdSidenav', () => {
9+
let fixture: ComponentFixture<SidenavWithFixedPosition>;
10+
let sidenavEl: HTMLElement;
11+
12+
beforeEach(async(() => {
13+
TestBed.configureTestingModule({
14+
imports: [MdSidenavModule, NoopAnimationsModule],
15+
declarations: [SidenavWithFixedPosition],
16+
});
17+
18+
TestBed.compileComponents();
19+
20+
fixture = TestBed.createComponent(SidenavWithFixedPosition);
21+
fixture.detectChanges();
22+
23+
sidenavEl = fixture.debugElement.query(By.directive(MdSidenav)).nativeElement;
24+
}));
25+
26+
it('should be fixed position when in fixed mode', () => {
27+
expect(sidenavEl.classList).toContain('mat-sidenav-fixed');
28+
29+
fixture.componentInstance.fixed = false;
30+
fixture.detectChanges();
31+
32+
expect(sidenavEl.classList).not.toContain('mat-sidenav-fixed');
33+
});
34+
35+
it('should set fixed bottom and top when in fixed mode', () => {
36+
expect(sidenavEl.style.top).toBe('20px');
37+
expect(sidenavEl.style.bottom).toBe('30px');
38+
39+
fixture.componentInstance.fixed = false;
40+
fixture.detectChanges();
41+
42+
expect(sidenavEl.style.top).toBeFalsy();
43+
expect(sidenavEl.style.bottom).toBeFalsy();
44+
});
45+
});
46+
47+
48+
@Component({
49+
template: `
50+
<md-sidenav-container>
51+
<md-sidenav
52+
#drawer
53+
[fixedInViewport]="fixed"
54+
[fixedTopGap]="fixedTop"
55+
[fixedBottomGap]="fixedBottom">
56+
Drawer.
57+
</md-sidenav>
58+
<md-sidenav-content>
59+
Some content.
60+
</md-sidenav-content>
61+
</md-sidenav-container>`,
62+
})
63+
class SidenavWithFixedPosition {
64+
fixed = true;
65+
fixedTop = 20;
66+
fixedBottom = 30;
67+
}

src/lib/sidenav/sidenav.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '@angular/core';
1515
import {MdDrawer, MdDrawerContainer, MdDrawerContent} from './drawer';
1616
import {animate, state, style, transition, trigger} from '@angular/animations';
17+
import {coerceBooleanProperty, coerceNumberProperty} from '@angular/cdk/coercion';
1718

1819

1920
@Component({
@@ -77,19 +78,28 @@ export class MdSidenavContent extends MdDrawerContent {
7778
})
7879
export class MdSidenav extends MdDrawer {
7980
/** Whether the sidenav is fixed in the viewport. */
80-
@Input() fixedInViewport = true;
81+
@Input()
82+
get fixedInViewport() { return this._fixedInViewport; }
83+
set fixedInViewport(value) { this._fixedInViewport = coerceBooleanProperty(value); }
84+
private _fixedInViewport = true;
8185

8286
/**
8387
* The gap between the top of the sidenav and the top of the viewport when the sidenav is in fixed
8488
* mode.
8589
*/
86-
@Input() fixedTopGap = 0;
90+
@Input()
91+
get fixedTopGap() { return this._fixedTopGap; }
92+
set fixedTopGap(value) { this._fixedTopGap = coerceNumberProperty(value); }
93+
private _fixedTopGap = 0;
8794

8895
/**
8996
* The gap between the bottom of the sidenav and the bottom of the viewport when the sidenav is in
9097
* fixed mode.
9198
*/
92-
@Input() fixedBottomGap = 0;
99+
@Input()
100+
get fixedBottomGap() { return this._fixedBottomGap; }
101+
set fixedBottomGap(value) { this._fixedBottomGap = coerceNumberProperty(value); }
102+
private _fixedBottomGap = 0;
93103
}
94104

95105

0 commit comments

Comments
 (0)