React TextEllipsis Component - A powerful and flexible text truncation component with expand/collapse functionality.
- π― Multi-line text truncation with precise control
- π Three ellipsis positions: start, middle, end
- π Expand/collapse functionality
- π¨ Customizable action buttons
- π± Responsive - auto-recalculates on window resize
- ποΈ Imperative API via ref
- πͺ TypeScript support
- β‘ Efficient binary search algorithm
π Live Demo - See all features in action!
npm install rc-text-ellipsis --saveor
yarn add rc-text-ellipsisimport React from 'react';
import TextEllipsis from 'rc-text-ellipsis';
import 'rc-text-ellipsis/assets/index.css';
function App() {
return (
<TextEllipsis
rows={3}
content="This is a very long text that needs to be truncated..."
expandText="Expand"
collapseText="Collapse"
/>
);
}<TextEllipsis
rows={2}
content="Your long text here..."
action={(expanded) => (
<span style={{ color: 'red', fontWeight: 'bold' }}>
{expanded ? '[Fold]' : '[Unfold]'}
</span>
)}
/><TextEllipsis
rows={2}
content="Your long text here..."
suffix={(expanded, isOverflow) => (
<span style={{ color: 'blue', marginLeft: '4px' }}>
{isOverflow ? (expanded ? '[Collapse]' : '[Expand]') : '[Complete]'}
</span>
)}
/>import React, { useRef } from 'react';
import TextEllipsis, { TextEllipsisRef } from 'rc-text-ellipsis';
function App() {
const textEllipsisRef = useRef<TextEllipsisRef>(null);
const handleExpand = () => {
textEllipsisRef.current?.toggle(true);
};
return (
<>
<TextEllipsis
ref={textEllipsisRef}
rows={2}
content="Your long text here..."
expandText="Expand"
collapseText="Collapse"
/>
<button onClick={handleExpand}>Expand from outside</button>
</>
);
}// Ellipsis at the end (default)
<TextEllipsis
position="end"
rows={2}
content="Your text..."
/>
// Ellipsis at the start
<TextEllipsis
position="start"
rows={2}
content="Your text..."
/>
// Ellipsis in the middle
<TextEllipsis
position="middle"
rows={2}
content="Your text..."
/>The component supports two ways to display additional content:
- Only visible when text is truncated
- Disappears when text doesn't overflow
- Used for expand/collapse functionality
- Always visible, regardless of text overflow
- Receives two parameters:
expanded: Current expand/collapse stateisOverflow: Whether the text is truncated
- Perfect for status indicators, badges, or persistent actions
Important: action and suffix are mutually exclusive. If both are provided, suffix takes priority.
// Suffix shows different states
<TextEllipsis
rows={2}
content={longText}
suffix={(expanded, isOverflow) => (
<span style={{ color: isOverflow ? 'orange' : 'green' }}>
{isOverflow
? (expanded ? 'β² Show Less' : 'βΌ Show More')
: 'β Complete'
}
</span>
)}
/>| Property | Type | Default | Description |
|---|---|---|---|
| className | string |
- | Additional CSS class for the root element |
| style | React.CSSProperties |
- | Inline styles for the root element |
| rows | number |
1 |
Number of lines to display before truncating |
| dots | string |
'...' |
The ellipsis text to show when truncated |
| content | string |
'' |
The text content to display |
| expandText | string |
'' |
Text for the expand action button |
| collapseText | string |
'' |
Text for the collapse action button |
| position | 'start' | 'middle' | 'end' |
'end' |
Position of the ellipsis |
| onClickAction | (e: React.MouseEvent) => void |
- | Callback when action button is clicked |
| action | (expanded: boolean) => React.ReactNode |
- | Custom render function for action button |
| suffix | (expanded: boolean, isOverflow: boolean) => React.ReactNode |
- | Custom render function for suffix (always visible). Mutually exclusive with action - if both are provided, suffix takes priority |
| Method | Type | Description |
|---|---|---|
| toggle | (expanded?: boolean) => void |
Programmatically toggle expand/collapse state |
import TextEllipsis, {
TextEllipsisProps,
TextEllipsisRef
} from 'rc-text-ellipsis';# Install dependencies
npm install
# Start development server
npm start
# Visit http://localhost:8000/examples/ to see examples# Build the component
npm run build
# Run linter
npm run lint# Run tests
npm test
# Run tests in Chrome
npm run chrome-test
# Generate coverage report
npm run coverageModern browsers and IE11+
We welcome contributions! Please feel free to submit a Pull Request.
rc-text-ellipsis is released under the MIT license.