Textarea component allows users to enter text that can span multiple lines, such as a message. For shorter input, use the Input component.
import { Textarea } from "@kesko/components";
export default () => <Textarea label="Label" name="message" placeholder="Placeholder" />;<k-textarea>
<label class="k-label" for="message">Label</label>
<div class="k-field">
<textarea id="message" name="message" placeholder="Placeholder"></textarea>
</div>
</k-textarea>import { Textarea } from "@kesko/components";
export default () => (
<Textarea
label="Delivery instructions"
name="instructions"
description="Door code, floor, or where to leave the bags."
/>
);<k-textarea>
<label class="k-label" for="instructions">Delivery instructions</label>
<div class="k-field">
<textarea id="instructions" name="instructions" aria-describedby="instructions-description"></textarea>
</div>
<div class="k-textarea-footer">
<p class="k-field-description" id="instructions-description">Door code, floor, or where to leave the bags.</p>
</div>
</k-textarea>import { Stack, Textarea } from "@kesko/components";
export default () => (
<Stack>
<Textarea label="Review" name="review" counter maxLength={200} defaultValue="Fresh bread, friendly staff." />
<Textarea label="Notes" name="notes" counter description="Anything the driver should know." />
</Stack>
);<k-stack>
<k-textarea counter>
<label class="k-label" for="review">Review</label>
<div class="k-field">
<textarea id="review" name="review" maxlength="200" aria-describedby="review-count">Fresh bread, friendly staff.</textarea>
</div>
<div class="k-textarea-footer">
<span class="k-textarea-count" id="review-count">
<span aria-hidden="true">28/200</span>
<k-visually-hidden>28 of 200 characters</k-visually-hidden>
</span>
</div>
</k-textarea>
<k-textarea counter>
<label class="k-label" for="notes">Notes</label>
<div class="k-field">
<textarea id="notes" name="notes" aria-describedby="notes-description notes-count"></textarea>
</div>
<div class="k-textarea-footer">
<p class="k-field-description" id="notes-description">Anything the driver should know.</p>
<span class="k-textarea-count" id="notes-count">
<span aria-hidden="true">0</span>
<k-visually-hidden>0 characters</k-visually-hidden>
</span>
</div>
</k-textarea>
</k-stack>
<script>
for (const id of ["review", "notes"]) {
const textarea = document.getElementById(id);
const [visible, spoken] = document.getElementById(`${id}-count`).children;
const update = () => {
const length = textarea.value.length;
const limit = textarea.maxLength;
visible.textContent = limit > 0 ? `${length}/${limit}` : length;
spoken.textContent = limit > 0 ? `${length} of ${limit} characters` : `${length} characters`;
};
update();
textarea.addEventListener("input", update);
}
</script>import { Textarea } from "@kesko/components";
const subtle = {
"--k-label-text": "var(--k-color-text-dim)",
"--k-label-weight": "var(--k-font-weight-medium)",
"--k-label-required": "var(--k-color-status-error)",
};
export default () => <Textarea label="Custom label" name="custom" required style={subtle} />;<k-textarea
style="
--k-label-text: var(--k-color-text-dim);
--k-label-weight: var(--k-font-weight-medium);
--k-label-required: var(--k-color-status-error);"
>
<label class="k-label" for="custom">Custom label<span class="k-label-required" aria-hidden="true">*</span></label>
<div class="k-field">
<textarea id="custom" name="custom" required></textarea>
</div>
</k-textarea>import { Stack, Textarea } from "@kesko/components";
export default () => (
<Stack direction="row">
<Textarea
label="Short note"
name="note"
style={{ "--k-textarea-inline-size": "16rem", "--k-textarea-block-size": "6rem" }}
/>
<Textarea label="Long message" name="long" style={{ "--k-textarea-block-size": "20rem" }} />
</Stack>
);<k-stack direction="row">
<k-textarea style="--k-textarea-inline-size: 16rem; --k-textarea-block-size: 6rem;">
<label class="k-label" for="note">Short note</label>
<div class="k-field">
<textarea id="note" name="note"></textarea>
</div>
</k-textarea>
<k-textarea style="--k-textarea-block-size: 20rem;">
<label class="k-label" for="long">Long message</label>
<div class="k-field">
<textarea id="long" name="long"></textarea>
</div>
</k-textarea>
</k-stack>import { Textarea } from "@kesko/components";
export default () => (
<Textarea label="Message" name="message" defaultValue="Thanks for the quick delivery!" disabled />
);<k-textarea>
<label class="k-label" for="message">Message</label>
<div class="k-field">
<textarea id="message" name="message" disabled>Thanks for the quick delivery!</textarea>
</div>
</k-textarea>import { Textarea } from "@kesko/components";
export default () => (
<Textarea
label="Delivery address"
name="address"
defaultValue={"Maija Meikäläinen\nMannerheimintie 1 A 2\n00100 Helsinki"}
readOnly
/>
);<k-textarea>
<label class="k-label" for="address">Delivery address</label>
<div class="k-field">
<textarea id="address" name="address" readonly>Maija Meikäläinen
Mannerheimintie 1 A 2
00100 Helsinki</textarea>
</div>
</k-textarea>import { Stack, Textarea } from "@kesko/components";
export default () => (
<Stack>
<Textarea label="Reason for return" name="reason" required />
<Textarea label="Additional details" name="details" required hideRequired />
</Stack>
);<k-stack>
<k-textarea>
<label class="k-label" for="reason">Reason for return<span class="k-label-required" aria-hidden="true">*</span></label>
<div class="k-field">
<textarea id="reason" name="reason" required></textarea>
</div>
</k-textarea>
<k-textarea>
<label class="k-label" for="details">Additional details</label>
<div class="k-field">
<textarea id="details" name="details" required></textarea>
</div>
</k-textarea>
</k-stack>import { Stack, Textarea } from "@kesko/components";
export default () => (
<Stack>
<Textarea label="Resizable" name="vertical" placeholder="Drag the corner to make room" />
<Textarea label="Fixed height" name="none" resize="none" placeholder="Stays this size" />
<Textarea label="Grows with content" name="auto" resize="auto" placeholder="Keeps everything visible" />
</Stack>
);<k-stack>
<k-textarea>
<label class="k-label" for="vertical">Resizable</label>
<div class="k-field">
<textarea id="vertical" name="vertical" placeholder="Drag the corner to make room"></textarea>
</div>
</k-textarea>
<k-textarea resize="none">
<label class="k-label" for="none">Fixed height</label>
<div class="k-field">
<textarea id="none" name="none" placeholder="Stays this size"></textarea>
</div>
</k-textarea>
<k-textarea resize="auto">
<label class="k-label" for="auto">Grows with content</label>
<div class="k-field">
<textarea id="auto" name="auto" placeholder="Keeps everything visible"></textarea>
</div>
</k-textarea>
</k-stack>import { Stack, Textarea } from "@kesko/components";
export default () => (
<Stack>
<Textarea label="Large" size="lg" placeholder="Placeholder" />
<Textarea label="Medium" placeholder="Placeholder" />
</Stack>
);<k-stack>
<k-textarea size="lg">
<label class="k-label" for="large">Large</label>
<div class="k-field">
<textarea id="large" placeholder="Placeholder"></textarea>
</div>
</k-textarea>
<k-textarea>
<label class="k-label" for="medium">Medium</label>
<div class="k-field">
<textarea id="medium" placeholder="Placeholder"></textarea>
</div>
</k-textarea>
</k-stack>import { Button, Stack, Textarea } from "@kesko/components";
export default () => (
<form onSubmit={event => event.preventDefault()}>
<Stack>
<Textarea label="Feedback" name="feedback" required placeholder="What went well, what did not?" />
<Textarea
label="Review"
name="review"
defaultValue="Good"
minLength={20}
error="Write at least 20 characters so other customers get the picture."
/>
<Textarea label="Message" name="message" defaultValue="Thanks for the quick delivery!" success="Message sent." />
<Button type="submit">Submit</Button>
</Stack>
</form>
);<form>
<k-stack>
<k-textarea>
<label class="k-label" for="feedback">Feedback<span class="k-label-required" aria-hidden="true">*</span></label>
<div class="k-field">
<textarea id="feedback" name="feedback" required placeholder="What went well, what did not?"></textarea>
</div>
</k-textarea>
<k-textarea>
<label class="k-label" for="review">Review</label>
<div class="k-field">
<textarea id="review" name="review" minlength="20" aria-describedby="review-message" aria-invalid="true">Good</textarea>
</div>
<k-field-message variant="error" role="alert"><svg viewBox="0 0 24 24" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"><path d="M22.113 18.023L13.745 3.08c-.75-1.343-2.738-1.338-3.49 0L1.888 18.023C1.153 19.332 2.13 21 3.633 21h16.735c1.503 0 2.476-1.666 1.745-2.978zM20.367 19.5H3.633a.504.504 0 0 1-.435-.745l8.364-14.94a.506.506 0 0 1 .875 0l8.368 14.942a.501.501 0 0 1-.438.743zm-8.885-5.66l-.31-4.785c0-.115.08-.195.194-.195h1.265c.116 0 .196.08.196.195l-.3 4.613c0 .127-.058.217-.208.242l-.61.102c-.128.023-.217-.035-.23-.172zM13 16.14c0 .552-.403.978-1 .978s-1-.425-1-.978.403-.965 1-.965 1 .425 1 .965z" fill="currentColor" /></svg><span id="review-message">Write at least 20 characters so other customers get the picture.</span></k-field-message>
</k-textarea>
<k-textarea success>
<label class="k-label" for="message">Message</label>
<div class="k-field">
<textarea id="message" name="message" aria-describedby="message-message">Thanks for the quick delivery!</textarea>
</div>
<k-field-message variant="success" role="status"><svg viewBox="0 0 24 24" aria-hidden="true" xmlns="http://www.w3.org/2000/svg"><path d="M17.5 8.182a.4.4 0 0 1 .055.563l-5.728 6.98a.748.748 0 0 1-.554.275h-.025a.745.745 0 0 1-.545-.235l-3.725-3.958a.397.397 0 0 1 .017-.564l.51-.48a.397.397 0 0 1 .565.017l3.14 3.338 5.185-6.32a.4.4 0 0 1 .563-.055l.54.445zm2.525-2.14a.396.396 0 0 0-.603-.047l-.505.505a.399.399 0 0 0-.035.52 8.45 8.45 0 0 1 1.62 4.982c0 4.688-3.812 8.5-8.5 8.5S3.5 16.687 3.5 12 7.313 3.5 12 3.5c1.86 0 3.58.602 4.983 1.62.16.117.38.105.52-.035l.505-.505a.4.4 0 0 0-.048-.603A9.945 9.945 0 0 0 12 2.002c-5.515 0-10 4.485-10 10s4.485 10 10 10 10-4.485 10-10c0-2.23-.735-4.292-1.975-5.957z" fill="currentColor" /></svg><span id="message-message">Message sent.</span></k-field-message>
</k-textarea>
<button class="k-button" type="submit">Submit</button>
</k-stack>
</form>Properties #
| Name | Description | Type | Default |
|---|---|---|---|
label | Label for the textarea. Always provide one, even when it is hidden with | ReactNode | — |
hideLabel | Hides the label visually while keeping it available to assistive technology. | boolean | false |
description | Assistive text shown below the control, such as what to write. | ReactNode | — |
error | Optional error message for the textarea, shown instead of the built-in validation message. Also marks the control invalid for assistive technology. | ReactNode | — |
success | Optional success message for the textarea. Shown only while there is no error. A plain string is also announced to screen readers when it appears. | ReactNode | — |
size | The size of the textarea. | "md" | "lg" | "md" |
resize | Controls whether the textarea is resizable. By default it is manually resizable vertically and never shrinks below its default height. Set to | "vertical" | "none" | "auto" | "vertical" |
counter | Shows a count of the characters in the textarea below the control. When combined with | boolean | false |
name | The name the value is submitted under. | string | — |
value | The value of the control. Pair with | string | — |
defaultValue | The starting value of an uncontrolled field. | string | — |
form | The | string | — |
placeholder | Short hint shown while the control is empty. Not a substitute for a label. | string | — |
minLength | The fewest characters the value may have before the form submits. | number | — |
maxLength | The most characters the control accepts. Shown next to the count when | number | — |
wrap | How long lines are handled. | "soft" | "off" | — |
required | Requires a value before the form submits. Draws the marker on the label and is announced to assistive technology. | boolean | false |
hideRequired | Hides the required marker on the label while keeping | boolean | false |
readOnly | Stops the value being edited while keeping the control focusable and its value submitted. Reach for | boolean | — |
disabled | Disables the control. Blocks interaction and drops the value from the submitted data. | boolean | — |
autoComplete | Tells the browser what the value is for, so it can offer to fill it in. | string | — |
Events #
| Name | Description | Type |
|---|---|---|
onChange | Handler called as the value changes. Fires on every keystroke. | (event: ChangeEvent<HTMLTextAreaElement>) => void |
onInput | Handler called on the native | (event: SyntheticEvent<HTMLTextAreaElement>) => void |
onSelect | Handler called when the selected text changes. | (event: SyntheticEvent<HTMLTextAreaElement>) => void |
onFocus | Handler called when the control receives focus. | (event: FocusEvent<HTMLTextAreaElement>) => void |
onBlur | Handler called when the control loses focus. | (event: FocusEvent<HTMLTextAreaElement>) => void |
Styles #
CSS custom properties provide finer-grained control than properties. See Styling API for more details. Prefer the Properties API when it suits your need.
| Name | Description | Default |
|---|---|---|
--k-textarea-bg | Overrides the background color. | var(--k-color-bg-base) |
--k-textarea-text | Overrides the text color. | var(--k-color-text-base) |
--k-textarea-border | Overrides the border color in every state. | var(--k-color-border-control) |
--k-textarea-radius | Overrides the border radius. | var(--k-radius-md) |
--k-textarea-inline-size | Overrides the width of the field. | 100% |
--k-textarea-block-size | Overrides the default height, which is also the minimum height when resizing. Defaults to about five lines of text. | calc(5lh + 2 * var(--k-space-md)) |
Accessibility #
Textarea is built on a native <textarea> and <label>, so it behaves as expected with assistive technologies.
- Always pass a
label. A screen reader reads it as the name, and clicking it focuses the control. If there is no room for it, usehideLabelrather than leaving it out. placeholderis a hint, not a description. Don’t put anything people need in order to fill in the field in the placeholder. It disappears as soon as they type. Usedescriptionfor that instead.- Say what to write. Use
descriptionfor what the text is for and how it will be used, andautoCompletewhere the browser can fill it in, such asstreet-address. - Leave
resizeon. The default lets people drag the box taller, which helps anyone reviewing a long answer. Useresize="none"only when the layout truly cannot grow, andresize="auto"when the all of the content should stay visible. - Show the limit, don't just enforce it.
maxLengthalone cuts the text silently. Pair it withcounterso the count and the limit are visible and read with the field on focus. - Avoid
disabledfor validation. A disabled control leaves the tab order and cannot explain itself. Keep it enabled and let the message say what is wrong. UsereadOnlyfor a value people need to see and copy but not edit. - Check contrast if you override colors. Text needs 4.5:1 against
--k-textarea-bg, and--k-textarea-borderneeds 3:1 against the surface the field sits on. Verify with the WebAIM contrast checker.
See the accessibility guidelines and the A11Y checklist for more details.
Usage #
There are two necessary parts to using the Textarea component. The @kesko/components package and the CSS Framework are required. Everything else is optional:
npm install @kesko/components @kesko/csspnpm add @kesko/components @kesko/cssyarn add @kesko/components @kesko/cssbun add @kesko/components @kesko/cssOnce installed, load the CSS Framework in your app’s entry point or root layout:
import "@kesko/css";Then, you can import the Textarea:
import { Textarea } from "@kesko/components";Projects without React can get the same styles from @kesko/css by importing the components stylesheet as well:
@import "@kesko/css";
@import "@kesko/css/components";Resources #
Usage guidelines
Learn how to use the component library with React, Next.js, or basic HTML.
Localization
How to translate the components for multiple languages.
CSS Framework
Utilities and tokens for everything the components don’t cover.
Themes
How to customize the component library theme.
Accessibility checklist
Interactive, browser-based Kesko accessibility checklist.