Truncate
Multi-line text clamping with expandable show-more toggle.
<arc-truncate>Overview
Truncate cuts long text off after lines lines and adds a "Show more" / "Show less" toggle. The clamp is CSS -webkit-line-clamp, so no JavaScript measures the text to cut it.
A ResizeObserver checks whether the text overflows the clamp, and the toggle appears only when it does, so short text gets no toggle. It checks again when the container resizes.
Clicking the toggle fires arc-toggle with { expanded } in the detail, and the expanded attribute is reflected for CSS. The toggle is uppercase text in --interactive.
Guidelines
Do
- Use it for long descriptions, comments or bios that would take over the layout
- Set `lines="2"` or `lines="3"` for card descriptions and list item summaries
- Use it in cards or list items to keep their heights even
Don't
- Don't use it for single-line text, `lines="1"` included. Use CSS `text-overflow: ellipsis`
- Don't put forms, buttons or other controls inside it. It's for text
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-truncate lines="3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.
Duis aute irure dolor in reprehenderit in voluptate velit esse.
</arc-truncate>import { Truncate } from '@arclux/arc-ui-react';
export default function Example() {
return (
<Truncate lines={3} onArcToggle={(e) => console.log(e.detail.expanded)}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.
</Truncate>
);
}<script setup>
import { Truncate } from '@arclux/arc-ui-vue';
</script>
<template>
<Truncate :lines="3" @arc-toggle="(e) => console.log(e.detail.expanded)">
Lorem ipsum dolor sit amet...
</Truncate>
</template><script>
import { Truncate } from '@arclux/arc-ui-svelte';
</script>
<Truncate lines={3} on:arc-toggle={(e) => console.log(e.detail.expanded)}>
Lorem ipsum dolor sit amet...
</Truncate>import { Component } from '@angular/core';
import { Truncate } from '@arclux/arc-ui-angular';
@Component({
imports: [Truncate],
template: `
<arc-truncate [lines]="3" (arc-toggle)="onToggle($event)">
Lorem ipsum dolor sit amet...
</arc-truncate>
`,
})
export class MyComponent {
onToggle(e: CustomEvent) { console.log(e.detail.expanded); }
}import { Truncate } from '@arclux/arc-ui-solid';
export default function Example() {
return (
<Truncate lines={3}>
Lorem ipsum dolor sit amet...
</Truncate>
);
}import { Truncate } from '@arclux/arc-ui-preact';
export default function Example() {
return (
<Truncate lines={3}>
Lorem ipsum dolor sit amet...
</Truncate>
);
}API
linesnumber3- Maximum number of visible lines before clamping
expandedbooleanfalse- Whether the text is fully expanded
Events
arc-toggledetail:{ expanded: boolean }- Fired when expand/collapse toggle is clicked, with { expanded } detail