2026-01-05 22:42:25 +01:00
2026-01-05 18:40:48 +01:00
2026-01-05 18:40:48 +01:00
2026-01-05 18:40:48 +01:00
2026-01-05 18:40:48 +01:00
2026-01-05 19:48:54 +01:00
2026-01-05 18:40:48 +01:00
2026-01-05 22:42:25 +01:00
2026-01-05 18:40:48 +01:00
2026-01-05 18:40:48 +01:00

wc-timer

A lightweight, dependency-free Web Component for countdowns and elapsed time. It uses the native Intl API for formatting and supports automatic language detection.

Features

  • Zero Dependencies: Built with standard Web Components API.
  • Smart Formatting: Supports various presets like hms, dhms, or ydhms.
  • Performance: Uses a shared global ticker (TickHub) to sync all instances and save CPU.
  • Light DOM: Renders text directly, making it easy to style with global CSS.
  • Responsive: Automatically updates when attributes change.

Installation

npm install wc-timer

Usage

  1. Import the script:
import 'wc-timer';
// Or via script tag
// <script type="module" src="path/to/wc-timer.js"></script>

  1. Use the tag in HTML:
<wc-timer target="2025-12-31T23:59:59Z"></wc-timer>

<wc-timer 
  target="2026-01-01T00:00:00Z" 
  preset="dhms" 
  labels="short" 
  compact
  locale="en-US">
</wc-timer>

API Reference

Attributes

Attribute Type Default Description
target ISO 8601 now The target date/time (e.g., 2025-12-31T23:59:59Z).
preset string auto Format preset: auto, hms, dhms, ydhms, date.
labels string long Unit labels: none (00:00:00), short (1h 5m), long (1 hour 5 minutes).
compact boolean false If present, hides zero-value units (e.g., skips "0 years").
locale BCP 47 auto Forces a specific language (e.g., pl, en, de). Defaults to browser settings.

JavaScript Properties

All attributes are reflected as properties on the DOM element.

const el = document.querySelector('wc-timer');

// Update target dynamically
el.target = new Date('2030-01-01');

// Change configuration
el.preset = 'ydhms';
el.compact = true;

Browser Support

Works in all modern browsers supporting Web Components and Intl.DurationFormat (or falls back gracefully).

License

MIT

Description
A high-performance, dependency-free Web Component for countdowns and timers.
Readme 97 KiB
Languages
JavaScript 87%
Dockerfile 6.6%
HTML 6.4%