The companion to input-otp

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.

Get started
$ npm install input-cooldown

Enter the code sent to you@example.com

hydrating

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.

resend-code.tsx
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.

attempt 0
// 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 hydrating status keeps server and client markup equal.
  • Screen readers. Status changes are announced once, not every second.
How it works
setInterval

0:30

input-cooldown

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.