Textarea
Slant / Forms
Multi-line text sized in rows, with optional autosize.
Demo
Usage
import { Field, Textarea } from '@tuttn/slant'
// Controlled: value and onChange, as a native textarea.
const [notes, setNotes] = useState('')
<Textarea value={notes} onChange={event => setNotes(event.target.value)} aria-label="Notes" />
// Uncontrolled: a defaultValue, read from the form or a ref.
<Textarea name="notes" defaultValue="First draft" rows={5} ref={notesRef} aria-label="Notes" />
// Autosize: from rows lines up to maxRows, then it scrolls.
<Textarea autosize rows={2} maxRows={8} aria-label="Message" />
// In a Field: the label, description and error are wired for you.
<Field invalid={tooLong}>
<Field.Label>Hand-off notes</Field.Label>
<Textarea autosize maxRows={6} value={notes} onChange={event => setNotes(event.target.value)} />
<Field.Description>Up to 280 characters.</Field.Description>
<Field.Error>Trim it to 280 characters.</Field.Error>
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
rows | number | 3 | The visible lines of text: the height, or with `autosize` the smallest height. |
autosize | boolean | false | Grows the textarea with its content, from `rows` lines up to `maxRows`, and drops the resize handle. |
maxRows | number | no limit | With `autosize`, the most lines it grows to before it scrolls. |
invalid | boolean | — | Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this and points the textarea at its error. |
mono | boolean | — | Monospace text, for code, logs and keys. |
…rest | TextareaHTMLAttributes | — | value, defaultValue, onChange, name, placeholder, maxLength and every other textarea attribute pass through; the ref reaches the <textarea>, which is the root. |
It is a native <textarea>, so typing, selection, undo and form submission are the browser's. Without autosize it is rows lines tall (three by default) with a vertical resize handle. With it, the height follows the text, including lines that wrap, from rows to maxRows; past that it scrolls. It re-measures when the value changes, controlled or typed, and when its width changes. Autosize sets the textarea's inline height, so don't set one yourself.
Name it with a Field label, or with aria-label when there is no visible one. For a single line use Input.