Text area
A text area takes text that runs over several lines: notes, a description, a reason. It has the same label, helper text and error line as the text input, plus a character or word counter and a field that can grow with its text.
Installation
npm gives you every component from the package. shadcn copies the source into your app instead: a copy pinned to this version that you can change. The shadcn commands need the @kanok-ui registry in your components.json once, see Installation.
Use it
- The element fires native
inputon every keystroke andchangewhen the user leaves the field, so the React wrapper hasonInputandonChange. The React + TailwindonChangeis React's own (every keystroke).
Height
rows sets the starting height in lines, 4 by default as in Carbon. The user can drag the corner to make it taller (resize: vertical); a disabled field can't be resized.
Grow with the text
auto-grow starts at rows and grows a line at a time as the user types, with no resize handle. max-rows caps it; past that, the field scrolls. Carbon has no auto-grow.
Count characters or words
max-count shows a counter above the field. As on the text input, it's a soft limit: going over turns the counter red, shows an error at once and blocks the form, but nothing is cut. Screen readers hear when the limit is near.
counter-mode="word" counts words instead (Carbon's counterMode). Thai has no spaces between words, so the field asks the browser's word segmenter for its language (lang on the field or the page): "สวัสดีครับ ผมชื่อสมชาย" is 5 words, where Carbon's count splits Thai at every vowel and tone mark. Numbers don't count as words.
Change the messages with too-long-text ({max} becomes the limit) and near-limit-text. For a hard limit that stops typing, use maxlength. More in Forms and validation.
Your own counter messages
too-long-text is the error once the text is over the limit, and near-limit-text is what screen readers hear once when 10% or less is left. Write them for the field, so people know what to cut. This text is already too long, so the error shows at once.
Helper text, warnings and errors
Same as the text input: one line under the field, an error beats a warning, and a warning beats the helper text. The error and warning icons sit in the top-right corner of the field.
required, minlength and maxlength work on the element as on <textarea>. In React + Tailwind, validation is your app's: pass invalid and invalidText. Everything else about errors: Forms and validation.
Label and helper text with markup
For a label or a message with markup, use the label and helper-text slots (warn-text and invalid-text work the same way). The React + Tailwind field takes the markup in label and helperText.
Read-only, disabled and fluid
readonlyshows and submits the text, but the user can't change it.disabledgreys it out; it isn't submitted.fluid, or a fluid form around it, puts the label inside the box (Carbon'sFluidTextArea, see fluid forms).
Outlined (Kanok design)
In the Kanok design, fields are filled by default: a soft grey fill, no edge. Put data-field-variant="outlined" on an element around the field (a <form>, or <html> for the whole app) for a 1px edge on no fill; focus is still the soft blue halo. The attribute does nothing in the Carbon design, so the example below sits inside data-design="kanok". See Kanok design for the details.
Check it in a form
In a <form>, required and minlength block the submit until the text is long enough, and the field shows its error. Try sending this form with a short reason.
Sent:
Save a draft when the user leaves
change fires once the user leaves the field after changing it, so it is the moment to save a draft without a request on every keystroke.
Draft:
Skeleton
While the data of a form loads, <ui-text-area-skeleton> keeps the field's place: a label bar and a 100px field bar, or a fluid box with fluid. Screen readers skip it, so say "loading" on the form (for example aria-busy="true").
Accessibility
- The label is a real
<label>for the inner<textarea>, and the message line is itsaria-describedby.aria-labelon the element does not reach the textarea; uselabel(withhide-labelif it must not show). - Enter types a new line, as in any text area; it never submits the form.
- The counter is not read on every keystroke. Screen readers hear once when the limit is near and when it's passed.
Unstyled React parts with the same behaviour: Headless Text area.
API
Generated from the package. In React, pass a property as a prop of the same name; events are props too (the React column).
<ui-text-area> · React UiTextArea
Attributes
| Attribute | Property | Type | Default | Description |
|---|---|---|---|---|
label |
label |
string |
'' |
Visible label text; the label slot replaces it when markup is needed. |
hide-label |
hideLabel |
boolean |
false |
Keep the label for screen readers but don't show it. |
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
invalid-text |
invalidText |
string |
'' |
Error text shown while invalid or while the field fails validation. |
invalid |
invalid |
boolean |
false |
Forces the error state, e.g. after server-side validation. |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
value |
value |
string |
'' |
|
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
placeholder |
placeholder |
string |
'' |
|
rows |
rows |
number |
4 |
Initial number of text rows (Carbon default 4); the user can still resize. |
minlength |
minlength |
number | undefined |
||
maxlength |
maxlength |
number | undefined |
||
autocomplete |
autocomplete |
string |
'' |
Passed to the textarea (off, or an autofill token). |
inputmode |
inputmode |
string |
'' |
Passed to the text box: the virtual keyboard to show (numeric, decimal, email, tel, url, search…). |
enterkeyhint |
enterkeyhint |
string |
'' |
Passed to the text box: the label of the virtual keyboard's Enter key (search, send, next, done…). |
max-count |
maxCount |
number | undefined |
Soft limit with a visible counter (unlike maxlength, extra text isn't blocked). | |
counter-mode |
counterMode |
CounterMode |
'character' |
What max-count counts: character (default) or word (Carbon counterMode). |
auto-grow |
autoGrow |
boolean |
false |
Grows with the text from rows (no Carbon counterpart); max-rows caps it, then it scrolls. |
max-rows |
maxRows |
number | undefined |
The most lines auto-grow shows before scrolling (default: no cap). |
|
too-long-text |
tooLongText |
string |
'' |
Error when over max-count; {max} is replaced with the limit (default: the translation's, for characters or words). |
near-limit-text |
nearLimitText |
string |
'' |
Announced once when what is left of the limit drops to 10% or less. |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
fluid |
fluid |
boolean |
false |
Carbon FluidTextArea: the label inside a 64px box, an error / warning inside it. <ui-fluid-form> sets it for every field inside. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
Properties
| Property | Type | Description |
|---|---|---|
willValidate |
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(read-only) The <label>s that point at this field. |
Methods
| Method | Description |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Events
| Event | React | Description |
|---|---|---|
change |
onChange |
Re-dispatched from the native textarea (native change is not composed) |
input |
onInput |
Native input event (already composed, so it reaches the host) |
Slots
| Slot | Description |
|---|---|
label |
Label content with markup; the label attribute is the plain-text fallback |
helper-text |
Helper text with markup (links, bold); overrides helper-text |
warn-text |
Warning text shown while warn; overrides warn-text |
invalid-text |
Error text shown while invalid; overrides invalid-text |
CSS parts
| Part | Description |
|---|---|
base |
The field box around the textarea |
label |
The label |
input |
The native textarea |
counter |
The character (or word) counter |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
CSS custom properties
| Custom property | Default | Description |
|---|---|---|
--ui-field-border |
Border on every side of the box (default none: Carbon's underline only; Kanok: a 1px outline) | |
--ui-field-disabled-border-color |
Border colour when disabled (default: the underline transparent) | |
--ui-field-readonly-border-color |
Border colour when read-only (default: a hairline underline) | |
--ui-field-radius |
Corner radius of the box (default 0; Kanok: 6px) | |
--ui-field-background |
Fill of the box (default --ui-surface-1; Kanok: transparent) |
|
--ui-label-font |
Font of the label (default --ui-font-caption; Kanok: 14px semibold) |
|
--ui-label-color |
Colour of the label (default --ui-ink-muted; Kanok: --ui-ink) |
|
--ui-field-readonly-background |
The box's fill when read-only (default transparent; Kanok: a faint ink tint, so read-only reads as set and never weaker than disabled) | |
--ui-fluid-background |
/ --ui-fluid-gap - A fluid box's fill and the space under it (default the layer's fill, 0: boxes that touch; Kanok: transparent, 8px) |