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

Text: implement numberOfLines > 1 #13

Closed
necolas opened this issue Sep 8, 2015 · 25 comments
Closed

Text: implement numberOfLines > 1 #13

necolas opened this issue Sep 8, 2015 · 25 comments
Milestone

Comments

@necolas
Copy link
Owner

necolas commented Sep 8, 2015

As far as I know, this is non trivial cross-browser but should be possible using document.createRange and getClientRects: https://developer.mozilla.org/en-US/docs/Web/API/Range

@basketofsoftkittens
Copy link

+1

@necolas necolas modified the milestone: React Native 0.22 Mar 13, 2016
@nbclark
Copy link

nbclark commented Aug 10, 2016

Webkit only, but:

overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;

http://hackingui.com/front-end/a-pure-css-solution-for-multiline-text-truncation/

@nbclark
Copy link

nbclark commented Aug 11, 2016

doubledutch@c817f39

Will submit a pull once I add a test+sample

@necolas necolas removed this from the React Native 0.22 milestone Dec 24, 2016
@necolas necolas added this to the 0.1.0 release milestone Apr 13, 2017
@necolas necolas removed this from the 0.1.0 release milestone Jun 13, 2017
@necolas
Copy link
Owner Author

necolas commented Jun 13, 2017

Unfortunately, -webkit-line-clamp has poor i18n support. It doesn't work when the layout is RTL, or when the language has no spaces, and it doesn't render a language-appropriate equivalent of .

Repository owner deleted a comment from VinceBT Jul 5, 2017
@lstrrs
Copy link

lstrrs commented Dec 28, 2017

I can try and take a shot at this but would like some comments in the approach I have in mind.

The gist of it is:

  • Use getBoundingClientRect to get a targetWidth
  • Use getComputedStyle to get the text styles
  • Use a Canvas and measureText method
  • Break the targeted text into lines with the specified "ellipsis" component/text

Any comments? Of course there are some performance considerations.

@steida
Copy link

steida commented Jun 7, 2018

I think we can use #795 and compute the current number of lines via height / computed lineHeight, then restrict it.

@tafelito
Copy link

will any of this solutions work with rnw? react-text-truncate or react-lines-ellipsis

@brunolemos
Copy link
Contributor

brunolemos commented Sep 1, 2018

-webkit-line-clamp has poor i18n support. It doesn't work when the layout is RTL, or when the language has no spaces

I just found this RTL demo and it seems it works better nowadays: https://codepen.io/nilsynils/pen/QKgJAY

Anyway, today's support seems to have improved.
Browser support is currently at 85% with the prefix.

I created #1090 just in case.

@necolas
Copy link
Owner Author

necolas commented Sep 1, 2018

That demo doesn't show ellipsis for LTR and clips the Arabic text vertically. Still looks broken to me against that criteria alone

@brunolemos
Copy link
Contributor

brunolemos commented Sep 1, 2018

Okay... I'll keep using a fork then because I don't need RTL support.

@drumnation
Copy link

drumnation commented Oct 9, 2018

Was able to get react-lines-ellipsis working using Platform.OS === 'web' - not the best solution, but a good work around.

@greenais
Copy link

@drumnation, thank you for tip - I can confirm it works.
Only thing to note - one should explicitly set width of parent, otherwise it replaces whole text with ellipsis only after first word

@steida
Copy link

steida commented Oct 17, 2018

As I remember using getClientRects, it returned lines reliable. But the code would have to be rendered in script tag immediately after DOM for SSR.

Repository owner deleted a comment from geminiyellow Oct 17, 2018
@tafelito
Copy link

@necolas Have you seen how ant-design's Input handle number of lines? Is that something that might work in rnw?

@necolas
Copy link
Owner Author

necolas commented Nov 12, 2018

@tafelito that's an input component, not a text component

@tafelito
Copy link

Sorry, this is the one I meant

https://pro.ant.design/components/Ellipsis/

@necolas
Copy link
Owner Author

necolas commented Nov 12, 2018

I don't think so. That looks like it will be expensive to perform on many text nodes, and all the innerHTML calls are probably an XSS vector

@necolas
Copy link
Owner Author

necolas commented Dec 21, 2018

After talking to a lot of people about this, including browser vendors, there is currently no good option. I'm going to include the webkit line-clamp implementation as a "kinda-works-for-a-common-case-only" stop-gap until browsers provide a standard solution.


cc @stubbornella

numberOfLines is a prop on React Native's Text component. It allows (rich) text to be truncated after a certain number of lines, with support for RTL languages and locale-specific equivalents to the ellipsis. This can't be practically implemented on the web. -webkit-line-clamp is a past attempt at doing this, but it has a lot of problems including limited browser support, requires opting into a old flexbox implementation (i.e., doesn't work with inline, inline-block, or inline-flex text), and has no RTL support. There are lots of bad JS hacks that attempt to do this, and its a popular request from web developers [1] [2].

Examples of where this would be used on web: text in every Card on Twitter and Facebook. Cards in Tweets, Cards to display Profile information. Often we only want to display a couple of lines of contextual description that is then truncated in an accessible and friendly fashion.

https://facebook.github.io/react-native/docs/text#numberoflines

@necolas necolas added this to the 0.10.0 milestone Dec 21, 2018
@stubbornella
Copy link

stubbornella commented Dec 21, 2018 via email

@necolas
Copy link
Owner Author

necolas commented Dec 21, 2018

Yes please, something like line-clamp with localization considerations built-in. Thank you :)

@stubbornella
Copy link

stubbornella commented Dec 21, 2018 via email

@necolas
Copy link
Owner Author

necolas commented Dec 21, 2018

It needs to support RTL languages and I believe it needs to provide language-specific symbols to indicate "truncation". Not every language can use ellipsis. But I don't know the extent of the complexities involved in localization or accessibility (should screen readers read the full text or the truncated text?), just that -webkit-line-clamp falls well short

@stubbornella
Copy link

stubbornella commented Dec 21, 2018 via email

@necolas
Copy link
Owner Author

necolas commented Dec 21, 2018

There are a couple of links in #13 (comment)

@necolas necolas modified the milestones: 0.11.0, 0.10.0 Dec 31, 2018
@necolas necolas modified the milestones: 0.10.0, 0.11.0 Jan 30, 2019
@necolas necolas modified the milestones: 0.11.0, 0.12.0 Mar 6, 2019
necolas added a commit that referenced this issue Mar 8, 2019
The CSS base styles for certain primitives are implemented using classic CSS to
reduce browser layout times and better support 'null' values in
StyleSheet-defined styles. Combined with the previous patch this reduces the
benchmark layout times by about 30%.

Ref #1136
Fix #1044
Fix #1223
Fix #13
@necolas necolas modified the milestones: 0.12.0, 0.11.0 Mar 9, 2019
@necolas necolas added the has-PR label Mar 9, 2019
necolas added a commit that referenced this issue Mar 11, 2019
The CSS base styles for certain primitives are implemented using classic CSS to
reduce browser layout times and better support 'null' values in
StyleSheet-defined styles. Combined with the previous patch this reduces the
benchmark layout times by about 30%.

Ref #1136
Fix #1044
Fix #1223
Fix #13
@kopax-polyconseil
Copy link

How do you use numberOfLines in Web as it still didn't work in rnw for us. Thanks!

Repository owner locked and limited conversation to collaborators Sep 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests