# Text

Styled text with typography variants for headings, body copy, quotes, code and captions.

Text is an open-source component from suiss/ui for React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.

Source: https://ui.suiss.app/docs/components/text

**React Native, SwiftUI, Jetpack Compose**

![Preview](https://ui.suiss.app/previews/components-text--default.light.webp)

```tsx
import { Text } from "@/components/ui/text"

export function TextExample() {
  return (
    <Text>The quick brown fox jumps over the lazy dog.</Text>
  )
}
```

```swift
import EsuissUI
import SwiftUI

struct TextExample: View {
  var body: some View {
    Text("The quick brown fox jumps over the lazy dog.")
      .textVariant(.default)
  }
}
```

```kotlin
import androidx.compose.runtime.Composable
import com.esuiss.ui.components.Text

@Composable
fun TextExample() {
    Text("The quick brown fox jumps over the lazy dog.")
}
```

`Text` renders a React Native `Text` with one of twelve typography variants: `default`, `h1`–`h4`, `p`, `blockquote`, `code`, `lead`, `large`, `small` and `muted`. Heading variants also set the `heading` role.

It reads `TextClassContext`, so components such as `Button` can pass a text color and size down to any `Text` inside them.

## Installation

**React Native**

```bash title="Terminal"
npx shadcn@latest add @esuiss-rn/text
```

See [Installation](https://ui.suiss.app/docs/installation) for the `components.json` setup. Or use the suiss/ui CLI:

```bash title="Terminal"
npx @esuiss/ui@latest add text
```

**SwiftUI**

```bash title="Terminal"
npx @esuiss/ui@latest add text --platform swift
```

**Jetpack Compose**

```bash title="Terminal"
npx @esuiss/ui@latest add text --platform kotlin
```

### Manual

**React Native**

**Uniwind**

1. Install the dependencies:

```bash title="Terminal"
npx expo install class-variance-authority cn
```

2. Add the pieces it builds on: the primitives `components/primitives/types.tsx`, `components/primitives/use-render.tsx`.

3. Copy the source into `components/ui/text.tsx`:

```tsx title="uniwind/components/ui/text.tsx"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import * as React from "react"
import { Text as RNText, type Role } from "react-native"
import type { RenderProp } from "@/components/primitives/types"
import { useRender } from "@/components/primitives/use-render"

const textVariants = cva("text-base text-foreground", {
  variants: {
    variant: {
      default: "",
      h1: "text-3xl font-bold",
      h2: "text-2xl font-bold",
      h3: "text-xl font-semibold",
      h4: "text-lg font-semibold",
      p: "mt-3",
      blockquote: "mt-4 border-s-2 border-border ps-3 italic",
      code: "rounded-sm bg-muted px-1 py-0.5 font-mono text-sm font-semibold",
      lead: "text-lg text-muted-foreground",
      large: "text-lg font-semibold",
      small: "text-sm font-medium",
      muted: "text-sm text-muted-foreground",
    },
  },
  defaultVariants: {
    variant: "default",
  },
})

type TextVariantProps = VariantProps<typeof textVariants>

type TextVariant = NonNullable<TextVariantProps["variant"]>

const ROLE: Partial<Record<TextVariant, Role>> = {
  h1: "heading",
  h2: "heading",
  h3: "heading",
  h4: "heading",
}

const TextClassContext = React.createContext<string | undefined>(undefined)

function Text({
  className,
  render,
  variant = "default",
  ...props
}: React.ComponentProps<typeof RNText> &
  React.RefAttributes<React.ComponentRef<typeof RNText>> &
  TextVariantProps & {
    render?: RenderProp<Record<string, unknown>, Record<string, never>>
  }) {
  const textClass = React.useContext(TextClassContext)
  return useRender({
    defaultComponent: RNText,
    render,
    props: [
      {
        className: cn(textVariants({ variant }), textClass, className),
        role: variant ? ROLE[variant] : undefined,
      },
      props,
    ],
  })
}

export { Text, TextClassContext }
```

4. Update the import paths to match your project.

**NativeWind**

1. Install the dependencies:

```bash title="Terminal"
npx expo install class-variance-authority cn
```

2. Add the pieces it builds on: the primitives `components/primitives/types.tsx`, `components/primitives/use-render.tsx`.

3. Copy the source into `components/ui/text.tsx`:

```tsx title="nativewind/components/ui/text.tsx"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import * as React from "react"
import { Text as RNText, type Role } from "react-native"
import type { RenderProp } from "@/components/primitives/types"
import { useRender } from "@/components/primitives/use-render"

const textVariants = cva("text-base text-foreground", {
  variants: {
    variant: {
      default: "",
      h1: "text-3xl font-bold",
      h2: "text-2xl font-bold",
      h3: "text-xl font-semibold",
      h4: "text-lg font-semibold",
      p: "mt-3",
      blockquote: "mt-4 border-s-2 border-border ps-3 italic",
      code: "rounded-sm bg-muted px-1 py-0.5 font-mono text-sm font-semibold",
      lead: "text-lg text-muted-foreground",
      large: "text-lg font-semibold",
      small: "text-sm font-medium",
      muted: "text-sm text-muted-foreground",
    },
  },
  defaultVariants: {
    variant: "default",
  },
})

type TextVariantProps = VariantProps<typeof textVariants>

type TextVariant = NonNullable<TextVariantProps["variant"]>

const ROLE: Partial<Record<TextVariant, Role>> = {
  h1: "heading",
  h2: "heading",
  h3: "heading",
  h4: "heading",
}

const TextClassContext = React.createContext<string | undefined>(undefined)

function Text({
  className,
  render,
  variant = "default",
  ...props
}: React.ComponentProps<typeof RNText> &
  React.RefAttributes<React.ComponentRef<typeof RNText>> &
  TextVariantProps & {
    render?: RenderProp<Record<string, unknown>, Record<string, never>>
  }) {
  const textClass = React.useContext(TextClassContext)
  return useRender({
    defaultComponent: RNText,
    render,
    props: [
      {
        className: cn(textVariants({ variant }), textClass, className),
        role: variant ? ROLE[variant] : undefined,
      },
      props,
    ],
  })
}

export { Text, TextClassContext }
```

4. Update the import paths to match your project.

**StyleSheet**

1. Add the pieces it builds on: the primitives `components/primitives/types.tsx`, `components/primitives/use-render.tsx`.

2. Copy the source into `components/ui/text.tsx`:

```tsx title="stylesheet/components/ui/text.tsx"
import * as React from "react"
import {
  Text as RNText,
  type Role,
  StyleSheet,
  type TextStyle,
} from "react-native"
import type { RenderProp } from "@/components/primitives/types"
import { useRender } from "@/components/primitives/use-render"
import {
  fontSize,
  radius,
  type ThemeColors,
  useTheme,
} from "@/components/ui/theme"

type TextVariant =
  | "default"
  | "h1"
  | "h2"
  | "h3"
  | "h4"
  | "p"
  | "blockquote"
  | "code"
  | "lead"
  | "large"
  | "small"
  | "muted"

const ROLE: Partial<Record<TextVariant, Role>> = {
  h1: "heading",
  h2: "heading",
  h3: "heading",
  h4: "heading",
}

const styles = StyleSheet.create({
  default: { ...fontSize.base },
  h1: { ...fontSize["3xl"], fontWeight: "700" },
  h2: { ...fontSize["2xl"], fontWeight: "700" },
  h3: { ...fontSize.xl, fontWeight: "600" },
  h4: { ...fontSize.lg, fontWeight: "600" },
  p: { ...fontSize.base, marginTop: 12 },
  blockquote: {
    ...fontSize.base,
    marginTop: 16,
    borderStartWidth: 2,
    paddingStart: 12,
    fontStyle: "italic",
  },
  code: {
    ...fontSize.sm,
    fontFamily: "Menlo",
    fontWeight: "600",
    borderRadius: radius.sm,
    paddingHorizontal: 4,
    paddingVertical: 2,
  },
  lead: { ...fontSize.lg },
  large: { ...fontSize.lg, fontWeight: "600" },
  small: { ...fontSize.sm, fontWeight: "500" },
  muted: { ...fontSize.sm },
})

function variantColors(variant: TextVariant, colors: ThemeColors): TextStyle {
  switch (variant) {
    case "blockquote":
      return { color: colors.foreground, borderColor: colors.border }
    case "code":
      return { color: colors.foreground, backgroundColor: colors.muted }
    case "lead":
    case "muted":
      return { color: colors.mutedForeground }
    default:
      return { color: colors.foreground }
  }
}

const TextStyleContext = React.createContext<TextStyle | undefined>(undefined)

function Text({
  style,
  render,
  variant = "default",
  ...props
}: React.ComponentProps<typeof RNText> &
  React.RefAttributes<React.ComponentRef<typeof RNText>> & {
    render?: RenderProp<Record<string, unknown>, Record<string, never>>
    variant?: TextVariant
  }) {
  const { colors } = useTheme()
  const inherited = React.useContext(TextStyleContext)
  return useRender({
    defaultComponent: RNText,
    render,
    props: [
      {
        style: [
          styles[variant],
          variantColors(variant, colors),
          inherited,
          style,
        ],
        role: ROLE[variant],
      },
      props,
    ],
  })
}

export type { TextVariant }
export { Text, TextStyleContext }
```

3. Update the import paths to match your project.

**SwiftUI**

1. Target iOS 17 or later.

2. Copy the theme files every view uses:

- `swift/Theme/Theme.swift`: [source](https://github.com/e-suiss/ui/blob/main/packages/mobile/swiftui/Sources/EsuissUI/Theme/Theme.swift)

- `swift/Theme/Tokens.swift`: [source](https://github.com/e-suiss/ui/blob/main/packages/mobile/swiftui/Sources/EsuissUI/Theme/Tokens.swift)

3. Copy the source:

- `swift/Components/TextVariant.swift`: [source](https://github.com/e-suiss/ui/blob/main/packages/mobile/swiftui/Sources/EsuissUI/Components/TextVariant.swift)

4. Add the files to your app target.

**Jetpack Compose**

1. Set `minSdk` to 24 or higher and add the dependencies:

```kotlin title="build.gradle.kts"
implementation(platform("androidx.compose:compose-bom:2026.09.00"))
implementation("androidx.compose.foundation:foundation")
implementation("androidx.compose.ui:ui")
```

2. Copy the theme files every view uses:

- `kotlin/theme/Theme.kt`: [source](https://github.com/e-suiss/ui/blob/main/packages/mobile/kotlin/ui/src/main/kotlin/com/esuiss/ui/theme/Theme.kt)

- `kotlin/theme/Tokens.kt`: [source](https://github.com/e-suiss/ui/blob/main/packages/mobile/kotlin/ui/src/main/kotlin/com/esuiss/ui/theme/Tokens.kt)

3. Copy the source and every file it uses:

```kt title="kotlin/components/Text.kt"
package com.esuiss.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.heading
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import com.esuiss.ui.theme.Colors
import com.esuiss.ui.theme.EsuissTheme
import com.esuiss.ui.theme.Radius
import com.esuiss.ui.theme.Typography

enum class TextVariant { Default, H1, H2, H3, H4, P, Blockquote, Code, Lead, Large, Small, Muted }

@Composable
fun Text(
    text: String,
    modifier: Modifier = Modifier,
    variant: TextVariant = TextVariant.Default,
    color: Color = Color.Unspecified,
    textAlign: TextAlign = TextAlign.Unspecified,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    minLines: Int = 1,
    style: TextStyle = TextStyle.Default,
) {
    Text(AnnotatedString(text), modifier, variant, color, textAlign, overflow, softWrap, maxLines, minLines, style)
}

@Composable
fun Text(
    text: AnnotatedString,
    modifier: Modifier = Modifier,
    variant: TextVariant = TextVariant.Default,
    color: Color = Color.Unspecified,
    textAlign: TextAlign = TextAlign.Unspecified,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    minLines: Int = 1,
    style: TextStyle = TextStyle.Default,
) {
    val colors = EsuissTheme.colors
    val inherited = if (variant == TextVariant.Default) LocalContentStyle.current else TextStyle.Default
    val resolved = Typography.base.copy(color = colors.foreground)
        .merge(textVariantStyle(variant, colors))
        .merge(inherited)
        .merge(style)
        .merge(TextStyle(color = color, textAlign = textAlign))
    BasicText(
        text = text,
        modifier = modifier.then(textVariantModifier(variant, colors)),
        style = resolved,
        overflow = overflow,
        softWrap = softWrap,
        maxLines = maxLines,
        minLines = minLines,
    )
}

private fun textVariantStyle(variant: TextVariant, colors: Colors): TextStyle = when (variant) {
    TextVariant.Default, TextVariant.P -> TextStyle.Default
    TextVariant.H1 -> Typography.xl3.copy(fontWeight = FontWeight.Bold)
    TextVariant.H2 -> Typography.xl2.copy(fontWeight = FontWeight.Bold)
    TextVariant.H3 -> Typography.xl.copy(fontWeight = FontWeight.SemiBold)
    TextVariant.H4 -> Typography.lg.copy(fontWeight = FontWeight.SemiBold)
    TextVariant.Blockquote -> TextStyle(fontStyle = FontStyle.Italic)
    TextVariant.Code -> Typography.sm.copy(fontFamily = FontFamily.Monospace, fontWeight = FontWeight.SemiBold)
    TextVariant.Lead -> Typography.lg.copy(color = colors.mutedForeground)
    TextVariant.Large -> Typography.lg.copy(fontWeight = FontWeight.SemiBold)
    TextVariant.Small -> Typography.sm.copy(fontWeight = FontWeight.Medium)
    TextVariant.Muted -> Typography.sm.copy(color = colors.mutedForeground)
}

private fun textVariantModifier(variant: TextVariant, colors: Colors): Modifier = when (variant) {
    TextVariant.H1, TextVariant.H2, TextVariant.H3, TextVariant.H4 -> Modifier.semantics { heading() }
    TextVariant.P -> Modifier.padding(top = 12.dp)
    TextVariant.Blockquote -> Modifier
        .padding(top = 16.dp)
        .drawBehind {
            val stroke = 2.dp.toPx()
            val x = if (layoutDirection == LayoutDirection.Rtl) size.width - stroke else 0f
            drawRect(colors.border, Offset(x, 0f), Size(stroke, size.height))
        }
        .padding(start = 14.dp)
    TextVariant.Code -> Modifier
        .clip(RoundedCornerShape(Radius.sm))
        .background(colors.muted)
        .padding(horizontal = 4.dp, vertical = 2.dp)
    else -> Modifier
}
```

- `kotlin/components/Content.kt`: [source](https://github.com/e-suiss/ui/blob/main/packages/mobile/kotlin/ui/src/main/kotlin/com/esuiss/ui/components/Content.kt)

4. Update the `com.esuiss.ui` package names to match your project.

## Usage

```tsx
import { Text } from "@/components/ui/text"
```

```tsx
<Text>The quick brown fox jumps over the lazy dog.</Text>
```

Pick a `variant` for the role of the text, and add classes through `className` for one-off changes.

Use the `render` prop to render a different component with the same classes. `TextClassContext` is exported so your own components can style nested `Text` the same way.

## Examples

### Typography

Every variant in a sample article.

![Preview](https://ui.suiss.app/previews/components-text--typography.light.webp)

```tsx
import { View } from "react-native"
import { Text } from "@/components/ui/text"

export function TextTypographyExample() {
  return (
    <View className="gap-4">
      <Text variant="h1">Taxing Laughter</Text>
      <Text variant="h2">The People of the Kingdom</Text>
      <Text variant="h3">The Joke Tax</Text>
      <Text variant="h4">People stopped telling jokes</Text>
      <Text variant="p">
        The king, seeing how much happier his subjects were, realized the error
        of his ways and repealed the joke tax.
      </Text>
      <Text variant="blockquote">
        &quot;After all,&quot; he said, &quot;everyone enjoys a good joke, so
        it&apos;s only fair that they should pay for the privilege.&quot;
      </Text>
      <View className="flex-row">
        <Text variant="code">npx @esuiss/ui add text</Text>
      </View>
      <Text variant="lead">
        A modal dialog that interrupts the user with important content.
      </Text>
      <Text variant="large">Are you absolutely sure?</Text>
      <Text variant="small">Email address</Text>
      <Text variant="muted">Enter your email address.</Text>
    </View>
  )
}
```

```swift
import EsuissUI
import SwiftUI

struct TextTypographyExample: View {
  var body: some View {
    VStack(alignment: .leading, spacing: 16) {
      Text("Taxing Laughter").textVariant(.h1)
      Text("The People of the Kingdom").textVariant(.h2)
      Text("The Joke Tax").textVariant(.h3)
      Text("People stopped telling jokes").textVariant(.h4)
      Text("The king, seeing how much happier his subjects were, realized the error of his ways and repealed the joke tax.")
        .textVariant(.p)
      Text("\"After all,\" he said, \"everyone enjoys a good joke, so it's only fair that they should pay for the privilege.\"")
        .textVariant(.blockquote)
      Text(verbatim: "npx @esuiss/ui add text").textVariant(.code)
      Text("A modal dialog that interrupts the user with important content.").textVariant(.lead)
      Text("Are you absolutely sure?").textVariant(.large)
      Text("Email address").textVariant(.small)
      Text("Enter your email address.").textVariant(.muted)
    }
    .frame(maxWidth: .infinity, alignment: .leading)
  }
}
```

```kotlin
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.dp
import com.esuiss.ui.components.Text
import com.esuiss.ui.components.TextVariant

@Composable
fun TextTypography() {
    Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
        Text("Taxing Laughter", variant = TextVariant.H1)
        Text("The People of the Kingdom", variant = TextVariant.H2)
        Text("The Joke Tax", variant = TextVariant.H3)
        Text("People stopped telling jokes", variant = TextVariant.H4)
        Text(
            "The king, seeing how much happier his subjects were, realized the error of his ways and repealed the joke tax.",
            variant = TextVariant.P,
        )
        Text(
            "\"After all,\" he said, \"everyone enjoys a good joke, so it's only fair that they should pay for the privilege.\"",
            variant = TextVariant.Blockquote,
        )
        Row {
            Text("npx @esuiss/ui add text", variant = TextVariant.Code)
        }
        Text("A modal dialog that interrupts the user with important content.", variant = TextVariant.Lead)
        Text("Are you absolutely sure?", variant = TextVariant.Large)
        Text("Email address", variant = TextVariant.Small)
        Text("Enter your email address.", variant = TextVariant.Muted)
    }
}
```

### Headings

The four heading levels.

![Preview](https://ui.suiss.app/previews/components-text--headings.light.webp)

```tsx
import { View } from "react-native"
import { Text } from "@/components/ui/text"

export function TextHeadingsExample() {
  return (
    <View className="gap-4">
      <Text variant="h1">Heading 1</Text>
      <Text variant="h2">Heading 2</Text>
      <Text variant="h3">Heading 3</Text>
      <Text variant="h4">Heading 4</Text>
    </View>
  )
}
```

```swift
import EsuissUI
import SwiftUI

struct TextHeadingsExample: View {
  var body: some View {
    VStack(alignment: .leading, spacing: 16) {
      Text("Heading 1").textVariant(.h1)
      Text("Heading 2").textVariant(.h2)
      Text("Heading 3").textVariant(.h3)
      Text("Heading 4").textVariant(.h4)
    }
    .frame(maxWidth: .infinity, alignment: .leading)
  }
}
```

```kotlin
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.dp
import com.esuiss.ui.components.Text
import com.esuiss.ui.components.TextVariant

@Composable
fun TextHeadings() {
    Column(verticalArrangement = Arrangement.spacedBy(16.dp)) {
        Text("Heading 1", variant = TextVariant.H1)
        Text("Heading 2", variant = TextVariant.H2)
        Text("Heading 3", variant = TextVariant.H3)
        Text("Heading 4", variant = TextVariant.H4)
    }
}
```

## API Reference

### Text

The text element with typography variants.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `RenderProp<Record<string, unknown>, Record<string, never>>` | - | Renders another component in place of the default `Text`, with the same props. |
| `variant` | `"default" \| "h1" \| "h2" \| "h3" \| "h4" \| "p" \| "blockquote" \| "code" \| "lead" \| "large" \| "small" \| "muted"` | `"default"` | Typography style. |

Also accepts the props of `React.ComponentProps<typeof RNText>`, `React.RefAttributes<typeof RNText>`, `TextVariantProps`.

## Accessibility

- `h1` to `h4` set `role="heading"`, so VoiceOver and TalkBack can navigate by heading.
- Text scales with the system font size.

## Notes

- Colors use the `text-foreground` and `text-muted-foreground` tokens; `code` uses `bg-muted` and `blockquote` a `border-border` start border.
- Light and dark colors follow the system appearance.

