Textarea

updated

Input, but it has more to say.

Source
Demo

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.

Rows

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().

Autoresize

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.

In a scroll area

The textarea grows with its text and the scroll area scrolls it.

API Reference

class is also accepted and merged over the base classes.

Data attributes

data-slotAttributesDescription
textareadata-variant, data-sizeThe textarea. Both attributes are set even when left at their defaults, outline and md.

Changelog

  1. registry 0.22.0

    29fb25c — InputGroupTextarea exposes resize(), like Textarea.

    939037b — A focused text control that is :user-invalid (a required Input, Textarea, InputFloating or PinInput slot left empty) keeps its destructive border; the focus border used to win over it, leaving a primary outer edge around a destructive inner ring.

    d5720b8 — An autoresize Textarea mounted while hidden (a closed tab, a Collapsible, a hidden md:block column) measures itself when it shows instead of staying collapsed until the user types, and it re-measures by itself when its width changes.

    0c80a2d — Textarea works its vertical padding out from the control height of its size and its own line height, so a one-row textarea is as tall as the Input of the same size and variant, on narrow screens too (it was 2px taller, 6px below 768px, and the filled variant needs 1px more at the top for its single border). Every textarea loses 1 to 6px of vertical padding. A py-* class on it still wins.

  2. registry 0.21.0

    e5495ef — size works the same way everywhere. Every component exports its size type (AlertSize, BadgeSize, CardSize, CheckboxSize, RadioSize, SwitchSize, EmptySize, InputFloatingSize, ItemSize, ListboxSize, SeparatorSize, SliderSize, StepperSize, TabsSize, TextareaSize, TimelineSize) and the props take it without null; Button-based parts take ButtonSize, Input TextControlSize, SelectTrigger SelectSize and Tree TreeSize. ToggleGroup and ToolbarToggleGroup carry data-size on the group as well. Table's row heights moved into tableSize, so a class on DataTable's ui.table overrides --table-row-h at every size, not only at md.

  3. registry 0.13.0

    93e57a0 — Every size on the control scale is named xs to xl; default and the bare icon are gone. To move over:

    - Button: size="default" becomes size="md", or leave size out; size="icon" becomes size="icon-md". The full set is xs to xl and icon-xs to icon-xl. Toggle, ToggleGroup, ToggleGroupItem, the Toolbar parts, AlertDialogAction, AlertDialogCancel and ToastAction take the same names; Button, Toggle and ToggleGroup default to md, the Toolbar parts keep sm and ToastAction keeps xs. InputGroupButton keeps its own xs, sm, icon-xs and icon-sm, 24 and 32px to fit inside the group, xs by default. Pagination hands its md to Button as it is. Button, Toggle and ToggleGroupItem set data-variant, data-color and data-size even when you leave them at their defaults. - AlertDialogContent: size="default" becomes size="md", still the default; sm is unchanged. The size option of useConfirm's confirm, alert and prompt takes the same names. - Table and DataTable: density becomes size, data-density becomes data-size, and the TableDensity type is now TableSize. sm, md and lg keep their 36, 44 and 52px rows; xs (28px) and xl (60px) are new, and DataTable's virtual row estimates cover them. The cells' padding above and below the text is now --table-cell-py on the table: 8px as before, 3px at xs. At xs, DataTable's sort buttons and expand and group toggles shrink to fit the row. The table-density example is now table-sizes. - DrawerMenu: size takes xs to xl like Menu and reads the control scale, plus room for a thumb. sm, md and lg keep their 40, 48 and 56px rows, padding and icons; xs is 36px and xl 64px. Overriding a --control-* token on :root now resizes DrawerMenu rows as well. - Select and Combobox: the list opens at the trigger's or anchor's size instead of always at md, so <SelectTrigger size="xl"> opens an xl list. size on SelectContent or ComboboxList picks another. Behind an as-child button, set size on ComboboxAnchor to match the button. The Select list now sets its menu variables on SelectContent instead of its viewport, so a class such as [--menu-item-height:2.5rem] on SelectContent reaches the items. - Input, Textarea, InputFloating, SelectTrigger, ComboboxAnchor and Separator set data-size with the default filled in, and the anchor sets data-variant too. With as-child, the anchor leaves the button's own data-variant and data-size alone.