Cuts text that doesn't fit by fading its last 24px instead of swapping the end for an ellipsis, and only when the text is cut: a label that fits is left alone. The fade follows the writing direction. CSS only, no script and no wrapper element.
Code block, command palette, prompt input, message thread and sources use it.
Use it
Compose it into the class that holds the text:
.fileName { composes: truncate from "../text/text.module.css"; font-weight: var(--ui-weight-medium);}.excerpt { composes: truncateLines from "../text/text.module.css"; --truncate-lines: 3;}truncate: one line. In a flex row it already setsmin-width: 0.truncateLines: several lines, 2 unless--truncate-linessays otherwise. The element has to be block-level (or a grid or flex item).
The same file holds three more utilities the components compose:
wrap: text that's never cut. A long name, email or path breaks onto the next line, inside a word only when there's nowhere else to break.srOnly: visually hidden, still read by screen readers.sweep: work in progress. The text dims and a light band crosses it once every--ui-dur-sweep(2.4s),linear;--sweep-dimsets the dim (default 0.55). Underprefers-reduced-motion: reducethe band and the dim are both dropped and the text stays still.
When to use it
- Names that come from data and can run long: file names, page titles, domains, breadcrumbs.
When not to
- Labels you wrote. Shorten the copy instead.
- The only label on a control. If cutting it hides what the control does, give it room or wrap it.
- Text that is the point of the row, such as an error message. Let it wrap.
Keep the full text reachable
The whole string stays in the DOM, so screen readers read all of it. Where the cut part carries meaning (a file name's extension, a page title), also put the full text in title, or in a tooltip if the row has one.
Copying text
copyText(text) from copy-text.ts puts text on the clipboard and resolves whether it got there. It tries the Clipboard API, and where that's missing or refused (a social app's in-app browser, a page that isn't served securely, a denied permission) it copies from a hidden, read-only textarea, which those browsers still allow inside a tap. Call it straight from the press, before awaiting anything else. Code block, Copy field, Response actions, Terminal output and Message thread copy through it.
async function copy() { setState((await copyText(link)) ? "copied" : "failed");}On false, say so in place and select the text so it can be copied by hand; never show "Copied" for a copy that didn't happen.
How it works
overflow: hiddenmakes the element a scroll container. It only has a scroll range while its content overflows.- An animation on
scroll(self inline)(orblockfor several lines) sets--truncate-fadeto 24px. With no scroll range the timeline is inactive, the animation applies nothing, and the fade stays 0px. - The mask is
linear-gradient(to right, #000 calc(100% - fade), transparent), turned toto leftinside[dir="rtl"](an attribute, not:dir(), which some builds rewrite into a language list). For several lines, one layer keeps every line but the last solid and a second fades the last one.
Browsers without scroll timelines fall back to text-overflow: ellipsis and -webkit-line-clamp, rather than fading every line or cutting through a letter.
Don't
- Put another
animationormask-imageon the same element: they replace this one. Put a shimmer or sweep on a span inside instead; masks on nested elements multiply. - Use
overflow: clip. It isn't a scroll container, so nothing detects the overflow.