Skip to content
Drivn
5 min read

React Textarea Component Examples

Copy-paste React Textarea examples: a basic field, a controlled textarea with a character counter, a labelled form field, and a disabled state.

Reach for a textarea whenever the answer will not fit on one line: a support message, a product review, a profile bio, release notes in an admin panel. Drivn's Textarea is a React.forwardRef component that renders the native <textarea> and merges your className over a single styles.base string with cn(). It pulls in no Radix, no cva, and no other runtime UI package, so what npx drivn add textarea writes into your project is a short file you can read in one pass and edit freely.

The props type is React.TextareaHTMLAttributes<HTMLTextAreaElement>, so the component adds no API of its own to learn. rows, maxLength, name, readOnly, value, and onChange all reach the DOM node unchanged, and the forwarded ref points at the real element. The base styles set an 80px minimum height, a resize-y drag handle, a three-pixel focus-visible ring in ring-ring/50, and the same border-input token and rounded-[10px] corners as the Input, so the two line up in a form without extra classes.

Every snippet below is TypeScript, matching the .tsx source the Drivn CLI installs at @/components/ui/textarea. Four patterns follow: an uncontrolled field, a controlled field with a live character counter, a labelled field with a hint line, and the disabled state.

A basic textarea

The simplest use is an uncontrolled Textarea with a placeholder. It renders a full-width box with a min-h-[80px] starting height, a rounded border-input border, and a muted placeholder, and the user can drag the bottom edge to make it taller because the base styles include resize-y. You write no state and no handlers — the browser owns the value until you read it from the form on submit.

This is the right default for most cases: a contact form message, a quick note field, a description box. Because the component spreads {...props} onto the underlying <textarea>, you can add rows={5} to set the initial visible height or name="message" so the value is included when the surrounding <form> submits. See the full prop list on the Textarea reference.

1import { Textarea } from "@/components/ui/textarea"
2
3export default function Page() {
4 return (
5 <Textarea placeholder="Type your message..." />
6 )
7}

Controlled textarea with a character counter

To read or limit the value as the user types, switch to controlled mode by holding the text in React.useState and passing value and onChange. Because the Textarea forwards every native attribute, you add maxLength to cap the length and read value.length to render a live counter beneath the field. The wrapper is marked 'use client' for the useState hook.

This is the pattern for a tweet-style box, a review with a length limit, or any field where you want to show remaining characters — drop a Button below it to submit. The onChange handler receives the standard React change event, so you read e.target.value and store it — the same signature you would use with a native textarea, no adapter needed. Pair the counter text with a muted colour so it sits quietly under the field.

1"use client"
2
3import * as React from "react"
4import { Textarea } from "@/components/ui/textarea"
5
6export default function CounterTextarea() {
7 const [value, setValue] = React.useState("")
8
9 return (
10 <div>
11 <Textarea
12 value={value}
13 onChange={(e) => setValue(e.target.value)}
14 maxLength={200}
15 placeholder="Write a short bio..."
16 />
17 <p className="mt-2 text-sm text-muted-foreground">
18 {value.length}/200
19 </p>
20 </div>
21 )
22}

A labelled field with a hint

A form field needs a visible label that assistive tech can announce. Give the Textarea an id, point a native <label> at it with htmlFor, and add a name so the value lands in the form data on submit. For helper copy under the field, such as a privacy note or formatting guidance, give the hint paragraph its own id and reference it from the textarea with aria-describedby. Screen readers then announce the hint after the label when the field takes focus. None of this needs special wiring, because the component spreads every remaining prop onto the <textarea>.

The forwarded ref keeps the same field compatible with react-hook-form: spread register("feedback") onto it exactly as you would on an Input, with no Controller wrapper. Close the form with a Button set to type="submit", and the label, hint, and value all behave like native HTML.

1import { Textarea } from "@/components/ui/textarea"
2
3export default function FeedbackField() {
4 return (
5 <div className="grid gap-2">
6 <label htmlFor="feedback" className="text-sm font-medium">
7 Your feedback
8 </label>
9 <Textarea
10 id="feedback"
11 name="feedback"
12 rows={5}
13 aria-describedby="feedback-hint"
14 placeholder="Tell us what you think..."
15 />
16 <p id="feedback-hint" className="text-sm text-muted-foreground">
17 We read every message. Please leave out personal details.
18 </p>
19 </div>
20 )
21}

Disabled textarea

Pass the native disabled attribute to lock the field. The base styles include disabled:opacity-50 disabled:cursor-default, so a disabled Textarea dims to half opacity and drops the text cursor, signalling clearly that it cannot be edited. This is the state for a read-only message during submission, a field gated behind a toggle, or content awaiting a permission.

Because disabled is a real DOM attribute spread onto the <textarea>, the browser also excludes the field from form submission and skips it in tab order — you get the full native disabled behaviour, not just the visual dimming. To show non-editable content that should still be selectable and submitted, use readOnly instead of disabled; both pass through the same way since the props type is the native React.TextareaHTMLAttributes.

1import { Textarea } from "@/components/ui/textarea"
2
3export default function DisabledTextarea() {
4 return (
5 <Textarea
6 disabled
7 placeholder="This field is disabled"
8 />
9 )
10}
Get started

Install Drivn in one command

Copy the source into your project and own every line. Zero runtime dependencies, pure React + Tailwind.

npx drivn@latest create

Requires Node 18+. Works with npm, pnpm, and yarn.

Enjoying Drivn?
Star the repo on GitHub to follow new component releases.
Star →

Frequently asked questions

Import { Textarea } from "@/components/ui/textarea" and render it with a placeholder. It is a forwardRef wrapper around the native <textarea>, so you can leave it uncontrolled and read the value from the form on submit, or pass value and onChange for controlled mode. No state or handlers are required for the basic case — the browser owns the value until you read it.

Pass the native maxLength attribute, which spreads straight onto the underlying textarea. For a live counter, hold the value in React.useState, pass value and onChange, and render value.length next to the field. Because Drivn types its props as React.TextareaHTMLAttributes, maxLength behaves exactly as it would on a plain textarea.

Yes, vertically. The base styles include resize-y, so the user can drag the bottom edge to make the box taller, starting from a min-h-[80px] height. It does not auto-grow with content or resize horizontally. To set a different initial height, pass the rows attribute, which controls how many lines are visible before scrolling.

Yes. The Textarea is wrapped in React.forwardRef, so its ref forwards to the DOM node. Spread register("fieldName") from react-hook-form onto the Textarea and it registers like a native control — no Controller wrapper needed. The same forwarding lets you focus the field imperatively or read it through a ref.

Pass the native disabled attribute. The base styles include disabled:opacity-50 disabled:cursor-default, so the field dims and drops the text cursor, and the browser excludes it from submission and tab order. If you want non-editable content that is still selectable and submitted, use readOnly instead — both pass through because the props type is the native textarea attributes.