shadcn.io is not affiliated with official shadcn/ui
Textarea examples · Standard
Read-only Textarea for React
A read-only textarea whose text can be selected and copied but not edited.
A read-only Textarea shows a value people can read, select and copy but not change: a generated summary, an API response, the terms they're agreeing to. readOnly keeps the field's normal look and focus ring, and the text is set once as its default value, since typing can never change it.
Preview
Installation
FAQ
Can users still select and copy text from read-only textareas?
Yes. Selecting, copying and scrolling work as in any Textarea; only typing, pasting and cutting are blocked. That makes it a good fit for values people copy elsewhere, such as a key or a generated snippet.
Can read-only textareas receive keyboard focus?
Yes. It stays in the tab order and shows the Textarea's focus ring, so keyboard users can reach it and screen readers can read it line by line. That's the main reason to prefer it over disabled for content people need to read.
Are read-only values included in form submissions?
Yes, unlike disabled ones: a read-only Textarea with a name submits its value. Browsers skip constraint validation such as required on read-only fields, so check the value elsewhere if it matters.
How do I visually distinguish read-only from editable textareas?
By default it looks exactly like an editable Textarea. Add a class such as read-only:bg-muted to set it apart, and keep enough contrast that the text stays easy to read.
When should I use readOnly instead of disabled?
Use readOnly when the value matters: people need to read or copy it, or the form must submit it. Use disabled when the field doesn't apply right now, such as before a prerequisite is met, as in the Disabled Textarea example.
Why use defaultValue instead of value for read-only textareas?
Nothing typed can change the text, so there's nothing to keep in sync: setting it once is enough. Bind the value instead only when it changes from outside, such as a log that streams in.
React accepts value without onChange on a read-only field, with no warning.
Related
- Disabled TextareaA disabled textarea that is dimmed and
- Simple TextareaA plain textarea with a placeholder and
- Textarea with LabelA textarea with a visible label above it
- Input with Helper TextAn email field with helper text under it
- Short TextareaA compact textarea fixed at three rows
- Required TextareaA required textarea with a red asterisk