PXD
v0.2.1-dev

Countdown

Do something when you arrive at the appointed time.

Default

00:00:05
<script setup>
import { ref } from 'vue'

const active = ref(false)
const duration = ref(5000)

function resetStatus() {
  active.value = false
}
</script>

<template>
  <PStack direction="vertical">
    <PToggle v-model="active" label="Active" />

    <PCountdown :enabled="active" :duration="duration" @finish="resetStatus" />
  </PStack>
</template>

Format

01 00:00:00
01 00:00:00.0
01 00:00:00.00
01 00:00:00.000
<script setup>
import { ref } from 'vue'

const active = ref(false)
const duration = ref(86400000)

function resetStatus() {
  active.value = false
}
</script>

<template>
  <PStack direction="vertical">
    <PToggle v-model="active" label="Active" />

    <PCountdown
      :enabled="active"
      format="DD HH:mm:ss"
      :duration="duration"
      @finish="resetStatus"
    />

    <PCountdown
      :enabled="active"
      format="DD HH:mm:ss.S"
      :duration="duration"
      @finish="resetStatus"
    />

    <PCountdown
      :enabled="active"
      format="DD HH:mm:ss.SS"
      :duration="duration"
      @finish="resetStatus"
    />

    <PCountdown
      :enabled="active"
      format="DD HH:mm:ss.SSS"
      :duration="duration"
      @finish="resetStatus"
    />
  </PStack>
</template>

Intuitive

By default, because the time is less than one second, it will be displayed as 0.123 at the end, and it will be displayed as a time more in line with human intuition after setting.

The millisecond will always be 0 after turning on.
00:00:05.000
<script setup>
import { shallowRef } from 'vue'

const active = shallowRef(false)

function resetStatus() {
  active.value = false
}
</script>

<template>
  <PStack direction="vertical">
    <PToggle v-model="active" label="Active" />

    <PCountdown
      ref="countdownRef"
      intuitive
      :enabled="active"
      :duration="5000"
      format="HH:mm:ss.SSS"
      @finish="resetStatus"
    />
  </PStack>
</template>

Reset

00:00:10
<script setup>
import { shallowRef } from 'vue'

const active = shallowRef(false)
const countdownRef = shallowRef()

function reset() {
  countdownRef.value.reset()
}

function resetStatus() {
  active.value = false
}
</script>

<template>
  <PStack direction="vertical">
    <PToggle v-model="active" label="Active" />

    <PButton @click="reset">Reset</PButton>

    <PCountdown ref="countdownRef" :enabled="active" :duration="10000" @finish="resetStatus" />
  </PStack>
</template>

EndTime

Passing in a target time will automatically calculate the remaining time.

01:00:00
<script setup>
import { shallowRef } from 'vue'

const active = shallowRef(false)

const endTime = Date.now() + 3600000 // 1 hour later

function resetStatus() {
  active.value = false
}
</script>

<template>
  <PStack direction="vertical">
    <PToggle v-model="active" label="Active" />

    <PCountdown :enabled="active" :endTime="endTime" @finish="resetStatus" />
  </PStack>
</template>

Seconds timestamp

Passing in a target time will automatically calculate the remaining time.

01:00:00
<script setup>
import { shallowRef } from 'vue'

const active = shallowRef(false)

const endTime = 3600 // 3600 seconds(1 hour)

function resetStatus() {
  active.value = false
}
</script>

<template>
  <PStack direction="vertical">
    <PToggle v-model="active" label="Active" />

    <PCountdown :enabled="active" :millisecond="false" :duration="endTime" @finish="resetStatus" />
  </PStack>
</template>

StartAt

Specifies when to start, and is often used to resume timing from an interrupted timer.

00:00:07.000
00:00:03.000
<script setup>
import { ref } from 'vue'

const active = ref(false)

function resetStatus() {
  active.value = false
}
</script>

<template>
  <PStack direction="vertical">
    <PToggle v-model="active" label="Active" />

    <PCountdown
      :enabled="active"
      :start-at="3000"
      format="HH:mm:ss.SSS"
      :duration="10000"
      @finish="resetStatus"
    />

    <PCountdown
      inverted
      :enabled="active"
      :start-at="3000"
      format="HH:mm:ss.SSS"
      :duration="10000"
      @finish="resetStatus"
    />
  </PStack>
</template>

Invert

Setting the inverted property can be converted into positive timing, and the duration passed in is the end time. If it is not set, it will not stop automatically.

00:00:00
00:00:03
<script setup>
import { ref } from 'vue'

const active = ref(false)

function resetStatus() {
  active.value = false
}
</script>

<template>
  <PStack direction="vertical">
    <PToggle v-model="active" label="Active" />

    <PCountdown inverted :enabled="active" :duration="5000" @finish="resetStatus" />

    <!-- timers without duration will not stop voluntarily -->
    <PCountdown inverted :enabled="active" :start-at="3000" />
  </PStack>
</template>

Customize

00:05
<script setup>
import PlayCircleIcon from '@gdsicon/vue/play-circle'
import PauseCircleIcon from '@gdsicon/vue/pause-circle'

import { ref } from 'vue'

const active = ref(false)
</script>

<template>
  <div class="h-16">
    <PCountdown intuitive :enabled="active" :duration="5000" @finish="active = false">
      <template #default="{ times }">
        <PButton variant="primary" class="relative z-10 font-mono" @click="active = !active">
          <template #prefix>
            <Component :is="active ? PauseCircleIcon : PlayCircleIcon" />
          </template>

          {{ active ? 'Pause' : 'Start' }}
        </PButton>

        <div
          class="absolute bg-red-200 left-0 top-0 flex gap-1.5 items-center justify-center pt-0.5 text-sm px-2 w-full h-full rounded-bl-lg rounded-br-lg transition-all opacity-0 z-1"
          :class="{ 'translate-y-[90%] opacity-100': active }"
        >
          <i class="size-2 rounded-full bg-red-800" />
          {{ times.mm }}:{{ times.ss }}
        </div>
      </template>
    </PCountdown>
  </div>
</template>

Props

NameTypeDefaultDescription
enabledbooleanfalseStart the timer; switching it off pauses, switching it back on resumes
invertedbooleanfalseCount up instead of down
startAtnumber-Time already elapsed when the timer starts; in count-up mode the starting value
endTimenumber0Absolute end timestamp; takes precedence over duration
durationnumber0Timer length; in count-up mode 0 counts up indefinitely
autoResetbooleantrueRestart from the initial time when enabled turns on after finish
millisecondbooleantrueInterpret startAt, endTime and duration as milliseconds; false means seconds
intuitivebooleanfalseRound the remaining time up to whole seconds
formatstring'HH:mm:ss'dayjs duration format of the rendered value

Events

NameTypeDescription
change(enabled: boolean, details: ChangeDetails) => voidEmitted when the enabled prop changes.
reset() => voidEmitted when the countdown is restarted from its initial time.
finish() => voidEmitted when the countdown reaches its end.

Slots

NameDescription
defaultRenders the formatted time when empty. Slot props: enabled, times ({ dd, hh, mm, ss, ms }).

Methods

NameTypeDescription
reset() => voidRestart the countdown from its initial time.
times{ dd: string, hh: string, mm: string, ss: string, ms: string }Read the remaining time broken down into days, hours, minutes, seconds and milliseconds.

Source

Source