React Shorten
React component to shorten text
React component to shorten text by lines, words or characters with some customizations and callbacks.
Installation
- Using yarn
yarn add react-shorten
- Or using npm
npm install react-shorten
General Usage
import Shorten from 'react-shorten';
<Shorten
by={'lines' | 'characters' | 'words'}
length={Number}
onExpand={() => {}}
onShorten={() => {}}
ellipsis="more..."
ellipsisStyle={{
color: 'white',
fontSize: 20
}}
ellipsisClassName="ellipsis-btn">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</Shorten>
Demo
API
by
Specifies how to shorten the children
Type: 'lines' or 'characters' or 'words'
Default: 'lines'
Example<Shorten by="words">Lorem ipsum dolor sit amet</Shorten>
length
Specifies how many 'lines' or 'characters' or 'words' of text should be preserved until it gets shorten.
Type: integer
Default: 3
Example<Shorten length={3}>Lorem ipsum dolor sit amet</Shorten>
ellipsis
String that is added to the end of the text when it is shorten.
Type: string
Default: ' more...'
Example<Shorten ellipsis="...">Lorem ipsum dolor sit amet</Shorten>
children
The text to be shorten.
Type: string
Default: ''
Example<Shorten>Lorem ipsum dolor sit amet</Shorten>
onShorten
Function that is called every time when original text is shorten.
Type: function
Default: () => {}
<Shorten onShorten={() => {}}>Lorem ipsum dolor sit amet</Shorten>
onExpand
Function that is called when more link is clicked and original text is shown fully.
Type: function
Default: () => {}
<Shorten onExpand={() => {}}>Lorem ipsum dolor sit amet</Shorten>
ellipsisStyle
Object that overrides the inline-styles of ellipsis's button element.
Type: object
Default: {}
<Shorten ellipsisStyle={{color: 'white', fontSize: 20}}>Lorem ipsum dolor sit amet</Shorten>
ellipsisClassName
The css class name of ellipsis's button element.
Type: string
Default: ''
Example<Shorten ellipsisClassName="ellipsis-btn">Lorem ipsum dolor sit amet</Shorten>
License
This project is licensed under the MIT License - see the LICENSE file for details
Contact
If you have any questions or feedback please email me.