Truncate

A line of text that ends in an ellipsis when it doesn't fit, with the rest in a tooltip.

  • q3-board-update-final-v4-with-appendix.pdf2.4 MB
  • homepage-hero-illustration-dark@2x.png860 KB
  • brand-assets.zip48 MB
  • customer-interview-transcripts-batch-2.docx312 KB

Installation

  1. Add the registryOnce per project
  2. Add truncate

Usage

Truncate keeps its text on one line and cuts it off with an ellipsis once it's wider than the space it has. Hovering the text shows all of it in a tooltip, and so does tapping it on a touch screen or tabbing to it, but only while it's truncated. Inside a link, a button or anything else that takes focus, like a tree item, a tap and the tab key belong to that, so only hovering shows the tooltip. A scrolling list doesn't count, even one that takes focus so it can be scrolled from the keyboard. The text is measured again whenever its space or its own width changes, so resizing a panel or making the text bold brings back whatever fits.

It's a block, so give it a width the way you would any block, like flex-1 in a row. It takes a string, not other elements.

Examples

Positions

The ellipsis goes at the end by default. position="middle" keeps the start and end of the text and cuts out the middle instead, for file names, paths, hashes and URLs, which differ in their last few characters. Before the page loads in the browser, the middle cut shows as an ellipsis at the end. Screen readers hear the whole text either way, and copying it copies all of it, without the ellipsis.

End~/projects/website/src/components/marketing/pricing-table.tsx
Middle~/projects/website/src/components/marketing/pricing-table.tsx

In a table

A table cell grows to fit its text unless the table has a fixed layout. Add table-fixed to the table and give the columns that don't truncate a width.

Each truncated text is a tab stop of its own, so a long table or list of them takes many presses of the tab key to get through. Pass tabIndex={-1} to leave them out of the tab order. Hovering and tapping still show the tooltip, and screen readers still hear the whole text.

Without a tooltip

tooltip={false} leaves the tooltip out, for text whose whole version is a click away.

Props

Truncate