Skip to content
caliper ui docs

Components

Char counter

A character counter drawn as a ring that fills as you type. Quiet and small until you near the limit; then it turns amber and shows how many are left, and past the limit it turns red and shows how many are over.

Free
pnpm dlx shadcn@latest add @caliper/char-counter

Profile bio

Type past the limit.

Installs components/ui/char-counter.tsxDepends on none beyond ReactMotion none in component source

When to use

Use it for

  • Posts, bios, descriptions and messages with a hard limit.
  • Fields where the limit is a rule, not a guess.

Skip it when

  • Fields with no limit or a very large one.
  • Word limits; count words yourself and pass them in as length and max.

Decisions

Why it is built this way, so you know what to keep when you change your copy.

Behaviour

Silent until it matters
Nothing is shown as a number until 85% of the limit, so it does not nag during ordinary typing.

Usage

Told twice, not every key
Screen readers hear how many are left once when the limit is near and once when it is passed; link it to the field with aria-describedby.

Installation

terminal
pnpm dlx shadcn@latest add @caliper/char-counter

Adds its 2 files to components/ui/, each with its own stylesheet, and installs their packages. No account needed. The first part also adds Caliper UI’s tokens; import them once as in Installation.

Usage

This is the code behind the preview above.

example.tsx
import { useState, type ReactNode } from "react"
import { CharCounter } from "@/components/ui/char-counter"
import { Textarea } from "@/components/ui/textarea"

function Panel({ title, context, className = "max-w-md", children }: { title: string; context: string; className?: string; children: ReactNode }) {
  return (
    <section className={`grid w-full gap-5 rounded-overlay border border-border bg-background p-6 shadow-resting max-sm:p-4 ${className}`}>
      <div className="grid gap-1">
        <p className="font-medium">{title}</p>
        <p className="text-sm text-muted-foreground">{context}</p>
      </div>
      {children}
    </section>
  );
}

export function CharCounterDemo() {
  const [text, setText] = useState("Shipping a small thing every week.");
  const max = 60;
  return (
    <Panel title="Profile bio" context="Type past the limit.">
      <Textarea aria-label="Bio" aria-describedby="bio-count" value={text} onChange={(event) => setText(event.target.value)} className="border border-border" />
      <div className="flex justify-end">
        <CharCounter id="bio-count" length={text.length} max={max} />
      </div>
    </Panel>
  );
}

API reference

CharCounter

component

Also accepts the native props of its root element and a ref.

PropTypeDefaultDescription
length *number—How many characters are in the field. Pass the string's length.
max *number—The most the field allows.
warnAtnumber0.85The share of the limit at which the ring warns and the figure appears.

Accessibility

It exposes status semantics, with aria-hidden kept in step with state. Changes that matter are announced through a live region.

No custom key handling; Tab and the native activation keys do the work.

Motion

It declares no animation in its component source. Any transition comes from its stylesheet, which uses the duration tokens that collapse to instant under prefers-reduced-motion.

All Changing values examples