Expandable text
Text held to a few lines with a fade, and a Read more that lets the rest out. The button shows only when the text is longer than that.

Needs Alpine and the package's script. See getting started. Without them the text is shown in full and no button appears.
Use it
<x-ds::expandable-text>
{{ $ticket->body }}
</x-ds::expandable-text>
<x-ds::expandable-text :lines="5" moreLabel="Show the whole review" lessLabel="Show less">
{{ $review->text }}
</x-ds::expandable-text>
It measures the text, and measures again when its box changes width or its text changes, so a card that widens loses its button when the text fits.
Props
| Prop | Type | Default | What it does |
|---|---|---|---|
lines | int | 3 | How many lines show while it is held. |
moreLabel | string | Read more | The button's words while held. |
lessLabel | string | Show less | Its words while open. |
Accessibility
- A real button with
aria-expandedandaria-controls. Pressing it moves no focus. - A screen reader reads all of the text either way: the clamp is visual.
- The height animates only when the reader has not asked for reduced motion.
Don't
- Don't clamp what the reader must read to act: terms, warnings, errors.
- Links in the part that hides are taken out of the Tab order while it is held (and put back when it is let out), so focus never lands on something clipped. Still, a link the reader needs belongs on a line they can see.
- Don't clamp to one line. Truncate, or link to the full text.
More in specs/expandable-text.md.
From the package repo: edit this page on GitHub.