truncateText
Truncate text to a maximum length with ellipsis.
Installation
pnpm dlx shadcn@latest add https://reactusekit.dev/r/truncate-text.json
Usage
import { truncateText } from "@/lib/truncate-text";
function App() {
const longText = "This is a very long text that needs to be truncated";
const shortText = truncateText(longText, 20);
return (
<div>
<p>Original: {longText}</p>
<p>Truncated: {shortText}</p>
</div>
);
}
API
Parameters
Parameter | Type | Description |
---|---|---|
text | string | The text to truncate |
maxLength | number | Maximum length before truncation |
Returns
Type | Description |
---|---|
string | The truncated text with ellipsis if needed |