Textarea
A multi-line input that grows as you type, in a small card with a send button. A counter keeps track of the characters and turns amber, then red, as the limit gets close.
Options
Code
import { useLayoutEffect, useRef, useState } from "react";
import "./textarea.css";
/* A multi-line input that grows with its text, with a character counter and
a send button under it.
<Textarea label="Project details" limit={280} action="Send brief" value={text} onChange={setText} onSend={send} /> */
export function Textarea({ label, limit = 280, value, onChange, action = "Send", onSend, ...props }) {
const area = useRef(null);
const used = value.length;
/* Grows the box where the browser cannot do it alone. */
useLayoutEffect(() => {
if (CSS.supports("field-sizing", "content")) return;
area.current.style.height = "auto";
area.current.style.height = area.current.scrollHeight + "px";
}, [value]);
return (
<div
className="dh-area"
data-near={used >= limit * 0.9 && used < limit ? "" : undefined}
data-full={used >= limit ? "" : undefined}
>
<label>
<span>{label}</span>
<textarea
ref={area}
rows={3}
maxLength={limit}
value={value}
onChange={(event) => onChange(event.target.value)}
{...props}
/>
</label>
<footer>
<small aria-live="polite">
<b>{used}</b> / {limit}
</small>
<button type="button" onClick={onSend}>
{action}
</button>
</footer>
</div>
);
}
export function Example() {
const [text, setText] = useState("We need a new site for our studio, with a small shop for prints.");
return (
<Textarea
label="Project details"
limit={280}
action="Send brief"
placeholder="Tell us what you have in mind"
value={text}
onChange={setText}
/>
);
}/* A multi-line input that grows as you type, in a small card with a character
counter and a send button. The counter turns amber, then red, near the limit. */
.dh-area {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
display: grid;
width: 23em;
max-width: 100%;
overflow: hidden;
border-radius: 0.9em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-size: 1em;
line-height: 1.5;
text-align: left;
transition: box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-area:has(textarea:focus-visible) {
box-shadow:
0 0 0 2px var(--dh-faint, #8a8a8a),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
.dh-area label {
display: grid;
padding: 0.85em 1em 0.6em;
}
.dh-area label span {
color: var(--dh-faint, #8a8a8a);
font-size: 0.8em;
line-height: 1.4;
}
.dh-area textarea {
field-sizing: content;
min-height: 4.5em;
max-height: 12em;
margin-top: 0.2em;
padding: 0;
border: 0;
background: none;
color: inherit;
font: inherit;
letter-spacing: -0.005em;
outline: none;
resize: none;
}
.dh-area textarea::placeholder {
color: var(--dh-faint, #8a8a8a);
}
.dh-area footer {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.55em 0.6em 0.55em 1em;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
}
.dh-area small {
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.78em;
font-variant-numeric: tabular-nums;
}
.dh-area small b {
color: var(--dh-muted, #5c5c5c);
font-weight: inherit;
transition: color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-area[data-near] small b {
color: #b87503;
}
.dh-area[data-full] small b {
color: #d92d20;
}
.dh-area button {
height: 2.15em;
padding: 0 0.85em;
border: 0;
border-radius: 0.55em;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
font: inherit;
font-size: 0.92em;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-area button:active {
transform: scale(0.97);
}
.dh-area button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
@media (pointer: coarse) {
.dh-area textarea {
font-size: max(1em, 16px);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-area,
.dh-area small b,
.dh-area button {
transition: none;
}
}<div class="dh-area">
<label>
<span>Project details</span>
<textarea rows="3" maxlength="280" placeholder="Tell us what you have in mind">We need a new site for our studio, with a small shop for prints.</textarea>
</label>
<footer>
<small aria-live="polite"><b>64</b> / 280</small>
<button type="button">Send brief</button>
</footer>
</div>/* A multi-line input that grows as you type, in a small card with a character
counter and a send button. The counter turns amber, then red, near the limit. */
.dh-area {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
display: grid;
width: 23em;
max-width: 100%;
overflow: hidden;
border-radius: 0.9em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-size: 1em;
line-height: 1.5;
text-align: left;
transition: box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-area:has(textarea:focus-visible) {
box-shadow:
0 0 0 2px var(--dh-faint, #8a8a8a),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
.dh-area label {
display: grid;
padding: 0.85em 1em 0.6em;
}
.dh-area label span {
color: var(--dh-faint, #8a8a8a);
font-size: 0.8em;
line-height: 1.4;
}
.dh-area textarea {
field-sizing: content;
min-height: 4.5em;
max-height: 12em;
margin-top: 0.2em;
padding: 0;
border: 0;
background: none;
color: inherit;
font: inherit;
letter-spacing: -0.005em;
outline: none;
resize: none;
}
.dh-area textarea::placeholder {
color: var(--dh-faint, #8a8a8a);
}
.dh-area footer {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.55em 0.6em 0.55em 1em;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
}
.dh-area small {
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.78em;
font-variant-numeric: tabular-nums;
}
.dh-area small b {
color: var(--dh-muted, #5c5c5c);
font-weight: inherit;
transition: color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-area[data-near] small b {
color: #b87503;
}
.dh-area[data-full] small b {
color: #d92d20;
}
.dh-area button {
height: 2.15em;
padding: 0 0.85em;
border: 0;
border-radius: 0.55em;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
font: inherit;
font-size: 0.92em;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-area button:active {
transform: scale(0.97);
}
.dh-area button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
@media (pointer: coarse) {
.dh-area textarea {
font-size: max(1em, 16px);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-area,
.dh-area small b,
.dh-area button {
transition: none;
}
}/* Counts the characters, and grows the box where the browser cannot do it alone. */
document.querySelectorAll(".dh-area").forEach((field) => {
const area = field.querySelector("textarea");
const count = field.querySelector("small b");
const limit = Number(area.getAttribute("maxlength"));
const grows = CSS.supports("field-sizing", "content");
const update = () => {
const used = area.value.length;
count.textContent = used;
field.toggleAttribute("data-near", used >= limit * 0.9 && used < limit);
field.toggleAttribute("data-full", used >= limit);
if (grows) return;
area.style.height = "auto";
area.style.height = area.scrollHeight + "px";
};
area.addEventListener("input", update);
update();
});Textarea is not in the Figma file yet.
Use the React or HTML code for now.