Overview
A cooldown button disables itself after one click and counts down before it can be pressed again. Its label shows the countdown, then returns to its own label or to one you set. It is a .nds-cooldown modifier on Button, so every variant and size applies. The component owns only the cooldown and its countdown label.
Pick another component when:
- the action has no rate limit to respect: Button
Markup
Variants
The variants and sizes are the Button ones. The cooldown attributes are sample content: set the seconds and the labels in the markup.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Variant | Primary | .nds-primary |
.nds-btn |
The main action in a view. Use one per view |
| Variant | Neutral | .nds-neutral |
.nds-btn |
A strong action next to the primary one |
| Variant | Secondary (default) | .nds-secondary |
.nds-btn |
A supporting action, with a light fill |
| Variant | Secondary outline | .nds-secondary-outline |
.nds-btn |
A supporting action, with a border and no fill |
| Variant | Subtle | .nds-subtle |
.nds-btn |
A low-emphasis action. The fill shows on hover |
| Variant | Transparent | .nds-transparent |
.nds-btn |
The lowest emphasis. No fill in any state |
| Size | LG (default) | — | — | 40px high. It needs no class |
| Size | MD | .nds-md |
.nds-btn |
32px high |
| Size | SM | .nds-sm |
.nds-btn |
24px high |
Built-in Features
-
Auto-initialization
Every
.nds-cooldownon the page is wired at load, and any added later. No init call is needed. -
Live Countdown Label
The label shows the
data-cooldown-labeltext and updates every second, with each{s}replaced by the seconds left. -
Stable Width
The label reserves its widest text from page load: the first label, the countdown at its longest, or the resend label. The button never resizes during the cooldown.
-
Your Loading State
The component has no loading phase: a timer cannot know how long a response takes. Add
data-state="loading"fromnds:cooldown:start, and remove it when the response arrives. The state hides the label, so the countdown runs under the spinner. -
Your Confirmation
The component shows no alert of its own. Show one with Alert when the response arrives, in the same
nds:cooldown:startlistener. A failed send then never reports success. -
Programmatic Control
Start a cooldown from JavaScript, resume one after a page reload with the seconds left, or end one early.
Best Practices
- Use it for a resend action with a per-user rate limit, such as a one-time code (OTP), a verification email or a password reset. Set
data-cooldownto the server’s limit exactly. - Use it for a retry button after a failed request, so the user cannot send repeated requests to a server that is already failing.
- Do not use it to block a double form submit. Disable the submit button while the form sends instead.
- Do not use it for cooldowns over a few minutes. A long countdown reads as nagging and ties the user to the page. Show the time when the action is available again instead.
- Put the button text in a
<span class="nds-label">. The countdown writes into that span. A button without one never counts down and logs a console warning. - On an
.nds-icon-onlybutton, write an empty<span class="nds-label"></span>before the icon. The countdown takes the icon’s place in the same square, so the button keeps its size. Give the button anaria-label: the empty label gives it no name at rest. - Omit
data-resend-labelwhen the action reads the same every time, such as “Try again”. The button then returns to its first label. - Keep the countdown text short, such as “Resend in 30s”. Stable Width holds the button at its longest text, so a long countdown makes it wide at rest too. Shorten the wording, not the CSS.
- Call
NDS.Cooldown.reset()when the request fails. The user can then retry at once, instead of waiting out a cooldown for a send that failed.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-cooldown |
.nds-cooldown |
Seconds to hold the cooldown. Required, unless start() passes seconds. A value of 0 or less turns the cooldown off. Read once, when the button is wired. Editing it later has no effect |
data-cooldown-label |
.nds-cooldown |
Countdown text. Every {s} is replaced by the seconds left. Default {s}: the number alone. Only {s} counts down: %s, %d and {seconds} show as typed, and a label with no {s} logs a console warning when the button is wired. Read at the start of each cooldown |
data-resend-label |
.nds-cooldown |
Label shown when any cooldown ends, by the timer or by reset(). Omit it to restore the label the cooldown started with. Read at the start of each cooldown |
data-state="cooldown" |
.nds-cooldown |
Set by the component, together with disabled, during the cooldown. Read it to style or query a button during its cooldown. Do not set it yourself |
JavaScript
| Method | Effect |
|---|---|
NDS.Cooldown.init() |
Wires every .nds-cooldown on the page, now and later. Runs once at load. Call it only if you disabled the loader |
NDS.Cooldown.start(btn, opts?) |
Starts a cooldown by hand. Does nothing while a cooldown is already running |
NDS.Cooldown.reset(btn) |
Ends the cooldown early and restores the button, as a finished cooldown does |
| Option | Default | Effect |
|---|---|---|
seconds |
data-cooldown |
Seconds to run instead. With it, a button needs no data-cooldown and can run entirely from JavaScript |
silent |
false |
Skip nds:cooldown:start, for a send that already happened elsewhere. tick and end still fire |
All events bubble.
| Event | Fired on | Detail |
|---|---|---|
nds:cooldown:start |
.nds-cooldown, when the cooldown starts. Skipped by silent |
None. Issue the request here, and confirm from its response |
nds:cooldown:tick |
.nds-cooldown, every second during the cooldown, starting at the full duration |
{ remaining }: the seconds left |
nds:cooldown:end |
.nds-cooldown, when the cooldown finishes or reset() is called |
None. The button is enabled again and its label restored |
The full API is in the banner of _js/nds-cooldown.js.