Skip to content

Commit

Permalink
Issue ShinyChang#69 | Don't truncate if text fits into an element
Browse files Browse the repository at this point in the history
This happens if the truncate-component if wrapped by a flex element,
and that flex element only takes minimal required space to fix its
content.

The issue here is that `measureText` methods uses rounding, while
width of the truncate component's ref is calculated without rounding.

Removing rounding and applying good-enough floats comparison
fixes the issue.
  • Loading branch information
Pavel Savchuk committed Feb 21, 2022
1 parent 250b498 commit 8527eb2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var config = require('./webpack.config.dev');

var app = express();
var compiler = webpack(config);
var PORT = 3000

app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
Expand All @@ -17,11 +18,11 @@ app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(3000, 'localhost', function(err) {
app.listen(PORT, 'localhost', function(err) {
if (err) {
console.log(err);
return;
}

console.log('Listening at http://localhost:3000');
console.log(`Listening at http://localhost:${PORT}`);
});
15 changes: 15 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ export class App extends Component {
/>
</div>
</div>
<div id="sample-13">
<h5>
13. Issue{" "}
<a href="https://github.com/ShinyChang/React-Text-Truncate/issues/69">
#69
</a>
</h5>
<div className="container">
<TextTruncate
line={1}
truncateText="...trancated"
text={"V3"}
/>
</div>
</div>
</div>
</div>
);
Expand Down
20 changes: 12 additions & 8 deletions src/TextTruncate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React, {Component, createElement} from 'react';
import PropTypes from 'prop-types';

const PRECISION = 0.0001;
const isEqual = (n1, n2) => Math.abs(n1 - n2) < PRECISION;

export default class TextTruncate extends Component {
static propTypes = {
containerClassName: PropTypes.string,
Expand Down Expand Up @@ -54,7 +57,7 @@ export default class TextTruncate extends Component {
if (this.rafId) {
window.cancelAnimationFrame(this.rafId);
}
this.rafId = window.requestAnimationFrame(this.update.bind(this))
this.rafId = window.requestAnimationFrame(this.update.bind(this));
};

onToggled = (truncated) => {
Expand Down Expand Up @@ -83,7 +86,7 @@ export default class TextTruncate extends Component {
};

measureWidth(text) {
return Math.ceil(this.canvas.measureText(text).width);
return this.canvas.measureText(text).width;
}

getRenderText() {
Expand All @@ -109,8 +112,9 @@ export default class TextTruncate extends Component {
return null;
}

const fullTextWidth = this.measureWidth(text);
// return if all of text can be displayed
if (scopeWidth >= this.measureWidth(text)) {
if (scopeWidth > fullTextWidth || isEqual(scopeWidth, fullTextWidth)) {
this.onToggled(false);
return createElement(textElement, props, text);
}
Expand All @@ -136,7 +140,7 @@ export default class TextTruncate extends Component {
let lastSpaceIndex = -1;
let ext = '';
let loopCnt = 0;

while (displayLine-- > 0) {
ext = displayLine ? '' : truncateText + (childText ? (' ' + childText) : '');
while (currentPos <= maxTextLength) {
Expand Down Expand Up @@ -172,15 +176,15 @@ export default class TextTruncate extends Component {
}
truncatedText = text.substr(startPos, currentPos);
} else {
currentPos--;
currentPos--;
truncatedText = text.substr(startPos, currentPos);
}
} else {
currentPos--;
truncatedText = text.substr(startPos, currentPos);
}
width = this.measureWidth(truncatedText + ext);
} while (width >= scopeWidth && truncatedText.length > 0);
} while ((width > scopeWidth || isEqual(width, scopeWidth)) && truncatedText.length > 0);
startPos += currentPos;
break;
}
Expand All @@ -202,7 +206,7 @@ export default class TextTruncate extends Component {
this.onToggled(false);
return createElement(textElement, props, text);
}

this.onTruncated();
this.onToggled(true);
return (
Expand Down Expand Up @@ -232,7 +236,7 @@ export default class TextTruncate extends Component {

const { fontWeight, fontStyle, fontSize, fontFamily } = style;

const renderText = this.scope && line ? this.getRenderText() : createElement(textElement, props, text);;
const renderText = this.scope && line ? this.getRenderText() : createElement(textElement, props, text);
const rootProps = {
ref: (el) => {this.scope = el},
className: containerClassName,
Expand Down

0 comments on commit 8527eb2

Please sign in to comment.