Components · Inputs
Textarea
A multi-line text field for forms, at least three lines high, with an optional character counter.
import { Textarea } from "@botiverse/raft-ui";A textarea takes several lines of text in a form: a description, a note, instructions for an agent. It has the same ink border and states as an Input, is at least three lines of field text high (96px on a phone, 90px on a desktop), and people can drag it taller.
When to use
Anatomy
Textarea: the field. Required.TextareaGroup: holds the textarea and its counter. Needed only with a counter.TextareaCounter: the count and the limit, below the field at its end (72/280).
States
- Invalid:
data-invalidgives the textarea adanger-strongborder; the message goes in the field (Field). - Over the limit: the counter turns bold
danger-strongonce the count passes the limit. - Disabled: the shared disabled look (States), and it can no longer be resized. Disabled wins over invalid.
Behavior
- Let people drag the textarea taller. It never gets shorter than three lines of field text.
- To make it grow with its content, use
field-sizing-contenttogether with a maximum height, so a long draft scrolls inside the field instead of pushing the page down. A field that scrolls is aScrollAreaTextarea(A field that scrolls). - Add a counter when the value has a limit. Pass the current value and the limit to
TextareaCounter. - Leave out the native
maxLengthwhen the counter shows the limit:maxLengthstops typing at the limit, so the counter could never show that the text is over it.
A field that scrolls
Textarea is a native <textarea>, and a native textarea that scrolls shows the platform's scrollbar: on Windows, or on a Mac set to always show scrollbars, a classic bar that takes room from the text when it appears and is drawn in the platform's colors. Most textareas are a few lines and never scroll, so Textarea stays native. When a field is meant to scroll, because its height is capped (max-h-*) or its rows are fixed and the text is long, such as instructions for an agent, use ScrollAreaTextarea drawn as the field. A textarea can't hold raft-ui's scrollbar, so ScrollAreaTextarea draws it over a frame around the textarea.
textareaScrollAreaVariants()(@botiverse/raft-ui/wip) gives the field's look, split over the two elements:wrapper()goes towrapperClassNameandtextarea()toclassName. The frame draws the border, the radius, the background and the focus ring; the textarea keeps the field's type and padding. Don't copy Textarea's classes onto either by hand.- Cap the height on the frame,
wrapper({ className: "max-h-40" }), and let the textarea grow to it,textarea({ className: "field-sizing-content" }). For a field of a fixed height, passrowsto the textarea. The textarea never gets taller than a capped frame, so its resize grip stops at the cap as a capped Textarea's does; with no cap the frame grows with it. The field is never shorter than three lines of field text, as a Textarea, and with the same value it has the same box. - The states are read from the textarea inside the frame:
disabledgives the shared disabled look, anddata-invalidthedanger-strongborder. Setaria-invalidbesidedata-invalidyourself:Textareaadds it,ScrollAreaTextareapasses native props through as they are. - The scrollbar shows while the pointer is over the field or the text scrolls, and only when the text is taller than the field. It stops above the bottom corner, where the textarea's resize grip is.
- Name it as any textarea, with a
FieldLabelwhosehtmlFormatches itsid.id,ref,valueand every other native prop go to the textarea, not the frame.
Accessibility
Textarea renders a native <textarea>; it is not a Base UI control.
- Name it with a
FieldLabelwhosehtmlFormatches the textarea'sid(Field). data-invalidalso setsaria-invalid.- Reference the counter from
aria-describedbywhen the limit matters, so it is read with the field.
API
Textarea
Takes the attributes of its element.
TextareaCounter
Also takes the attributes of its element.
TextareaGroup
Takes the attributes of its element.
Related
Changes
- 0.10.0 · Breaking ChangesField frames (
Input,Textarea,InputGroup,Select,Combobox,ComboboxSelect,Composer, optionCard,SearchTrigger) are drawn at 1px inline-fieldinstead of 1.5px, on the phone too. - 0.4.0 · Breaking ChangesInvalid-state styling now keys off
data-invalidinstead ofaria-invalid.Input,Textarea, and form controls style the error visual fromdata-invalidand mirror it toaria-invalid; passingaria-invalidalone no longer shows the error style. Update callers to passdata-invalid. ([b3786da](https://github.com/botiverse/rui/commit/b3786da))