Components

Character Counter

Counts the characters or words in a field and shows them against a limit, turning to a warning near it and an error past it.

Usage

import { CharacterCounter } from 'nyctoui/character-counter'
import { Field, FieldLabel } from 'nyctoui/field'

<Field>
  <FieldLabel>Issue title</FieldLabel>
  <Input />
  <CharacterCounter max={60} />
</Field>

Put the counter anywhere inside a Field. It reads the field’s control as the user types, whether the input is controlled or not, so it needs no value.

The text is allowed to run past max, so people can paste and then trim. Once it does, the counter marks the field invalid: the input turns red, it gets aria-invalid, and a FieldError shows. Add a native maxLength to the input if you would rather stop typing at the limit.

Good for

  • A post composer in a social app, counting toward the character limit.
  • A bio or profile description field with a word limit.
  • A product description in a marketplace listing form.
  • An SMS or push notification editor in a marketing tool, where length decides the cost.

Accessibility

The counter is one of the field’s descriptions, so a screen reader reads it with the input on focus. It reads a full sentence, such as “12 characters left of 60”, in place of the short visible text.

While someone types, a polite live region announces the count after a one-second pause, and only when the count is near or past the limit or has just come back under it. Changes made from code, like clearing a draft, are not announced.

Examples

Words

Set mode="words" to count words. A word is a run of text between spaces that holds a letter or a number, as in a word processor, so “keyboard-first” counts once. Text in Chinese, Japanese, Thai and other scripts written without spaces is split by the browser’s dictionary. This example counts a Textarea, which works the same as an input.

Near the limit

The counter turns orange at 90% of max. Pass warnAt to start the warning at another count.

Over the limit

Past the limit the counter turns red and invalidates the field. Give the field a FieldError to say what to do about it.

With maxLength

Give the control a native maxLength to stop typing at the limit, and pass the counter the same number as max. Text can no longer run over, so the counter stays orange at the limit instead of turning red, and the field never turns invalid. Pasted text is cut off at the limit.

The browser measures maxLength in UTF-16 code units, where an emoji such as 🚀 takes two. When the control has one, the counter counts the same way, so it never shows room the browser won’t let anyone use.

Without a limit

Leave out max to show the count alone.

Custom text

Pass a function as children to write the visible text yourself. It gets the count, the limit, what is left and the status. Screen readers keep the built-in sentence.

Controlled

A controlled input works the same way. Values set from code, like the cleared reply here, update the count too.

Disabled

In a disabled field the counter dims with the label.

Outside a field

To count text that isn’t in a Field, pass it as value and link the counter to the control yourself.

<Input aria-label="Title" aria-describedby="title-count" value={title} onValueChange={setTitle} />
<CharacterCounter id="title-count" value={title} max={60} />

Best practices

Do
  • Put it inside the Field of the control it counts, so it reads the field as people type.
  • Set warnAt so the counter turns before the limit, while there’s still room to edit.
  • Count words with mode when the limit is in words, as for an abstract or an essay answer.
  • Let text run past max and show the error, so people can paste and then trim.
Don’t
  • Show a counter on a field with no real limit, or one nobody comes near. It adds noise.
  • Cut people off at the limit with maxLength and a counter that never warns them.

API reference

CharacterCounter

Renders a <p>, plus a hidden live region beside it. Inside a Field it is the field’s FieldDescription.

PropTypeDefault
max
The limit. Without one the counter shows the count alone.
number—
mode
What to count.
'characters' | 'words''characters'
warnAt
Count at which the counter turns to a warning.
numberceil(max × 0.9)
value
Text to count. Read from the field’s control when left out.
string—
children
Replaces the visible text. State holds count, max, remaining, mode and status.
(state: CharacterCounterState) => ReactNode—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

Data attributes

Set on the counter for styling.

PropTypeDefault
data-warning
The count has reached warnAt.
present—
data-exceeded
The count is past max.
present—
data-disabled
The field is disabled.
present—

countCharacters, countWords

The counting functions the component uses, for checking a value before submit.

PropTypeDefault
countCharacters
Counts what a person sees as characters: an emoji or an accented letter is one. The component uses this unless the control has a native maxLength; then it counts UTF-16 code units, as the browser does.
(text: string) => number—
countWords
Counts words as mode="words" does.
(text: string) => number—