0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Live character or word count under a textarea, with warning and error states near and over the limit and polite screen-reader announcements.
Overview
data-count on a <textarea> adds a live counter after it. It counts characters (limit from the native maxlength) or words (limit from data-maxwords), turns to a warning colour at 80% of the limit and an error colour at 100%, and tells screen-reader users about the count only when it matters.
Usage
Character, word and unlimited counters
123456789101112131415
<form-field><labelfor="bio">Bio (max 200 characters)</label><textareaid="bio"name="bio"maxlength="200"data-countrows="3"></textarea><outputclass="hint"for="bio">Keep it short and sweet.</output></form-field><form-field><labelfor="essay">Essay (max 100 words)</label><textareaid="essay"name="essay"data-count="words"data-maxwords="100"rows="5"></textarea></form-field><form-field><labelfor="notes">Notes (no limit)</label><textareaid="notes"name="notes"data-countrows="3"></textarea></form-field>
What the script does:
Adds an <output class="textarea-counter"> with id {textarea id}-count (a generated id when the textarea has none) and appends that id to the textarea's aria-describedby.
Places it at the end of the enclosing <form-field>, or directly after the textarea outside one, followed by a visually hidden live region.
Recounts on every input event and after the owning form is reset, and sets data-state on the counter.
Turns the counter on. Empty counts characters, words counts words. Any other value (for example a number) is treated as empty: it is not a limit.
maxlength
number
none
Native attribute. Character limit, enforced by the browser. Ignored in word mode.
data-maxwords
number
none
Word limit in word mode. Advisory: typing is not stopped and the field does not become invalid.
data-label-characters, data-label-words
template with {count}
{count} characters / {count} words
Counter text without a limit. Add a plural category for languages that need one, for example data-label-characters-one; the category comes from Intl.PluralRules for the nearest lang and is chosen from {count}.
data-label-limit
template with {count} and {max}
{count} of {max} characters / {count} of {max} words
Counter text with a limit. Plural variants (data-label-limit-one, …) are chosen from {max}.
data-count-init
set by the script
—
Marks a wired textarea. Do not set it yourself. It is not the double-binding guard: a cloned textarea that copies the attribute is still wired, and adopts a cloned counter instead of adding a second one.
Numbers in the text are formatted with Intl.NumberFormat for the nearest lang. Word mode splits on whitespace, so languages written without spaces (Chinese, Japanese, Thai) count each unbroken run as one word; use character mode for them.
Counter states
data-state
When
""
Below 80% of the limit, or no limit
"warning"
At or above 80% of the limit
"error"
At or above 100% of the limit
Events
No custom events. The counter listens for input on the textarea. Setting textarea.value from script fires no event, so dispatch one afterwards; the same event also re-measures data-grow. Form resets are handled for you.
To read the counter, follow the textarea's aria-describedby rather than walking to a sibling; inside <form-field> the counter is not next to the textarea.
The counter is styled through the .textarea-counter class and its data-state. There are no custom properties. The shipped rules (web-components/_shared/form-enhancements.css):
--color-warning-text and --color-error-text are tuned for at least 4.5:1 text contrast in light and dark mode (the raw --color-warning is too light for small text). Themes adjust the tokens, not this rule.
Accessibility
The visible counter is linked with aria-describedby and is aria-live="off", so it is read with the field but not on every keystroke.
Near or over the limit, a visually hidden aria-live="polite", aria-atomic="true" region announces the count 700ms after typing pauses.
The text says what is counted ("170 of 200 characters") instead of a bare "170 / 200".
The error state is bold as well as coloured, so it does not rely on colour alone.
The word limit is advisory. If it must hold, validate it on the server or with setCustomValidity; use maxlength for a hard character limit.
Without JavaScript the textarea works normally with no counter.
Examples
Character count
Pair with maxlength to show "42 of 200 characters" and enforce the limit.
<form-fieldlang="de"><labelfor="kommentar">Kommentar</label><textareaid="kommentar"maxlength="300"data-countdata-label-limit="{count} von {max} Zeichen"></textarea></form-field>
Added later
Textareas added after load are enhanced automatically. The counter is created next to the textarea when it is wired; if you later move the textarea elsewhere, move or remove its counter too (find it through aria-describedby as above).
Dynamically added textarea
12345
const textarea = document.createElement('textarea');textarea.maxLength = 140;textarea.dataset.count = '';document.querySelector('form-field').append(textarea);// The counter appears without any init call.
Related
<textarea> — the element this attribute goes on, and native maxlength/minlength.
data-grow — auto-expanding height; combines with the counter.
<form-field> — the counter joins the field's other messages at its end.