Skip to content
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

Newline handling #28

Closed
obecker opened this issue Mar 24, 2024 · 1 comment · Fixed by #29
Closed

Newline handling #28

obecker opened this issue Mar 24, 2024 · 1 comment · Fixed by #29

Comments

@obecker
Copy link
Contributor

obecker commented Mar 24, 2024

If the text to be clamped contains line breaks, then they will be preserved in the created element (by adding <br> elements). This should not happen. The display of line breaks should only be controlled by the CSS white-space property.

Solution: the text should be inserted by using textContent instead of innerText.
See https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent

Moreover, if line breaks are preserved and the last displayed line is short enough for the containing element, it is still shortened before adding the ellipsis.
Example:

This is some example text
with multiple
short lines
to demonstrate
the behaviour.

Using <NanoClamp text={text} lines={2} /> for this text (in a wide enough div) results in

This is some example text
with mult...

I would expect that the word multiple is not shortened (since it's not necessary), so the result should rather be

This is some example text
with multiple...

Solution: in the loop that finds the proper index in the text always append the ellipsis before checking the height of the element.

@obecker
Copy link
Contributor Author

obecker commented Mar 24, 2024

See #29

obecker added a commit to obecker/nanoclamp that referenced this issue Mar 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant