Skip to content

Commit 448db8b

Browse files
crisbetommalerba
authored andcommitted
fix(tabs): server-side rendering error when aligning ink bar (#5455)
* Fixes a server-side rendering error in the ink bar due to a lack of `requestAnimationFrame`. The error is being logged, but it didn't fail the build, because it happens in a lifecycle hook. * Enables the checkbox and button toggle cases in the kitchen sink since it looks like they're not failing anymore.
1 parent 8de3b98 commit 448db8b

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

src/lib/tabs/ink-bar.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ export class MdInkBar {
3333
alignToElement(element: HTMLElement) {
3434
this.show();
3535

36-
this._ngZone.runOutsideAngular(() => {
37-
requestAnimationFrame(() => {
38-
this._renderer.setStyle(this._elementRef.nativeElement, 'left',
39-
this._getLeftPosition(element));
40-
this._renderer.setStyle(this._elementRef.nativeElement, 'width',
41-
this._getElementWidth(element));
36+
if (typeof requestAnimationFrame !== 'undefined') {
37+
this._ngZone.runOutsideAngular(() => {
38+
requestAnimationFrame(() => this._setStyles(element));
4239
});
43-
});
40+
} else {
41+
this._setStyles(element);
42+
}
4443
}
4544

4645
/** Shows the ink bar. */
@@ -54,18 +53,14 @@ export class MdInkBar {
5453
}
5554

5655
/**
57-
* Generates the pixel distance from the left based on the provided element in string format.
56+
* Sets the proper styles to the ink bar element.
5857
* @param element
5958
*/
60-
private _getLeftPosition(element: HTMLElement): string {
61-
return element ? element.offsetLeft + 'px' : '0';
62-
}
59+
private _setStyles(element: HTMLElement) {
60+
const left = element ? (element.offsetLeft || 0) + 'px' : '0';
61+
const width = element ? (element.offsetWidth || 0) + 'px' : '0';
6362

64-
/**
65-
* Generates the pixel width from the provided element in string format.
66-
* @param element
67-
*/
68-
private _getElementWidth(element: HTMLElement): string {
69-
return element ? element.offsetWidth + 'px' : '0';
63+
this._renderer.setStyle(this._elementRef.nativeElement, 'left', left);
64+
this._renderer.setStyle(this._elementRef.nativeElement, 'width', width);
7065
}
7166
}

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,21 @@ <h2>Button</h2>
2121
<a md-fab href="https://google.com">Google</a>
2222
<a md-mini-fab href="https://google.com">Google</a>
2323

24-
<!-- Button toggle doesn't work due to https://github.com/angular/angular/issues/17050 -->
25-
<!--<h2>Button toggle</h2>-->
26-
27-
<!--<h3>Single selection</h3>-->
28-
<!--<md-button-toggle-group>-->
29-
<!--<md-button-toggle>Ketchup</md-button-toggle>-->
30-
<!--<md-button-toggle>Mustard</md-button-toggle>-->
31-
<!--<md-button-toggle>Mayo</md-button-toggle>-->
32-
<!--</md-button-toggle-group>-->
33-
34-
<!--<h3>Multi selection</h3>-->
35-
<!--<md-button-toggle-group multiple>-->
36-
<!--<md-button-toggle>Ketchup</md-button-toggle>-->
37-
<!--<md-button-toggle>Mustard</md-button-toggle>-->
38-
<!--<md-button-toggle>Mayo</md-button-toggle>-->
39-
<!--</md-button-toggle-group>-->
24+
<h2>Button toggle</h2>
25+
26+
<h3>Single selection</h3>
27+
<md-button-toggle-group>
28+
<md-button-toggle>Ketchup</md-button-toggle>
29+
<md-button-toggle>Mustard</md-button-toggle>
30+
<md-button-toggle>Mayo</md-button-toggle>
31+
</md-button-toggle-group>
32+
33+
<h3>Multi selection</h3>
34+
<md-button-toggle-group multiple>
35+
<md-button-toggle>Ketchup</md-button-toggle>
36+
<md-button-toggle>Mustard</md-button-toggle>
37+
<md-button-toggle>Mayo</md-button-toggle>
38+
</md-button-toggle-group>
4039

4140
<h3>Standalone</h3>
4241
<md-button-toggle>Hyperspinner enabled</md-button-toggle>
@@ -63,11 +62,10 @@ <h3>Card</h3>
6362
</md-card-footer>
6463
</md-card>
6564

66-
<!-- Checkbox doesn't work due to https://github.com/angular/angular/issues/17050 -->
67-
<!--<h2>Checkbox</h2>-->
65+
<h2>Checkbox</h2>
6866

69-
<!--<md-checkbox>With a label</md-checkbox>-->
70-
<!--<md-checkbox></md-checkbox>Without a label-->
67+
<md-checkbox>With a label</md-checkbox>
68+
<md-checkbox></md-checkbox>Without a label
7169

7270
<h2>Chips</h2>
7371

0 commit comments

Comments
 (0)