Text

Text on the system's type scale.

Text is the system’s type scale. It’s the only place that sets font size, weight, family, line height or tracking. Every other component styles its text with textVariants, the same scale as a class string. The sync check fails when a component, block or example sets a size or weight itself.

Preview
basic.tsx
import { Text, textVariants } from "~/components/ui/text"

export default function Example() {
  return (
    <article className="grid max-w-md gap-sm">
      <h2 className={textVariants({ size: "xl", weight: "semibold", family: "heading" })}>
        Weekend edition
      </h2>
      <Text size="sm" tone="muted">
        Twenty-four pages, out Saturday. The front page leads with the harbor plan; the{" "}
        <Text render={<strong />} weight="medium" tone="default">
          long read
        </Text>{" "}
        is on page 8.
      </Text>
      <Text size="xs" caps weight="medium" tone="muted">
        Print · Edition 412
      </Text>
    </article>
  )
}

It renders a <p>. Give it render for another element, like <span /> inside a sentence. For headings, put textVariants on the heading itself, so the heading keeps its content where accessibility checks can see it. Without size, text inherits the size around it.

Sizes

Each step sets a line height to match. control is for form fields: md on narrow screens, so iOS doesn’t zoom into a focused field, then sm.

Preview
scale.tsx
import { Inline } from "~/components/ui/inline"
import { Stack } from "~/components/ui/stack"
import { Text, textSizes } from "~/components/ui/text"

export default function Example() {
  return (
    <Stack gap="md" className="w-full">
      {textSizes.map((size) => (
        <Inline key={size} gap="xl" align="baseline" wrap={false}>
          <Text render={<span />} size="xs" family="mono" tone="muted" className="w-16 shrink-0">
            {size}
          </Text>
          <Text render={<span />} size={size} truncate className="min-w-0">
            The harbor plan, explained
          </Text>
        </Inline>
      ))}
    </Stack>
  )
}

Weights and families

Preview
styles.tsx
import { Text } from "~/components/ui/text"

const weights = ["normal", "medium", "semibold"] as const
const families = ["body", "heading", "mono"] as const

export default function Example() {
  return (
    <div className="grid w-full gap-xl sm:grid-cols-2">
      <div className="grid gap-sm">
        {weights.map((weight) => (
          <Text key={weight} size="lg" weight={weight}>
            {weight}
          </Text>
        ))}
      </div>
      <div className="grid gap-sm">
        {families.map((family) => (
          <Text key={family} size="lg" family={family}>
            {family}
          </Text>
        ))}
      </div>
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Mocha, read from the imports in its source.

UsesNothing else in the system.
Used byButtonprimitiveCardDialogFieldform itemInputform itemLabelform itemMenuPopoverSegmented Controlform itemSelectform itemSheetSliderform itemTabsTextareaform itemToastTooltip
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20tailwind-variants@4.1.0

Installation

Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/mocha/text.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 tailwind-variants@4.1.0

Then copy the source:

components/ui/text.tsx
import { renderElement, type RenderProp } from "@ui-sdk/react/primitive"
import type * as React from "react"
import { tv, type VariantProps } from "tailwind-variants"

import { cn } from "~/lib/utils"

/*
 * The system's type scale: the only place it sets font size, weight, line
 * height, family or tracking, and the text colors to pick from. Components style their text with
 * `textVariants`; pages and features use `<Text>`.
 */

/** The size names, smallest first, then `control`. */
const textSizes = ["xs", "sm", "md", "lg", "xl", "2xl", "3xl", "control"] as const

const sizes: Record<(typeof textSizes)[number], string> = {
  xs: "text-xs/4",
  sm: "text-sm/5",
  md: "text-base/6",
  lg: "text-lg/7",
  xl: "text-xl/7",
  "2xl": "text-2xl/8 tracking-tight",
  "3xl": "text-3xl/9 tracking-tight",
  /** Form controls: md on narrow screens, so iOS doesn't zoom into a focused field, then sm. */
  control: "text-base/6 md:text-sm/5",
}

const textVariants = tv({
  variants: {
    size: sizes,
    weight: {
      normal: "font-normal",
      medium: "font-medium",
      semibold: "font-semibold",
      bold: "font-bold",
    },
    family: {
      body: "font-sans",
      heading: "font-heading",
      mono: "font-mono",
    },
    /** Line height, when the size's own is wrong for the spot, like a one-line label. */
    leading: {
      none: "leading-none",
      tight: "leading-tight",
      snug: "leading-snug",
      relaxed: "leading-relaxed",
    },
    /** Small capitals for section titles: uppercase and spaced out. */
    caps: {
      true: "tracking-wider uppercase",
    },
    /** The text's color. Inherits it when unset. */
    tone: {
      default: "text-foreground",
      muted: "text-muted-foreground",
      primary: "text-primary",
      destructive: "text-destructive",
    },
    /** One line, cut off with an ellipsis. */
    truncate: {
      true: "truncate",
    },
    /** Figures of equal width, so numbers line up in columns and don't jump as they change. */
    tabular: {
      true: "tabular-nums",
    },
  },
})

type TextVariants = VariantProps<typeof textVariants>

type TextProps = Omit<React.ComponentProps<"p">, "children"> & {
  children?: React.ReactNode
  /** A step on the type scale. Inherits the size around it when unset. */
  size?: NonNullable<TextVariants["size"]>
  weight?: NonNullable<TextVariants["weight"]>
  /** The font. Inherits it when unset, which is usually the body font. */
  family?: NonNullable<TextVariants["family"]>
  leading?: NonNullable<TextVariants["leading"]>
  caps?: boolean
  tone?: NonNullable<TextVariants["tone"]>
  truncate?: boolean
  tabular?: boolean
  /** The element to render instead of a `<p>`, like `<h2 />` or `<span />`. */
  render?: RenderProp
}

/** Text on the system's type scale. A `<p>` unless `render` gives another element. */
function Text({
  size,
  weight,
  family,
  leading,
  caps,
  tone,
  truncate,
  tabular,
  render,
  className,
  ...props
}: TextProps) {
  return renderElement(
    "p",
    {
      "data-slot": "text",
      className: cn(
        textVariants({ size, weight, family, leading, caps, tone, truncate, tabular }),
        className,
      ),
      ...props,
    },
    render,
  )
}

export { Text, textSizes, textVariants }
export type { TextProps }

API reference

Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Text

Text on the system's type scale. A <p> unless render gives another element.

capsours

Type boolean

familyours

The font. Inherits it when unset, which is usually the body font.

Type NonNullable<"body" | "heading" | "mono">

leadingours

Type NonNullable<"none" | "tight" | "snug" | "relaxed">

renderours

The element to render instead of a <p>, like <h2 /> or <span />.

Type RenderProp

sizeours

A step on the type scale. Inherits the size around it when unset.

Type NonNullable<"xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "control">

tabularours

Type boolean

toneours

Type NonNullable<"default" | "muted" | "primary" | "destructive">

truncateours

Type boolean

weightours

Type NonNullable<"normal" | "medium" | "semibold" | "bold">

textSizes

The size names, smallest first, then control.

Takes the props of a native div.