-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
text is broken wrongly and randomly with webpack #16
Comments
I'm missing a bit of a context here to figure out where the problem might be. Could you provide some additional information about:
Assuming you are using the latest version of |
Hi Pablo, thanks for your quick reply, some context:
the getComputedStyle gives: Just to give more context, this is a big application, with lots of reducers, etc (so it has some busy miliseconds). The react-truncate component is the latests one and im using latest chrome browser, but it also happens in chromium and firefox. It also uses webpack and inline styles, not sure if that can affect Thank you for your help and work |
the style is .blabla{
} |
Thank you for the context :-) This seems to be either related to the font size or the When the component mounts it renders the full children, then figures out how much space it can use and then renders the truncated version. If you don't have a scroll bar when you mount the component but have a scroll bar once the component is done rendering, there would be some ~ Could you make a try with replacing all your overflows to Just to be sure - this is not on mobile, right? (There is a known issue) |
nope, desktop |
i replaced the overflow with hidden and is the same, just to give you another clue, when i open the inspector (and resizes) renders correctly, so i guess is related with the first mount/calculation |
That's some valuable information. Could you open target && console.log(target.parentNode.getBoundingClientRect().width); between those lines? (The code looks slightly different after having gone through babel - just find a line in the render method where To not clutter the console it would be helpful if you could only mount the affected Then let's see what numbers you get, they should not vary between renders. |
number doesnt change, logs 304 all the time, before and after resizing but is still rendered incorrect at the beggining and correct after resize |
Okay. Then I'm pretty sure it's about the font. Could it be that a custom font is not loaded yet at the first render? |
ill be back to you, diagnosing now, seems you are right... |
If this was the cause, it would be a pretty hard problem to solve within this module. I could do some arbitrary canvas checks before and after rendering and see if they give the same width. However, this would also not guarantee that it works 100% of the time, just reduce the probability of failure. My advise here is to put your stylesheet into the page head and block page rendering until it's done loading and make sure that your font file is served as fast as possible (CDN, cached). |
if I put If i dont and leave as I have (weird webpack setup with inline plugin and so on) i get first: and after resize (as I think it should be) So obviously font and probably webpack, and how is being handled fonts and styles by webpack can be the issue, but is weird why only in the third case is the output as expected, and not in the first case. |
If you don't need to be able to resize the container, you can add a css rule like that: .your-truncate-class > span {
white-space: nowrap;
} Of course the line breaks will still be calculated wrong, but at least the lines will have the same width and are aligned (and hopefully not break out of your design). Thank you for reporting anyway, I am sure other people might bump into that as well. Closing this issue since this is out of the scope of the component, unfortunatly. I might come back to this issue when I have a demo playground running for this repository. |
Thanks Pablo for your help, very much! |
Suggested fix: Use font-family: 'Custom Font', 'Courier New', sans-serif in order to prevent the weird line breaking when the custom font is loaded and increases in width, as Truncate works before the font loads. Courier New will need to be wider than the custom font. |
I wrote a (sorta) elegant solution to this that forces truncate to rerender itself upon font face load completion. import { lifecycle } from 'recompose';
import onFontFaceLoad from 'fontfaceonload';
// little HOC that force updates it's contents once a font face is loaded
export default (fontFace) =>
lifecycle({
componentDidMount () {
onFontFaceLoad(fontFace, {
success: () => {
setTimeout(() => this.forceUpdate(), 0);
}
});
},
}); Then, from the component that consumes the library: import withFontLoadRender from ...
const UpdatingTruncate = withFontLoadRender('YourFontFamily')(Truncate);
export default () => (
<UpdatingTruncate lines={2}>Text to truncate</UpdatingTruncate>
) (I have no idea why the setTimeout is needed, but it is) |
Hi. I solve this creating a import { useState, useEffect } from 'react';
import Truncate from 'react-truncate';
export default function ReadMore({ lines, children }) {
const [expanded, setExpanded] = useState(true);
const [truncated, setTruncated] = useState(false);
const [ready, setReady] = useState(false);
function handleTruncate(t) {
if (truncated !== t) setTruncated(t);
}
function toggleLines(e) {
e.preventDefault();
setExpanded(!expanded);
}
useEffect(() => {
if (ready) return;
document.fonts.ready.then(function () {
setReady(true);
setExpanded(false);
});
});
return (
<>
<Truncate
lines={!expanded && lines}
ellipsis={<span>... <button className="readMore" onClick={toggleLines}>Ver Más</button></span>} onTruncate={handleTruncate}>
{children}
</Truncate>
{!truncated && expanded && (
<span>... <button className="readMore" onClick={toggleLines}>Ver Menos</button></span>
)}
</>
)
} |
I want 4 lines for example and i get 5 with an extra one with the ellipsis, the div around is just to see if it was that but the issue remains
Lorem ipsum dolor sit amet,Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,
Lorem ipsum dolor sit amet,Lorem ipsum dolor sit amet,
consectetur adipiscing elit. Duis dui
…
something like that im getting, and is kind of random with the hot reload sometimes is like that, sometimes different, makes it not really reliable, I tried but will have to switch to another component.
The text was updated successfully, but these errors were encountered: