All Projects → matt-d-rat → react-middle-truncate

matt-d-rat / react-middle-truncate

Licence: MIT License
A React component for intelligently truncating text in the middle of the string.

Programming Languages

javascript
184084 projects - #8 most used programming language
CSS
56736 projects
HTML
75241 projects

Projects that are alternatives of or similar to react-middle-truncate

strings-truncation
Truncate strings with fullwidth characters and ANSI codes.
Stars: ✭ 45 (+66.67%)
Mutual labels:  text, truncate, truncation
ngx-line-truncation
An Angular line truncating solution. Truncate text block by given line number and add ellipsis to the end.
Stars: ✭ 13 (-51.85%)
Mutual labels:  truncate, truncation
markdown-utils
Convert plain text into snippets of markdown.
Stars: ✭ 28 (+3.7%)
Mutual labels:  text
RichText
Easily show RichText(html) in SwiftUI
Stars: ✭ 25 (-7.41%)
Mutual labels:  text
glfont
A modern opengl text rending library for Golang
Stars: ✭ 27 (+0%)
Mutual labels:  text
ss-search
The most basic, yet powerful text search.
Stars: ✭ 41 (+51.85%)
Mutual labels:  text
react-text-translator
An experimental way to translate text inside React components with context
Stars: ✭ 15 (-44.44%)
Mutual labels:  text
pytextcodifier
📦 Turn your text files into codified images or your codified images into text files.
Stars: ✭ 14 (-48.15%)
Mutual labels:  text
pygame-text-input
a small module that enables you to input text with your keyboard using pygame
Stars: ✭ 114 (+322.22%)
Mutual labels:  text
craft-text-detector
Packaged, Pytorch-based, easy to use, cross-platform version of the CRAFT text detector
Stars: ✭ 151 (+459.26%)
Mutual labels:  text
any-text
Get text content from any file
Stars: ✭ 19 (-29.63%)
Mutual labels:  text
text2class
Multi-class text categorization using state-of-the-art pre-trained contextualized language models, e.g. BERT
Stars: ✭ 15 (-44.44%)
Mutual labels:  text
split
A string split function and iterator for Lua
Stars: ✭ 15 (-44.44%)
Mutual labels:  text
StGitlab
Sublime text Gitlab manager
Stars: ✭ 13 (-51.85%)
Mutual labels:  text
awesome-web-styling
Awesome Web Styling with CSS Animation Effects ⭐️
Stars: ✭ 109 (+303.7%)
Mutual labels:  text
replace-in-files
Replace text in one or more files or globs.
Stars: ✭ 21 (-22.22%)
Mutual labels:  text
TextInputLayout
The objective of this code is to guide you to create login screen with TextInputLayout in iOS app.
Stars: ✭ 30 (+11.11%)
Mutual labels:  text
react-text-more-less
📃React component to show more text or show less
Stars: ✭ 16 (-40.74%)
Mutual labels:  truncate
vue-resize-text
A vue directive which automatically resize font size based on element width.
Stars: ✭ 65 (+140.74%)
Mutual labels:  text
ELMO-NLP
ELMO在QA问答,文本分类等NLP上面的应用
Stars: ✭ 15 (-44.44%)
Mutual labels:  text

react-middle-truncate

github_forks_img github_stars_img

A React component for intelligently truncating text in the middle of the string with an ellipsis. You can see the component in action in this demo.

Installation

npm install react-middle-truncate --save

Usage

The truncation point is determined by taking into account the inherited font CSS properties of the element, and rendering the result to Canvas in order to dynamically calculate the width of the text. Should the width of the text not fit into the available space to render, truncation is applied. The result of which looks like this:

import MiddleTruncate from 'react-middle-truncate';

<MiddleTruncate
  text="Hello world this is a really long string"
  start={/Hello\sworld/}
  end={6} />

Result of Middle Truncation using the code sample above

You'll note from the code sample above we can use the start and end props to determine how much of the start and end of the string to preserve. These props can either be numeric, which indicates how many characters of the string from that direction to preserve, or you can provide a Regular Expression to dynamically match parts of the string.

In the case of using a Regular Expression for the end prop, remember to match from the end of the pattern using the $ flag.

Options

Prop Type Description Default
text String The input text to truncate if there isn't enough space to render the entire string. ''
ellipsis String The ellipsis to use when the text is truncated. '...'
start Number RegExp The number of characters or a Regular Expression match from the start of the text to preserve. 0
end Number RegExp The number of characters or a Regular Expression match from the end of the text to preserve. 0
smartCopy Boolean String Can be one of the following: false, 'partial' or 'all'. Allows the the full un-truncated text to be copied to the user's clipboard when they select and copy the truncated text. 'all'
onResizeDebounceMs Number A delay in milliseconds to debounce the window resize event listener. 100

Why and when to middle truncate?

We have access to CSS text truncation with the text-overflow: ellipsis; which will truncate the text at the end of a string for us. So why do you need middle truncation, and when should you use it?

Ultimately it boils down to whether text at the end or in the middle of the string is more likely to differentiate the item.

In the case of a street address, the end of the string is likely to be less important in helping the user to differentiate items from one another, as most strings of that category are likely to end in road, street, avenue etc... So in that case truncating at the end of the string would be a better solution.

In the case of a connection string or a URL, the start and/or end of the string is likely to contain more valuable information to help the user differentiate items from one another, such as the protocol and sub domain parts at the start, and the port number at the end of the string are more likely to be different. In that case truncating in the middle of the string would be a better solution.

Contributing

Pull requests of any kind are welcome from the community. Please ensure you have read the guidelines for Contributing and this project's Code of Conduct before raising a pull request.

Maintainers

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].