@molecule/app-countdown-angular

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

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

npm install @molecule/app-countdown-angular

npm · Source on GitHub

How it works

@molecule/app-countdown-angular 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.

import { Component } from '@angular/core'
import { Countdown } from '@molecule/app-countdown-angular'

@Component({
  selector: 'app-sale',
  standalone: true,
  imports: [Countdown],
  template: `
    <ng-template #ended><span>Sale ended!</span></ng-template>
    <mol-countdown [target]="'2026-12-31T23:59:59Z'" [expired]="ended" />
  `,
})
export class Sale {}

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

Reference

Angular 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 function (a signal per value).
  • <Countdown> — display component with compact/long/colon formats and a custom render template.

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

import { Component } from '@angular/core'
import { Countdown } from '@molecule/app-countdown-angular'

@Component({
  selector: 'app-sale',
  standalone: true,
  imports: [Countdown],
  template: `
    <ng-template #ended><span>Sale ended!</span></ng-template>
    <mol-countdown [target]="'2026-12-31T23:59:59Z'" [expired]="ended" />
  `,
})
export class Sale {}

Type

feature

Installation

npm install @molecule/app-countdown-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

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.

Node-valued props are templates in Angular (an input cannot carry a node): expired and render are <ng-template> refs, render's receiving the live state as $implicit / a0.

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 as its template context. */
  render?: TemplateRef<unknown> | null
  /** Optional template rendered when the timer expires. */
  expired?: TemplateRef<unknown> | null
  /** Extra classes. */
  className?: string
}

Classes

Countdown

Countdown component.

Live countdown display with three default formats, the Angular counterpart of @molecule/app-countdown-react's Countdown: the same props (React names), the same DOM for the same input.

React's root is the display <span> — or, once the target passes and an expired node was given, that node alone (a fragment, with no wrapper). The rendered content therefore stands in front of the host's place and the host itself leaves the page (placeContentBefore + setElementPresent).

Node-valued props are <ng-template> refs (expired, and render, whose template context carries the live state as $implicit / a0).

@Component({
  selector: 'mol-countdown',
  standalone: true,
  imports: [NgTemplateOutlet],
  hostDirectives: [MolPlacement],
  inputs: ['target', 'format', 'render', 'expired', 'className'],
  template: `@if (expiredTemplate(); as expired) {
      <ng-container [ngTemplateOutlet]="expired" />
    } @else if (renderTemplate(); as custom) {
      <span [class]="className"
        ><ng-container [ngTemplateOutlet]="custom" [ngTemplateOutletContext]="renderContext()"
      /></span>
    } @else {
      <span [class]="classes()">{{ display() }}</span>
    }`,
})
export class Countdown implements DoCheck, OnDestroy {
  /** Target date / ISO string / epoch ms. */
  target!: Date | string | number
  /** Display format. Defaults to `'compact'` at read time. */
  format?: 'compact' | 'long' | 'colon'
  /** Renderer override — its template context carries the live state. */
  render?: TemplateRef<unknown> | null
  /** Optional template rendered when the timer expires. */
  expired?: TemplateRef<unknown> | null
  /** Extra classes. */
  className?: string

  /** React's `useCountdown` state, kept current by the interval. */
  private readonly state = signal<CountdownState>({
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    msRemaining: 0,
    expired: false,
  })

  /** The target the running timer belongs to (`null` before the first check). */
  private timerTarget: number | null = null

  /** Stops the running timer. */
  private stop: (() => void) | null = null

  /** The component's host element (React renders no element of its own). */
  private readonly host: ElementRef<HTMLElement> = inject(ElementRef)

  constructor() {
    // React's root is the display `<span>` (or the expired node) itself: it renders in
    // front of the host's place and the host leaves the page.
    usePlacement(() => {
      placeContentBefore(this.host.nativeElement, this.host.nativeElement)
      setElementPresent(this.host.nativeElement, false)
    })
  }

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * The wrapper class list (React's `cm.cn(className)`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(this.className)
  }

  /**
   * The display text for the current format (React's `format` branches).
   * @returns The display text.
   */
  display(): string {
    const current = this.state()
    return formatDisplay(
      this.format ?? 'compact',
      current.days,
      current.hours,
      current.minutes,
      current.seconds,
    )
  }

  /**
   * The `expired` template, when the target has passed and the caller supplied one.
   * @returns The template, or null.
   */
  expiredTemplate(): TemplateRef<unknown> | null {
    return this.state().expired && isTemplate(this.expired) ? this.expired : null
  }

  /**
   * The render template, when the caller supplied one.
   * @returns The template, or null.
   */
  renderTemplate(): TemplateRef<unknown> | null {
    return isTemplate(this.render) ? this.render : null
  }

  /**
   * The render template's context: React's `render(state)` argument as `$implicit` / `a0`.
   * @returns The template context.
   */
  renderContext(): Record<string, unknown> {
    const current = this.state()
    return { $implicit: current, a0: current, a1: undefined, a2: undefined }
  }

  /**
   * Follows the `target` input and starts, restarts or stops the timer. Angular
   * writes inputs as plain fields, so the change is picked up here rather than
   * by an effect.
   */
  ngDoCheck(): void {
    const ts = targetMs(this.target)
    if (ts === this.timerTarget) return
    this.timerTarget = ts
    this.stop?.()
    this.stop = tickCountdown(ts, 1_000, (next) => this.state.set(next))
  }

  /** Stops the timer when the component is destroyed. */
  ngOnDestroy(): void {
    this.stop?.()
    this.stop = null
  }
}

Functions

useCountdown(target, tickMs)

Live-updating countdown to a target date, React's useCountdown in Angular's idiom: call it in an injection context (a component field initializer or constructor). The target and interval may each be a plain value or a signal, and the countdown follows a changed signal on the same host — a component re-rendering with a new target input behaves as React's hook does.

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

Returns: A signal per live countdown field.

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.13.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

  • @angular/common
  • @angular/core
  • @angular/platform-browser
  • @molecule/app-angular
  • @molecule/app-ui
  • @molecule/app-ui-angular

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 pass a render <ng-template> whose context carries the live state ($implicit / a0) 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.