# Text
Type styles for headings, body and inline text.
Source page: https://dinachi.dev/docs/components/text

## Installation
```bash
npx @dinachi/cli@latest add text
```
Adds to your project:
- `components/ui/text/text.tsx`
- `components/ui/text/index.ts`
Installs:
- `class-variance-authority` ^0.7.1

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

export function Intro() {
  return (
    <div>
      <Text variant="h2">Release notes</Text>
      <Text variant="muted">Updated every Tuesday.</Text>
    </div>
  )
}
```

## Examples

### Variants
Every type style, from headings to muted text.
```tsx
import { Text } from '@/components/ui/text';

export function Example() {
  return (
    <div className="space-y-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>
      <Text variant="p">Paragraph — the default variant with comfortable line height.</Text>
      <Text variant="large">Large text for emphasized body content.</Text>
      <Text variant="small">Small text for captions and metadata.</Text>
      <Text variant="lead">Lead text for introductory paragraphs.</Text>
      <Text variant="muted">Muted text for secondary information and descriptions.</Text>
      <Text variant="blockquote">The best way to predict the future is to invent it.</Text>
      <Text variant="code">console.log("hello world")</Text>
      <Text variant="span">Inline span text for use within other elements.</Text>
    </div>
  );
}
```

### Default
A plain paragraph.
```tsx
import { Text } from '@/components/ui/text';

export function Example() {
  return (
    <Text>
      The quick brown fox jumps over the lazy dog. This is a default paragraph
      rendered as a {'<p>'} element with comfortable leading.
    </Text>
  );
}
```

### Element override
The as prop changes the element and keeps the style.
```tsx
import { Text } from '@/components/ui/text';

export function Example() {
  return (
    <div className="space-y-4">
      <Text variant="h2" as="h3">
        Styled as h2, rendered as {'<h3>'}
      </Text>
      <Text variant="muted" as="span">
        Styled as muted, rendered as {'<span>'} instead of {'<p>'}
      </Text>
    </div>
  );
}
```

## Notes

### Rendered elements
- `h1` to `h4`: their heading.
- `blockquote`, `code`: that element.
- `span`: an unstyled `<span>`.
- `p`, `large`, `small`, `lead`, `muted`: a `<p>`. Use `as="span"` inside another paragraph.

### Heading level
Set the level from the outline and the variant from the design: `<Text variant="h2" as="h3">`.

### textVariants
Also exported. Call it to style your own element, such as a link or label.

## API reference

### Text
Text in a type style, on a matching element.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"h1" \| "h2" \| "h3" \| "h4" \| "p" \| "large" \| "small" \| "lead" \| "muted" \| "blockquote" \| "code" \| "span"` | `"p"` | The type style. Also picks the element unless `as` is set. |
| `as` | `ElementType` | - | Renders another element with the same style, such as an `h2` look on an `h3`. |
| `children` | `ReactNode` | - | The text. |

## Accessibility
- Each variant renders a semantic element; headings join the outline.
- Keep heading levels in order; use `as` when design and outline disagree.

## Source
`components/ui/text/text.tsx`
```tsx
"use client"

import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"

const textVariants = cva("", {
  variants: {
    variant: {
      h1: "scroll-m-20 text-4xl font-extrabold tracking-tight lg:text-5xl",
      h2: "scroll-m-20 text-3xl font-semibold tracking-tight",
      h3: "scroll-m-20 text-2xl font-semibold tracking-tight",
      h4: "scroll-m-20 text-xl font-semibold tracking-tight",
      p: "leading-7",
      large: "text-lg font-semibold",
      small: "text-sm font-medium leading-none",
      lead: "text-xl text-muted-foreground",
      muted: "text-sm text-muted-foreground",
      blockquote: "border-l-2 pl-6 italic",
      code: "relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-sm font-semibold",
      span: "",
    },
  },
  defaultVariants: {
    variant: "p",
  },
})

type Variant = NonNullable<VariantProps<typeof textVariants>["variant"]>

const variantElementMap: Record<Variant, React.ElementType> = {
  h1: "h1",
  h2: "h2",
  h3: "h3",
  h4: "h4",
  p: "p",
  large: "p",
  small: "p",
  lead: "p",
  muted: "p",
  blockquote: "blockquote",
  code: "code",
  span: "span",
}

export interface TextProps
  extends React.HTMLAttributes<HTMLElement>,
    VariantProps<typeof textVariants> {
  as?: React.ElementType
}

const Text = React.forwardRef<HTMLElement, TextProps>(
  ({ className, variant = "p", as, children, ...props }, ref) => {
    const Component = as ?? variantElementMap[variant!]

    return (
      <Component
        data-slot="text"
        ref={ref}
        className={cn(textVariants({ variant }), className)}
        {...props}
      >
        {children}
      </Component>
    )
  }
)

Text.displayName = "Text"

export { Text, textVariants }
```
