Skip to Content
NarakUI

Truncated Text

Text that ends with an ellipsis when it doesn't fit, and shows the full text in a tooltip only while it is cut off.

Single line

By default the text stays on one line. Hover or focus a prompt that is cut off to read all of it; prompts that fit show no tooltip. Overflow is measured when the pointer enters or the text gets focus, so there is no layout work until someone wants to read it.

Set typography and color with className. TruncatedText needs a TooltipProvider above it, which apps already render at the root.

Best CRM for startups
Which project management tools integrate with Slack and Google Drive for remote teams?
Top running shoes for flat feet
How do enterprise security teams compare SSO providers for compliance with SOC 2 and HIPAA?

In a flex row

In a flex row, give the text min-w-0 flex-1 so it can shrink, and shrink-0 to the icons and metadata that should keep their size. Any flex parent between the row and the text also needs min-w-0.

Q3 visibility report for Acme across ChatGPT, Perplexity and Gemini
Report
Competitor citations
Sheet
Launch brief for the spring product line and answer engine strategy
Document

Multiple lines

lines clamps the text to that many lines before the ellipsis. The tooltip still appears only when text is hidden.

Visibility
Acme's share of voice in ChatGPT rose 12% after its pricing page was cited in 40 new answers about CRM tools.

Custom tooltip

The tooltip shows the children by default. Pass tooltip when the children are rich content and the tooltip should be plain text, and tooltipSide or tooltipSideOffset to move it. When a parent owns the tooltip, disableTooltip turns it off; the ref still points at the truncating element so the parent can measure it.

Maya Chen added 3 citations from nytimes.com to the brand report
2h

Installation

pnpm dlx shadcn@latest add @narakui/truncated-text

Add "@narakui": "https://ui.narakcode.dev/r/{name}.json" to components.json, or install directly from https://ui.narakcode.dev/r/truncated-text.json.

Usage

import { TruncatedText } from '@/components/ui/truncated-text';

<TruncatedText className="text-body-small-regular">{prompt.text}</TruncatedText>
<TruncatedText lines={2} className="text-body-regular">{headline}</TruncatedText>

Properties

PropTypeDefaultDescription
childrenReactNodeRequiredThe text to show. Also the tooltip content unless tooltip is set.
tooltipReactNode—Tooltip content to show instead of children.
linesnumber1Lines to show before the ellipsis.
disableTooltipbooleanfalseTurns off the tooltip when a parent owns it.
tooltipSide'top' | 'right' | 'bottom' | 'left''top'Side the tooltip opens toward.
tooltipSideOffsetnumber—Distance in pixels between the tooltip and the text. Defaults to the tooltip's own offset.
classNamestring—Additional CSS classes for the truncating element, such as typography and color.
refRef<HTMLDivElement>—Ref to the truncating element.