111 lines
3.8 KiB
Markdown
111 lines
3.8 KiB
Markdown
# wc-timer
|
|
|
|
A high-performance, dependency-free Web Component for countdowns, elapsed time, and static dates. It uses a shared ticker and cached `Intl` formatters for maximum efficiency.
|
|
|
|
## Features
|
|
|
|
* **High Performance**: Uses a shared global ticker (TickHub) to sync all instances. It calculates dates using O(1) math and caches `Intl` objects to save memory.
|
|
* **Smart Formatting**: Supports `Intl.DurationFormat` with a robust fallback for older browsers.
|
|
* **Versatile Presets**: Choose from `hms`, `dhms`, `ydhms`, `auto`, or static `date`.
|
|
* **Light DOM**: Renders as a standard Text Node. The text is selectable and inherits global CSS styles easily.
|
|
* **Auto-Restart**: If you update the target to a future date, the timer restarts automatically.
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npm install wc-timer
|
|
|
|
```
|
|
|
|
## Usage
|
|
|
|
1. **Import the script**:
|
|
|
|
```javascript
|
|
import 'wc-timer';
|
|
// Or via script tag:
|
|
// <script type="module" src="wc-timer.js"></script>
|
|
|
|
```
|
|
|
|
2. **Use in HTML**:
|
|
|
|
```html
|
|
<wc-timer target="2026-01-01T00:00:00Z"></wc-timer>
|
|
|
|
<wc-timer
|
|
target="2026-06-01T12:00:00Z"
|
|
preset="dhms"
|
|
labels="long"
|
|
compact
|
|
locale="pl">
|
|
</wc-timer>
|
|
|
|
<wc-timer preset="date" target="2025-12-31"></wc-timer>
|
|
|
|
```
|
|
|
|
## API Reference
|
|
|
|
### Attributes
|
|
|
|
| Attribute | Type | Default | Description |
|
|
| --- | --- | --- | --- |
|
|
| `target` | `string` | `now` | The target date (ISO 8601 string or Date compatible). |
|
|
| `preset` | `string` | `auto` | formatting mode: `auto`, `hms`, `dhms`, `ydhms`, `date`. |
|
|
| `labels` | `string` | `short` | unit labels: `none` (00:00:00), `short` (1h 5m), `long` (1 hour 5 minutes). |
|
|
| `compact` | `boolean` | `false` | if present, hides zero-value high units (e.g. hides "0 years"). |
|
|
| `locale` | `string` | `auto` | overrides browser language (e.g. `pl`, `en-US`). |
|
|
|
|
### Presets Details
|
|
|
|
* **auto**: shows `HH:MM:SS`. If duration > 30 days, it adds days.
|
|
* **hms**: hours, minutes, seconds.
|
|
* **dhms**: days, hours, minutes, seconds.
|
|
* **ydhms**: years, months, days, hours, minutes, seconds.
|
|
* **date**: static date formatted via `Intl.DateTimeFormat`.
|
|
|
|
### JavaScript Properties
|
|
|
|
You can control the element programmatically.
|
|
|
|
```javascript
|
|
const el = document.querySelector('wc-timer');
|
|
|
|
// Update target (accepts Date object, string, or timestamp)
|
|
el.target = new Date('2030-01-01');
|
|
|
|
// Change settings on the fly
|
|
el.preset = 'ydhms';
|
|
el.compact = true;
|
|
el.labels = 'long';
|
|
|
|
```
|
|
|
|
## Styling
|
|
|
|
Since `wc-timer` uses Light DOM, you can style it with standard CSS.
|
|
|
|
```css
|
|
wc-timer {
|
|
font-family: 'Courier New', monospace;
|
|
font-weight: bold;
|
|
color: #2563eb;
|
|
}
|
|
|
|
```
|
|
|
|
## Browser Support
|
|
|
|
Works in all modern browsers. It uses `Intl.DurationFormat` if available. Otherwise, it falls back to a custom formatter using `Intl.PluralRules`.
|
|
|
|
## License
|
|
|
|
Copyright 2026 Dávid Ali
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|