Skip to main content
239

Search Lumen

Find components, APIs, guides, and recipes.

GitHub
Themes
Web docs
Forms

CharacterCount

Shows the characters left for an input or textarea and announces politely only at thresholds.

Frameworks
Astro · React · Elements · Dioxus
Registry
lumen add CharacterCount
Interaction
Enhanced runtime
01

See it and copy it

Framework example

Choose Astro, React, Elements, or Dioxus to compare native usage against the shared visual output. Each example assumes the framework-level setup is already complete.

AstroCode target · live preview uses Astro

Shown on your public profile.

24 characters left

View CharacterCount code

Framework usage

Switch targets to compare the adapter code. This live preview runs the Astro example. Before copying an example, complete the matching framework setup; React, Elements, and Dioxus use their own behavior APIs.Width controls resize the preview container. Resize your browser to test viewport breakpoints.
astro
---
import { Button, CharacterCount, Field, FieldError, Form, Label, Textarea } from '@santi020k/lumen-astro'

const bio = 'Designer and maintainer of small, accessible interfaces.'
---

<Form data-ex-character-count style="display: grid; gap: 1rem; max-width: 30rem;">
  <Field controlId="ex-bio">
    <Label for="ex-bio">Bio</Label>
    <Textarea id="ex-bio" name="bio" rows={3}>{bio}</Textarea>
    <p data-ui-field-hint>Shown on your public profile.</p>
    <CharacterCount controlId="ex-bio" max={80} value={bio} />
    <FieldError />
  </Field>
  <Button type="submit">Save profile</Button>
</Form>

<script>
  import { countLumenCharacters } from '@santi020k/lumen-core'

  const form = document.querySelector('[data-ex-character-count]')

  // The counter never truncates or validates. The form owns the limit and its error message.
  form?.addEventListener('ui:validate', event => {
    const control = event instanceof CustomEvent ? event.detail?.control : null

    if (!(control instanceof HTMLTextAreaElement) || control.name !== 'bio') return

    control.setCustomValidity(countLumenCharacters(control.value) > 80 ? 'Shorten your bio to 80 characters or fewer.' : '')
  })

  form?.addEventListener('submit', event => {
    event.preventDefault()
  })
</script>
Edit CharacterCount in browser
02

Choose your target

Add this component

Choose the package for your runtime. All adapters share the Lumen stylesheet. Use the matching registry command when you want a local wrapper for that framework.

bash@santi020k/lumen-astro
pnpm add @santi020k/lumen-astro
bashAstro wrapper
lumen add CharacterCount

API reference

Lumen-specific props and runtime attributes for the Astro primitive. Use the framework differences above and the copyable examples for React composition and Elements attributes; the public adapter types define their supported contracts.

AttributeValuesDefaultDescription
maxnumberrequiredMaximum user-perceived characters (grapheme clusters; code points without Intl.Segmenter). Input is never truncated: validate the limit in the form. A missing or invalid limit renders no text.
controlIdstringenclosing Field controlID of the paired input or textarea. Elements uses for. The counter adds its text to the control's aria-describedby and removes it on unmount.
remainingLabel, overLabelstring | { one?, few?, many?, other, ... }"{count} characters left", "{count} characters over the limit"Templates with {count}, picked by Intl.PluralRules. Elements uses remaining-label and over-label and accepts a JSON object for plural forms.
announceAtnumber[][20, 10, 0]Remaining counts that update a polite live region when typing crosses them. Going over or back within the limit also announces; other keystrokes stay silent. Elements uses announce-at="20 10 0".
localestringruntime defaultFormats the count and selects plural forms through Intl. Malformed tags fall back to the default.
valuestring""Astro and React: the server-rendered or controlled value. Without it React reads the control on mount and on input.
data-state"within" | "near" | "over"-near starts at the highest announceAt value below max; over adds data-ui-character-count-over="true" to the control for styling.
class, classNamestring""Merges custom classes with the generated ui-* root classes.
...native attributesHTML attributes-Forwards standard attributes to the root element unless the component consumes them.

Customize CharacterCount

Start with semantic theme tokens and a class on the public component. Keep its accessible name, focus treatment, and interaction state intact.

cssScoped theme overrides
.product-character-count {
  --surface: 0 0% 100%;
  --ink: 220 20% 15%;
  --line: 220 15% 82%;
}

Apply product-character-count through Astro's class, React's className, or the custom element's class. Verify foreground and background contrast in both schemes.

HookValuesDefaultContract
data-state"within" | "near" | "over"-near starts at the highest announceAt value below max; over adds data-ui-character-count-over="true" to the control for styling.
class, classNamestring""Merges custom classes with the generated ui-* root classes.

Use the documented hooks above and adapter types when targeting component states. Theme guide · Accessibility verification scope


Web docsFull catalog