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.
| Prop | Type | Default |
|---|---|---|
...optionsSame as useCooldown(). | UseCooldownOptions | — |
cooldownAn existing cooldown. Mutually exclusive with options. | CooldownApi | — |
childrenContent, 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.
| Prop | Type | Default |
|---|---|---|
childrenLabel, or a render function. | ReactNode | (cd) => ReactNode | — |
disabledBlocks it like a cooldown does (still focusable). | boolean | — |
| Attribute | Values |
|---|---|
data-state | the current status |
data-remaining | seconds left, while counting |
aria-disabled | true when it can't resend |
aria-busy | true while loading |
Cooldown.Value
A <span> with the remaining time, using tabular numbers.
| Prop | Type | Default |
|---|---|---|
formatDefault output is m:ss, e.g. 0:09. | (seconds: number) => ReactNode | formatRemaining |
Cooldown.Announcer
A visually hidden polite live region. It speaks when the status changes, never every second.
| Prop | Type | Default |
|---|---|---|
messagesOverride 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.