Code Monkey home page Code Monkey logo

Comments (11)

namjul avatar namjul commented on July 17, 2024 1

The test are working in master and v2.0.3
Here a gif with 2.0.4:
output
2.0.3:
output2
The sample text has no styles applied.
Code:

<Truncate lines={1}>
  lkajsdöfasd aöskldjföalkjdsfölakjdöfiaüoeiasdfölajsdlfjalekjrbalsdjbflasjbdfabsdaö
</Truncate>

from react-truncate.

pablosichert avatar pablosichert commented on July 17, 2024 1

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.

arasmussen avatar arasmussen commented on July 17, 2024 1

Will take a look!

from react-truncate.

pablosichert avatar pablosichert commented on July 17, 2024

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.

namjul avatar namjul commented on July 17, 2024

Thx for your fast response.

from react-truncate.

thomasbaldwin avatar thomasbaldwin commented on July 17, 2024

@pablosichert did this workaround ever get added?
http://stackoverflow.com/a/40469992

from react-truncate.

pablosichert avatar pablosichert commented on July 17, 2024

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.

arasmussen avatar arasmussen commented on July 17, 2024

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.

pablosichert avatar pablosichert commented on July 17, 2024

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.

arasmussen avatar arasmussen commented on July 17, 2024

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.

pablosichert avatar pablosichert commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.