Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Textarea for Radix UI

A multi-line text field for React on Radix UI, for comments, messages and notes, that grows with its text where the browser supports it.

On this page9 sections

Textarea is a native multi-line text box for a comment, a message, a bio or a support note. Its classes include field-sizing-content, so in browsers that support CSS field-sizing it starts small and grows with the text instead of scrolling.

For a name, an email address or anything else that stays on one line, use an Input. To put a button or a character count inside the textarea's border, wrap it in an Input Group.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Textarea anatomy: textarea, caret, placeholder, resize handle

Usage

import { Textarea } from "@/components/ui/textarea";

aria-label names the textarea when nothing on screen does. In a form, give it a visible label with Field instead, as the Field demo does.

<Textarea
  aria-label="Message"
  className="max-w-xs"
  placeholder="Type your message here."
/>

Accessibility

Textarea renders a native <textarea>, so the browser handles typing, selection, scrolling and line breaks: Enter adds a new line rather than submitting the form, and Tab moves on rather than typing a tab. What it needs from you is a name. FieldLabel is a plain <label>, so give the Textarea an id and the label a matching htmlFor. Field names its FieldDescription in the textarea's aria-describedby, and invalid on Field sets aria-invalid on it. Without a visible label, set aria-label.

Demos

Field

Message as the label, a line of help, then the box. The description sits between the label and the textarea, so people see it before they start typing, which suits instructions about what to write. The label's htmlFor matches the textarea's id, so clicking Message focuses it.

Preview width

Max height

Ten lines of meeting notes in a box capped by max-h-40. The textarea grows with its text up to 160 pixels, then scrolls, and shrinks again as lines are deleted. Cap any box where a long answer would push the rest of the form off the screen. In a browser without field-sizing, it keeps its starting height and scrolls.

Preview width

Disabled

The Message box is disabled, so it can't be focused or typed in, shows a not-allowed cursor, and stays out of the form data. data-disabled set to true on Field fades the label to match. Use it for a message that can't be edited yet, such as one waiting on an earlier choice.

Preview width

Invalid

The field is marked invalid with invalid on Field alone, which passes aria-invalid to the box: its border and focus ring turn to the destructive color, the label follows, and a screen reader hears that the text is wrong. The line under it asks for a valid message. In your form, say what's wrong instead, in a FieldError you show only while it applies.

Preview width

Button

A message box with a full-width Send message button under it, in a plain grid with a small gap. Use it when sending that text is the whole form, such as a feedback widget or a reply box. To put the button inside the textarea's border instead, use an Input Group with an addon at block-end.

Preview width

RTL

Feedback in Arabic, with dir set to rtl on the Field, so the label, the caret and the note under the box start from the right. rows is 4, which sets the starting height only in browsers without field-sizing. Use it when the page or the field is in Arabic or Hebrew.

Preview width

FAQ

How do I make a Textarea grow with its content in React?

In browsers that support the CSS field-sizing property it already does. The Textarea's classes include field-sizing-content, so it grows with its text from a min-h-16 minimum. In a browser without field-sizing, it keeps a fixed height and scrolls.

Radix UI has no textarea of its own to do this, so for those browsers set the height from the textarea's scrollHeight in an input handler of your own, or let them keep the fixed height.

How do I set a maximum height on a Textarea?

Add a max height class such as max-h-40, as the Max height demo does. The textarea grows up to it and then scrolls its text. Add resize-none too if people shouldn't drag the corner to make it taller.

The other end works the same way: the box starts at its min-h-16 minimum, and a class such as min-h-24 makes it start taller.

Why doesn't rows change the height of my Textarea?

In browsers that support field-sizing, the field-sizing-content class sizes the box to its text, and rows no longer sets its height. Set a starting height with a min height class such as min-h-32 instead.

To get the browser's own sizing back for one Textarea, add field-sizing-fixed to its classes, which replaces field-sizing-content, and rows works again. The RTL demo sets rows to 4, which only shows in a browser without field-sizing.

How do I add a label and help text to a Textarea?

Put it in a Field with a FieldLabel, give the Textarea an id and set the label's htmlFor to the same value, as the Field demo does. For help text, add a FieldDescription. Field names it in the textarea's aria-describedby, so a screen reader reads it when the box takes focus.

How do I show a validation error on a Textarea?

Set invalid on its Field, as the Invalid demo does, which passes aria-invalid to the Textarea, or set aria-invalid on the Textarea alone. Either way its border and focus ring turn to the destructive color and a screen reader hears that the text is invalid.

Put the message in a FieldError, which is announced as an alert when it appears and hides itself while invalid is false.

How do I limit and count characters in a Textarea?

Set maxLength to the limit, and the browser stops accepting text past it, pasted text included. To show a count such as 120 / 280, keep the value in state and print its length in the field's description.

Pass value and update it in onChange from event.target.value. Leave the count out of a live region, or a screen reader reads it after every key.

How do I send a Textarea's message with Enter and keep Shift+Enter for new lines?

Handle the key yourself, since Enter in a textarea always adds a line. In a keydown handler, when the key is Enter and Shift isn't held, call preventDefault and submit the form with its requestSubmit method, which runs the browser's validation first.

Skip that while an input method is composing text, such as Japanese or Chinese input, or pressing Enter to confirm a word sends the message. The browser's keyboard event reports it as isComposing, which React's own event leaves out, so read it from nativeEvent.

Does the Textarea work with forms and form libraries in React?

Yes. It renders a native <textarea>, so with a name its text is part of the form's data, and required stops the form from submitting while it's empty. Read it with FormData in your submit handler.

React Hook Form's register spreads onto it, since the component passes its ref and handlers through to the <textarea>. Show the library's errors as the validation answer describes.

API Reference

Textarea

<textarea> data-slot="textarea"

PropType
className?string

Merged with the part's classes.