x-read-more
Truncated content with a smooth height expand, a truncation state for the button, and automatic expansion for focus, print, hash links, and find-in-page.
Truncated content with a smooth height expand, a truncation state for the button, and automatic expansion for focus, print, hash links, and find-in-page.
x-read-more
The root. Holds the expanded state and runs the height animation
x-read-more:button
Click expands the content
x-read-more:preview
Optional. The clamped preview shown while collapsed. It measures itself for $readMore.isTruncated, its id attributes are stripped so the full copy keeps the real ones, and its links leave the tab order so Tab reaches the button first
x-read-more:content
Optional. The full copy, marked hidden="until-found" while collapsed
$readMore.isExpanded
Whether the content is expanded
$readMore.isTruncated
Whether the preview clips its content. undefined until the preview is measured, and always undefined without a preview
$readMore.expand()
Expand programmatically (no-op if already expanded)
The root also expands itself whenever collapsing would hide something the user is looking for: focus entering the truncated content (a script focusing a link inside), printing the page, the URL hash pointing at an element inside the root, and find-in-page or text-fragment navigation hitting x-read-more:content. All except focus skip the animation. A keyboard user never triggers the focus case: the preview's links are out of the tab order while collapsed, so Tab reaches the button, Enter expands and moves focus to the full copy, and the next Tab lands on its first link. There is no collapse control. Expansion is one-way, except that a print-triggered expand collapses again after printing.
Examples
Basic truncation
Clamp the preview, pair it with the full copy, and show the button only while the clamp cuts something:
<div x-read-more class="overflow-clip">
<div x-read-more:preview class="line-clamp-3">Long content…</div>
<div x-read-more:content hidden="until-found">Long content…</div>
<button type="button" x-read-more:button x-show="$readMore.isTruncated && !$readMore.isExpanded">
Read more
</button>
</div>The preview hides on expand, so it always needs the full copy next to it. The preview binds x-overflow in vertical scroll mode on itself, and $readMore.isTruncated reads its edges: true while the clamp cuts content, false when the text fits, and undefined until the preview has a box and is measured. The value follows a resize, a font load, or a content change.
Find-in-page support
The full copy on x-read-more:content also serves find-in-page. A line clamp alone cannot receive the browser's beforematch event:
Searching the page, or following a link with a text fragment, reveals the full copy with no animation.
Keeping the button's space until measured
x-show removes the button from the flow, so a long text shifts the content below it when the button appears after the first measurement. The _product-description block keeps the server-rendered button in place while isTruncated is undefined, then hides it with invisible and removes it with hidden once the text fits or is expanded:
Animation
The expand runs as a Web Animation on the root, from the collapsed height at the moment of the click to the content height. The root then returns to auto height, so it follows its content after a later resize or font load.
The root's transition-duration and transition-timing-function set the curve, with 300ms and ease as the defaults. A Tailwind ease-in-out class on the root is enough to change the easing. transition: none on the root skips the animation, and so does a prefers-reduced-motion: reduce preference.
Options
There is no options object and no modifiers. Everything is markup and the root's transition properties.
Last updated
Was this helpful?

