Component
Textarea Readonly
Read-only textarea using the real native readOnly attribute — focusable, selectable, submitted.
- Component
- React
- TSX
- Tailwind CSS
- MIT
Preview
Live component
9:41 100%
devsnips.dev/library/react/components/textareas/textarea-readonly/ fluid · no fixed width 100%
Install
Add to your project
npx devsnips add React/Components/Textareas/textarea-readonly React/Components/Textareas/textarea-readonly import type { ReactNode, TextareaHTMLAttributes } from "react";
import { useId } from "react";
function cx(...parts: Array<string | false | null | undefined>): string {
return parts.filter(Boolean).join(" ");
}
const TEXTAREA_BASE =
"w-full min-h-[80px] resize-y rounded-[var(--ds-radius-sm)] border bg-[var(--ds-color-input)] px-3 py-2 text-sm leading-5 text-[var(--ds-color-foreground)] shadow-none transition-colors duration-150 ease-out placeholder:text-[var(--ds-color-muted-foreground)] hover:bg-[var(--ds-color-input-hover,var(--ds-color-input))] focus:bg-[var(--ds-color-input-focus,var(--ds-color-input))] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:bg-[var(--ds-color-muted)] disabled:text-[var(--ds-color-muted-foreground)] disabled:opacity-60 read-only:bg-[var(--ds-color-surface-subtle)] read-only:text-[var(--ds-color-muted-foreground)] motion-reduce:transition-none";
const TEXTAREA_BORDER =
"border-[var(--ds-color-border)] focus:border-[var(--ds-color-border-strong)]";
export interface TextareaReadonlyProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
/** Visible label; omit and pass `aria-label` for a bare control. */
label?: ReactNode;
}
/**
* Read-only textarea. Uses the real native `readOnly` attribute (defaulted
* to `true`): the value cannot be edited but the control stays focusable,
* its text stays selectable and copyable, and its value IS submitted with
* the form — unlike `disabled`, which blocks all of that. The readonly
* treatment (subtle surface, muted text) comes from the native
* `:read-only` pseudo-class. Use it for values the user may inspect or
* copy but not change (a generated summary, an audit trail, a locked
* message draft).
*/
export function TextareaReadonly({ label, id, className, rows = 3, readOnly = true, ...props }: TextareaReadonlyProps) {
const generatedId = useId();
const textareaId = id ?? `textarea-${generatedId}`;
const control = (
<textarea
id={textareaId}
rows={rows}
readOnly={readOnly}
className={cx(TEXTAREA_BASE, TEXTAREA_BORDER, className)}
{...props}
/>
);
if (label === undefined || label === null) {
return control;
}
return (
<div className="w-full">
<label
htmlFor={textareaId}
className="mb-2 block text-[13px] font-medium leading-5 text-[var(--ds-color-foreground)]"
>
{label}
</label>
{control}
</div>
);
}
export default TextareaReadonly; /* DevSnips React — JavaScript parity build.
* Same API, behavior, and classes as code.tsx; TypeScript types removed.
* Regenerated from code.tsx — edit code.tsx and re-run the generator.
*/
import { useId } from "react";
function cx(...parts) {
return parts.filter(Boolean).join(" ");
}
const TEXTAREA_BASE = "w-full min-h-[80px] resize-y rounded-[var(--ds-radius-sm)] border bg-[var(--ds-color-input)] px-3 py-2 text-sm leading-5 text-[var(--ds-color-foreground)] shadow-none transition-colors duration-150 ease-out placeholder:text-[var(--ds-color-muted-foreground)] hover:bg-[var(--ds-color-input-hover,var(--ds-color-input))] focus:bg-[var(--ds-color-input-focus,var(--ds-color-input))] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--ds-color-focus-ring)] disabled:pointer-events-none disabled:bg-[var(--ds-color-muted)] disabled:text-[var(--ds-color-muted-foreground)] disabled:opacity-60 read-only:bg-[var(--ds-color-surface-subtle)] read-only:text-[var(--ds-color-muted-foreground)] motion-reduce:transition-none";
const TEXTAREA_BORDER = "border-[var(--ds-color-border)] focus:border-[var(--ds-color-border-strong)]";
export function TextareaReadonly({ label, id, className, rows = 3, readOnly = true, ...props }) {
const generatedId = useId();
const textareaId = id ?? `textarea-${generatedId}`;
const control = <textarea
id={textareaId}
rows={rows}
readOnly={readOnly}
className={cx(TEXTAREA_BASE, TEXTAREA_BORDER, className)}
{...props}
/>;
if (label === undefined || label === null) {
return control;
}
return <div className="w-full">
<label
htmlFor={textareaId}
className="mb-2 block text-[13px] font-medium leading-5 text-[var(--ds-color-foreground)]"
>
{label}
</label>
{control}
</div>;
}
export default TextareaReadonly; # Textarea Readonly
Read-only textarea using the real native readOnly attribute — focusable, selectable, submitted.
## Usage
```tsx
<TextareaReadonly label="Generated summary" value={summary} rows={4} />
```
## JavaScript
A `code.jsx` build is provided for projects that ship plain JSX. It exposes the same API and behavior as `code.tsx` — only the TypeScript types are removed.
```jsx
<TextareaReadonly label="Generated summary" value={summary} rows={4} />
```
## Props
| Name | Type | Default | Description |
|---|---|---:|---|
| `label` | `ReactNode` | — | Visible label (omit and pass `aria-label` for a bare control). |
| `value` / `defaultValue` | `string` | — | Controlled / uncontrolled value. |
| `onChange` | `(event) => void` | — | Native change callback. |
| `rows` | `number` | `3` | Visible rows — the natural height floor (with `min-h-[80px]`). |
| `placeholder` | `string` | — | Muted placeholder (never critical information). |
| `disabled` | `boolean` | — | Native disabled — not focusable, not submitted. |
| `readOnly` | `boolean` | `true` | Native read-only — focusable, selectable, submitted. |
| `required` / `name` / `id` | `boolean` / `string` | — | Native form semantics (`id` also the label `htmlFor`). |
| `minLength` / `maxLength` | `number` | — | Native length constraints. |
| `className` | `string` | — | Extra Tailwind classes merged onto the control. |
| other native props / `aria-*` | — | — | Passed through to the `<textarea>`. |
## States
Native textarea with `readOnly` defaulted to `true`. The value cannot be edited, but the control stays in the tab order, its text stays selectable and copyable, and its value IS submitted with the form. The subtle surface + muted text come from the `:read-only` pseudo-class — visually distinct from the muted `disabled` treatment. Pass `readOnly={false}` to flip it back.
## Accessibility
Real `readOnly` attribute — screen readers announce the field as read-only while it remains focusable, so the content is reachable and reviewable by keyboard users (unlike `disabled`, which is skipped). Visible `focus-visible` ring still applies.
## Styling
Built with React, Tailwind CSS, and DevSnips design tokens. The component consumes the `--ds-*` semantic tokens via arbitrary values (for example `bg-[var(--ds-color-input)]`). Define the tokens once in your theme — no component-specific CSS file is required.
## Design Tokens
See [React/DESIGN_TOKENS.md](../../../DESIGN_TOKENS.md) for the authoritative token specification. This textarea uses the semantic color, radius, spacing, and motion tokens.
## Notes
Use read-only for values the user may inspect or copy but not change: a generated summary, an audit-trail entry, the rendered version of a template. Disabled and read-only are different states — this variant exists to keep that distinction honest. 60 lines UTF-8 · LF · Spaces: 2
Continue browsing