# Typography

## Introduction

The Typography component is a core component for displaying text. It is based on Joy UI Typography and provides a variety of text levels and styles. You can apply consistent typography across all text elements such as headings, body text, and labels.

```tsx
<Typography children="Typography" />
```

| Field | Description | Default |
| ----- | ----------- | ------- |
| level | —           | —       |
| color | —           | —       |

## Usage

```tsx
import { Typography } from '@ceed/cds';

function MyComponent() {
  return (
    <div>
      <Typography level="h1">Main Heading</Typography>
      <Typography level="body-md">This is body text.</Typography>
      <Typography level="body-sm" color="neutral">
        This is supporting description text.
      </Typography>
    </div>
  );
}
```

## Typography Levels

### Overview

Typography levels are divided into three groups: **Headings**, **Titles**, and **Body**. The default level is `body-md`.

- **Headings** (`h1`–`h4`): Used for page and section headings. Uses the display font family (Inter).
- **Titles** (`title-lg`, `title-md`, `title-sm`): Used for UI labels, card titles, and emphasized text. Uses the body font family (Inter).
- **Body** (`body-lg`, `body-md`, `body-sm`, `body-xs`): Used for readable content and descriptions. Uses the body font family (Inter).

```tsx
<>
    <Stack direction="row" spacing={2}>
      <Typography level="h1">h1</Typography>
      <Typography {...args} level="h1" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="h2">h2</Typography>
      <Typography {...args} level="h2" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="h3">h3</Typography>
      <Typography {...args} level="h3" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="h4">h4</Typography>
      <Typography {...args} level="h4" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="title-lg">title-lg</Typography>
      <Typography {...args} level="title-lg" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="title-md">title-md</Typography>
      <Typography {...args} level="title-md" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="title-sm">title-sm</Typography>
      <Typography {...args} level="title-sm" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="body-lg">body-lg</Typography>
      <Typography {...args} level="body-lg" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="body-md">body-md</Typography>
      <Typography {...args} level="body-md" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="body-sm">body-sm</Typography>
      <Typography {...args} level="body-sm" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="body-xs">body-xs</Typography>
      <Typography {...args} level="body-xs" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="marketing-lg">marketing-lg</Typography>
      <Typography {...args} level="marketing-lg" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="marketing-md">marketing-md</Typography>
      <Typography {...args} level="marketing-md" />
    </Stack>
    <Stack direction="row" spacing={2}>
      <Typography level="marketing-sm">marketing-sm</Typography>
      <Typography {...args} level="marketing-sm" />
    </Stack>
  </>
```

### Style Reference

| Level    | Font Family     | Font Size       | Font Weight    | Line Height | Letter Spacing | Default Color  | HTML Tag |
| -------- | --------------- | --------------- | -------------- | ----------- | -------------- | -------------- | -------- |
| h1       | display (Inter) | 2.25rem (36px)  | 700 (Bold)     | 1.333       | -0.025em       | text.primary   | `<h1>`   |
| h2       | display (Inter) | 1.875rem (30px) | 700 (Bold)     | 1.333       | -0.025em       | text.primary   | `<h2>`   |
| h3       | display (Inter) | 1.5rem (24px)   | 600 (SemiBold) | 1.333       | -0.025em       | text.primary   | `<h3>`   |
| h4       | display (Inter) | 1.25rem (20px)  | 600 (SemiBold) | 1.5         | -0.025em       | text.primary   | `<h4>`   |
| title-lg | body (Inter)    | 1.125rem (18px) | 600 (SemiBold) | 1.333       | -              | text.primary   | `<p>`    |
| title-md | body (Inter)    | 1rem (16px)     | 500 (Medium)   | 1.5         | -              | text.primary   | `<p>`    |
| title-sm | body (Inter)    | 0.875rem (14px) | 500 (Medium)   | 1.429       | -              | text.primary   | `<p>`    |
| body-lg  | body (Inter)    | 1.125rem (18px) | inherit        | 1.5         | -              | text.secondary | `<p>`    |
| body-md  | body (Inter)    | 1rem (16px)     | inherit        | 1.5         | -              | text.secondary | `<p>`    |
| body-sm  | body (Inter)    | 0.875rem (14px) | inherit        | 1.5         | -              | text.tertiary  | `<p>`    |
| body-xs  | body (Inter)    | 0.75rem (12px)  | 500 (Medium)   | 1.5         | -              | text.tertiary  | `<span>` |

### Headings

Heading levels define page structure and hierarchy. They use the display font family with heavier weights for visual prominence.

```tsx
<>
    <Typography level="h1">h1 - Main page heading</Typography>
    <Typography level="h2">h2 - Section heading</Typography>
    <Typography level="h3">h3 - Subsection heading</Typography>
    <Typography level="h4">h4 - Detail heading</Typography>
  </>
```

```tsx
<Typography level="h1">H1 - Main page heading</Typography>
<Typography level="h2">H2 - Section heading</Typography>
<Typography level="h3">H3 - Subsection heading</Typography>
<Typography level="h4">H4 - Detail heading</Typography>
```

### Titles

Title levels are used for UI labels, card titles, and other emphasized text that is not a document heading.

```tsx
<>
    <Typography level="title-lg">title-lg - Large title</Typography>
    <Typography level="title-md">title-md - Medium title</Typography>
    <Typography level="title-sm">title-sm - Small title</Typography>
  </>
```

```tsx
<Typography level="title-lg">Large Title</Typography>
<Typography level="title-md">Medium Title</Typography>
<Typography level="title-sm">Small Title</Typography>
```

### Body Text

Body levels are used for readable content and descriptions. The default level `body-md` is suitable for most body text. Use `body-xs` sparingly for auxiliary information like timestamps or counters.

```tsx
<>
    <Typography level="body-lg">body-lg - Large body text</Typography>
    <Typography level="body-md">body-md - Regular body text</Typography>
    <Typography level="body-sm">body-sm - Small body text</Typography>
    <Typography level="body-xs">body-xs - Extra small text</Typography>
  </>
```

```tsx
<Typography level="body-lg">Large body text</Typography>
<Typography level="body-md">Regular body text</Typography>
<Typography level="body-sm">Small body text</Typography>
<Typography level="body-xs">Extra small text</Typography>
```

## Design Token Reference

### Font Size Scale

| Token | Value           |
| ----- | --------------- |
| xs    | 0.75rem (12px)  |
| sm    | 0.875rem (14px) |
| md    | 1rem (16px)     |
| lg    | 1.125rem (18px) |
| xl    | 1.25rem (20px)  |
| xl2   | 1.5rem (24px)   |
| xl3   | 1.875rem (30px) |
| xl4   | 2.25rem (36px)  |

### Font Weight Scale

| Token | Value          |
| ----- | -------------- |
| sm    | 300 (Light)    |
| md    | 500 (Medium)   |
| lg    | 600 (SemiBold) |
| xl    | 700 (Bold)     |

### Line Height Scale

| Token | Value |
| ----- | ----- |
| xs    | 1.333 |
| sm    | 1.429 |
| md    | 1.5   |
| lg    | 1.556 |
| xl    | 1.667 |

### Font Families

- **body**: `"Inter"`, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif
- **display**: `"Inter"`, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif
- **code**: `"Source Code Pro"`, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace

## Colors

Typography supports five semantic colors via the `color` prop. Use `textColor` to apply any palette color directly.

```tsx
<>
    <Typography color="primary">Primary color</Typography>
    <Typography color="neutral">Neutral color</Typography>
    <Typography color="danger">Danger color</Typography>
    <Typography color="success">Success color</Typography>
    <Typography color="warning">Warning color</Typography>
  </>
```

```tsx
<Typography color="primary">Primary color</Typography>
<Typography color="neutral">Neutral color</Typography>
<Typography color="danger">Danger color</Typography>
<Typography color="success">Success color</Typography>
<Typography color="warning">Warning color</Typography>

{/* Use textColor for specific palette values */}
<Typography textColor="neutral.600">Custom palette color</Typography>
```

## Component Prop

Use the `component` prop to render Typography as a different HTML element or React component. Each level has a default HTML tag mapping shown below.

```tsx
<>
    <Typography level="h1" component="h2">
      h1 style rendered as h2 tag
    </Typography>
    <Typography level="body-md" component="span">
      body-md rendered as span
    </Typography>
    <Typography level="title-md" component="label">
      title-md rendered as label
    </Typography>
  </>
```

```tsx
<Typography level="h1" component="h2">
  h1 style rendered as an h2 tag
</Typography>

<Typography level="body-md" component="span">
  Inline text
</Typography>

<Typography level="title-md" component="label">
  Rendered as a label element
</Typography>
```

**Default HTML tag mapping:**

| Level    | Default HTML Tag |
| -------- | ---------------- |
| h1       | `<h1>`           |
| h2       | `<h2>`           |
| h3       | `<h3>`           |
| h4       | `<h4>`           |
| title-lg | `<p>`            |
| title-md | `<p>`            |
| title-sm | `<p>`            |
| body-lg  | `<p>`            |
| body-md  | `<p>`            |
| body-sm  | `<p>`            |
| body-xs  | `<span>`         |

## Decorators

Use `startDecorator` and `endDecorator` to add icons or other elements before or after the text.

```tsx
<>
    <Typography startDecorator={<InfoOutlined />}>Text with start decorator</Typography>
    <Typography endDecorator={<ArrowForward />}>Text with end decorator</Typography>
    <Typography startDecorator={<InfoOutlined />} endDecorator={<ArrowForward />}>
      Text with both decorators
    </Typography>
  </>
```

```tsx
import InfoOutlined from '@mui/icons-material/InfoOutlined';
import ArrowForward from '@mui/icons-material/ArrowForward';

<Typography startDecorator={<InfoOutlined />}>
  Text with start decorator
</Typography>

<Typography endDecorator={<ArrowForward />}>
  Text with end decorator
</Typography>

<Typography startDecorator={<InfoOutlined />} endDecorator={<ArrowForward />}>
  Text with both decorators
</Typography>
```

## Text Overflow (noWrap)

Use the `noWrap` prop to truncate overflowing text with an ellipsis. Pair it with a `maxWidth` to constrain the text width.

```tsx
<Typography
    noWrap
    sx={{
      maxWidth: 200
    }}
  >
    This is a long text that will be truncated with an ellipsis when it exceeds the maximum width of
    the container.
  </Typography>
```

```tsx
<Typography noWrap sx={{ maxWidth: 200 }}>
  This is a long text that will be truncated with an ellipsis when it exceeds the maximum width.
</Typography>
```

## Responsive Typography

You can pass a responsive object to the `level` prop, or use `sx` to set responsive font sizes.

```tsx
{
  /* Responsive level */
}
<Typography level={{ xs: 'h3', sm: 'h2', md: 'h1' }}>Responsive Heading</Typography>;

{
  /* Responsive font size via sx */
}
<Typography
  sx={{
    fontSize: { xs: '1rem', sm: '1.25rem', md: '1.5rem' },
  }}
>
  Responsive text
</Typography>;
```

## Common Use Cases

### Page Structure

```tsx
function ArticlePage() {
  return (
    <article>
      <Typography level="h1" sx={{ mb: 2 }}>
        Article Title
      </Typography>

      <Typography level="body-sm" color="neutral" sx={{ mb: 3 }}>
        January 15, 2024 · Written by John Doe
      </Typography>

      <Typography level="body-md" sx={{ mb: 2 }}>
        The main content of the article goes here. This text uses a readable size and line spacing.
      </Typography>

      <Typography level="h2" sx={{ mt: 4, mb: 2 }}>
        Section Title
      </Typography>

      <Typography level="body-md">The section content continues here.</Typography>
    </article>
  );
}
```

### Card Content

```tsx
<Card>
  <CardContent>
    <Typography level="title-md" sx={{ mb: 1 }}>
      Product Name
    </Typography>

    <Typography level="body-sm" color="neutral" sx={{ mb: 2 }}>
      Category: Electronics
    </Typography>

    <Typography level="body-md" sx={{ mb: 2 }}>
      A detailed description of the product goes here.
    </Typography>

    <Typography level="title-lg" color="primary">
      ₩199,000
    </Typography>
  </CardContent>
</Card>
```

### Form Labels and Help Text

```tsx
<Stack spacing={2}>
  <FormControl>
    <Typography level="title-sm" component="label">
      Username
    </Typography>
    <Input placeholder="Enter your name" />
    <Typography level="body-xs" color="neutral">
      Please enter your real name.
    </Typography>
  </FormControl>

  <FormControl error>
    <Typography level="title-sm" component="label">
      Email Address
    </Typography>
    <Input placeholder="email@example.com" />
    <Typography level="body-xs" color="danger">
      The email format is invalid.
    </Typography>
  </FormControl>
</Stack>
```

> 💡 **Tip: Use built-in form props instead**
>
> Input-family components such as Input, Select, Textarea, and DatePicker support `label` and `helperText` props natively.
> When building forms, it is recommended to use each component's built-in props rather than creating labels and helper text manually with Typography.
>
> ```tsx
> // ✅ Recommended: use the component's built-in props
> <Input label="Username" helperText="Please enter your real name." />
>
> // ❌ Not recommended: manually composing with Typography
> <FormControl>
>   <Typography level="title-sm" component="label">Username</Typography>
>   <Input placeholder="Enter your name" />
>   <Typography level="body-xs" color="neutral">Please enter your real name.</Typography>
> </FormControl>
> ```

### Status and Indicators

```tsx
<Stack spacing={2}>
  <Box>
    <Typography level="body-md">Server Status:</Typography>
    <Typography level="body-md" color="success">
      Operating Normally
    </Typography>
  </Box>

  <Box>
    <Typography level="body-md">Last Updated:</Typography>
    <Typography level="body-sm" color="neutral">
      2 minutes ago
    </Typography>
  </Box>
</Stack>
```

### List Content

```tsx
<Stack spacing={1}>
  <Typography level="title-md">Todo List</Typography>

  {todoItems.map((item) => (
    <Box key={item.id} sx={{ pl: 2 }}>
      <Typography
        level="body-md"
        sx={{
          textDecoration: item.completed ? 'line-through' : 'none',
          color: item.completed ? 'neutral.500' : 'text.primary',
        }}
      >
        {item.text}
      </Typography>
      <Typography level="body-xs" color="neutral">
        {item.dueDate}
      </Typography>
    </Box>
  ))}
</Stack>
```

## Props and Customization

### Key Props

| Prop             | Type                                                                                                                         | Default     | Description                                       |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------- | ----------- | ------------------------------------------------- |
| `children`       | `ReactNode`                                                                                                                  | -           | Text content                                      |
| `level`          | `'h1' \| 'h2' \| 'h3' \| 'h4' \| 'title-lg' \| 'title-md' \| 'title-sm' \| 'body-lg' \| 'body-md' \| 'body-sm' \| 'body-xs'` | `'body-md'` | Typography level (maps to semantic HTML elements) |
| `component`      | `ElementType`                                                                                                                | auto        | Override the root element type                    |
| `color`          | `'primary' \| 'neutral' \| 'danger' \| 'success' \| 'warning'`                                                               | `'neutral'` | Color scheme                                      |
| `variant`        | `'solid' \| 'soft' \| 'outlined' \| 'plain'`                                                                                 | -           | Visual style (optional)                           |
| `noWrap`         | `boolean`                                                                                                                    | `false`     | Truncate text with ellipsis                       |
| `startDecorator` | `ReactNode`                                                                                                                  | -           | Content before the text                           |
| `endDecorator`   | `ReactNode`                                                                                                                  | -           | Content after the text                            |
| `textColor`      | `string`                                                                                                                     | -           | Direct text color override                        |
| `sx`             | `SxProps`                                                                                                                    | -           | Custom styles                                     |

> **Note**: Typography also accepts all Joy UI Typography props.

## Best Practices

1. **Semantic Structure**: Use heading levels in order (`h1` → `h2` → `h3`) to create a clear document hierarchy.

```tsx
// ✅ Correct order
<Typography level="h1">Main heading</Typography>
<Typography level="h2">Section heading</Typography>
<Typography level="h3">Subsection</Typography>

// ❌ Incorrect order — skipping h2
<Typography level="h1">Main heading</Typography>
<Typography level="h3">Section heading</Typography>
```

2. **Consistency**: Use the same level for text serving the same purpose across the application.

3. **Readability**: Provide proper line spacing and paragraph separation for body text.

4. **Color Contrast**: Maintain sufficient color contrast to ensure accessibility.

5. **Level Selection Guide**:
   - **Headings** (`h1`–`h4`): Use for document structure and section titles.
   - **Titles** (`title-lg/md/sm`): Use for UI labels, card headers, and emphasized text.
   - **Body** (`body-lg/md/sm/xs`): Use for content, descriptions, and supporting text.

6. **Avoid inline style overrides**: Prefer the `level` prop over `sx` for font sizing. Use `level` for consistent design tokens.

7. **Use `textColor` for custom colors**: When the five semantic colors are insufficient, use `textColor` to apply any palette value directly.

8. **Use `body-xs` sparingly**: Reserve `body-xs` (12px) for auxiliary information such as timestamps, counters, and footnotes — not for primary content.

## Accessibility

- Use sequential heading levels (`h1` → `h2` → `h3`). Do not skip levels.
- Use the `component` prop to separate visual style from semantic meaning (e.g., `level="h1" component="h2"` when you need h1 styling but h2 semantics).
- Avoid using `body-xs` (12px) for essential content — the small size may be difficult to read.
- Maintain sufficient color contrast ratios (WCAG AA: 4.5:1 for normal text, 3:1 for large text).
