Skip to content

Countdown

Counts down from a target time, exposing hours, minutes, seconds, and days via slot props.

bash
npm install @vuecs/countdown

Basic usage

vue
<script setup lang="ts">
import { VCCountdown } from '@vuecs/countdown';
const time = 3600 * 1000;
</script>

<template>
    <VCCountdown :time="time">
        <template #default="props">
            <span class="font-medium">
                {{ props.hours }}h
                {{ props.minutes }}m
                {{ props.seconds }}s
            </span>
            remaining
        </template>
    </VCCountdown>
</template>
css
@import "tailwindcss";
@import "@vuecs/design";

@custom-variant dark (&:where(.dark, .dark *));

Props

PropTypeDefaultDescription
timenumber0Time in milliseconds to count down
autoStartbooleantrueStart the countdown automatically on mount and whenever time / now change
emitEventsbooleantrueEmit the start / progress / abort / end events (set false to silence them)
intervalnumber1000Update interval in milliseconds between progress ticks
now() => number() => Date.now()Returns the current timestamp — used to compute the end time and to re-sync after the tab regains visibility
tagstring'span'HTML tag to render
themeClassPartial<CountdownThemeClasses>undefinedPer-instance theme override
themeVariantRecord<string, string | boolean>undefinedPer-instance variant values

Events

All events are suppressed when emitEvents is false.

EventPayloadDescription
startCountdown started — via autoStart or an explicit start() call
progressOmit<CountdownSlotProps, 'isCounting'>Fires on every interval tick while time remains, with the remaining time broken down ({ days, hours, minutes, seconds, milliseconds, totalDays, totalHours, totalMinutes, totalSeconds, totalMilliseconds })
abortCountdown stopped early via abort()
endCountdown reached zero (naturally or via end())

Slots

SlotPropsDescription
defaultCountdownSlotProps{ isCounting, days, hours, minutes, seconds, milliseconds, totalDays, totalHours, totalMinutes, totalSeconds, totalMilliseconds }

Exposed

Attach a template ref to inspect running state and drive the timer imperatively.

MemberTypeDescription
isCountingReadonly<Ref<boolean>>true while the countdown is running. Flips to false after abort() or natural completion (end)
start()() => voidBegin (or restart) the countdown
abort()() => voidStop and emit abort
end()() => voidStop, zero the remaining time, and emit end

See also

Released under the Apache 2.0 License.