CharacterCount sits under a Textarea or an Input and prints how much of the limit is used ("120 of 2,000") in meta. It follows the field named by htmlFor, so the field stays a server component. Inside the last 10 % the figure turns to the ink at weight 600 and says how many characters are left.
Example#
0 of 280
Usage#
Use a character count under any field with a hard limit, so writers see the budget before they run out. It suits messages, bios and short answers; a field with no limit needs no count.
- Placement. Directly under its field, in the meta role, linked with
aria-describedbyso the count is read with the field. - The last tenth. Inside the last 10 % of
maxLengththe figure turns to the text ink at weight 600 and a polite live region says how many characters are left. Before that it stays silent. - Server-safe fields. It follows the field named by
htmlFor, so theTextareaorInputstays a server component.
Anatomy#
0 of 280
- FigureUsed of the limit, in the meta role; near the end, what is left.
data-mk-part="figure"
Volume#
Reading layer: identical at every volume.
Quiet
0 of 280
Studio
0 of 280
Press
0 of 280
Rave
0 of 280
Content#
- The figure reads "120 of 2,000", with the thousands separator.
- Near the limit it says what is left: "18 characters left". It never scolds.
- Set limits that fit the answer you want; a 280-character "Tell us about your project" asks for a tweet.
Do and don't#
Do
0 of 280
Don't
Accessibility#
- The figure is not live, so typing is never interrupted; a polite live region speaks only inside the last 10 % ("200 characters left").
- Give it an
idand add that id to the field'saria-describedby. - The field's own
maxLengthstops the typing; the count only reports it.
The colour pairs it prints, with their contrast on both papers:
| Text | Surface | Role | Bone | Night |
|---|---|---|---|---|
--mk-text-2 | --mk-surface | ui | not in this look | not in this look |
--mk-text | --mk-surface | ui | not in this look | not in this look |
Props#
| Prop | Type | Default | Description |
|---|---|---|---|
htmlFor required | string | none | The id of the field it counts. |
maxLength required | number | none | |
defaultLength | number | 0 | The count before the field reports (server render; default 0). |
copy | Partial<CharacterCountCopy> | none |
Tokens#
System tokens (8)
Changelog#
Arrived in 0.9.0.
CharacterCount,TableSortButton(in./table) andBeforeAfter. Read the whole entry