TextArea
@codeui/solid v0.3.0
A multi-line text input with a label, a description, and an error message.
When to use
Use TextArea for text that can span several lines, such as a caption, alt text, or a pasted code snippet. It renders the label, the textarea, the hint, and the error message as one field.
When to choose another component
- Use TextField for one line, such as a file name or a URL. Enter in a TextField submits the form.
- Use NumberField for a number.
Live demo
import { TextArea } from "@codeui/solid";
export default function TextAreaBasic() {
return (
<div style={{ width: "360px" }}>
<TextArea
label="Snippet"
description="Paste the code to render."
placeholder="Paste your code"
options={{ autoResize: true }}
defaultValue={"const greeting = 'Hello CodeImage';\nconsole.log(greeting);"}
/>
</div>
);
}Anatomy
Tap a part to outline it in the demo.
- 01TextArea
textfieldclasses.root - 02Label
textfield-labelclasses.label - 03Input
textfield-inputclasses.input - 04Description
textfield-descriptionclasses.description - 05Error message
textfield-error-messageclasses.errorMessage
TextArea shares the data-cui names of TextField, so one selector styles both.
| Export | Renders | classes key | data-cui | Role |
|---|---|---|---|---|
TextArea | Root div | root | textfield | group |
label, when label is set | label | textfield-label | none | |
textarea | input | textfield-input | textbox | |
Hint div, when description is set | description | textfield-description | none | |
Error div, when invalid and errorMessage is set | errorMessage | textfield-error-message | none |
The parts carry Kobalte's state attributes: data-invalid, data-valid, data-required, data-disabled, and data-readonly. The root also carries data-field-size when you set size.
Variants
Size
import { Stack, TextArea } from "@codeui/solid";
import { For } from "solid-js";
const sizes = ["xs", "sm", "md", "lg"] as const;
export default function TextAreaSizes() {
return (
<Stack gap="4" style={{ width: "360px" }}>
<For each={sizes}>
{size => <TextArea size={size} label={`Size ${size}`} placeholder="Add a caption" />}
</For>
</Stack>
);
}size sets the height, the font size, and the corner radius with the same scale as TextField. The height is fixed. The default sm textarea is 36px at comfortable density. The rows attribute leaves the fixed height in place. Set options={{ autoResize: true }} or a min-height through classes.input to show more lines.
Filled and inline
import { Stack, TextArea } from "@codeui/solid";
export default function TextAreaThemes() {
return (
<Stack gap="4" style={{ width: "360px" }}>
<TextArea variant="filled" label="Filled" defaultValue="Renders the greeting in the console." />
<TextArea variant="inline" label="Inline" defaultValue="Renders the greeting in the console." />
</Stack>
);
}filled draws the field fill. inline drops the fill and the radius and keeps a 2px bottom border. These are the only two variants the source defines.
Auto resize and submit on Enter
import { TextArea } from "@codeui/solid";
export default function TextAreaAutoResize() {
return (
<div style={{ width: "360px" }}>
<TextArea
label="Caption"
description="Grows with the text. Enter submits the form, Shift+Enter adds a line."
placeholder="Describe the snippet"
options={{ autoResize: true, submitOnEnter: true }}
/>
</div>
);
}options passes props to Kobalte's TextField.TextArea. autoResize grows the textarea to fit its text on every change. submitOnEnter makes Enter call requestSubmit() on the parent form and keeps Shift+Enter for a new line. With submitOnEnter, the textarea gets aria-multiline="false".
States
Required, read-only, and disabled
import { Stack, TextArea } from "@codeui/solid";
export default function TextAreaStates() {
return (
<Stack gap="4" style={{ width: "360px" }}>
<TextArea label="Caption" required placeholder="Required" />
<TextArea label="Generated alt text" readOnly options={{ autoResize: true }} defaultValue="A TypeScript snippet that logs a greeting." />
<TextArea label="Notes" disabled defaultValue="Locked by the workspace owner." />
</Stack>
);
}required sets required and aria-required on the textarea. readOnly keeps the text selectable and focusable and blocks edits. disabled removes the textarea from the tab order and dims it to 40% opacity.
Invalid
import { TextArea } from "@codeui/solid";
import { createSignal } from "solid-js";
const LIMIT = 80;
export default function TextAreaInvalid() {
const [caption, setCaption] = createSignal(
"A long caption that explains every line of the snippet, one by one, in detail.",
);
return (
<div style={{ width: "360px" }}>
<TextArea
label="Caption"
options={{ autoResize: true }}
value={caption()}
onChange={setCaption}
description={`${caption().length} of ${LIMIT} characters`}
validationState={caption().length > LIMIT ? "invalid" : "valid"}
errorMessage={`Keep the caption under ${LIMIT} characters.`}
/>
</div>
);
}Set validationState="invalid" to show errorMessage and set aria-invalid. The demo counts characters in the hint and turns invalid past 80. The kit runs no validation of its own.
Ready API
TextArea
Renders the root, the label, the textarea, the hint, and the error message.
Prop
Type
Inherited from Kobalte's text field root:
Prop
Type
The native attributes that TextField sends to its input go to the textarea here, such as maxLength, spellcheck, autocomplete, and the aria-* set. Every other prop goes to the root. options.id replaces the generated textarea id.
import { TextArea } from "@codeui/solid";
import "./classes.css";
export default function TextAreaClasses() {
return (
<TextArea
label="Snippet"
defaultValue={"const greeting = 'Hello CodeImage';"}
spellcheck={false}
classes={{ root: "snippet-code-field", input: "snippet-code-input" }}
/>
);
}Use classes to style one part. The demo sets a monospace font and a minimum height on the textarea. slotClasses is a deprecated alias that stays until 1.0. The root accepts no class prop.
TextAreaLabel
Renders Kobalte's TextField.Label with the kit label style. Use it only inside a Kobalte TextField root that you compose yourself. TextArea already renders its label.
Prop
Type
Styled parts API
Use @codeui/solid/text-area/parts to arrange the label and messages around a textarea. Input renders a textarea. Use its rows or autoResize prop to size the text area, and put the field value and validation state on Root. Keep Label, Input, Description, and ErrorMessage under that root.
Accessibility
Keyboard
| Key | Action |
|---|---|
| Tab | Move focus to the textarea. |
| Enter | Add a new line. With submitOnEnter, submit the parent form. |
| Shift+Enter | Add a new line. |
ARIA
- The root is a
divwithrole="group". - The label
forpoints at the textareaid. - The textarea gets
aria-labelledby, andaria-describedbyfor the hint and, while invalid, the error message. - The textarea gets
aria-invalid,aria-required,aria-disabled, andaria-readonlyfrom the matching props.
Focus
TextArea moves no focus on its own. On focus, the border takes --cui-focus-color, not the Inkstone 2px ring.
SSR notes
Every part renders on the server, and none uses a portal. The kit sets the textarea id to ${id}-input, or to options.id, and points the label for at it during render. The server HTML links the label before hydration. autoResize measures the textarea in a client effect, so the server HTML has the default height until hydration.
Theming hooks
TextArea reads the same field variables as TextField, plus --cui-space-2 for the vertical padding. The size scale uses --cui-font-size-xs to --cui-font-size-lg and --cui-radius-xs to --cui-radius-xl.
Composition examples
- Build forms uses an auto-resizing TextArea for an optional caption.
Related
- TextField takes the same props for one line of text.
- Field helpers give your own control the field style.