Textarea
Displays a form textarea for several lines of text.
Installation
Install the following dependencies:
Set up the token layer and the StyleX compiler, if you have not already. See
Installation, and paste states.ts from the same page.
The textarea's field surface, its focus ring and its invalid ring come from
it. The examples
caption their textareas with a Label or a
Field.
Paste field-state.ts from the same installation page.
Copy and paste the following code into your project.
Update the import paths to match your project setup.
Usage
The control is a plain <textarea>, so value, defaultValue, onChange,
name, maxLength and the rest of the element's attributes work as they
always do. Pair it with a Label and match htmlFor to the textarea's id.
The box sizes itself to its content from a two-line floor, so it grows as the text does instead of scrolling at a fixed height.
rows sets no height here. field-sizing: content measures the content
instead, and the attribute is ignored. Pass a minHeight or a maxHeight
through xstyle to change where the growing starts and stops.
Field
Use a Field to add a label and a description around the control.
Enter your message below.
Disabled
Set disabled on Field to disable its control and dim its label, or on a bare control directly.
Invalid
Set invalid on Field to style its control, or aria-invalid on a bare control. The examples show both.
Button
Put the textarea and its submit button in a VStack for a comment form.
Input group
To draw one frame around the textarea and a toolbar, wrap it in an
InputGroup. There is no InputGroupTextarea.
Give the control data-slot="input-group-control" so the group's focus ring
finds it, and textareaStyles.inGroup so it drops its own frame.
API reference
Pass render to replace a part's element or compose it with another component.
Textarea renders a <textarea> and accepts every attribute the element
takes.
Textarea
| Prop | Type | Default |
|---|---|---|
disabled | boolean | false |
aria-invalid | boolean | — |
placeholder | string | — |
xstyle | StyleXStyles | — |
className | string | — |
textareaStyles
The style maps behind the component. Spread them onto any element.
| Key | What it is |
|---|---|
base | The control: frame, fill, type scale, placeholder, disabled |
invalid | The destructive border and ring |
inGroup | The frameless form a textarea takes inside an InputGroup |
base is three styles, in merge order: the control's own, the shared
states.focusRing, and states.fieldSurface, which is the hairline edge, the
fill and the drop. invalid is states.invalidRing from theme/states.ts. Set
a border of your own through --carbon-border-color rather than borderColor.
A flat borderColor replaces the property with all of its conditions, so the ring loses its focus colour.
Data attributes
| Attribute | On | Effect |
|---|---|---|
data-slot="textarea" | the control | Targets the control from a parent |
aria-invalid | the control | Set from the prop, and dropped when it is false |