---
title: Textarea
description: Edit multi-line text with optional auto-sizing and Field integration.
package: moraine
version: 0.11.0
repository: https://github.com/subframe7536/moraine
---

# Textarea

> Edit multi-line text with optional auto-sizing and Field integration.

## Anatomy

```text
Textarea [component; slot=root; <textarea>]
```

## Usage

Use Textarea for multi-line native text entry. Use [Input](https://moraine.subf.dev/components/input.md) for a single line.
Use [InputGroup](https://moraine.subf.dev/components/input-group.md) to put controls around it without changing which element
receives typing and focus.

```tsx
import { Field, Textarea } from 'moraine'

export function Example() {
  return (
    <Field label="Message">
      <Textarea placeholder="Write a message" />
    </Field>
  )
}
```

### Native text behavior

`Textarea` forwards native textarea attributes and works with controlled or uncontrolled values.
Pair `value` with `onValueChange` for controlled text, or use `defaultValue`; a native form reset
restores `defaultValue`. A controlled textarea restores its displayed text to `value` after every
edit, so update `value` from `onValueChange`. Use `readOnly` to preserve selection and focus without
edits, or `disabled` to prevent interaction.

```tsx
<Textarea value={bio()} onValueChange={setBio} rows={3} />
```

`modifiers` works as on [Input](https://moraine.subf.dev/components/input.md#value-modifiers): `trim`, `lazy`, `number`, and
`empty` transform the text before `onValueChange` and form state. In Textarea, `trim` and `lazy`
apply on `change` (blur).

### Size and composition

`rows` sets the visible height (default `3`). Set `autoResize` to grow the textarea with its content:
it never shrinks below `rows`, and `maxRows` caps the growth before it scrolls (`0`, the default, is
unlimited). `autoResizeDelay` postpones the first resize after mount. Use
`InputGroup orientation="vertical"` with `Leading` and `Trailing` for header and footer regions.
Your application owns counters, submit buttons, and keyboard shortcuts.

```tsx
<InputGroup orientation="vertical">
  <Textarea placeholder="Write a comment..." autoResize rows={2} maxRows={6} />
  <InputGroup.Trailing>
    <span>Markdown supported</span>
    <Button size="xs">Comment</Button>
  </InputGroup.Trailing>
</InputGroup>
```

## Examples

### Draft a release announcement

Turn on `autoResize` and cap growth with `maxRows`. Keep the draft in local state.

```tsx
import { Textarea } from 'moraine'
import { createSignal } from 'solid-js'

export function Autoresize() {
  const [post, setPost] = createSignal(
    'Just deployed the new Moraine component library docs! Smooth animations, fully typed SolidJS components, and accessible primitives out of the box 🚀',
  )
  const maxLength = 280

  return (
    <div class="p-4 b-1 b-border rounded-xl max-w-xl space-y-3">
      <label class="text-xs text-muted-foreground font-medium block">
        Draft Release Announcement
      </label>
      <Textarea
        autoResize
        rows={3}
        maxRows={8}
        value={post()}
        onValueChange={(next) => setPost(next ?? '')}
        placeholder="What's happening in your project?"
      />
      <div class="text-xs flex items-center justify-between">
        <span class="text-muted-foreground">Auto-expands as you type up to 8 rows.</span>
        <span
          class={`font-medium font-mono ${
            post().length > maxLength ? 'text-danger' : 'text-muted-foreground'
          }`}
        >
          {post().length} / {maxLength}
        </span>
      </div>
    </div>
  )
}
```

### Chat, message, and code panes

Use a vertical InputGroup for a header, a footer, or both around the textarea. The textarea still
owns the value.

```tsx
import { Button, Icon, InputGroup, Textarea } from 'moraine'
import { createSignal } from 'solid-js'

export function HeaderFooter() {
  const [value, setValue] = createSignal('Hello Moraine!')

  return (
    <div class="gap-6 grid w-full lg:grid-cols-3">
      <InputGroup orientation="vertical">
        <InputGroup.Leading class="border-b border-border">
          <span class="font-semibold">Info text</span>
          <Icon name="i-lucide:info" class="text-base ms-auto" />
        </InputGroup.Leading>
        <Textarea placeholder="Ask, search or chat..." class="min-h-24" />
      </InputGroup>
      <InputGroup orientation="vertical">
        <Textarea
          value={value()}
          onValueChange={(next) => setValue(next ?? '')}
          placeholder="Write your message..."
          autoResize
          class="min-h-24"
        />
        <InputGroup.Trailing class="border-t border-border">
          <span>{value().length}/280 characters</span>
          <Button type="button" size="sm" class="ms-auto">
            Send
          </Button>
        </InputGroup.Trailing>
      </InputGroup>
      <InputGroup orientation="vertical">
        <InputGroup.Leading class="border-b border-border">
          <Icon name="i-lucide:code" class="text-base" />
          <span>script.js</span>
        </InputGroup.Leading>
        <Textarea placeholder="console.log('Hello, world!');" class="min-h-28" />
        <InputGroup.Trailing class="border-t border-border">
          <span>Line 1, Column 1</span>
          <span class="ms-auto">JavaScript</span>
        </InputGroup.Trailing>
      </InputGroup>
    </div>
  )
}
```

### Count remaining characters

Keep the count in application state. `InputGroup.Trailing` is a place to render it; Moraine has no
built-in counter.

```tsx
import { Textarea, InputGroup } from 'moraine'
import { createSignal } from 'solid-js'

const MAX_LENGTH = 160

export function CharacterCount() {
  const [value, setValue] = createSignal('')

  return (
    <InputGroup orientation="vertical">
      <Textarea
        value={value()}
        maxLength={MAX_LENGTH}
        onValueChange={setValue}
        placeholder="Add a short description..."
      />
      <InputGroup.Trailing>
        <span class="text-xs text-muted-foreground">
          {value().length}/{MAX_LENGTH}
        </span>
      </InputGroup.Trailing>
    </InputGroup>
  )
}
```

### Bind a description field

Place Textarea in `form.Field` so schema validation owns the error.

```tsx
import { Button, createForm, Textarea } from 'moraine'
import { createSignal } from 'solid-js'
import * as v from 'valibot'

export function FormIntegration() {
  const [submittedFeedback, setSubmittedFeedback] = createSignal('')
  const form = createForm({
    schema: v.object({
      feedback: v.pipe(
        v.string(),
        v.minLength(10, 'Feedback must be at least 10 characters long.'),
      ),
    }),
    initialInput: { feedback: '' },
    validate: 'input',
  })

  return (
    <form.Form onSubmit={(output) => setSubmittedFeedback(output.feedback)}>
      <div class="max-w-xl space-y-4">
        <form.Field
          name="feedback"
          label="Product Feedback"
          description="Tell us what features or fixes you'd like to see next."
          required
        >
          <Textarea placeholder="Write your detailed feedback here..." rows={4} autoResize />
        </form.Field>
        <div class="flex gap-3 items-center">
          <Button type="submit" variant="soft" tone="neutral" size="sm">
            Send Feedback
          </Button>
          <p class="text-xs text-muted-foreground">
            Characters submitted: {submittedFeedback().length}
          </p>
        </div>
      </div>
    </form.Form>
  )
}
```

## API Reference

### Attributes

| Attributes | Slot | Description |
| --- | --- | --- |
| `data-autoresize` | `textarea` | Present when the text control grows with its content. |
| `data-disabled` | `textarea` | Present when the component, slot, or item is disabled. |
| `data-invalid` | `textarea` | Present when the field or form has a validation error. |
| `data-readonly` | `textarea` | Present when the field is in read-only mode. |
| `data-required` | `textarea` | Present when the field input is required. |

### Props

Props for the Textarea component.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| autofocus | boolean \| undefined | false | Whether the textarea is focused after mounting, by script rather than the native `autofocus`<br>attribute. Focus is skipped when the textarea is disabled, and `autofocusDelay` postpones it. |
| autofocusDelay | number \| undefined | 0 | Delay in milliseconds between mount and focusing the textarea. Only used with `autofocus`. |
| autoResize | boolean \| undefined | false | Whether the height grows with the content. The textarea shows at least `rows` rows and, when<br>`maxRows` is set, at most `maxRows` rows before it scrolls. Overflow is hidden while it fits. |
| autoResizeDelay | number \| undefined | 0 | Delay in milliseconds before the first autoresize after mount. Later resizes run without it. Only used with `autoResize`. |
| defaultValue | Value \| undefined | — | Initial value for uncontrolled usage. Also the value restored when the owning form is reset. |
| disabled | boolean \| undefined | false | Whether the control is disabled: it cannot be interacted with and sets `aria-disabled`.<br>When omitted, the control inherits `disabled` from the surrounding `Field`; an explicit<br>`false` overrides the `Field`. |
| id | string \| undefined | — | The ID of the control's main focusable element, which a surrounding `Field` label points to<br>with `for`. A unique ID is generated when omitted. The control's related parts (hidden<br>input, label, description) derive their IDs from it, so keep it unique on the page. |
| maxLength | number \| string \| undefined | — | Native `maxlength`: the maximum number of characters the user can type. Numeric strings are accepted. |
| maxRows | number \| undefined | 0 | Maximum number of rows the textarea grows to with `autoResize`; longer content scrolls.<br>`0` means unlimited. Ignored without `autoResize`. |
| modifiers | TModifiers \| undefined | — | Transforms the text before it reaches `onValueChange` and form state, and sets the callback's<br>value type.<br><br>- `trim`: strips surrounding whitespace, also from the displayed text on `change` (blur).<br>- `lazy`: updates the value on `change` instead of on every keystroke.<br>- `number`: parses the text to a `number`; non-numeric text gives `undefined`, and empty text follows `empty`.<br>- `empty`: `'null'` or `'undefined'` turns an empty (whitespace-only) string into that value; `'preserve'` keeps the string. |
| name | string \| undefined | — | The name of the control, used for native form submission. When omitted, the control uses<br>the name of its form binding or a string `name` set on the surrounding `Field`. |
| onBlur | JSX.FocusEventHandlerUnion<HTMLTextAreaElement, FocusEvent> \| undefined | — | Native blur event handler. |
| onChange | JSX.EventHandlerUnion<HTMLTextAreaElement, Event> \| undefined | — | Native change event, after value synchronization and Field notification. |
| onFocus | JSX.FocusEventHandlerUnion<HTMLTextAreaElement, FocusEvent> \| undefined | — | Native focus event handler. |
| onInput | JSX.InputEventHandlerUnion<HTMLTextAreaElement, InputEvent> \| undefined | — | Native `input` event handler, called after the component has handled the event. It fires on every keystroke, also with `modifiers.lazy`. |
| onValueChange | ((value: ModifierValue<TModifiers>) => void) \| undefined | — | Called with the new value, after `modifiers` are applied, as the user edits. It fires on<br>every keystroke, or on `change` (blur) with `modifiers.lazy`. Its argument type follows<br>`modifiers`: `string` by default, `number` with `number`, and may include `null` or<br>`undefined` through `empty`. |
| placeholder | string \| undefined | — | Hint text shown while the textarea is empty. It is not a substitute for a label. |
| readOnly | boolean \| undefined | false | Whether the control is read-only: the value is visible and focusable but cannot be changed,<br>and `aria-readonly` is set. When omitted, the control inherits `readOnly` from the<br>surrounding `Field`. |
| ref | Ref<HTMLTextAreaElement> \| undefined | — | Ref callback or variable that receives the native `<textarea>` element. |
| required | boolean \| undefined | false | Whether the control is required, which sets `aria-required` (and the native `required`<br>attribute where the control renders a native input). When omitted, the control inherits<br>`required` from the surrounding `Field`. |
| rows | number \| undefined | 3 | Number of visible text rows. With `autoResize` it is the minimum height in rows. |
| size | 'sm' \| 'md' \| 'lg' \| undefined | 'md' | Visual size of the component. |
| value | Value \| undefined | — | The current value (controlled). `undefined` leaves the textarea uncontrolled; the displayed<br>text is restored to this value after every edit, so update it from `onValueChange`. |
| variant | 'outline' \| 'subtle' \| 'ghost' \| 'none' \| undefined | 'outline' | Visual treatment of the component. |
| class | SlotClassValue \| undefined | — | Class applied to the component root or trigger element. |
| classes | Classes \| undefined | — | Family slot class defaults for this instance. |
| style | SlotStyleValue \| undefined | — | Style applied to the component root or trigger element. |
| styles | Styles \| undefined | — | Family slot style defaults for this instance. |

**`modifiers` example**

```tsx
import { Textarea } from 'moraine'

<Textarea
  modifiers={{ trim: true, lazy: true, empty: 'undefined' }}
  onValueChange={(note) => console.log(note)} // string | undefined, once per blur
/>
```
