InputsReact
A native textarea that grows with its text: one line at first, a line more for each line typed, pasted or wrapped, up to maxRows, after which it scrolls.
Rendered by React 19.3.1
Loading React…Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<TextareaAutosize />Examples
Controlled
A bio held by the app, filled and cleared by buttons, with the characters counted in Field.Description.
Chat
A composer that grows to four lines under a message list, kept scrolled down from onHeightChange.
Actions
A template written to the element from script, sized by updateHeight() through actionsRef.
Form
Required feedback with a default text and minRows 3 in a form, a message when it is empty, and a reset.
API reference
7 props across 1 parts, in React syntax. className, style, render are listed once, under Shared props.
TextareaAutosizeTextareaAutosize
A native textarea that grows with its text between minRows and maxRows and works with Field. Renders a <textarea>.
| Prop | Type | Default |
|---|---|---|
minRowsThe smallest height, in rows. | number | 1 |
maxRowsThe largest height, in rows. | number | Infinity |
onHeightChangeCallback fired with the border-box height in CSS pixels after mount and after each change. | ((height: number) => void) | — |
actionsRefA ref to imperative actions. | React.RefObject<TextareaAutosizeActions | null> | — |
valueThe value of the textarea. | string | number | readonly string[] | — |
defaultValueThe default value of the textarea. | string | number | readonly string[] | — |
onValueChangeCallback fired when the value changes. | ((value: string, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element | undefined; }) => void) | — |
| Data attribute | Description |
|---|---|
data-disabled | Present when the textarea is disabled. |
data-valid | Present when the textarea is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the textarea is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the textarea has been touched (when wrapped in Field.Root). |
data-dirty | Present when the textarea's value has changed (when wrapped in Field.Root). |
data-filled | Present when the textarea is filled (when wrapped in Field.Root). |
data-focused | Present when the textarea is focused (when wrapped in Field.Root). |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
TextareaAutosize is a native <textarea>; the browser handles its keys. Enter inserts a line break and never submits the form.
Accessibility
| Part | Sets | So that |
|---|---|---|
| TextareaAutosize | renders a <textarea> with no role and no aria-multiline attribute | it is read as the multi-line text field it is |
| TextareaAutosize | the label, aria-describedby and aria-invalid of the Field around it | the textarea is read with its name, hint and error |
| TextareaAutosize | rows from the initial text, then only height, min-height and max-height | it never writes a width, an outline or a padding the app set |
Focus order
- TextareaAutosize is one tab stop.