A group truncation component (added in v0.7.0) that treats each direct child as an indivisible item. When items overflow the allowed rows, the remaining ones are hidden behind an overflow indicator (+N by default). It works well for tag lists, avatar groups, and action buttons, and visible items keep their original React event handlers.
Usage
Pass your items as direct children. By default, it keeps a single row and appends +N when items overflow.
import React from 'react'
import { TruncateGroup } from '@re-dev/react-truncate'
const tags = ['React', 'TypeScript', 'UI', 'Accessibility', 'Docs', 'Testing']
const TagList: React.FC = () => {
return (
<TruncateGroup>
{tags.map((tag) => (
<span key={tag} className="tag">
{tag}
</span>
))}
</TruncateGroup>
)
}When to use?
Use <TruncateGroup /> when you have a list of tags, badges, or avatars where cutting an item in half does not make sense—you want to show all complete items that fit and summarize the rest with +3 or 3 more items.
Type Declarations
This is the Props type of the component, extending standard div HTML attributes.
/**
* Measurement result passed to `ellipsis` render functions and `onTruncate`
*
* @since V0.7.0
*/
export interface TruncateGroupResult {
/**
* Number of direct children currently rendered
*/
visibleCount: number
/**
* Number of direct children hidden due to overflow
*/
hiddenCount: number
/**
* Total number of direct children
*/
totalCount: number
/**
* Whether any direct children were hidden
*/
truncated: boolean
}
/**
* Props for the `<TruncateGroup />` component
*
* @since V0.7.0
*/
export interface TruncateGroupProps
extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
/**
* Direct child elements to lay out as indivisible items (e.g. tags, avatars,
* buttons)
*/
children: React.ReactNode
/**
* Maximum number of item rows to display before truncating
*
* Zero or invalid values disable truncation.
* Note: This limits rows of items, not text lines inside a single child.
*
* @default 1
*/
lines?: number
/**
* Overflow indicator shown after the visible items when truncated
*
* Accepts a React node or a render function receiving the current counts.
*
* @default '+${hiddenCount}'
*/
ellipsis?:
| React.ReactNode
| ((result: TruncateGroupResult) => React.ReactNode)
/**
* Callback fired after measurement settles and whenever the visible count
* changes
*
* @param result - Current visible and hidden item counts
*/
onTruncate?: (result: TruncateGroupResult) => void
}A few notes when using it:
- Item spacing defaults to
4pxand can be customized with standard CSSgapviastyleorclassName. - Hidden items are unmounted, so provide stable
keys and keep persistent state in the parent component. - Container resizes and font loading are observed automatically. During SSR, all children are rendered initially until browser layout determines the visible prefix.
Live demo
Adjust the slider to see how many tags fit at different container widths, and toggle “Custom indicator” to compare the default +N with a custom render function.