Skip to content

Commit

Permalink
Fixed a bug caused by attempting to use window.getComputedStyle() on an
Browse files Browse the repository at this point in the history
`undefined` property.

Worked around min-height ruining my groove.
  • Loading branch information
turibe committed May 24, 2018
1 parent 435e8a5 commit f884948
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export default class LineClamp {
* maxLines.
*/
constructor (elm, maxLines = 1, useSoftClamp = true, strict = false) {
this._element = elm;

const style = this._getStyle(),
lineHeight = parseInt(style.lineHeight, 10);

this._element = elm;
this._originalWords = elm.textContent.split(/\s+/);
this._maxHeight = lineHeight * this.maxLines;
this._originalFontSize = parseInt(style.fontSize, 10);
Expand Down Expand Up @@ -108,6 +109,7 @@ export default class LineClamp {
hardClamp () {
// const style = this.getStyle();
// const padding = parseFloat(style.paddingTop) + parseFloat(style.paddingBottom);
this._element.style.minHeight = 0;
[this._element.textContent] = this._originalWords;

for (let i = 1, len = this._originalWords.length; i < len; ++i) {
Expand All @@ -118,6 +120,8 @@ export default class LineClamp {
break;
}
}

this._element.style.removeProperty('min-height');
}

/**
Expand All @@ -128,12 +132,14 @@ export default class LineClamp {
*/
softClamp () {
this._element.style.fontSize = '';
this._element.style.minHeight = '0';

for (let i = this._originalFontSize; i >= this.minFontSize; --i) {
if (this._shouldClamp()) {
this._element.style.fontSize = `${i}px`;
}
else {
this._element.style.removeProperty('min-height');
return;
}
}
Expand Down

0 comments on commit f884948

Please sign in to comment.