Textarea
A field for a few lines of text, growing with what is typed.
Installation
pnpm dlx @human-kit/design add textareanpx @human-kit/design add textareayarn dlx @human-kit/design add textareabunx @human-kit/design add textareaThe command copies the files to src/lib/components/ui/textarea. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Textarea from '$lib/components/ui/textarea';
let notes = $state('');
</script>
<Textarea.Root aria-label="Notes" bind:value={notes} /> Examples
In a field
Inside a Field.Root, spread what the field hands over on the textarea. It gets its id, its label, the line under it and the error state, and turns red with the error.
Write why the appointment is cancelled.
Write why the appointment is cancelled.Guidelines
- Use a textarea for text that can run to more than one line: a note, a reason, a message. For a name or a number, use an
Input.Root. - Use
defaultin a form, andunderlinewhere the surface around it already is a box. - It starts at three lines and grows with what is typed up to twelve, then scrolls. Set
minRowsto how much you expect: one for a comment, more for a report. - Say in the label or the hint what to write, not in the placeholder alone: the placeholder goes away once the reader types.
- Use
readonlyto show a text that cannot be changed: its box turns grey and loses its hover, and it still takes the focus to be selected and copied.
Accessibility
- It renders a native
<textarea>, so every key works as the browser has it,Enterincluded. - Name it with a
Field.Root, a<label for>, oraria-labelwhen there is no visible label. invalidsetsaria-invalid; with aField.Root, the error is tied to it througharia-describedby.disabledtakes it out of the tab order.readonlykeeps it in, and a screen reader says it cannot be edited.- The box grows instead of scrolling, so the end of the text is not hidden behind a scrollbar until it passes
maxRows.
API reference
| Prop | Type | Default |
|---|---|---|
variant | 'default' | 'filled' | 'ghost' | 'underline' A white box, or one line under the text. | 'default' |
size | 'sm' | 'default' | 'lg' The type, the corner and the inset, matching the fields of the same size. | 'default' |
value | string The text. Bindable. | — |
autoResize | boolean Grows the box with the text, between | true |
minRows | number The height at rest, in lines. | 3 |
maxRows | number The tallest it grows, in lines. Past it the text scrolls. | 12 |
invalid | boolean Marks the value as wrong: a red ring and | false |
readonly | boolean Shows the text in a grey box. It can be focused and copied, not changed. | false |
disabled | boolean Turns the field off and takes it out of the tab order. | false |
element | HTMLTextAreaElement | null The rendered element. Bindable. | — |
Plus every attribute of a native <textarea>.