Skip to content

Text

Text utilities: truncation that fades the end instead of adding an ellipsis, text for screen readers only, a shimmer for work in progress, and copying text to the clipboard where the Clipboard API is refused.

One line that fades when it’s cut

  • guest-list.csv24 KB
  • saturday-supper-at-14-alder-lane-final-menu-with-allergies-v3.pdf24 KB
  • seating.png24 KB

Short names are left alone; only the long one fades.

Several lines

Slow-roast a pork shoulder from late morning and serve it with smashed potatoes and a big green salad. Start with burrata and roasted grapes. Your sister’s lemon tart covers dessert, and there’s room in the oven for a second tray if two more friends can come.

Work in progress

Reading the guest list

Installation

With UI Polish connected, ask your agent:

Add the UI Polish text to this project.

Or with the shadcn CLI:

npx shadcn@latest add https://uipolish.com/r/text.json

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:

CSS
.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 sets min-width: 0.
  • truncateLines: several lines, 2 unless --truncate-lines says 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-dim sets the dim (default 0.55). Under prefers-reduced-motion: reduce the 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.

TSX
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

  1. overflow: hidden makes the element a scroll container. It only has a scroll range while its content overflows.
  2. An animation on scroll(self inline) (or block for several lines) sets --truncate-fade to 24px. With no scroll range the timeline is inactive, the animation applies nothing, and the fade stays 0px.
  3. The mask is linear-gradient(to right, #000 calc(100% - fade), transparent), turned to to left inside [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 animation or mask-image on 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.