仕組み
Looks like the text input (fill, hairline, hover, growing focus halo, error tint, disabled), with a native <textarea> inside. A single row is exactly as tall as the same-size input. Auto-grow collapses the height to read scrollHeight, clamps it between the minimum and maximum rows, and slides to the new height, even continuing from halfway. The count has its own line inside the box, and IME composition does not make it jump.
向いている場面
- Notes, descriptions, comments, reviews and rejection reasons: anything written as a paragraph.
- Content that varies a lot in length: turn on autoSize and set maxRows so the page does not stretch.
- A length limit: set maxLength together with showCount so people see how much is left.
向かない場面
- One-line values such as names, titles and phone numbers: use a text input.
- Content that needs bold, images or layout: use a rich text editor.
- Chat boxes where Enter sends: Enter inserts a new line here, so wire sending to onKeyDown yourself.
使い方
import { useState } from 'react'
import { Textarea } from './Textarea.jsx'
export function ReviewField() {
const [review, setReview] = useState('')
return (
<>
<label htmlFor="review">Review</label>
<Textarea
id="review"
value={review}
onChange={setReview}
autoSize={{ minRows: 2, maxRows: 6 }}
showCount
maxLength={200}
placeholder="For example: comfortable fit, battery lasts a week"
/>
</>
)
}
// Fixed 4 rows, scrolling inside when there is more: <Textarea rows={4} />プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
value | string | — | Controlled value; leave it out for uncontrolled use. |
defaultValue | string | — | Initial value when uncontrolled. |
onChange | (value, event) => void | — | Called on change: the first argument is the string value, the second the native event. |
size | 'sm' | 'md' | 'lg' | 'md' | Font 14 / 15 / 16px, line height 20 / 22 / 24px, corners 10 / 12 / 14px; one row is 36 / 44 / 52px tall like the text input. Use lg on mobile. |
rows | number | 3 | Visible rows when autoSize is off; more content scrolls inside. |
autoSize | boolean | { minRows?, maxRows? } | false | Height follows the content: true grows from rows without limit; an object sets the minimum and maximum rows, and beyond the maximum it scrolls inside. |
resize | 'none' | 'vertical' | 'none' | Whether users can drag the corner to change the height. The width can never be dragged. Ignored with autoSize. |
maxLength | number | — | Maximum characters (native maxLength; extra input is rejected). |
showCount | boolean | false | Shows the count at the bottom right inside the box, as “typed / limit” when maxLength is set. |
labels | object | — | Replaces the text read out for the count (count: (n) => string, countOf: (n, max) => string). Shallow-merged over the English defaults. |
invalid | boolean | false | Error state: danger hairline and a faint danger tint, plus aria-invalid="true". Place the message under the box and link it with aria-describedby. |
disabled | boolean | false | Disabled: the fill stays, the hairline goes, text is muted and it cannot be focused. |
ref | Ref<HTMLTextAreaElement> | — | Forwarded to the native <textarea>, so it works with react-hook-form's register. |
...rest | TextareaHTMLAttributes | — | className and style go to the outer element; other native attributes (id, name, placeholder, readOnly, onBlur) go to the <textarea>. |
アクセシビリティ
- It is a native <textarea>; link the label with <label htmlFor>.
- invalid sets aria-invalid="true"; link the message with aria-describedby.
- The count is linked through aria-describedby and read as “36 characters entered, maximum 120” rather than character by character; replace the wording with labels.
- Count text keeps at least 4.5:1 contrast and turns danger red over the limit.
- On touch devices a single row is still at least 44px tall; with reduced motion the height changes without a transition.
モーション
Growing and shrinking: height 240ms ease-out, able to continue from halfway. On focus the fill turns white in 160ms and the accent line and halo grow in over 220ms.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
Textarea.jsxtextarea.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。





