Text
The Text component is a versatile typographic component that supports various styling options through variants, colors, and text alignment. It picks a sensible default HTML element based on the selected variant, which you can override with as.
Use variant to set the typographic style (headings, body, and caption scales) and color to tint it — leave color unset to inherit the variant’s own default. align controls horizontal alignment, while wrap tunes how lines break. For overflow, truncate cuts a single line with an ellipsis and lineClamp limits the text to a set number of lines (it takes precedence over truncate). Try them in the playground below.
Highlight text
HighlightText renders a string with the characters matching match wrapped in <mark>. Matching is fuzzy (the same fuzzysort matching used by file search), so match="rdm" highlights the letters of “README”. With no match, or no match found, it renders plain text.
import { HighlightText } from "@harnessio/ui/components";
<HighlightText text={file.name} match={searchQuery} />;Installation
import { Text } from "@harnessio/ui/components";
// Basic<Text>Default text with the body-normal variant</Text>
// Variant + color<Text variant="heading-section" color="foreground-1">Section heading</Text>
// Override the element for correct semantics<Text variant="heading-hero" as="h1">Hero heading as a real h1</Text>
// Truncation<Text truncate>Truncated with an ellipsis if it overflows</Text>Anatomy
Text renders a single typographic element chosen from the variant, with the color, alignment, wrapping, and truncation applied as classes.
- Each variant maps to a default HTML element:
heading-*andbody-*variants render as<p>,body-codeandbody-code-strongas<pre>, andcaption-*as<span>. - Heading variants render as
<p role="heading">rather than a real<h1>–<h6>. Useas(e.g.as="h2") when you need a true heading level in the document outline. asoverrides the element;asChildmerges Text’s props onto the single child you provide instead of rendering its own element.truncateandlineClampboth add a nativetitletooltip carrying the full text;lineClampwins when both are set.
Best practices
Do
- Choose the
variantthat matches the content’s role so hierarchy stays consistent — don’t recreate sizes withclassName. - Use
asto give headings the correct semantic level (h1–h6); the visual variant and the element are independent. - Prefer
lineClampfor multi-line clamping andtruncatefor single-line ellipsis. - Let
colorconvey state (success,warning,danger) rather than hard-coded color classes.
Don’t
- Don’t rely on
heading-*variants alone for document structure — on their own they’re<p role="heading">with no level. - Don’t set both
truncateandlineClampexpecting them to combine;lineClamptakes precedence.
Accessibility
Labeling & semantics
- Heading variants expose
role="heading"but no heading level. For a meaningful outline, pass the matchingaselement (as="h2", etc.). - When truncated (via
truncateorlineClamp), the full text is preserved in the element’stitleattribute so it remains discoverable on hover.
Focus & states
Textis non-interactive and adds no focus behavior of its own. Any interactive element you render throughas/asChildkeeps its native behavior.
Properties
Prop | Required | Default | Type |
|---|---|---|---|
| variant | false | 'body-normal' | 'heading-hero' | 'heading-section' | 'heading-default' | 'heading-subsection' | 'heading-base' | 'heading-small' | 'body-normal' | 'body-single-line-normal' | 'body-strong' | 'body-single-line-strong' | 'body-code' | 'body-code-strong' | 'caption-normal' | 'caption-strong' | 'caption-code' | 'caption-code-strong' | 'caption-single-line-normal' |
| color | false | 'foreground-2' | 'inherit' | 'foreground-1' | 'foreground-2' | 'foreground-3' | 'foreground-4' | 'disabled' | 'success' | 'warning' | 'danger' | 'merged' | 'brand' |
| align | false | 'left' | 'center' | 'right' | |
| as | false | (derived from variant) | 'div' | 'span' | 'p' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'label' | 'legend' | 'caption' | 'figcaption' | 'blockquote' | 'pre' | 'kbd' | 'var' | 'code' | 'samp' | 'cite' | 'time' | 'address' | 'strong' | 'abbr' | 'em' | 'li' | 'dt' | 'dd' | 'del' | 'ins' |
| asChild | false | false | boolean |
| truncate | false | false | boolean |
| lineClamp | false | '1' | '2' | '3' | '4' | '5' | '6' | 'default' | |
| wrap | false | 'wrap' | 'nowrap' | 'pretty' | 'balance' | |
| className | false | string | |
| children | true | ReactNode |
HighlightText
Prop | Required | Default | Type |
|---|---|---|---|
| text | true | string | |
| match | false | string | null | |
| className | false | string |