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
Not in Shell yet
Shell doesn't have this example, or every component it uses.

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 { View } from "nsh"

import { Text, textSizes } from "~/components/ui/text"

export default function Example() {
  return (
    <View class="w-full gap-(--space-md)">
      <Each of={textSizes}>
        {(size) => (
          <View class="flex-row items-baseline gap-(--space-xl)">
            <Text size="xs" tone="muted" class="w-16 shrink-0">
              {size()}
            </Text>
            <Text size={size()} truncate class="min-w-0 flex-1">
              The harbor plan, explained
            </Text>
          </View>
        )}
      </Each>
    </View>
  )
}

Weights and families

Preview
Not in Shell yet
Shell doesn't have this example, or every component it uses.

Dependencies

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

UsesNothing else in the system.
Used byButtonprimitive
Packages None.

Installation

Set the project up for Shell once first, as Getting started shows. Then copy the source:

src/components/ui/text.tsx
import { Text as NativeText, type JSX } from "nsh"

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

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

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

type TextSize = (typeof textSizes)[number]
type TextWeight = "normal" | "medium" | "semibold"
type TextTone = "default" | "muted" | "primary" | "destructive" | "warning" | "success"

const sizes: Record<TextSize, string> = {
  xs: "text-xs/4",
  sm: "text-sm/5",
  md: "text-base/6",
  lg: "text-lg/7 tracking-tight",
  xl: "text-xl/7 tracking-tight",
  "2xl": "text-2xl/8 tracking-tight",
  "3xl": "text-3xl/9 tracking-tight",
}

const weights: Record<TextWeight, string> = {
  normal: "font-normal",
  medium: "font-medium",
  semibold: "font-semibold",
}

const tones: Record<TextTone, string> = {
  default: "text-foreground",
  muted: "text-muted-foreground",
  primary: "text-primary",
  destructive: "text-destructive",
  warning: "text-warning",
  success: "text-success",
}

type TextVariants = {
  /** A step on the type scale. */
  size?: TextSize
  weight?: TextWeight
  /** The text's color. */
  tone?: TextTone
  /** One line, cut off with an ellipsis. */
  truncate?: boolean
}

/** The classes for a size, weight, tone and truncation, for components that draw their own text. */
function textVariants({ size, weight, tone, truncate = false }: TextVariants = {}): string {
  return cn(
    size !== undefined && sizes[size],
    weight !== undefined && weights[weight],
    tone !== undefined && tones[tone],
    truncate && "truncate",
  )
}

type TextProps = TextVariants & {
  /** Classes for layout, after the type scale's. */
  class?: string
  children?: JSX.Element
}

/** Text on the type scale: a size, weight and tone, the body size and the foreground by default. */
function Text({
  size = "md",
  weight,
  tone = "default",
  truncate,
  class: className,
  children,
}: TextProps) {
  return (
    <NativeText class={cn(textVariants({ size, weight, tone, truncate }), className)}>
      {children}
    </NativeText>
  )
}

export { Text, textSizes, textVariants }
export type { TextProps, TextSize, TextTone, TextVariants, TextWeight }

API reference

Generated from Shell's types. Props marked ours are added here; the rest come from the nsh/base part it wraps. Every part also takes nsh's view props, like class, style and testID.

Text

Text on the type scale: a size, weight and tone, the body size and the foreground by default.

classours

Classes for layout, after the type scale's.

Type string

sizeours

A step on the type scale.

Type "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" · Default md

toneours

The text's color.

Type TextTone · Default default

truncateours

One line, cut off with an ellipsis.

Type boolean

weightours

Type TextWeight

textSizes

The size names, smallest first.

Takes the props of a native div.

textVariants

The classes for a size, weight, tone and truncation, for components that draw their own text.

sizeours

A step on the type scale.

Type "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl"

toneours

The text's color.

Type TextTone

truncateours

One line, cut off with an ellipsis.

Type boolean · Default false

weightours

Type TextWeight