I need a text truncation tool, that will insert ellipsis in the center of the string to…
- User
I need a text truncation tool, that will insert ellipsis in the center of the string to make it shortened (we still show the start and the end of the string) if the width of it surpasses parent container. Also on hover we need to show a tooltip that contains the full string. This has to be dynamic (I don't know parent width and it should response to screen resize)
- Assistant
- User
I need this for React, no jQuery
- Assistant
- User
could you show the css file please?
- Assistant
Certainly! Here's an example of the CSS file for the
TruncatedTextcomponent:.parentContainer { max-width: 100%; overflow: hidden; position: relative; } .textContainer { display: flex; overflow: hidden; text-overflow: ellipsis; transition: width 0.5s; } .textContainer span { flex-shrink: 1; flex-grow: 1; } .tooltip { display: none; white-space: normal; position: absolute; top: 100%; left: 0; background: #fff; padding: 5px; border: 1px solid #ccc; z-index: 999; } .parentContainer:hover .tooltip { display: block; }This CSS file defines the styles for the
TruncatedTextcomponent. You can either create a separate CSS file (e.g.,TruncatedText.css) and import it into theTruncatedText.jsfile, or you can include these styles in your existing CSS file.Make sure to adjust the class names in the CSS file to match the corresponding class names used in the component.
- User
I think our truncation mechanism just adds 3 dots inside the actual string. But we need to truncate it to make it fit to the container & also add 3 dots inside it. (it should fit the container including the dots)
- Assistant
- User
still does not work nice. and when we resize it, text completely turns into dots
- Assistant
Apologies for the inconvenience. Let's make the necessary modifications to ensure the truncation works as expected and preserves the text when resizing. Please update the code as follows: