Relative Time¶
A live time, such as "3 minutes ago" or "in 2 hours", with the full date as its title. Every live time on a page runs on one shared clock, which ticks only when a text is due to change, more slowly as a value ages, and stops when none is left on the page.
Decision¶
| When | Use |
|---|---|
| A last sync, a last seen, or a next run | Relative Time |
| A wait that ends soon, such as a gathering window before a sync sends | countdown: true |
| A column in a Data Table, or a Filtered List row | renderRelative(key), as the column's render or in the row |
| A moment where the exact time matters more than how long ago it was | A plain date |
Usage¶
import { createRelativeTime, renderRelative, createDataTable } from './configurationpage?name=myplugin_stolon.js';
var last = createRelativeTime({ value: status.LastSync });
view.querySelector('#lastSync').appendChild(last.element);
var gather = createRelativeTime({ countdown: true, ring: true, start: status.GatherStart, value: status.GatherEnd, onDone: refresh });
createDataTable({ columns: [{ key: 'name', label: 'Server' }, { key: 'seen', label: 'Last Seen', render: renderRelative('seen') }], rows: servers });
Classes: stolon-relative-time, with stolon-countdown and stolon-done modifiers, and the part stolon-relative-time-text. The handle is { element, getValue, setValue, destroy }.
States and Modifiers¶
valueis a Date, milliseconds, or a date string such as an ISO date.setValue(value, start)shows another one.- The text comes from the browser's
Intl.RelativeTimeFormatin the page's language, orlocale. Under 45 seconds it reads "just now". - The text sits in a
timeelement withdatetimeset, and itstitleis the full date.emptyTextshows when there is no moment. - A countdown shows the time left as a clock, such as 1:28, and
doneText("Now") once it ends, whenonDoneruns once anddoneis added.ring: truewithstartadds a small pie Progress Ring that fills as the wait passes, named byringLabel("Time elapsed"). relativeText(diff, locale),countdownText(ms), andtoDate(value)give the same text and parsing without an element.
Gotchas¶
- A live time left on the page keeps ticking. One that leaves the page drops off the clock at its next tick, and
destroy()drops it at once.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createRelativeTime¶
function createRelativeTime(options?: RelativeTimeOptions): RelativeTimeHandle
A live time, such as "3 minutes ago" or "in 2 minutes", with the full date as its title. Every live time on a page runs on one shared clock that ticks only when a text is due to change, more slowly as a value ages, and stops when none is left on the page. The countdown mode shows the time left as a clock, such as 4:05, with an optional progress ring from start to the moment, and calls onDone once when it reaches zero.
| Option | Type | What it does |
|---|---|---|
value |
RelativeTimeValue |
the moment, a Date, milliseconds, or a date string [optional] |
countdown |
boolean |
true shows the time left as a clock (default false) |
start |
RelativeTimeValue |
when the wait began, which the ring fills from [optional] |
ring |
boolean |
true adds a small progress ring beside a countdown, which needs start (default false) |
doneText |
string |
the text once a countdown reaches zero (default 'Now') |
emptyText |
string |
the text with no moment (default '') |
ringLabel |
string |
the ring's accessible name (default 'Time elapsed') |
locale |
string |
the language of the text and the title (default the browser's) |
onDone |
(): void |
function() called once when a countdown reaches zero [optional] |
| Handle member | Type |
|---|---|
getValue |
(): Date | null |
setValue |
(value: RelativeTimeValue, start?: RelativeTimeValue): void |
destroy |
(): void |
renderRelative¶
function renderRelative<T>(pick: string | ((row: T) => RelativeTimeValue), options?: RelativeTimeOptions): (row: T) => HTMLElement
A render function for a Data Table column or a Filtered List that draws a live relative time from each row.
| Option | Type | What it does |
|---|---|---|
value |
RelativeTimeValue |
|
countdown |
boolean |
|
start |
RelativeTimeValue |
|
ring |
boolean |
|
doneText |
string |
|
emptyText |
string |
|
ringLabel |
string |
|
locale |
string |
|
onDone |
(): void |
| Type | What it is |
|---|---|
RelativeTimeValue |
A moment a relative time shows: a Date, milliseconds since 1970, or a date string such as an ISO date. |
| Class | What it looks like |
|---|---|
stolon-relative-time |
A live time, such as "3 minutes ago", with its full date as the title, in even width digits |
stolon-relative-time.stolon-countdown |
A countdown, a clock of the time left with an optional small ring that fills as the wait passes |
Example¶
Example Code¶
<!-- title: Relative Time -->
<div id="mount" style="display: grid; gap: 12px;"></div>
<script type="module">
const base = await window.__baseReady;
const mount = document.getElementById('mount');
const row = (label, node) => {
const line = document.createElement('div');
line.textContent = label + ' ';
line.appendChild(node);
mount.appendChild(line);
};
const now = Date.now();
row('Last sync', base.createRelativeTime({ value: now - 20 * 1000 }).element);
row('Last error', base.createRelativeTime({ value: now - 3 * 60 * 1000 }).element);
row('Next run', base.createRelativeTime({ value: now + 2 * 60 * 60 * 1000 }).element);
row('Added', base.createRelativeTime({ value: now - 9 * 24 * 60 * 60 * 1000 }).element);
row('Gathering ends in', base.createRelativeTime({ countdown: true, ring: true, start: now, value: now + 90 * 1000, doneText: 'Sending now' }).element);
const table = base.createDataTable({
columns: [
{ key: 'name', label: 'Server' },
{ key: 'seen', label: 'Last Seen', render: base.renderRelative('seen') }
],
rows: [{ name: 'Den', seen: now - 40 * 1000 }, { name: 'Cabin', seen: now - 26 * 60 * 1000 }, { name: 'Office', seen: now - 3 * 24 * 60 * 60 * 1000 }]
});
mount.appendChild(table.element);
</script>