Truncate

Cuts a long line and keeps the whole value.

Position

  • Refund every duplicate charge

    enddefault

  • trace_01J9ZKQ4E7B2N6M3P8R5T1V4E7Q

    middle

Usage

When to use it
Use
An ID, a hash or a path in a narrow column, where the tail is what tells two apart.
Use
A title in a row that must stay one line.
Use
Any cut value someone needs to read whole: the hint shows it on hover and focus.
Not for
A paragraph that can wrap; let it wrap. Use Card.
Not for
A row's title, which already truncates. Use Item.

Rules

6 rules
Must
Use position="middle" for an ID, a hash, a key or a path. Because IDs share their prefix, so an end cut leaves run_01J9ZK… on every row and nobody can tell them apart.
Must
Pass the full value as a plain string child. Because Truncate measures and splits the string, and the hint repeats it; an element child cannot be cut in the middle.
Never
Cut a value in code with slice or a fixed character count. Because a sliced string loses the rest for copy, find and a screen reader, and cuts at the wrong place at every other width.
Must
Give Truncate a parent with a width it cannot grow past: min-w-0 in a flex row, or a max-w. Because a flex child grows to its content by default, so the value never runs out of room and never cuts.
Never
Add a title attribute for the full value; the hint already shows it. Because a title opens a second, unstyled tooltip over the hint, and only for mouse users.
Should
Set an ID's mono through type-code-sm or type-code-xs on Truncate. Because mono at the sans size reads a size bigger than the text beside it.

Accessibility

3 checks
The DOM holds the whole value, so a screen reader, copy and find in page get all of it.
Only a cut value takes a tab stop; focus opens the hint, Escape closes it.
The hint is Tooltip's ink chip, 20:1, and is the same words as the value.

Keyboard

2 keys
Tab
Reaches a cut value and opens its hint.
Escape
Closes the hint.

Edge cases

6 edge cases
Value fits
Plain text: no hint and no tab stop.
Very narrow
The head goes first; the tail and the ellipsis stay.
Emoji or accents
The cut lands between characters a person sees, never inside one.
Copy of a cut value
Copies the full string.
Touch screen
A tap pins the hint open; the next tap or Escape closes it.
Font loads late
It measures again once the font is ready.

API

5 props
children
string
position
'end' | 'middle' default 'end'
tail
number | 'auto' default 'auto'
hint
boolean default true
focusable
boolean default true

Tokens

8 tokens
--ink
#050505
--text-inverse
#ffffff
--radius-tag
6px
--type-label-sm
500 12px/1.35 var(--font-sans)
--tooltip-max
256px
--dur-overlay
200ms
--dur-state
160ms
--z-overlay
50

Install

For your coding agent

GitHub

Please star me pookie

141

A new design system every week

Leave your email before you copy the prompt.