Menu

Components

Text

A paragraph, a caption, a label, a line of secondary text, styled from the theme.

Introduction

Text is the plain text of a page: paragraphs, captions, hints, labels, the line under a title. It puts the size, weight, color and line breaks in props that come from the theme, so text looks the same across the app and follows light and dark on its own.

  • Six sizes, four weights, and six tones: default, muted for secondary text, and primary, success, warning and danger.
  • Alignment, line height and line breaks: balance evens out a short text and pretty keeps a long one from ending on a lone word.
  • It can keep text to a number of lines with an ellipsis, or cut a single line short.
  • It is a p unless you pick another element with as, so text inside a line can be a span and emphasis a strong.
  • It has no client code, so it works in Server Components.

Text does not make titles: use Heading for them, which is a real heading in the outline of the page.

Installation

centerUI has no CLI or registry yet: components are copied into your project.

# bun
bun add tailwind-variants tailwind-merge clsx

# pnpm
pnpm add tailwind-variants tailwind-merge clsx

# npm
npm install tailwind-variants tailwind-merge clsx

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/text-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for Text. The size sets the font size and, with it, a line height that
// reads well for a paragraph of that size; `leading` changes only the line
// height. All the class names are written out in full so Tailwind can find them.
export const textStyles = tv({
  base: "m-0",
  variants: {
    size: {
      xs: "text-xs leading-4",
      sm: "text-sm leading-5",
      md: "text-base leading-6",
      lg: "text-lg leading-7",
      xl: "text-xl leading-8",
      "2xl": "text-2xl leading-8",
    },
    weight: {
      normal: "font-normal",
      medium: "font-medium",
      semibold: "font-semibold",
      bold: "font-bold",
    },
    // Colors follow the theme. `muted` is the softer color of secondary text.
    tone: {
      default: "text-fg",
      muted: "text-muted-fg",
      primary: "text-primary",
      success: "text-success",
      warning: "text-warning",
      danger: "text-danger",
    },
    align: {
      left: "text-left",
      center: "text-center",
      right: "text-right",
      justify: "text-justify",
    },
    // Line height, apart from the size.
    leading: {
      none: "leading-none",
      tight: "leading-tight",
      normal: "leading-normal",
      relaxed: "leading-relaxed",
      loose: "leading-loose",
    },
    // How lines break: `balance` evens out the lines of a short text, `pretty`
    // keeps a long one from ending on a lone word.
    wrap: {
      wrap: "text-wrap",
      balance: "text-balance",
      pretty: "text-pretty",
      nowrap: "text-nowrap",
    },
    font: {
      sans: "font-sans",
      mono: "font-mono",
    },
    decoration: {
      none: "no-underline",
      underline: "underline underline-offset-4",
      "line-through": "line-through",
    },
    isItalic: {
      true: "italic",
      false: "",
    },
    isUppercase: {
      true: "tracking-wider uppercase",
      false: "",
    },
  },
  defaultVariants: {
    size: "md",
    tone: "default",
    isItalic: false,
    isUppercase: false,
  },
});

export const textClamps = {
  1: "line-clamp-1",
  2: "line-clamp-2",
  3: "line-clamp-3",
  4: "line-clamp-4",
  5: "line-clamp-5",
  6: "line-clamp-6",
} as const;

2. Add the Text file

src/components/ui/text/text.tsxShow code
import type { ElementType, ReactNode } from "react";
import { cn } from "@/lib/cn";
import { textClamps, textStyles } from "@/types/text-styles";

export interface TextProps {
  children: ReactNode;
  /**
   * The element: `p` for a paragraph (the default), `span` for text inside a
   * line, `div` for a block, or one of `strong`, `em`, `small`, `label`, `li`
   * and `blockquote` when the text has that meaning.
   */
  as?:
    | "p"
    | "span"
    | "div"
    | "strong"
    | "em"
    | "small"
    | "label"
    | "li"
    | "blockquote";
  /** How big it is. Defaults to `md`, the size of body text. */
  size?: "xs" | "sm" | "md" | "lg" | "xl" | "2xl";
  weight?: "normal" | "medium" | "semibold" | "bold";
  /** The color: `muted` for secondary text, or a status color. Defaults to `default`. */
  tone?: "default" | "muted" | "primary" | "success" | "warning" | "danger";
  align?: "left" | "center" | "right" | "justify";
  /** The line height, when the one that comes with the size is not right. */
  leading?: "none" | "tight" | "normal" | "relaxed" | "loose";
  /**
   * How the lines break: `balance` evens out a short text, `pretty` keeps a
   * long one from ending on a lone word, `nowrap` keeps it on one line.
   */
  wrap?: "wrap" | "balance" | "pretty" | "nowrap";
  font?: "sans" | "mono";
  decoration?: "none" | "underline" | "line-through";
  isItalic?: boolean;
  /** Small capitals-style label: uppercase with wider spacing. */
  isUppercase?: boolean;
  /** Keep it to this many lines and end it with an ellipsis. `1` also stops it from wrapping. */
  lineClamp?: 1 | 2 | 3 | 4 | 5 | 6;
  /** Cut it off with an ellipsis at the end of the line instead of wrapping, when it is longer than its box. */
  isTruncated?: boolean;
  id?: string;
  className?: string;
}

/**
 * Text: a paragraph, a caption, a label, a line of secondary text. It sets the
 * size, weight, color, alignment and line breaks from the theme, so text looks
 * the same everywhere and does not need a class by hand each time. It has no
 * client code, so it works in Server Components.
 *
 * Use `Heading` for titles: it is a real heading in the outline of the page.
 *
 * @example
 * <Text>Your changes are saved automatically.</Text>
 * <Text size="sm" tone="muted">Last edited 3 minutes ago</Text>
 * <Text as="span" weight="medium" tone="danger">Required</Text>
 */
export function Text({
  children,
  as: Tag = "p",
  size,
  weight,
  tone,
  align,
  leading,
  wrap,
  font,
  decoration,
  isItalic,
  isUppercase,
  lineClamp,
  isTruncated,
  id,
  className,
}: TextProps) {
  const Component: ElementType = Tag;

  return (
    <Component
      id={id}
      className={cn(
        textStyles({
          size,
          weight,
          tone,
          align,
          leading,
          wrap,
          font,
          decoration,
          isItalic,
          isUppercase,
        }),
        isTruncated && "block truncate",
        lineClamp && textClamps[lineClamp],
        className,
      )}
    >
      {children}
    </Component>
  );
}

3. Use it

import { Text } from "@/components/ui/text/text";

Anatomy

One element, with its look in props.

<Text size="sm" tone="muted">
  Last edited 3 minutes ago
</Text>
PartRendered asNotes
Textp by defaultOne element that sets the size, weight, color, alignment and line breaks. Change it with as.

Examples

Usage

Your changes are saved automatically. You can close this page at any time.

Sizes

Each size comes with a line height that suits it. Medium is the size of body text.

Extra small

Small

Medium

Large

Extra large

2x large

Weight

Normal

Medium

Semibold

Bold

Tone

The color of the text. Muted is for secondary text, such as a hint or a date. The others follow the status colors of the theme.

Default

Muted

Primary

Success

Warning

Danger

Alignment

Left

Center

Right

centerUI is a set of components you copy into your project and own. Each one is built on react-aria for behavior and accessibility, styled with Tailwind, and follows your theme in light and dark.

Line height

Change the space between lines when the one that comes with the size is not right.

centerUI is a set of components you copy into your project and own. Each one is built on react-aria for behavior and accessibility, styled with Tailwind, and follows your theme in light and dark.

centerUI is a set of components you copy into your project and own. Each one is built on react-aria for behavior and accessibility, styled with Tailwind, and follows your theme in light and dark.

centerUI is a set of components you copy into your project and own. Each one is built on react-aria for behavior and accessibility, styled with Tailwind, and follows your theme in light and dark.

Clamp and truncate

lineClamp keeps the text to a number of lines and ends it with an ellipsis. isTruncated cuts a single line short instead of wrapping it.

centerUI is a set of components you copy into your project and own. Each one is built on react-aria for behavior and accessibility, styled with Tailwind, and follows your theme in light and dark.

centerUI is a set of components you copy into your project and own. Each one is built on react-aria for behavior and accessibility, styled with Tailwind, and follows your theme in light and dark.

Line breaks

Balance evens out the lines of a short text, and pretty keeps a long one from ending on a lone word.

A short text whose lines are evened out

centerUI is a set of components you copy into your project and own. Each one is built on react-aria for behavior and accessibility, styled with Tailwind, and follows your theme in light and dark.

Font and style

Monospace for code and values, italic, underline and strikethrough, and uppercase for a small label.

const answer = 42;

Italic

Underlined

Struck through

Overline

Elements

Text is a paragraph. Use as for text inside a line, or when the text has a meaning of its own.

Press Ctrl K to search, and save often.

Prices include tax.
“Simple is better than complex.”

Component API

Text takes the props below.

PropTypeDefault
childrenReactNode-
as"p" | "span" | "div" | "strong" | "em" | "small" | "label" | "li" | "blockquote""p"
size"xs" | "sm" | "md" | "lg" | "xl" | "2xl""md"
weight"normal" | "medium" | "semibold" | "bold"-
tone"default" | "muted" | "primary" | "success" | "warning" | "danger""default"
align"left" | "center" | "right" | "justify"-
leading"none" | "tight" | "normal" | "relaxed" | "loose"-
wrap"wrap" | "balance" | "pretty" | "nowrap"-
font"sans" | "mono"-
decoration"none" | "underline" | "line-through"-
isItalicbooleanfalse
isUppercasebooleanfalse
lineClamp1 | 2 | 3 | 4 | 5 | 6-
isTruncatedbooleanfalse
idstring-
classNamestring-