Skip to content

Build UI / Forms / Textarea

Textarea ​

A multi-line text field that grows with what's typed into it. It wraps a real <textarea> and reports value and validity to the owning <form> through ElementInternals, so it submits like a native control — with the manual resize handle removed, since the height is managed for you.

preview
Code
html
<kb-textarea name="bio" placeholder="Tell us about yourself" required>
  <kb-label>Bio</kb-label>
  <kb-helper>A sentence or two.</kb-helper>
</kb-textarea>

Usage ​

html
<kb-textarea name="bio" required>
  <kb-label>Bio</kb-label>
</kb-textarea>
js
document.querySelector('kb-textarea').addEventListener('changed', (event) => {
  console.log(event.detail) // the current value
})

When to use ​

  • Free-form text longer than a line — a bio, a note, a message, a description.
  • Content whose length varies a lot between users — the field grows instead of forcing a scroll inside a fixed box.

When not to use ​

  • A single-line value — use <kb-input>, which carries the full set of constraint attributes (type, pattern, min/max) this element doesn't.
  • Rich text — this is plain text: no formatting, no preview, no toolbar.
  • Very long documents — a field that grows without bound pushes the submit action off screen. Past a few paragraphs, an editor view serves better.

Composition ​

  • Can contain: content for its three named slots — label, helper and validity. Anything unslotted is dropped. <kb-label>, <kb-helper> and <kb-validity> all assign themselves to the matching slot on connect, so nesting them is the whole setup.
  • Can be a child of: a <form>, a <kb-form>'s template, or nothing — it works standalone, it just has no form to submit to.
preview
Code
html
<kb-textarea name="note" required>
  <kb-label>Note</kb-label>
  <kb-validity state="valueMissing">Write something first.</kb-validity>
</kb-textarea>

Height and growth ​

The field starts at --textarea-size-min-height (128px, roughly four lines) and grows on every input: the handler resets the height and re-applies scrollHeight, so the box always fits its content exactly.

Two consequences:

  • It never shrinks below the minimum, and it never scrolls. overflow: hidden with resize: none means the content is always fully visible. A long answer makes a long field.
  • The growth is inline style on the inner element, applied per input event. Setting a height from outside is overwritten as soon as the user types — use --textarea-size-min-height to change the starting size.

Pick the minimum height to signal the expected answer length: a two-line box invites a sentence, an eight-line box invites a paragraph.

preview
Code
html
<div style="--textarea-size-min-height: 72px;">
  <kb-textarea name="brief" placeholder="One sentence is enough">
    <kb-label>Summary</kb-label>
  </kb-textarea>
</div>

Validation ​

Constraints are declared as attributes and evaluated by the browser; the outcome is mirrored onto the host as an invalid custom state.

  • required is the constraint that applies here — the length and format constraints of <kb-input> are not forwarded by this element.
  • Read the result through checkValidity(), reportValidity(), validity and validationMessage.
  • While the host is invalid, the helper slot is hidden, so the error message replaces the hint rather than stacking under it.

reset() clears the value and the invalid state and dispatches reset, which is how <kb-form>'s reset reaches the field.

Attributes ​

AttributeTypeDefaultDescription
namestring''Field name in the owning form's FormData.
valuestring—Current value. Setting it re-runs validation and dispatches changed.
placeholderstring—Placeholder, forwarded to the inner <textarea>.
requiredbooleanfalseWhether a value is required for validity.
disabledbooleanfalseDisables the field and excludes it from submission.
readonlybooleanfalseBlocks editing but keeps the value in FormData.
idstringfalls back to nameUsed by the inner <label for>.
widthauto | fill | lengthautoHow the field fills its container.
hiddenbooleanfalseRemoves the field from layout and the accessibility tree.

Methods ​

MethodReturnsDescription
checkValidity()booleanValidates and fires invalid if it fails.
reportValidity()booleanValidates and reports the problem to the user.
reset()thisClears the value and the invalid state, dispatches reset.

Events ​

EventFires whendetail
changedthe value changes, per keystrokethe new value

The same event name <kb-input> publishes, so one arc works for either field.

Styling ​

Custom propertyDefaultControls
--textarea-color-backgroundvar(--color-master-lightest)Field background.
--textarea-color-background_disabledvar(--color-master-lighter)Background when disabled or read-only.
--textarea-color-bordervar(--color-master-light)Border at rest.
--textarea-color-focusvar(--color-primary)Border on focus.
--textarea-color-invalidvar(--color-danger)Border while invalid.
--textarea-color-textvar(--color-master-darkest)Typed text.
--textarea-color-text_disabledvar(--color-master)Text when disabled or read-only.
--textarea-color-placeholdervar(--color-master)Placeholder text.
--textarea-font-familyvar(--font-family-base)Type family.
--textarea-font-sizevar(--font-size-xxs)Type size.
--textarea-line-heightvar(--line-height-lg)Line height — the main lever on how many lines fit the starting height.
--textarea-size-min-height128pxStarting height, before the field grows.
--textarea-space-insetvar(--spacing_inset-nano) var(--spacing_inset-xs)Padding; accepts the full shorthand.
--textarea-border-radiusvar(--border-radius-sm)Corner rounding.
--textarea-space-gapvar(--spacing-nano)Gap between label, field and helper.

Width is an attribute, not a custom property — set width rather than a CSS rule.

States and accessibility ​

  • hidden removes the field from layout and from the accessibility tree.
  • invalid is a custom state derived from the Constraint Validation API, not an attribute you set.
  • The inner <label for> targets the inner textarea via id, falling back to name — a field with neither has an unlabelled control.
  • Growing the field moves everything below it down. Keep the submit action in a position that survives that, so it doesn't jump away while the user types.
  • disabled removes the field from submission; readonly keeps its value in the FormData.

Do's and don'ts ​

DoDon't
Size --textarea-size-min-height to the expected answerShip the default 128px for a field that always holds one line
Set name (or id) so the label associates and the value submitsRely on a visible <kb-label> alone to name the field
Use <kb-input> when the answer is a single lineReach for a textarea because it "looks roomier"
Throttle before driving a request from changedFire a request per keystroke straight from the event