Textarea
updatedInput, but it has more to say.
Open Props in the corner of the example to try variant, size, disabled and aria-invalid.
The same five variants as the input: outline, soft, filled, ghost and subtle.
Each size takes the height and text size of the input size of the same name and works its padding out from them, so a one-row textarea is as tall as the input next to it, in every variant.
Installation
Usage
<script setup lang="ts">
import { Textarea } from '@/components/ui/textarea'
const message = ref('')
</script>
<template>
<Textarea v-model="message" placeholder="Type your message here." />
</template>
Every attribute goes to the <textarea> itself, so name, maxlength, placeholder and the native validation attributes work as they do in HTML. Inside a Field it takes its id, description, error and state from it, as the input does, and it turns invalid the same two ways: through aria-invalid, or through the browser's own validation after the user has interacted with it.
Examples
Rows
rows sets the height in lines of text. It defaults to 3, one more than the browser's own, so an empty textarea reads as a place for more than a line. The user can still drag the corner to make it taller.
Autoresize
With autoresize the textarea grows to fit its text instead of scrolling, and the drag handle goes away. rows becomes the height it never shrinks below, and maxrows the height at which it stops growing and scrolls instead. It measures itself when the value or its width changes, and when it first becomes visible after mounting hidden; after any other change to its text's size, a font that loads late say, call the exposed resize().
From 2 to 6 rows, then it scrolls.
In a scroll area
Don't like the browser's scrollbar? Turn on autoresize and wrap the textarea in a fixed-height ScrollArea.
Then move the border: take it off the textarea and put it on the scroll area, using has-focus-visible: and has-aria-invalid: for the focus and error states.
Stretch the scroll area's content, as in the example below, so a click anywhere inside the frame starts typing.
The textarea grows with its text and the scroll area scrolls it.
API Reference
class is also accepted and merged over the base classes.
Textarea
| Prop | Default | Type and description |
|---|---|---|
| id | — | stringOverrides the |
| defaultValue | — | stringThe starting value when |
| variant | — | "filled" | "outline" | "soft" | "ghost" | "subtle" | nullThe same five variants as the input. |
| size | "md" | TextareaSizePadding and text size, matching the input sizes of the same name. |
| rows | 3 | numberHeight in lines of text. With |
| maxrows | — | numberWith |
| autoresize | — | booleanGrows the textarea to fit its text instead of scrolling, and removes the resize handle. |
| disabled | — | booleanDisables the textarea. Also set by a disabled field. |
| required | — | booleanMarks the textarea required. Also set by a required field. |
| modelValue | — | stringThe value, bound with |
| Event | Payload and description |
|---|---|
| update:modelValue | [value: string | undefined]Fires as the value changes. |
| Member | Type and description |
|---|---|
| resize | () => voidFits the height to the text again, for a change it cannot see (width changes are followed on their own). Does nothing without |
Props
- id
stringOverrides theidtaken from a surrounding field.- defaultValue
stringThe starting value whenv-modelis not used.- variant
"filled" | "outline" | "soft" | "ghost" | "subtle" | nullThe same five variants as the input.- size
"md" TextareaSizePadding and text size, matching the input sizes of the same name.- rows
3 numberHeight in lines of text. Withautoresize, the height it never shrinks below.- maxrows
numberWithautoresize, the height in lines past which it stops growing and scrolls.0means no limit.- autoresize
booleanGrows the textarea to fit its text instead of scrolling, and removes the resize handle.- disabled
booleanDisables the textarea. Also set by a disabled field.- required
booleanMarks the textarea required. Also set by a required field.- modelValue
stringThe value, bound withv-model.
Emits
- update:modelValue
[value: string | undefined]Fires as the value changes.
Exposed
- resize
() => voidFits the height to the text again, for a change it cannot see (width changes are followed on their own). Does nothing withoutautoresize.
Data attributes
| data-slot | Attributes | Description |
|---|---|---|
| textarea | data-variant, data-size | The textarea. Both attributes are set even when left at their defaults, outline and md. |