Vitral 0.2.18
Misc

Countdown

The time left until a moment, in tiles or in words, named in the reader’s language and announced to a screen reader without a word every second.

Import

main.ts
import { Countdown } from '@vitral/vue';

Basic

`to` is a Date, an ISO string or a timestamp. The unit names come from the browser, in the locale's language, and a leading unit at zero is left out.

<script setup lang="ts">import { Countdown } from '@vitral/vue'; const launch = new Date(Date.now() + ((3 * 24 + 5) * 60 + 42) * 60 * 1000);</script> <template>    <Countdown :to="launch" /></template>

Words, units and a pause

`variant="text"` writes it as a line. `units` picks what to count in — here minutes and seconds only, so an hour and a half reads 90 minutes. `paused` stops the clock where it is, and `end` fires when it gets there.

The session ends in .

<script setup lang="ts">import { Button, Countdown } from '@vitral/vue';import { ref } from 'vue'; const to = ref(Date.now() + 90 * 60 * 1000);const quick = ref(Date.now() + 15 * 1000);const paused = ref(false);const done = ref(false); function restart() {    quick.value = Date.now() + 15 * 1000;    done.value = false;}</script> <template>    <div style="display: grid; gap: 1rem">        <p style="margin: 0">The session ends in <Countdown :to="to" variant="text" :units="['minutes', 'seconds']" :paused="paused" />.</p>        <div style="display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center">            <Countdown :to="quick" :units="['seconds']" @end="done = true" />            <span role="status">{{ done ? 'Time is up.' : '' }}</span>        </div>        <div style="display: flex; gap: 0.5rem">            <Button :label="paused ? 'Resume' : 'Pause'" severity="secondary" size="small" @click="paused = !paused" />            <Button label="Fifteen seconds again" severity="secondary" size="small" @click="restart" />        </div>    </div></template>

Drawn your own way

The default slot is given what is left and whether it is over, and draws the rest itself.

05:11:59
<script setup lang="ts">import { Countdown } from '@vitral/vue'; const sale = new Date(Date.now() + (5 * 60 + 12) * 60 * 1000);const pad = (n: number) => String(n).padStart(2, '0');</script> <template>    <Countdown :to="sale">        <template #default="{ hours, minutes, seconds, done }">            <span v-if="done">The sale is over.</span>            <span v-else style="font: 600 1.5rem/1 ui-monospace, monospace; padding: 0.5rem 0.75rem; border-radius: 0.5rem; background: #111827; color: #fbbf24">                {{ pad(hours) }}:{{ pad(minutes) }}:{{ pad(seconds) }}            </span>        </template>    </Countdown></template>

API

Read from packages/vue/src/components/Countdown/types.ts, so it says what the component actually accepts.

Props

NameTypeDescription
to * Date | string | numberThe moment to count to: a Date, an ISO string or a timestamp.
units CountdownUnit[]The units to show, largest first. Defaults to days, hours, minutes and seconds.
variant 'tiles' | 'text'Tiles, or a line of words. Defaults to `'tiles'`.
hideLeadingZeros booleanLeave out the leading units that are at zero: no "0 days" a day before. Defaults to true.
paused booleanStops the clock where it is.

Plus pt, dt and unstyled from BaseProps, see pass-through and unstyled mode.

Emits

EventPayloadDescription
end —The moment arrived.

Slots

NameSlot propsDescription
default (context: { days: number; hours: number; minutes: number; seconds: number; done: boolean })Replaces the whole drawing: given what is left.