Comments (11)
The test are working in master and v2.0.3
Here a gif with 2.0.4:
2.0.3:
The sample text has no styles applied.
Code:
<Truncate lines={1}>
lkajsdöfasd aöskldjföalkjdsfölakjdöfiaüoeiasdfölajsdlfjalekjrbalsdjbflasjbdfabsdaö
</Truncate>
from react-truncate.
You are right, that commit 324d1bf is indeed faulty - which I'm going to revert on master.
The canvas context.font
API does not expect a letter spacing / word spacing argument.
Looks like this might be a viable workaround.
from react-truncate.
Will take a look!
from react-truncate.
Could you provide me with information about
- which font-related CSS rules are applied to your truncated text
- does
npm test
succeed locally for both revisions - screenshots
So I can help figuring out where the problem might be
from react-truncate.
Thx for your fast response.
from react-truncate.
@pablosichert did this workaround ever get added?
http://stackoverflow.com/a/40469992
from react-truncate.
I just tested some code:
const canvas = document.createElement('canvas');
// document.body.append(canvas);
canvas.style.letterSpacing = '1px';
const context = canvas.getContext('2d');
context.measureText('foo');
It only affects the measuring in Chrome and only if you uncomment the second line, which means that it only works if the canvas is mounted to the DOM.
I wouldn't like to add code that is so dependent on the browser.
from react-truncate.
A better solution is to manually calculate letter/word spacing in measureWidth
.
const targetStyle = window.getComputedStyle(target);
const letterSpacing = parseSpacing(targetStyle['letter-spacing']);
const wordSpacing = parseSpacing(targetStyle['word-spacing']);
const context = this.getCanvasContext(targetStyle);
const widthWithoutSpacing = context.measureText(text).width;
const widthWithSpacing = widthWithoutSpacing + (
letterSpacing * (text.length - 1) +
wordSpacing * (text.trim().split(/\s+/).length)
);
It works in all browsers, not just Chrome.
from react-truncate.
The previously mentioned solution would also work – as in not break. But the behavior would diverge depending on the browser.
I suppose you are suggesting to introduce a parseSpacing
method that falls back to 0
?
I think I'd rather use a partially-supported implementation than doing it manually, unless that would mean supporting a wider selection of browsers.
from react-truncate.
But the behavior would diverge depending on the browser.
In theory, every browser should calculate letter/word spacing the same way.
Manually calculating letter/word spacing, rather than relying on the browser to calculate it via canvasContext.measureText
, works in every browser.
from react-truncate.
I see your point, could be worth a shot.
Would you like to open a PR for that? I'd assist you in updating the test
from react-truncate.
Related Issues (20)
- <br> tags being added to truncated lines HOT 8
- 3rd party device recognition is blocking usage
- Maintenance status? HOT 2
- ellipsis still display when text isn't too long HOT 1
- ComponentWillUnmount ellipsis removal fails in test suites and other situations HOT 5
- Cannot set property 'font' of undefined HOT 3
- In jest tests - the ellipsis always show, regardless of whether it truncates. HOT 1
- Ellipsis not shown when text is only one word
- Truncate get error on 'width' prop when my app still work in another tab like keep-alive component
- Wrong lines calculation when width specified and having responsive container HOT 3
- React v17 support HOT 2
- Last line length changes when parent component set to display none
- Jest: Can not test onTruncate method HOT 3
- Update to React 17 HOT 5
- deleted
- wrong width of span on load HOT 2
- New release HOT 1
- React 18 support HOT 1
- Can't add as dependency in React 18 HOT 10
- Wrong line breaks
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-truncate.