# Textarea

A multi-line field that grows with its content between a minimum and a cap.

Live variants: https://design.arcadia.is/components/textarea

```tsx
import { Textarea } from "@arcadia/ui";
```

## When to use

- Text of many lines that grows to its cap, then scrolls
- A conversation's composer, in the `ghost` form at `lg`, in Headline
- A heading or a name renamed where it stands, in the `text` variant

## When not to use

- One line of text: use `Input`

## In context

Add lines to the notes. The field grows to six lines, then scrolls.

```tsx
import { Field, Textarea } from "@arcadia/ui";

<div {...sx("cmp-form", cmp.form)}>
  <Field label="Release notes">
    <Textarea minRows={2} maxRows={6} defaultValue={"Add keyboard shortcuts for the file picker.\nKeep focus on the trigger when it closes.\nRemember the last folder."} />
  </Field>
</div>
```

## Usage

```tsx
import { Textarea } from "@arcadia/ui";

<Textarea placeholder="What should change?" minRows={3} maxRows={8} aria-label="Instructions" />
```

It grows from `minRows` (3 by default) to a cap, `maxRows` or `maxHeight` (which wins), and scrolls past it: CSS `field-sizing: content`, with a measured fallback. Its `size` is the text style it stands in, any of the six: `sm` Caption, `md` Body, `lg` Headline (conversation text, as in the composer), `xl` Title 3, `2xl` Title 2 and `3xl` Title 1; `weight` takes the style's bold, and a title is bold already. Focus shows through the border, or through a ring in the borderless `plain` and `text` forms.

## Examples

### Source text

Use `variant="plain"` for a field without a fill, border or padding. Add `mono` for source text. Enter adds a line.

```tsx
import { Textarea } from "@arcadia/ui";

<Textarea variant="plain" mono aria-label="Markdown source" defaultValue={"# Run notes\n\nCheck the build output."} maxRows={6} />
```

### In place

`variant="text"` edits a heading where it stands: no frame and no padding, the heading's own style and metrics, so nothing moves when it takes the heading's place, and it keeps on Enter (`onKeep`) and discards on Escape (`onDiscard`). Press the heading, or Tab to it and press Enter.

```tsx
import { useRef, useState } from "react";
import { Button, Textarea } from "@arcadia/ui";

function InPlace() {
  const [name, setName] = useState("Launch plan");
  const [editing, setEditing] = useState(false);
  const opener = useRef<HTMLButtonElement>(null);
  const keep = (typed: string) => {
    const next = typed.replace(/\s+/g, " ").trim();
    if (next) setName(next);
    setEditing(false);
  };
  const back = () => requestAnimationFrame(() => opener.current?.focus());
  return (
    <div {...stylex.props(widths.column)}>
      {editing ? (
        <Textarea
          variant="text"
          size="xl"
          aria-label="Board name"
          defaultValue={name}
          maxLength={80}
          onKeep={(typed) => { keep(typed); back(); }}
          onDiscard={() => { setEditing(false); back(); }}
          onBlur={(e) => keep(e.currentTarget.value)}
        />
      ) : (
        <h2 {...stylex.props(heads.heading)}>
          <Button ref={opener} variant="bare" aria-description="Rename" onClick={() => setEditing(true)} rootStyle={heads.opener}>
            {name}
          </Button>
        </h2>
      )}
    </div>
  );
}
```

### On its container's edge

`variant="edge"` keeps the field's frame but stands it out by its padding and its frame on every side, so its words stay where the text it edits stood at rest: here a card's description, then the same words being edited, their first letters on one vertical and their first line on one height.

```tsx
import { Card, CardBody, Textarea } from "@arcadia/ui";

function OnTheEdge() {
  const words = "Move the nightly build to the new runner, and keep the cache warm between jobs.";
  return (
    <div {...stylex.props(edges.pair)}>
      <Card><CardBody><p {...stylex.props(edges.text)}>{words}</p></CardBody></Card>
      <Card><CardBody><Textarea variant="edge" minRows={1} aria-label="Description" defaultValue={words} /></CardBody></Card>
    </div>
  );
}
```

### A bold title

`weight="bold"` sets the text at 600. Here a card's title is typed in Headline bold, where it is shown.

```tsx
import { Textarea } from "@arcadia/ui";

<Textarea variant="ghost" size="lg" weight="bold" minRows={1} aria-label="Title" defaultValue="Move the build to the new runner" />
```

### In a field

Inside `Field`, with a label and a hint.

```tsx
import { useState } from "react";
import { Field, Textarea } from "@arcadia/ui";

function Autosize() {
  const [text, setText] = useState("Grows with its content.\nType a few more lines: at six it scrolls.");
  return (
    <Field label="Instructions" hint="Grows to six lines, then scrolls.">
      <Textarea value={text} onValueChange={setText} minRows={2} maxRows={6} />
    </Field>
  );
}
```

### In a form

Checked when it is left with something in it and on submit, never while empty before one. A failed submit puts focus back in the field; the error clears once the text is long enough.

```tsx
import { useRef, useState } from "react";

function InAForm() {
  const [text, setText] = useState("");
  // checked once it is left holding something, and on submit; never while it is empty before one
  const [left, setLeft] = useState(false);
  const [sent, setSent] = useState(false);
  const field = useRef<HTMLTextAreaElement>(null);
  const wrong = text.trim().length >
```

## Matrix

Size and state

Rows: Small, Medium, Large, Xl, 2xl, 3xl. Columns: Rest, Hover, Focus, Filled, Invalid, Disabled.

## Customising

`rootStyle` takes where the textarea sits and the `--textarea-*` knobs below. [Theming](/foundations/theming) says what else a product may change.

```tsx
import * as stylex from "@stylexjs/stylex";

const styles = stylex.create({
  notes: { "--textarea-py": "var(--space-2)", "--textarea-px": "var(--space-3)", "--textarea-radius": "var(--radius-control)" },
});

<Textarea aria-label="Notes" rootStyle={styles.notes} />
```

- `--textarea-py` (`TokenRef<Space>`, default `var(--space-1-5)`): Block padding (the min height counts it).
- `--textarea-px` (`TokenRef<Space> | Derived`, default `var(--space-3)`): Inline padding.
- `--textarea-radius` (`TokenRef<Radius>`): Corner radius (8, or the inset step 6 inside a card).
- `--textarea-bg` (`Paint`, default `transparent`): Fill.
- `--textarea-fg` (`Paint`, default `var(--text-1)`): Text colour.
- `--textarea-fg-disabled` (`Paint`, default `var(--text-4)`): Text colour when disabled.
- `--textarea-placeholder` (`Paint`, default `var(--text-3)`): Placeholder colour.
- `--textarea-border` (`Paint`, default `var(--stroke-field)`): Frame colour (1px) at rest.
- `--textarea-border-hover` (`Paint`, default `color-mix(in srgb, var(--stroke-field) 76%, var(--ink) 24%)`): Frame colour under the pointer.
- `--textarea-border-focus` (`Paint`, default `var(--stroke-field)`): Frame colour while focused.
- `--textarea-border-disabled` (`Paint`, default `var(--stroke-4)`): Frame colour when disabled.

A row of `minRows` and `maxRows` is the size's text style's line (Caption at `sm`, Body at `md`, Headline at `lg`, a title at `xl` and up), and `--textarea-py` pads it. Like Input, each state reads its own knob, and invalid always shows the danger frame; in the `text` form, a danger ring.

## Accessibility

A native `<textarea>`. Inside `Field`, the label, the hint and the error are wired by id (`for`, `aria-describedby`) and `invalid` or an `error` sets `aria-invalid`. Without a `Field`, name it with a `<label htmlFor>` or `aria-label`; a placeholder is not a label.

Pass `required` and say so in the label. Errors appear as they are set, so move focus back to the field on a bad submit to have the message read. In a native textarea Enter is a new line; a composer that sends on Enter should say so nearby and keep Shift+Enter for new lines.

The `text` form is named by its `aria-label` ("Board name"), opens with focus in it and its text selected, and draws the focus ring, since it has no frame to show focus. Enter keeps and Escape drops without closing a dialog around it; give focus back to what opened it after either. On a touch screen a field in a title keeps its style, which is larger than the 16px that stops a phone zooming in.

- Enter: A new line (a composer may send instead); in the text form, keep
- shift+enter: A new line in a composer
- Escape: In the text form, drop what was typed

## API reference

### Textarea

- `size` (`"sm" | "md" | "lg" | "xl" | "2xl" | "3xl"`, default `"md"`): The text style it stands in: `sm` Caption, `md` Body, `lg` Headline (conversation text, as in the composer), `xl` Title 3, `2xl` Title 2, `3xl` Title 1.
- `weight` (`TextWeight`, default `"regular"`): The weight the text it edits is shown in. A title is bold whatever this says.
- `variant` (`"default" | "ghost" | "text" | "edge" | "plain"`, default `"default"`): `ghost` drops fill and border (a composer inside its own frame). `text` edits a heading in place: no frame and no padding, the heading's own style and metrics, so nothing moves when it takes the heading's place; it shows the focus ring, opens focused with its text selected, and takes Enter to keep (`onKeep`) and Escape to discard (`onDiscard`). `edge` keeps its frame but stands out by its padding and frame on every side, so its words stay on its container's text edge, where the text it edits stood at rest. `plain` has no frame or padding and keeps multiline editing.
- `mono` (`boolean`, default `false`): Code in Obel Mono: Caption at `sm`, Body at other sizes.
- `invalid` (`boolean`)
- `onKeep` (`(value: string) => void`): The `text` form's Enter: keep what was typed. Enter never adds a line there.
- `onDiscard` (`() => void`): The `text` form's Escape: discard what was typed. Escape stops here, so a dialog around it stays open.
- `minRows` (`number`, default `variant === "text" ? 1 : 3`): Minimum height in lines (default 3, and 1 for the `text` form). The field grows with its content from there.
- `maxRows` (`number`): Growth cap in lines; past it the field scrolls. `maxHeight` wins when both are set.
- `maxHeight` (`number | string`): Growth cap as a CSS length (`"min(420px, 50vh)"`) or px.
- `onValueChange` (`(value: string) => void`): Called with the new text on every change (alongside `onChange`).
- `render` (`RenderProp`): Replaces the `<textarea>`.
- `rootStyle` (`TextareaStyle`): Typed StyleX override: where the textarea sits and its `--textarea-*` knobs.
- `placeholder` (`string`): Placeholder at text-4.
- `disabled` (`boolean`): Dims the field.

Rest props go to `<textarea>` props.
