@molecule/app-countdown-svelte

Feature · countdown · App (browser) · v1.1.2 · Apache-2.0

useCountdown hook + Countdown component for time-to-event displays

npm install @molecule/app-countdown-svelte

npm · Source on GitHub

How it works

@molecule/app-countdown-svelte is a ready-made countdown feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

<script lang="ts">
  import { Countdown } from '@molecule/app-countdown-svelte'
</script>

<!-- Compact default: "3d 4h 12m 5s" -->
<Countdown target="2026-12-31T23:59:59Z">
  {#snippet expired()}<span>Sale ended!</span>{/snippet}
</Countdown>

<!-- Colon format: "03:04:12:05" -->
<Countdown target={new Date('2026-12-31')} format="colon" />

<!-- Long format: "3 days 4 hours 12 minutes 5 seconds" -->
<Countdown target={Date.now() + 3_600_000} format="long" />

Works with: @molecule/app-svelte, @molecule/app-ui, @molecule/app-ui-svelte

Reference

Svelte time-remaining countdown — the counterpart of @molecule/app-countdown-react: the same exports, the same props (React names) and the same DOM for the same input.

Exports:

  • useCountdown(target, tickMs?) — live state hook (a readable store per value).
  • <Countdown> — display component with compact/long/colon formats and custom render.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

<script lang="ts">
  import { Countdown } from '@molecule/app-countdown-svelte'
</script>

<!-- Compact default: "3d 4h 12m 5s" -->
<Countdown target="2026-12-31T23:59:59Z">
  {#snippet expired()}<span>Sale ended!</span>{/snippet}
</Countdown>

<!-- Colon format: "03:04:12:05" -->
<Countdown target={new Date('2026-12-31')} format="colon" />

<!-- Long format: "3 days 4 hours 12 minutes 5 seconds" -->
<Countdown target={Date.now() + 3_600_000} format="long" />

Type

feature

Installation

npm install @molecule/app-countdown-svelte @molecule/app-svelte @molecule/app-ui @molecule/app-ui-svelte svelte

API

Interfaces

CountdownState

Snapshot of time remaining until a target date.

interface CountdownState {
  /** Days remaining. */
  days: number
  /** Hours (0-23) within current day. */
  hours: number
  /** Minutes (0-59) within current hour. */
  minutes: number
  /** Seconds (0-59) within current minute. */
  seconds: number
  /** Total milliseconds remaining (negative if past). */
  msRemaining: number
  /** True once `target` is in the past. */
  expired: boolean
}

CountdownProps

Props accepted by the Countdown component — React's prop names.

React's node-valued and render props are Svelte snippets here: render receives the live state, and expired renders once the target has passed.

interface CountdownProps {
  /** Target date / ISO string / epoch ms. */
  target: Date | string | number
  /** Display format. Defaults to `'compact'`. */
  format?: 'compact' | 'long' | 'colon'
  /** Renderer override — receives the live state for full control. */
  render?: Snippet<[CountdownState]>
  /** Optional rendered when the timer expires. */
  expired?: Snippet
  /** Extra classes. */
  className?: string
}

Functions

useCountdown(target, tickMs)

Live-updating countdown to a target date, React's useCountdown in Svelte's idiom: called during component init under a component tree, with a readable store per live value. The target and interval may each be a plain value or a readable store; the hook follows a changed store for the component's life, the way React's hook follows a re-render with new arguments.

function useCountdown(
  target: Date | string | number | Readable<Date | string | number>,
  tickMs?: number | undefined | Readable<number | undefined>,
): {
  /** Days remaining. */
  days: Readable<number>
  /** Hours (0-23) within current day. */
  hours: Readable<number>
  /** Minutes (0-59) within current hour. */
  minutes: Readable<number>
  /** Seconds (0-59) within current minute. */
  seconds: Readable<number>
  /** Total milliseconds remaining (negative if past). */
  msRemaining: Readable<number>
  /** True once the target is in the past. */
  expired: Readable<boolean>
}
  • target — Date / ISO string / epoch ms, or a store of one.
  • tickMs — Refresh interval, or a store of one. Defaults to 1000; 0 or less disables it.

Returns: A readable store per live countdown field.

Constants

Countdown

const Countdown: LegacyComponentType

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-svelte ^3.5.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-svelte ^1.1.0
  • svelte ^5.0.0

Runtime Dependencies

  • @molecule/app-svelte
  • @molecule/app-ui
  • @molecule/app-ui-svelte
  • svelte

The built-in 'compact' and 'long' formats hardcode English unit labels and pluralization ("3d 4h", "3 days 4 hours") — there is no locale bond. For localized apps use the render snippet with the live state and compose translated units via t() (or Intl.RelativeTimeFormat). expired only swaps the rendering once the target passes — schedule side effects (redirects, refetches) from useCountdown().expired in an effect, not from the component.