Skip to content

Textarea

Takes several lines of text, such as a receiving note, a reason or a message to a supplier.

Quality reads this note at the next review.

Installation

pnpm dlx shadcn@latest add @silkware/textarea

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/textarea.tsx:

components/ui/textarea.tsx
"use client"
import * as React from "react"import { cn } from "cn"import {  composeRenderProps,  TextArea as TextareaPrimitive,} from "react-aria-components"
function Textarea({  className,  ...props}: React.ComponentProps<typeof TextareaPrimitive>) {  return (    <TextareaPrimitive      data-slot="textarea"      className={composeRenderProps(className, (className) =>        cn(          "flex field-sizing-content min-h-16 w-full rounded-md border border-input bg-transparent px-2.5 py-2 text-base shadow-xs transition-[color,box-shadow] outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40",          className        )      )}      {...props}    />  )}
export { Textarea }

Usage

import { Field, FieldDescription, FieldLabel } from "@/components/ui/field"import { Textarea } from "@/components/ui/textarea"
<Field>  <FieldLabel htmlFor="note">Receiving note</FieldLabel>  <Textarea    id="note"    placeholder="Two pallets arrived with damaged wrapping."  />  <FieldDescription>    Quality reads this note at the next review.  </FieldDescription></Field>

Textarea is the bare React Aria TextArea. It renders no label, description or error, so compose it with Field and connect the id and aria-describedby yourself.

The textarea grows with its content, from a minimum height of 4rem. The rows attribute has no effect, because the height follows the content. Set a min-h-* class to start taller, or add resize-none to remove the resize handle.

Examples

Height

min-h-* sets the starting height, here min-h-32. The field still grows as the person types.

Disabled

disabled removes the textarea from the tab order and dims it. Add data-disabled to the Field so the label dims as well.

The order is closed, so its notes are locked.

Read-only

readOnly keeps the textarea focusable and selectable but stops edits. Use it for text people need to read and copy.

Invalid

Set aria-invalid on the textarea and data-invalid on the Field, and explain the problem in a FieldError that the textarea points to with aria-describedby.

Character count

Control the value with state and maxLength when the text has a limit. Show the remaining count in the description, and announce it with role="status" only when the count matters.

200 characters left.

With a button

Place a button under the textarea when the text is sent on its own, such as a comment.

Accessibility

KeyAction
TabMoves focus to the textarea, and away from it.
ShiftTabMoves focus to the previous control.
EnterInserts a new line.

The browser provides, because Textarea renders a native <textarea> through React Aria:

  • Text editing, selection, spell checking and autofill.
  • Focus in the tab order, with a visible ring through focus-visible.
  • Native required, minLength and maxLength checks on submit.

You provide:

  • A visible FieldLabel whose htmlFor matches the textarea id, or an aria-label.
  • aria-describedby that points to the description and the error.
  • A way to submit that does not depend on Enter, because Enter adds a line.

Guidelines

When to use

  • For text that can run to several sentences: notes, reasons, comments, messages.
  • When people need to see more than one line of what they wrote.

When not to use

  • For one line of text. Use Input.
  • For formatted text with headings and links. Use a rich text editor, which is outside this set.
  • For a choice from a short list. Use Radio Group or Select.

Content

  • Label with a noun: "Receiving note", "Reason for rejection".
  • Use the placeholder for an example of a good answer, not for the instruction.
  • If there is a limit, say it before the person reaches it.

Size

Use a textarea only when the answer is longer than a line.

Do

Use a textarea for notes that can run long.

Don’t

Don’t use a textarea for a short code. Use an input.

API reference

Textarea

Renders a <textarea> through the React Aria TextArea. It accepts every React Aria TextArea prop and the standard attributes of an HTML textarea, which the table lists in part.

PropTypeDefaultDescription
rows?numberNoneAccepted, but it has no visible effect: the height follows the content. Set a min-h class to start taller.
placeholder?stringNoneA short example of a good answer. It is not a label.
maxLength?numberNoneThe most characters the textarea accepts.
disabled?booleanfalseRemoves the textarea from the tab order and dims it.
readOnly?booleanfalseKeeps the textarea focusable but stops the value from changing.
required?booleanfalseAsks the browser to check that the textarea has a value on submit.
aria-invalid?booleanNoneMarks the value as wrong and applies the destructive styles.
className?string | ((values) => string)NoneClasses merged after the base classes.