A resend button that keeps time.
Headless and drift-free. It survives reloads, stays in sync across tabs and waits exactly as long as your server says.
Enter the code sent to you@example.com
What the trigger renders right now
<buttondata-state="hydrating"data-remainingaria-disabled="true"aria-busy>
Reload the page or open it in another tab. The countdown keeps going.
The wordmark above is the same cooldown. When it runs out, click it.
Usage
Wrap your resend button
The countdown persists across reloads, syncs across tabs and never drifts. Style it however you like.
import { Cooldown } from 'input-cooldown'
export function ResendCode({ email }: { email: string }) {
return (
<Cooldown.Root
storageKey={`otp:${email}`}
duration={30}
autoStart
onResend={() => api.sendCode(email)}
>
<Cooldown.Trigger>
{cd =>
cd.status === 'counting'
? <>Resend in <Cooldown.Value /></>
: 'Resend code'
}
</Cooldown.Trigger>
<Cooldown.Announcer />
</Cooldown.Root>
)
}Features
Small API. The hard parts handled.
Duration
Pass a number of seconds, or an array to back off on each attempt. Combine it with maxAttempts to stop after a few sends.
// 1st send: 3s, 2nd: 6s, then 12s
<Cooldown.Root storageKey="otp" duration={[3, 6, 12]}>Why
Why not a setInterval?
Try the same things on both. The left one is the timer most of us write first.
- Drift. Intervals are delayed and throttled in background tabs. input-cooldown stores an absolute
endsAt. - Reloads. A refresh resets most timers and lets users spam the button. This one is persisted.
- Hidden tabs. Time keeps running, as it does on the server, but nothing re-renders.
- SSR. A
hydratingstatus keeps server and client markup equal. - Screen readers. Status changes are announced once, not every second.
0:30
0:30
Simulated. Both started at 30 seconds.
Stop resetting timers on F5.
Install it, wire up onResend and let the server set the pace. MIT licensed, zero dependencies.