Skip to content

Commit 0174377

Browse files
crisbetotinayuangao
authored andcommitted
fix(tabs): server-side rendering error (#5348)
* Fixes an error that was being thrown by the tab header when using server-side rendering. * Fixes the `transform` potentially being set to an invalid value, if reading the scroll position fails (e.g. `translate3d(NaNpx, 0, 0)`).
1 parent 1b351cd commit 0174377

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/lib/tabs/tab-header.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
AfterContentInit,
2222
OnDestroy,
2323
NgZone,
24+
Renderer2,
2425
} from '@angular/core';
2526
import {
2627
RIGHT_ARROW,
@@ -136,6 +137,7 @@ export class MdTabHeader implements AfterContentChecked, AfterContentInit, OnDes
136137
constructor(
137138
private _elementRef: ElementRef,
138139
private _ngZone: NgZone,
140+
private _renderer: Renderer2,
139141
@Optional() private _dir: Directionality) { }
140142

141143
ngAfterContentChecked(): void {
@@ -299,12 +301,11 @@ export class MdTabHeader implements AfterContentChecked, AfterContentInit, OnDes
299301

300302
/** Performs the CSS transformation on the tab list that will cause the list to scroll. */
301303
_updateTabScrollPosition() {
302-
let translateX = this.scrollDistance + 'px';
303-
if (this._getLayoutDirection() == 'ltr') {
304-
translateX = '-' + translateX;
305-
}
304+
const scrollDistance = this.scrollDistance;
305+
const translateX = this._getLayoutDirection() === 'ltr' ? -scrollDistance : scrollDistance;
306306

307-
applyCssTransform(this._tabList.nativeElement, `translate3d(${translateX}, 0, 0)`);
307+
this._renderer.setStyle(this._tabList.nativeElement, 'transform',
308+
`translate3d(${translateX}px, 0, 0)`);
308309
}
309310

310311
/** Sets the distance in pixels that the tab header should be transformed in the X-axis. */
@@ -317,7 +318,7 @@ export class MdTabHeader implements AfterContentChecked, AfterContentInit, OnDes
317318

318319
this._checkScrollingControls();
319320
}
320-
get scrollDistance(): number { return this._scrollDistance; }
321+
get scrollDistance(): number { return this._scrollDistance; }
321322

322323
/**
323324
* Moves the tab list in the 'before' or 'after' direction (towards the beginning of the list or
@@ -413,7 +414,7 @@ export class MdTabHeader implements AfterContentChecked, AfterContentInit, OnDes
413414
_getMaxScrollDistance(): number {
414415
const lengthOfTabList = this._tabList.nativeElement.scrollWidth;
415416
const viewLength = this._tabListContainer.nativeElement.offsetWidth;
416-
return lengthOfTabList - viewLength;
417+
return (lengthOfTabList - viewLength) || 0;
417418
}
418419

419420
/** Tells the ink-bar to align itself to the current label wrapper */

src/universal-app/kitchen-sink/kitchen-sink.html

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,17 @@ <h2>Slider</h2>
183183
<md-slider tickInterval="1" min="1" max="10" value="5" thumbLabel></md-slider>
184184

185185
<h2>Tabs</h2>
186-
187-
<!-- Tabs don't work because `_updateTabScrollPosition` tries to set the transform on start-up -->
188-
<!--<md-tab-group>-->
189-
<!--<md-tab label="Overview">-->
190-
<!--The overview-->
191-
<!--</md-tab>-->
192-
<!--<md-tab>-->
193-
<!--<ng-template md-tab-label>-->
194-
<!--API docs-->
195-
<!--</ng-template>-->
196-
<!--The API docs-->
197-
<!--</md-tab>-->
198-
<!--</md-tab-group>-->
186+
<md-tab-group>
187+
<md-tab label="Overview">
188+
The overview
189+
</md-tab>
190+
<md-tab>
191+
<ng-template md-tab-label>
192+
API docs
193+
</ng-template>
194+
The API docs
195+
</md-tab>
196+
</md-tab-group>
199197

200198
<nav md-tab-nav-bar>
201199
<a md-tab-link href="https://google.com">Google</a>

0 commit comments

Comments
 (0)