English
Textarea
A multi-line text field. It mirrors Input on purpose — the same value binding, the same invalid and describedBy, the same callbacks — because a field should not change shape when it grows a second line.
The one addition is field-sizing-content: the browser grows the box with its text, starting from a minimum height, where this used to take a resize observer and a scroll-height measurement on every keystroke.
sh
ream nebula:add textareaAtom · atoms/Textarea.ts · no dependencies
Usage
ts
import { Textarea } from '@c9up/nebula'
Textarea({ name: 'bio', placeholder: 'A few words about you' })Examples
Following a signal
ts
import { html, signal } from '@c9up/aurora'
import { Textarea } from '@c9up/nebula'
const note = signal('')
html`
${Textarea({ id: 'note', value: note, onInput: (value) => note(value) })}
<p class="text-muted-foreground text-sm">${() => `${note().length} / 500`}</p>
`With a label and help text
Textarea takes no label prop: name it with a <label for>, or put it in a Field:
ts
import { html } from '@c9up/aurora'
import { Field, FieldDescription, FieldLabel, Textarea, fieldIds } from '@c9up/nebula'
const ids = fieldIds()
Field({
ids,
children: () => html`
${FieldLabel({ children: 'Message' })}
${Textarea({ id: ids.control, name: 'message', rows: 4, describedBy: ids.describedBy })}
${FieldDescription({ children: 'Your message will be copied to the support team.' })}
`,
})Read-only and disabled
ts
Textarea({ value: 'Generated by the server.', readonly: true })
Textarea({ disabled: true, placeholder: 'Comments are closed' })A read-only field can still be focused and copied from; a disabled one is out of the tab order and is not posted.
Bound to a form
TextAreaField wires a Textarea to one key of an Aurora form(), with its label and its error.
API
Textarea
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | — | The element's id — what a <label for> points at. |
name | string | — | The name the value is posted under. |
value | Reactive<string> | '' | Bound to the value property. Left out, the browser owns the text. |
placeholder | Reactive<string> | — | Hint shown while the field is empty. Not a label. |
rows | Reactive<number> | — | The native rows. |
disabled | Reactive<boolean> | false | Sets the native disabled. |
readonly | Reactive<boolean> | false | Sets the native readonly. |
required | Reactive<boolean> | false | Sets the native required. |
invalid | Reactive<boolean> | — | Sets aria-invalid="true", which the border and the focus ring key off. |
describedBy | Reactive<string | undefined> | — | Sets aria-describedby: the id of the help text or the error. |
class | Reactive<string> | — | Merged last, so a conflicting utility wins. |
onInput | (value: string, event: Event) => void | — | Every keystroke, with the current text. |
onChange | (value: string, event: Event) => void | — | The native change: on commit, usually when the field loses focus. |
onBlur | (event: FocusEvent) => void | — | The field lost focus. |
textareaClasses
textareaClasses: string — the field's class list, for a native <textarea> you write yourself.
Accessibility
A native <textarea>: Enter inserts a line, Tab leaves the field. invalid sets aria-invalid="true" and nothing when false, and describedBy sets aria-describedby. With no label prop, the field takes its name from a <label for> pointing at its id — a placeholder alone leaves it unnamed once the user starts typing.