About Truncate
Truncate is a utility component that shortens text content with an ellipsis (…) when it overflows its container. It ensures text fits within constrained layouts while maintaining readability.Installation
Usage
Props
Basic Example
Truncate long text within a constrained container:With Different Elements
Change the underlying HTML element:In Table Cells
Commonly used in table cells to prevent overflow:In Lists
Truncate items in constrained list layouts:With Links
Combine with anchor elements:Multiple Lines
Truncate only works for single-line text. For multi-line truncation, use CSS directly:Title Attribute
Thetitle prop is required and serves two purposes:
- Shows a tooltip on hover with the full text
- Provides the full content to screen readers
title to match the truncated content:
How It Works
Truncate applies these CSS properties:Accessibility
- The
titleattribute provides full text on hover - Screen readers announce the complete text from
title - Users can still access full content via tooltip
- Works with keyboard navigation
Best Practices
Do
- Always provide a
titlethat matches the content - Use in constrained layouts where space is limited
- Truncate predictable content like emails, URLs, filenames
- Provide alternative ways to see full content (modal, tooltip, etc)
Don’t
- Don’t truncate critical information without providing access to full text
- Don’t use for multi-line content (use CSS directly)
- Don’t truncate content that must be fully visible
- Don’t forget the
titleattribute