react-text-ellipsis-clamp
v1.0.0
Published
Ultra-lightweight React component (165 bytes gzipped) to clamp multiline text with CSS-only ellipsis. Zero dependencies, TypeScript support, and production-ready performance.
Maintainers
Keywords
Readme
react-text-ellipsis-clamp
A ultra-lightweight React component to clamp multiline text with CSS-only line-clamp. Perfect for truncating long text content with optional expand/collapse functionality.
🚀 Ultra-Performance & Size
- 📦 Ultra-Small: Only 165 bytes gzipped (CJS) / 5.2KB gzipped (ESM)
- ⚡ Lightning Fast: CSS-only solution with zero JavaScript overhead
- 🎯 Zero Dependencies: No external libraries, no bundle bloat
- 🔥 Production Ready: Optimized with aggressive minification
✨ Features
maxLines— how many lines to show before truncationexpandable— optional toggle to show/hide the full contentas— render as any intrinsic element (div,p,span, ...)- Zero dependencies — No external libraries required
- TypeScript support — Full type definitions included
- Accessible — ARIA attributes and keyboard navigation
- Responsive — Works on all screen sizes
- Performance optimized — CSS-only solution
Install
npm i react-text-ellipsis-clamp
# or
yarn add react-text-ellipsis-clamp
# or
pnpm add react-text-ellipsis-clampQuick Start
import { Ellipsis } from 'react-text-ellipsis-clamp';
function App() {
return (
<Ellipsis maxLines={2}>
This is a very long text that will be truncated after 2 lines...
</Ellipsis>
);
}Features
- 🎯 CSS-only solution - Uses
-webkit-line-clampfor optimal performance - 🔧 Highly customizable - Render as any HTML element with custom styling
- 📱 Responsive - Works perfectly on all screen sizes
- ♿ Accessible - Proper ARIA attributes and keyboard navigation
- 🎨 TypeScript support - Full type definitions included
- 📦 Tree-shakable - Only bundle what you use
- 🚀 Zero dependencies - No external dependencies
API Reference
Props
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| maxLines | number | 2 | Number of lines to show before truncation |
| expandable | boolean | false | Show expand/collapse toggle button |
| as | keyof JSX.IntrinsicElements | "div" | HTML element to render |
| children | React.ReactNode | - | Content to truncate |
| style | React.CSSProperties | - | Custom styles (merged with clamp styles) |
| ...rest | Omit<JSX.IntrinsicElements[T], "ref" \| "children"> | - | All other props passed to the element |
Examples
Basic Usage
import { Ellipsis } from 'react-text-ellipsis-clamp';
<Ellipsis maxLines={3}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</Ellipsis>Expandable Content
<Ellipsis maxLines={2} expandable>
This is a very long text that can be expanded and collapsed by clicking the toggle button.
</Ellipsis>Different HTML Elements
{/* As paragraph */}
<Ellipsis as="p" maxLines={2}>
This will render as a <p> element.
</Ellipsis>
{/* As span */}
<Ellipsis as="span" maxLines={1}>
This will render as a <span> element.
</Ellipsis>Custom Styling
<Ellipsis
maxLines={2}
style={{
color: '#e74c3c',
fontSize: '18px',
fontWeight: 'bold'
}}
>
Custom styled truncated text
</Ellipsis>Real-world Example: Article Cards
function ArticleCard({ title, description }) {
return (
<article className="card">
<h3>{title}</h3>
<Ellipsis maxLines={3} expandable>
{description}
</Ellipsis>
<button>Read More</button>
</article>
);
}🚀 Performance & Size Comparison
| Package | Gzipped Size | Dependencies | Performance | |---------|-------------|-------------|-------------| | react-text-ellipsis-clamp | 165 bytes | 0 | CSS-only | | react-lines-ellipsis | ~2.5KB | 1 | JavaScript-based | | react-truncate | ~3.2KB | 2 | JavaScript-based | | react-ellipsis-text | ~1.8KB | 1 | JavaScript-based |
Why choose react-text-ellipsis-clamp?
- 97% smaller than alternatives
- Zero dependencies = faster installs
- CSS-only = better performance
- TypeScript = better DX
Browser Support
This component uses CSS -webkit-line-clamp which is supported in:
- Chrome 6+
- Firefox 68+ (with
-webkit-prefix) - Safari 5+
- Edge 79+
For older browsers, the component gracefully degrades to show the full text.
TypeScript
Full TypeScript support is included:
import { Ellipsis, EllipsisProps } from 'react-multiline-ellipsis';
// Type-safe props
const props: EllipsisProps<'p'> = {
maxLines: 2,
expandable: true,
as: 'p',
children: 'Your text here'
};Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📊 Bundle Analysis
# Check bundle size
npm run size:check
# Build with size analysis
npm run build && npx bundlesizeSize breakdown:
- CJS: 165 bytes gzipped (ultra-minimal)
- ESM: 5.2KB gzipped (with full features)
- TypeScript definitions: 640 bytes
Development
# Install dependencies
npm install
# Run type checking
npm run typecheck
# Run tests
npm test
# Build the package
npm run build
# Build with development sourcemaps
npm run build:devLicense
MIT © Yaşar Tahir Köse
Changelog
1.0.0
- Initial release
- Basic ellipsis functionality
- Expandable toggle support
- TypeScript definitions
- Full test coverage
