Menu

Components

Unstyled parts that compose like Radix primitives. Every part also accepts a render function as children.

<Cooldown.Root storageKey="otp" onResend={send}>
  <Cooldown.Trigger>
    {cd => (cd.status === 'counting' ? <>Wait <Cooldown.Value /></> : 'Resend')}
  </Cooldown.Trigger>
  <Cooldown.Announcer />
</Cooldown.Root>

Cooldown.Root

Provides the state to the parts inside. Pass the useCooldown options to let it own the state, or cooldown to share one you created.

PropTypeDefault
...options
Same as useCooldown().
UseCooldownOptions—
cooldown
An existing cooldown. Mutually exclusive with options.
CooldownApi—
children
Content, or a render function.
ReactNode | (cd) => ReactNode—

Cooldown.Trigger

A native <button type="button">. It takes every button prop and forwards its ref. A click calls trigger() unless you call preventDefault() in your onClick.

PropTypeDefault
children
Label, or a render function.
ReactNode | (cd) => ReactNode—
disabled
Blocks it like a cooldown does (still focusable).
boolean—
AttributeValues
data-statethe current status
data-remainingseconds left, while counting
aria-disabledtrue when it can't resend
aria-busytrue while loading

Cooldown.Value

A <span> with the remaining time, using tabular numbers.

PropTypeDefault
format
Default output is m:ss, e.g. 0:09.
(seconds: number) => ReactNodeformatRemaining

Cooldown.Announcer

A visually hidden polite live region. It speaks when the status changes, never every second.

PropTypeDefault
messages
Override or silence (null) messages per status.
Partial<Record<Status, string | ((cd) => string) | null>>—

The default messages are in English. Translate them with messages:

<Cooldown.Announcer
  messages={{
    counting: cd => `Pode pedir um novo código dentro de ${cd.remaining} segundos.`,
    ready: 'Já pode pedir um novo código.',
  }}
/>

useCooldownContext()

Reads the nearest Root's state, to build custom parts.

Components | input-cooldown