Textarea
A text field of several lines: a note, a description, a message. It is the native textarea with the input's border, focus ring and error state. It can grow with its text and count characters against a limit. For one line use the input.
Install
npx shadcn@latest add https://thrumui.dev/r/textarea.jsonAdds components/ui/textarea.tsx. Nothing else in your project changes.
import { Textarea } from "@/components/ui/textarea";Props
| Prop | Type | Default | Description |
|---|---|---|---|
autoGrow | boolean | Default false | Grow with the text, from 64 pixels up to 192, then scroll. A max-h- class sets another limit. Without it the reader can drag the field taller. |
showCount | boolean | Default false | With maxLength: print used / limit under the field. Screen readers hear how many characters are left once 20 or fewer remain. |
maxLength | number | – | The native limit: the browser stops accepting text at this length. showCount needs it. |
rows | number | – | The native starting height in lines. The field is never shorter than 64 pixels. autoGrow ignores it. |
aria-invalid | boolean | – | Marks the text as wrong: the border takes the destructive colour. Pair it with a message linked by aria-describedby. |
disabled | boolean | Default false | The native attribute: the field is dimmed and cannot be focused or edited. |
className | string | – | Extra classes for the textarea, merged last. With the counter the field sits in a full-width wrapper, so set a width on the parent. |
...props | ComponentProps<"textarea"> | – | Every other textarea attribute and event handler: value, defaultValue, onChange, name, placeholder, readOnly. |
Motion and sound
Motion
- The border colour changes over 150 ms when the field becomes invalid or valid again.
- Growing is layout, not animation: the field takes its new height at once.
- Reduced motion: the border changes at once.
Sound
Silent. This component makes no sound.
Accessibility
- It is a native textarea element. Its name comes from a label element linked with htmlFor and id: a placeholder is not a name.
- The counter is linked to the field with aria-describedby, so it is read after the name as, for example, 58 / 280 characters used.
- A polite live region stays silent while there is room and reads the characters left once 20 or fewer remain.
- Auto-grow uses the CSS property field-sizing. A browser without it keeps the starting height and scrolls.
- Text is 16 pixels on phones, so the page does not zoom when the field takes focus, and 14 pixels on wider screens.
- An invalid field is marked with aria-invalid and its border changes colour. The message next to it carries the meaning.
| Keys | Action |
|---|---|
| Tab | Focus the field. Tab leaves it again: it does not type a tab character. |
| Enter | Start a new line. Typing, selection and editing keys are the browser's own. |
Source
"use client";
import { useId, useState, type ComponentProps } from "react";
import { cn } from "@/lib/utils";
export type TextareaProps = ComponentProps<"textarea"> & {
/** Grow with the text, from 64 pixels up to 192, then scroll. A `max-h-` class sets another limit. */
autoGrow?: boolean;
/** With `maxLength`: print "used / limit" under the field, and tell screen readers when 20 characters or fewer are left. */
showCount?: boolean;
};
/** From this many characters left, each change is announced. Above it a screen reader hears nothing while typing. */
const NEAR_LIMIT = 20;
/**
* A text field of several lines. It is a native `<textarea>`: give it a `<label>` and a `name`.
* With the counter it sits in a full-width wrapper, so set a width on the parent, not on the field.
*/
export function Textarea({ autoGrow = false, showCount = false, maxLength, value, defaultValue, onChange, className, "aria-describedby": describedBy, ...props }: TextareaProps) {
const countId = useId();
// Only the length is kept, and only for a field that holds its own text.
const [typed, setTyped] = useState(() => String(defaultValue ?? "").length);
const counted = showCount && maxLength !== undefined && maxLength >= 0;
const used = value !== undefined ? String(value).length : typed;
const left = (maxLength ?? 0) - used;
const field = (
<textarea
data-slot="textarea" data-surface="control"
maxLength={maxLength}
value={value}
defaultValue={defaultValue}
aria-describedby={[describedBy, counted && countId].filter(Boolean).join(" ") || undefined}
onChange={(event) => {
setTyped(event.target.value.length);
onChange?.(event);
}}
className={cn(
// 16px text on phones: anything smaller makes iOS zoom the page when the field takes focus.
"block min-h-16 w-full min-w-0 rounded-md border border-input bg-background px-2.5 py-1.5 text-base text-foreground placeholder:text-muted-foreground md:text-sm",
"transition-[border-color] duration-150 ease-out motion-reduce:transition-none",
"focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-ring",
"disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive",
// The browser sizes the field to its text. One that cannot keeps the starting height and scrolls.
autoGrow ? "field-sizing-content max-h-48 resize-none" : "resize-y",
className,
)}
{...props}
/>
);
if (!counted) return field;
return (
<div data-slot="textarea-field" className="grid w-full min-w-0 gap-1">
{field}
<p id={countId} data-slot="textarea-count" className={cn("justify-self-end text-xs leading-4 tabular-nums", left <= 0 ? "font-medium text-foreground" : "text-muted-foreground")}>
{used} / {maxLength}
<span className="sr-only"> characters used</span>
</p>
{/* Always in the page, so a screen reader notices when it gets its text. */}
<span aria-live="polite" className="sr-only">
{left <= NEAR_LIMIT ? `${Math.max(0, left)} characters left` : ""}
</span>
</div>
);
}Files and dependencies
- Adds
components/ui/textarea.tsx- Packages
- None
- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.