Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

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…

Read the release notes

data-count

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

What the script does:

  1. 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.
  2. Places it at the end of the enclosing <form-field>, or directly after the textarea outside one, followed by a visually hidden live region.
  3. Recounts on every input event and after the owning form is reset, and sets data-state on the counter.

Options

AttributeValuesDefaultEffect
data-count "", "words" "" 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-stateWhen
""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.

Styling

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.

Word count

Set data-count="words" and a data-maxwords limit.

Count without a limit

Without maxlength or data-maxwords the counter shows a running total and never changes state.

With data-grow

Both attributes work independently on the same textarea.

Localized text

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).

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.
  • data-autosave — restored drafts update the counter.
  • <chat-input> — a message composer; use it instead of a counted textarea when you need send controls.
  • minlength / maxlength — the native limits the counter reads.
↑