SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

characterCount

Why JS.

tsx
import { characterCount } from "@sdxc/ui/mixins";

Usage

Applied to a CharacterCount's host: follows the field named by for, and on every input sets the host's data-state and --ui-character-count-progress, writes the number left into [data-character-count-number], and announces a change of state only, so a screen reader hears "20 characters left" once rather than at every key.

Signature

typescript
characterCount(options: CharacterCountSync.Options): MixinDescriptor<HTMLElement>

Parameters

ParameterTypeRequiredDescription
optionsCharacterCountSync.OptionsYes

Examples

tsx
<span mix={[characterCount({ for: "draft", max: 280, warn: 20, holdSubmit: true, labels })]} />

The events, constants and types its module publishes, each imported from @sdxc/ui/mixins too.

Function

countCharacters

Counts what a reader sees as characters: grapheme clusters, so an emoji with skin tone or a flag counts once. A server checking the same limit calls this, so the two never disagree.

typescript
countCharacters(text: string): number
ParameterTypeRequiredDescription
textstringYesThe field's text.
Function

characterCountState

Places a length against its limit.

typescript
characterCountState(length: number, max: number, warn: number): CharacterCountState
ParameterTypeRequiredDescription
lengthnumberYesThe characters typed.
maxnumberYesThe limit.
warnnumberYesHow many characters left start the warning.
Function

isRingHidden

Whether a count is far enough over its limit to show the number alone.

typescript
isRingHidden(left: number): boolean
ParameterTypeRequiredDescription
leftnumberYesCharacters left; negative past the limit.
Interface

CharacterCountSync.Options

Configuration for characterCount.

MemberTypeRequiredDescription
forstringYesThe id of the field counted.
maxnumberYes
warnnumberYesHow many characters left show the number.
holdSubmitbooleanYesDisables the field's form's submit buttons while it is empty or over the limit.
labels{ remaining: string; over: string }YesAnnouncements, with {count} replaced by the characters left or over.
Type

CharacterCountState

Where a field stands against its limit, which the count's data-state carries.

typescript
type CharacterCountState = "empty" | "ok" | "warn" | "over"
@sdxc/ui

Can’t find it? Contact us