# Textarea

> A multiline text input with three sizes that integrates with Field.

```tsx title="hero.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import { Textarea } from '@langgenius/dify-ui/textarea'

export function TextareaHeroDemo() {
  return (
    <Field name="knowledgeDescription" className="w-full max-w-80">
      <FieldLabel>Description</FieldLabel>
      <Textarea placeholder="e.g. Product FAQs and troubleshooting guides for the support team." />
      <FieldDescription>Helps the agent decide when to search it.</FieldDescription>
    </Field>
  )
}
```

## Usage guidelines

- Use `Textarea` for free text that spans several lines, such as descriptions, prompts and notes. A single line of text uses `Input`.
- Put the textarea in a [`Field`](/components/field.md) with a `FieldLabel`, and add `FieldDescription` and `FieldError` where they help. Do not use `placeholder` as the label.
- Match `size` to the surrounding controls. `medium` is the default.
- The textarea keeps a minimum height; set `rows` only for a taller starting height. Content past the height scrolls.
- Control the value with both `value` and `onValueChange`; otherwise pass `defaultValue`. `onValueChange` receives the string value, not the change event.
- `Textarea` renders no character counter. `maxLength` limits the input natively. State the limit in the `FieldDescription`, and keep a visible count outside it so the count is not read with the description.
- Textareas that save together belong in a real form boundary: [`Form`](/components/form.md) or a native `<form>`, with a `Button` of `type="submit"`.

## Anatomy

```tsx
import { Field, FieldDescription, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import { Textarea } from '@langgenius/dify-ui/textarea'
```

```tsx
<Field>
  <FieldLabel />
  <Textarea />
  <FieldDescription />
  <FieldError />
</Field>
```

## Examples

### Sizes

`small`, `medium` and `large` set the padding, radius and text size together.

```tsx title="sizes.tsx"
import { Field, FieldLabel } from '@langgenius/dify-ui/field'
import { Textarea } from '@langgenius/dify-ui/textarea'

export function TextareaSizesDemo() {
  return (
    <div className="grid w-full max-w-80 gap-3">
      <Field name="noteSmall">
        <FieldLabel>Small</FieldLabel>
        <Textarea size="small" placeholder="Add a note for reviewers…" />
      </Field>
      <Field name="noteMedium">
        <FieldLabel>Medium</FieldLabel>
        <Textarea placeholder="Add a note for reviewers…" />
      </Field>
      <Field name="noteLarge">
        <FieldLabel>Large</FieldLabel>
        <Textarea size="large" placeholder="Add a note for reviewers…" />
      </Field>
    </div>
  )
}
```

### States

Set `invalid` on the `Field` when application code owns the validity, and render `<FieldError match>` while it is invalid; typing at least 20 characters clears the error. A read-only textarea stays focusable so its value can be selected and copied; a disabled one leaves the tab order and is not submitted.

```tsx title="states.tsx"
import { Field, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import { Textarea } from '@langgenius/dify-ui/textarea'
import * as React from 'react'

export function TextareaStatesDemo() {
  const [openingStatement, setOpeningStatement] = React.useState('Hi!')
  const invalidOpening = openingStatement.trim().length < 20

  return (
    <div className="grid w-full max-w-80 gap-3">
      <Field name="openingStatement" invalid={invalidOpening}>
        <FieldLabel>Opening statement</FieldLabel>
        <Textarea
          placeholder="e.g. Hi! How can I help you with Dify today?"
          value={openingStatement}
          onValueChange={setOpeningStatement}
        />
        {invalidOpening && <FieldError match>Use at least 20 characters.</FieldError>}
      </Field>
      <Field name="generatedSummary">
        <FieldLabel>Generated summary</FieldLabel>
        <Textarea
          readOnly
          defaultValue="Generated from the published workflow configuration."
          placeholder="e.g. Summarizes documents using a knowledge base."
        />
      </Field>
      <Field name="lockedInstructions" disabled>
        <FieldLabel>Instructions</FieldLabel>
        <Textarea
          defaultValue="Instructions are managed by the workspace owner."
          placeholder="e.g. Answer using the retrieved context."
        />
      </Field>
    </div>
  )
}
```

### Validation in a form

`required` and `minLength` drive the matching `FieldError` messages. Submit the empty form to see them.

```tsx title="validation.tsx"
import { Button } from '@langgenius/dify-ui/button'
import { Field, FieldDescription, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
import { Form } from '@langgenius/dify-ui/form'
import { Textarea } from '@langgenius/dify-ui/textarea'
import * as React from 'react'

type KnowledgeBaseValues = {
  description: string
}

export function TextareaValidationDemo() {
  const [saved, setSaved] = React.useState(false)

  return (
    <Form<KnowledgeBaseValues>
      aria-label="Knowledge base settings"
      className="grid w-full max-w-80 gap-4"
      onChange={() => {
        if (saved) setSaved(false)
      }}
      onFormSubmit={() => setSaved(true)}
    >
      <Field name="description">
        <FieldLabel>Description</FieldLabel>
        <Textarea
          required
          minLength={20}
          placeholder="e.g. Product FAQs and troubleshooting guides for the support team."
        />
        <FieldDescription>Shown to teammates when they choose a knowledge source.</FieldDescription>
        <FieldError match="valueMissing">Description is required.</FieldError>
        <FieldError match="tooShort">Use at least 20 characters.</FieldError>
      </Field>
      <div className="flex items-center justify-between gap-3">
        <span role="status" className="min-w-0 truncate system-xs-regular text-text-success">
          {saved ? 'Description saved' : ''}
        </span>
        <Button type="submit" variant="primary" className="shrink-0">
          Save
        </Button>
      </div>
    </Form>
  )
}
```

### Auto height and character count

The `field-sizing-content` utility lets the textarea grow with its content up to `max-h-40`, then scroll. Browsers without `field-sizing` keep the fixed height. The description states the limit that `maxLength` enforces; the running count sits next to it as plain text, outside the description, so screen readers do not read it with the description or announce it on every keystroke.

```tsx title="auto-height.tsx"
import { Field, FieldDescription, FieldLabel } from '@langgenius/dify-ui/field'
import { Textarea } from '@langgenius/dify-ui/textarea'
import * as React from 'react'

export function TextareaAutoHeightDemo() {
  const [prompt, setPrompt] = React.useState(
    'You are a friendly assistant for Dify Cloud billing questions.',
  )

  return (
    <Field name="systemPrompt" className="w-full max-w-80">
      <FieldLabel>System prompt</FieldLabel>
      <Textarea
        placeholder="You are an assistant for workspace admins…"
        value={prompt}
        onValueChange={setPrompt}
        maxLength={500}
        className="field-sizing-content max-h-40 resize-none"
      />
      <div className="flex items-start justify-between gap-2">
        <FieldDescription>Sent before every conversation. Up to 500 characters.</FieldDescription>
        <span className="shrink-0 py-0.5 body-xs-regular text-text-tertiary tabular-nums">
          {prompt.length}/500
        </span>
      </div>
    </Field>
  )
}
```

## API reference

### Textarea

A multiline text input that integrates with `Field` for labeling and validation. Renders a `<textarea>` element.

Controlled usage requires both `value` and `onValueChange`.

**Props**

| Prop             | Type                                                                | Default    | Description                                                                                                                              |
| ---------------- | ------------------------------------------------------------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `value?`         | `TextareaValue`                                                     | —          | The controlled value of the textarea. Requires `onValueChange`. To render an uncontrolled textarea, use the `defaultValue` prop instead. |
| `defaultValue?`  | `TextareaValue`                                                     | —          | The uncontrolled value of the textarea when it is initially rendered. To render a controlled textarea, use the `value` prop instead.     |
| `onValueChange?` | `(value: string, eventDetails: TextareaChangeEventDetails) => void` | —          | Callback fired when the value changes.                                                                                                   |
| `size?`          | `'large' \| 'medium' \| 'small'`                                    | `'medium'` | The padding, corner radius, and type scale of the textarea.                                                                              |
| `children?`      | `undefined`                                                         | —          | —                                                                                                                                        |

Valid prop combinations:

- `value: string | number`, `defaultValue?: undefined`, `onValueChange: (value: string, eventDetails: TextareaChangeEventDetails) => void`
- `value?: undefined`, `defaultValue?: TextareaValue`, `onValueChange?: (value: string, eventDetails: TextareaChangeEventDetails) => void`

Inherits `className`, `style` from [Base UI Field.Control](https://base-ui.com/react/components/field#control).

**Data attributes**

| Attribute       | Description                                    |
| --------------- | ---------------------------------------------- |
| `data-disabled` | Present when the field is disabled.            |
| `data-valid`    | Present when the field is in a valid state.    |
| `data-invalid`  | Present when the field is in an invalid state. |
| `data-touched`  | Present when the field has been touched.       |
| `data-dirty`    | Present when the field's value has changed.    |
| `data-filled`   | Present when the field is filled.              |
| `data-focused`  | Present when the field control is focused.     |
