Skip to content

Commit ccc067f

Browse files
committed
Merge pull request #20 from Panya/issue-12
Fix server-side rendering. Closes #12
2 parents 61c26f4 + 507173b commit ccc067f

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

lib/index.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,15 @@
102102
}
103103

104104
_createClass(TextTruncate, [{
105-
key: 'componentWillMount',
106-
value: function componentWillMount() {
105+
key: 'componentDidMount',
106+
value: function componentDidMount() {
107107
var canvas = document.createElement('canvas');
108108
var docFragment = document.createDocumentFragment();
109+
var style = window.getComputedStyle(this.refs.scope);
110+
var font = [style['font-weight'], style['font-style'], style['font-size'], style['font-family']].join(' ');
109111
docFragment.appendChild(canvas);
110112
this.canvas = canvas.getContext('2d');
111-
}
112-
}, {
113-
key: 'componentDidMount',
114-
value: function componentDidMount() {
115-
var style = window.getComputedStyle(this.refs.scope);
116-
var font = [];
117-
font.push(style['font-weight']);
118-
font.push(style['font-style']);
119-
font.push(style['font-size']);
120-
font.push(style['font-family']);
121-
this.canvas.font = font.join(' ');
113+
this.canvas.font = font;
122114
this.forceUpdate();
123115

124116
if (this.props.raf) {

src/TextTruncate.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@ export default class TextTruncate extends Component {
1818
raf: true
1919
};
2020

21-
componentWillMount() {
21+
componentDidMount() {
2222
let canvas = document.createElement('canvas');
2323
let docFragment = document.createDocumentFragment();
24+
let style = window.getComputedStyle(this.refs.scope);
25+
let font = [
26+
style['font-weight'],
27+
style['font-style'],
28+
style['font-size'],
29+
style['font-family']
30+
].join(' ');
2431
docFragment.appendChild(canvas);
2532
this.canvas = canvas.getContext('2d');
26-
}
27-
28-
componentDidMount() {
29-
let style = window.getComputedStyle(this.refs.scope);
30-
let font = [];
31-
font.push(style['font-weight']);
32-
font.push(style['font-style']);
33-
font.push(style['font-size']);
34-
font.push(style['font-family']);
35-
this.canvas.font = font.join(' ');
33+
this.canvas.font = font;
3634
this.forceUpdate();
3735

3836
if (this.props.raf) {

0 commit comments

Comments
 (0)