x-countdown
Directives and a magic for countdown timers: a target date, per-unit display elements, screen-reader text, and an expiry flag.
Directives and a magic for countdown timers: a target date, per-unit display elements, screen-reader text, and an expiry flag.
x-countdown="2028-12-31T11:59:00.000-0500"
The root. The expression is the target date in ISO 8601 format
x-countdown="weekly"
Counts down to the next Monday at 9:00 AM Pacific Time
x-countdown.no-pad
Removes the leading zero from days, hours, and minutes ("5" instead of "05"). Seconds stay two-digit padded
x-countdown:days / :hours / :minutes / :seconds
Shows the remaining value of that unit as text
$countdown
The state, readable anywhere inside the root: days, hours, minutes, seconds, label, duration, isExpired, endDate(date)
The timer updates once a second, just after each whole second of remaining time, and stops when it reaches zero. The unit values are display strings, zero-padded to two digits by default. Wrap them in Number() before arithmetic. label and duration give the remaining time as words and as an ISO 8601 duration. isExpired is true once the remaining time is zero or the target is in the past. endDate(date) sets a new target on a running countdown.
Examples
A basic countdown
Use the four unit directives inside a root with the target date:
<time x-countdown="2028-12-31T11:59:00.000-0500">
<span x-countdown:days></span> :
<span x-countdown:hours></span> :
<span x-countdown:minutes></span> :
<span x-countdown:seconds></span>
</time>Include a timezone offset in the date, so every visitor counts down to the same instant. Analog's countdown snippet builds this string in Liquid from the schedule settings.
Reading the time to screen readers
Use $countdown.label in a visually hidden element, and put aria-hidden="true" on the visual units:
The label is the remaining time in words in the page language, such as "6 days, 16 hours, 55 minutes". It comes from Intl.DurationFormat, so it needs no translation strings. It has minute precision, so a binding that reads it updates once a minute. It counts seconds only in the last minute.
Do not put the label in a live region. A screen reader reads it when the visitor reaches the timer. Analog's countdown snippet adds aria-hidden to the digit wheels with Alpine, so screen readers read the server-rendered digits before Alpine starts.
Marking up the remaining time
Use $countdown.duration in the datetime attribute of a <time> element:
The duration has the same precision as the label, such as P6DT16H55M, or PT45S in the last minute. A <time> element without datetime must contain a valid date or duration, and "06 D : 16 H" is neither. Render the first value in Liquid, so the attribute is valid before Alpine starts.
Removing the leading zeros
Use the no-pad modifier to show "5" instead of "05":
Seconds keep their two-digit padding either way, so the layout does not jump twice a minute.
Reacting to expiry
Use $countdown.isExpired to swap the timer for an end message, or to hide it:
The countdown block's "When timer ends" setting controls both behaviors: "Show message" toggles on isExpired, and "Hide section" puts x-show="!$countdown.isExpired" on the root.
A weekly target
Use the weekly value to count down to the next Monday at 9:00 AM Pacific Time:
The target rolls over as soon as Monday 9:00 AM passes, so the timer never shows zero for a repeating schedule.
Options
The expression
The target: an ISO 8601 date, or weekly
The countdown snippet renders schedules, repeat frequency, end messages, and animated digit wheels. It is used in the toolbar and countdown sections. See Header, toolbar & navigation.
Last updated
Was this helpful?

