Skip to content
ComposerCharacter counterMenu

Character counter

Character or token budget for the composer, with soft and hard limits, a ring variant and announcements only at thresholds.

Soft and hard limits as text and as a ring.

Installation

$ pnpm dlx shadcn@latest add https://chatcn.dev/r/character-counter.json

Usage

tsx
import { CharacterCounter, getCounterState } from "@/components/ui/chatcn/character-counter"

<CharacterCounter value={text} limit={4000} />
<PromptInputSubmit disabled={getCounterState(text.length, 4000) === "over"} />

Examples

Token estimate

Approximate token counts with an optional budget.

Props

PropTypeNotes
valuestringCounted by grapheme, so emoji and accents count once
countnumberYour own count. Wins over value
limitnumberHard limit
softLimitnumberWarning starts here. Default 90% of limit
mode"characters" | "tokens"Tokens estimate about four characters each
estimate(text) => numberYour tokenizer
variant"text" | "ring"Ring shows the number only near the limit
hideBelownumberStay hidden for short messages

Accessibility

  • Screen readers hear it only when it crosses a threshold: 28 characters left, then 17 characters over the limit, shorten the message to send it.
  • Past the limit the color change comes with the overage in words.
  • Numbers are tabular so the width never jitters while typing.

Motion

  • Color changes over 150 ms; the ring fills linearly. Reduced motion snaps the ring.