Text truncate
TextTruncate keeps text on one line and cuts it with an ellipsis when it does not fit. While the text is cut off, the full text shows in a tooltip on hover and on keyboard focus. It replaces EllipsisContent and is available from Click UI 0.15.0.
Example
Props
Prop | Type | Default value | Description |
|---|---|---|---|
Prop children * | Type ReactNode | Default value required | Description The content to truncate. Must be a string when ellipsisPosition is "middle" |
Prop ellipsisPosition | Type "end" | "middle" | Default value "end" | Description Where the ellipsis goes. "middle" keeps the end of the text visible, such as a file extension or an ID suffix |
Prop trailingChars | Type number | Default value 8 | Description Middle only. Number of characters kept after the ellipsis |
Prop maxWidth | Type string | Default value undefined | Description Caps the width with any CSS length, for example 160px or 20ch |
Prop showTooltip | Type boolean | Default value true | Description Shows the full text in a tooltip and adds a tab stop while truncated. Set to false in large lists or grid cells |
Prop tooltipContent | Type ReactNode | Default value children | Description Tooltip content. Pass plain text when children holds links or buttons |
Prop tooltipProps | Type { side?, align?, sideOffset? } | Default value undefined | Description Positions the tooltip, for example { side: "right" } |
Prop component | Type ElementType | Default value "span" | Description Element or component to render as. A component must forward ref and spread props |
Prop className | Type string | Default value undefined | Description Additional CSS class |
* denotes required field
Quick start
import { TextTruncate } from '@clickhouse/click-ui'
function FileName({ name }: { name: string }) {
return (
<TextTruncate maxWidth='20ch' ellipsisPosition='middle'>
{name}
</TextTruncate>
)
}Accessibility
While the text is cut off, TextTruncate becomes a tab stop so keyboard users can reach the tooltip. Inside a button, link, tab, menu item or option it adds no tab stop, and the tooltip is hover-only there. If children contains a link or button, pass plain text as tooltipContent; the component warns in the console when it is missing.
Related components
- EllipsisContent: The older component that TextTruncate replaces.
- Text: For text that is allowed to wrap.
- Tooltip: Used to show the full text.
Common use cases
Long lists
Turn the tooltip off when many truncated items render at once, such as rows in a long list.
import { TextTruncate } from '@clickhouse/click-ui'
function TableNames({ names }: { names: string[] }) {
return names.map(name => (
<TextTruncate key={name} maxWidth='180px' showTooltip={false}>
{name}
</TextTruncate>
))
}