@monster-orange/den-ui 0.0.0-stage → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +75 -2
- package/package.json +21 -4
- package/src/base.css +80 -0
- package/src/busy-words.js +65 -0
- package/src/busy.js +592 -0
- package/src/dialog.js +217 -0
- package/src/fonts/OFL.txt +97 -0
- package/src/fonts/rubik-latin-400-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-500-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-ext-400-normal.woff2 +0 -0
- package/src/fonts/rubik-latin-ext-500-normal.woff2 +0 -0
- package/src/fonts/space-mono-latin-400-normal.woff2 +0 -0
- package/src/fonts/space-mono-latin-ext-400-normal.woff2 +0 -0
- package/src/fonts.css +71 -0
- package/src/time.js +242 -0
- package/src/tokens.css +150 -0
- package/src/update-pill.css +32 -0
- package/src/update-pill.js +95 -0
package/src/dialog.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The kit's ask-and-tell dialogs: askConfirm, askText and tell, in place of
|
|
3
|
+
* the browser's confirm(), prompt() and alert(), which speak only the
|
|
4
|
+
* browser's language and carry no Report. Framework-free; the product
|
|
5
|
+
* plugs in its language, its words and its Report:
|
|
6
|
+
*
|
|
7
|
+
* configureDialog({
|
|
8
|
+
* lang: () => "en", the page's language (en, es, ja)
|
|
9
|
+
* words: { en: {...}, ... }, extra or replaced shared words (ok, cancel)
|
|
10
|
+
* report: () => html, the Report button's HTML for the footer
|
|
11
|
+
* ("" or null: none)
|
|
12
|
+
* })
|
|
13
|
+
*
|
|
14
|
+
* askConfirm(opts) → Promise<boolean> OK true; Cancel or Esc false
|
|
15
|
+
* askText(opts) → Promise<string|null> the text; Cancel or Esc null
|
|
16
|
+
* tell(opts) → Promise<void> resolves when it closes
|
|
17
|
+
*
|
|
18
|
+
* opts.title the heading (the dialog's name)
|
|
19
|
+
* opts.message the body; line breaks show as written
|
|
20
|
+
* opts.okLabel OK's word (default: the kit's "OK")
|
|
21
|
+
* opts.cancelLabel Cancel's word (default: the kit's "Cancel")
|
|
22
|
+
* opts.danger OK destroys something: styled as danger, and focus
|
|
23
|
+
* starts on Cancel, the safe answer
|
|
24
|
+
* opts.value askText: the field's first text (selected)
|
|
25
|
+
* opts.label askText: the field's own name (default: the title)
|
|
26
|
+
*
|
|
27
|
+
* askConfirm and tell are role="alertdialog"; askText is role="dialog".
|
|
28
|
+
* Each is named by its title (aria-labelledby) and described by its
|
|
29
|
+
* message (aria-describedby). Focus moves in (Cancel for danger, the
|
|
30
|
+
* field for askText, else OK) and goes back to what had it on close.
|
|
31
|
+
* Enter in askText's field is OK.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
export const DIALOG_WORDS = {
|
|
35
|
+
en: { ok: "OK", cancel: "Cancel" },
|
|
36
|
+
es: { ok: "Aceptar", cancel: "Cancelar" },
|
|
37
|
+
ja: { ok: "OK", cancel: "キャンセル" },
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const hook = {
|
|
41
|
+
lang: () =>
|
|
42
|
+
(typeof document !== "undefined" &&
|
|
43
|
+
document.documentElement.lang?.slice(0, 2)) ||
|
|
44
|
+
"en",
|
|
45
|
+
words: {},
|
|
46
|
+
report: null,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** The product's language, words and Report (see the top of this file). */
|
|
50
|
+
export function configureDialog(h) {
|
|
51
|
+
Object.assign(hook, h ?? {});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A shared word, in the page's language, with the product's own on top. */
|
|
55
|
+
export function dialogWord(key) {
|
|
56
|
+
const l = hook.lang();
|
|
57
|
+
return (
|
|
58
|
+
hook.words?.[l]?.[key] ??
|
|
59
|
+
DIALOG_WORDS[l]?.[key] ??
|
|
60
|
+
hook.words?.en?.[key] ??
|
|
61
|
+
DIALOG_WORDS.en[key] ??
|
|
62
|
+
key
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const esc = (s) =>
|
|
67
|
+
String(s ?? "").replace(
|
|
68
|
+
/[&<>"']/g,
|
|
69
|
+
(c) =>
|
|
70
|
+
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[
|
|
71
|
+
c
|
|
72
|
+
],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
/** Tokens only (UK-1): no colour of its own. */
|
|
76
|
+
const CSS = `
|
|
77
|
+
.dk-dlg{font-family:var(--sans);position:fixed;inset-block:auto;inset-inline:0;top:50%;transform:translateY(-50%);margin:0 auto;height:auto;max-height:calc(100vh - 32px);max-height:calc(100dvh - 32px);overflow:auto;width:min(440px,calc(100vw - 32px));max-width:100%;padding:0;border:1px solid var(--border-strong);border-radius:var(--radius);background:var(--surface);color:var(--text);box-shadow:var(--shadow-dialog);box-sizing:border-box}
|
|
78
|
+
.dk-dlg::backdrop{background:color-mix(in srgb,var(--sunken) 70%,transparent)}
|
|
79
|
+
.dk-b{padding:var(--space-4) calc(var(--space-4) + 2px);display:grid;gap:10px;margin:0}
|
|
80
|
+
.dk-b h2{font-size:var(--fs-l);margin:0;overflow-wrap:anywhere}
|
|
81
|
+
.dk-msg{margin:0;font-size:var(--fs-s);line-height:1.5;white-space:pre-wrap;overflow-wrap:anywhere;max-height:50dvh;overflow:auto}
|
|
82
|
+
.dk-in{font:inherit;font-size:var(--fs-s);min-height:var(--tap-min);padding:0 var(--space-2);border:1px solid var(--border-strong);border-radius:var(--radius-button);background:var(--bg);color:var(--text);width:100%;box-sizing:border-box}
|
|
83
|
+
.dk-in:focus-visible{outline:2px solid var(--accent);outline-offset:1px}
|
|
84
|
+
.dk-ft{display:flex;flex-wrap:wrap;gap:var(--space-2);justify-content:flex-end;align-items:center}
|
|
85
|
+
.dk-ft > :first-child:not(.dk-btn){margin-right:auto}
|
|
86
|
+
.dk-btn{font:inherit;font-size:var(--fs-s);min-height:var(--tap-min);padding:0 var(--space-3);border-radius:var(--radius-button);border:1px solid var(--border-strong);background:var(--surface);color:var(--text);cursor:pointer}
|
|
87
|
+
.dk-btn:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
|
|
88
|
+
.dk-btn.dk-primary{background:var(--accent-fill);border-color:var(--accent-fill);color:var(--on-accent)}
|
|
89
|
+
.dk-btn.dk-danger{background:var(--bad);border-color:var(--bad);color:var(--on-accent)}
|
|
90
|
+
@media (max-width:480px){.dk-btn{min-height:var(--tap)}}
|
|
91
|
+
`;
|
|
92
|
+
|
|
93
|
+
function ensureCss() {
|
|
94
|
+
if (document.getElementById("dk-css")) return;
|
|
95
|
+
const s = document.createElement("style");
|
|
96
|
+
s.id = "dk-css";
|
|
97
|
+
s.textContent = CSS;
|
|
98
|
+
document.head.append(s);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let seq = 0;
|
|
102
|
+
|
|
103
|
+
/** The dialog's markup, for one kind: confirm, text or tell. */
|
|
104
|
+
export function dialogHtml(kind, opts, ids) {
|
|
105
|
+
const o = opts ?? {};
|
|
106
|
+
const okCls = o.danger ? "dk-danger" : "dk-primary";
|
|
107
|
+
const report = (typeof hook.report === "function" && hook.report()) || "";
|
|
108
|
+
const title = o.title ?? "";
|
|
109
|
+
const message = o.message ?? "";
|
|
110
|
+
return (
|
|
111
|
+
`<form class="dk-b" method="dialog">` +
|
|
112
|
+
(title ? `<h2 id="${ids.h}">${esc(title)}</h2>` : "") +
|
|
113
|
+
(message ? `<p class="dk-msg" id="${ids.m}">${esc(message)}</p>` : "") +
|
|
114
|
+
(kind === "text"
|
|
115
|
+
? `<input class="dk-in" type="text" data-dk="field" autocomplete="off" spellcheck="false" aria-label="${esc(o.label ?? title)}" value="${esc(o.value ?? "")}">`
|
|
116
|
+
: "") +
|
|
117
|
+
`<div class="dk-ft">${report}` +
|
|
118
|
+
(kind === "tell"
|
|
119
|
+
? ""
|
|
120
|
+
: `<button type="button" class="dk-btn" data-dk="cancel">${esc(o.cancelLabel ?? dialogWord("cancel"))}</button>`) +
|
|
121
|
+
`<button type="submit" class="dk-btn ${okCls}" data-dk="ok">${esc(o.okLabel ?? dialogWord("ok"))}</button>` +
|
|
122
|
+
`</div></form>`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Where focus starts: Cancel for danger, the field for text, else OK. */
|
|
127
|
+
export function firstFocus(kind, opts) {
|
|
128
|
+
if (kind === "text") return "field";
|
|
129
|
+
if (kind === "confirm" && opts?.danger) return "cancel";
|
|
130
|
+
return "ok";
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** What a closed dialog answers: OK's answer, else Cancel's (Esc too). */
|
|
134
|
+
export function answerOf(kind, ok, text) {
|
|
135
|
+
if (kind === "confirm") return ok;
|
|
136
|
+
if (kind === "text") return ok ? String(text ?? "") : null;
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function open(kind, opts = {}) {
|
|
141
|
+
ensureCss();
|
|
142
|
+
const n = ++seq;
|
|
143
|
+
const ids = { h: `dk-h-${n}`, m: `dk-m-${n}` };
|
|
144
|
+
const opener = document.activeElement;
|
|
145
|
+
const d = document.createElement("dialog");
|
|
146
|
+
d.className = "dk-dlg";
|
|
147
|
+
d.dataset.dkKind = kind;
|
|
148
|
+
d.setAttribute("role", kind === "text" ? "dialog" : "alertdialog");
|
|
149
|
+
d.setAttribute("aria-modal", "true");
|
|
150
|
+
d.innerHTML = dialogHtml(kind, opts, ids);
|
|
151
|
+
if (d.querySelector(`#${ids.h}`)) d.setAttribute("aria-labelledby", ids.h);
|
|
152
|
+
else if (d.querySelector(`#${ids.m}`))
|
|
153
|
+
d.setAttribute("aria-labelledby", ids.m);
|
|
154
|
+
if (opts.title && d.querySelector(`#${ids.m}`))
|
|
155
|
+
d.setAttribute("aria-describedby", ids.m);
|
|
156
|
+
document.body.append(d);
|
|
157
|
+
|
|
158
|
+
return new Promise((resolve) => {
|
|
159
|
+
let done = false;
|
|
160
|
+
const field = d.querySelector('[data-dk="field"]');
|
|
161
|
+
const finish = (ok) => {
|
|
162
|
+
if (done) return;
|
|
163
|
+
done = true;
|
|
164
|
+
const answer = answerOf(kind, ok, field?.value);
|
|
165
|
+
if (d.open) d.close();
|
|
166
|
+
d.remove();
|
|
167
|
+
try {
|
|
168
|
+
if (opener && opener.isConnected && opener !== document.body)
|
|
169
|
+
opener.focus?.();
|
|
170
|
+
} catch {
|
|
171
|
+
/* nothing to go back to */
|
|
172
|
+
}
|
|
173
|
+
resolve(answer);
|
|
174
|
+
};
|
|
175
|
+
// Esc is Cancel (for tell, it closes). Handled on the dialog's own
|
|
176
|
+
// keydown too: a page that takes Esc first (or a browser that closes
|
|
177
|
+
// without a cancel event) must never leave it open or unanswered.
|
|
178
|
+
d.addEventListener("cancel", (e) => {
|
|
179
|
+
e.preventDefault();
|
|
180
|
+
finish(kind === "tell");
|
|
181
|
+
});
|
|
182
|
+
d.addEventListener("keydown", (e) => {
|
|
183
|
+
if (e.key !== "Escape" || e.isComposing) return;
|
|
184
|
+
e.preventDefault();
|
|
185
|
+
e.stopPropagation();
|
|
186
|
+
finish(kind === "tell");
|
|
187
|
+
});
|
|
188
|
+
d.addEventListener("close", () => finish(kind === "tell"));
|
|
189
|
+
d.querySelector('[data-dk="cancel"]')?.addEventListener("click", () =>
|
|
190
|
+
finish(false),
|
|
191
|
+
);
|
|
192
|
+
d.querySelector("form").addEventListener("submit", (e) => {
|
|
193
|
+
e.preventDefault();
|
|
194
|
+
finish(true);
|
|
195
|
+
});
|
|
196
|
+
if (typeof d.showModal === "function") d.showModal();
|
|
197
|
+
else d.setAttribute("open", "");
|
|
198
|
+
const first = d.querySelector(`[data-dk="${firstFocus(kind, opts)}"]`);
|
|
199
|
+
first?.focus();
|
|
200
|
+
if (first === field) field.select?.();
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** In place of confirm(): true on OK; false on Cancel or Esc. */
|
|
205
|
+
export function askConfirm(opts) {
|
|
206
|
+
return open("confirm", opts);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** In place of prompt(): the text on OK (Enter too); null on Cancel or Esc. */
|
|
210
|
+
export function askText(opts) {
|
|
211
|
+
return open("text", opts);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** In place of alert(): resolves once it is closed (OK or Esc). */
|
|
215
|
+
export function tell(opts) {
|
|
216
|
+
return open("tell", opts);
|
|
217
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
Rubik (rubik-*.woff2):
|
|
2
|
+
Copyright 2015 The Rubik Project Authors (https://github.com/googlefonts/rubik)
|
|
3
|
+
|
|
4
|
+
Space Mono (space-mono-*.woff2):
|
|
5
|
+
Copyright 2016 The Space Mono Project Authors (https://github.com/googlefonts/spacemono)
|
|
6
|
+
|
|
7
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
8
|
+
This license is copied below, and is also available with a FAQ at:
|
|
9
|
+
http://scripts.sil.org/OFL
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
-----------------------------------------------------------
|
|
13
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
14
|
+
-----------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
PREAMBLE
|
|
17
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
18
|
+
development of collaborative font projects, to support the font creation
|
|
19
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
20
|
+
open framework in which fonts may be shared and improved in partnership
|
|
21
|
+
with others.
|
|
22
|
+
|
|
23
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
24
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
25
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
26
|
+
redistributed and/or sold with any software provided that any reserved
|
|
27
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
28
|
+
however, cannot be released under any other type of license. The
|
|
29
|
+
requirement for fonts to remain under this license does not apply
|
|
30
|
+
to any document created using the fonts or their derivatives.
|
|
31
|
+
|
|
32
|
+
DEFINITIONS
|
|
33
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
34
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
35
|
+
include source files, build scripts and documentation.
|
|
36
|
+
|
|
37
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
38
|
+
copyright statement(s).
|
|
39
|
+
|
|
40
|
+
"Original Version" refers to the collection of Font Software components as
|
|
41
|
+
distributed by the Copyright Holder(s).
|
|
42
|
+
|
|
43
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
44
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
45
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
46
|
+
new environment.
|
|
47
|
+
|
|
48
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
49
|
+
writer or other person who contributed to the Font Software.
|
|
50
|
+
|
|
51
|
+
PERMISSION & CONDITIONS
|
|
52
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
53
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
54
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
55
|
+
Software, subject to the following conditions:
|
|
56
|
+
|
|
57
|
+
1) Neither the Font Software nor any of its individual components,
|
|
58
|
+
in Original or Modified Versions, may be sold by itself.
|
|
59
|
+
|
|
60
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
61
|
+
redistributed and/or sold with any software, provided that each copy
|
|
62
|
+
contains the above copyright notice and this license. These can be
|
|
63
|
+
included either as stand-alone text files, human-readable headers or
|
|
64
|
+
in the appropriate machine-readable metadata fields within text or
|
|
65
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
66
|
+
|
|
67
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
68
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
69
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
70
|
+
presented to the users.
|
|
71
|
+
|
|
72
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
73
|
+
Software shall not be used to promote, endorse or advertise any
|
|
74
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
75
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
76
|
+
permission.
|
|
77
|
+
|
|
78
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
79
|
+
must be distributed entirely under this license, and must not be
|
|
80
|
+
distributed under any other license. The requirement for fonts to
|
|
81
|
+
remain under this license does not apply to any document created
|
|
82
|
+
using the Font Software.
|
|
83
|
+
|
|
84
|
+
TERMINATION
|
|
85
|
+
This license becomes null and void if any of the above conditions are
|
|
86
|
+
not met.
|
|
87
|
+
|
|
88
|
+
DISCLAIMER
|
|
89
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
90
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
91
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
92
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
93
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
94
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
95
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
96
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
97
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/src/fonts.css
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* den-ui fonts: Rubik and Space Mono, self-hosted woff2 (SIL OFL 1.1, see
|
|
3
|
+
* fonts/OFL.txt). Latin and latin-ext only; Japanese uses the system's
|
|
4
|
+
* fonts. Bold is synthesised from the heaviest face here.
|
|
5
|
+
*/
|
|
6
|
+
@font-face {
|
|
7
|
+
font-family: "Rubik";
|
|
8
|
+
font-style: normal;
|
|
9
|
+
font-weight: 400;
|
|
10
|
+
font-display: swap;
|
|
11
|
+
src: url("fonts/rubik-latin-400-normal.woff2") format("woff2");
|
|
12
|
+
unicode-range:
|
|
13
|
+
U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
|
|
14
|
+
U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212,
|
|
15
|
+
U+2215, U+FEFF, U+FFFD;
|
|
16
|
+
}
|
|
17
|
+
@font-face {
|
|
18
|
+
font-family: "Rubik";
|
|
19
|
+
font-style: normal;
|
|
20
|
+
font-weight: 400;
|
|
21
|
+
font-display: swap;
|
|
22
|
+
src: url("fonts/rubik-latin-ext-400-normal.woff2") format("woff2");
|
|
23
|
+
unicode-range:
|
|
24
|
+
U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304,
|
|
25
|
+
U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB,
|
|
26
|
+
U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
|
|
27
|
+
}
|
|
28
|
+
@font-face {
|
|
29
|
+
font-family: "Rubik";
|
|
30
|
+
font-style: normal;
|
|
31
|
+
font-weight: 500;
|
|
32
|
+
font-display: swap;
|
|
33
|
+
src: url("fonts/rubik-latin-500-normal.woff2") format("woff2");
|
|
34
|
+
unicode-range:
|
|
35
|
+
U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
|
|
36
|
+
U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212,
|
|
37
|
+
U+2215, U+FEFF, U+FFFD;
|
|
38
|
+
}
|
|
39
|
+
@font-face {
|
|
40
|
+
font-family: "Rubik";
|
|
41
|
+
font-style: normal;
|
|
42
|
+
font-weight: 500;
|
|
43
|
+
font-display: swap;
|
|
44
|
+
src: url("fonts/rubik-latin-ext-500-normal.woff2") format("woff2");
|
|
45
|
+
unicode-range:
|
|
46
|
+
U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304,
|
|
47
|
+
U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB,
|
|
48
|
+
U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
|
|
49
|
+
}
|
|
50
|
+
@font-face {
|
|
51
|
+
font-family: "Space Mono";
|
|
52
|
+
font-style: normal;
|
|
53
|
+
font-weight: 400;
|
|
54
|
+
font-display: swap;
|
|
55
|
+
src: url("fonts/space-mono-latin-400-normal.woff2") format("woff2");
|
|
56
|
+
unicode-range:
|
|
57
|
+
U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC,
|
|
58
|
+
U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212,
|
|
59
|
+
U+2215, U+FEFF, U+FFFD;
|
|
60
|
+
}
|
|
61
|
+
@font-face {
|
|
62
|
+
font-family: "Space Mono";
|
|
63
|
+
font-style: normal;
|
|
64
|
+
font-weight: 400;
|
|
65
|
+
font-display: swap;
|
|
66
|
+
src: url("fonts/space-mono-latin-ext-400-normal.woff2") format("woff2");
|
|
67
|
+
unicode-range:
|
|
68
|
+
U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304,
|
|
69
|
+
U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB,
|
|
70
|
+
U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
|
|
71
|
+
}
|
package/src/time.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Date-times for people: in the person's own time zone, never UTC.
|
|
3
|
+
*
|
|
4
|
+
* The data keeps ISO-8601 UTC; what a person reads is local. Framework-free
|
|
5
|
+
* and pure: formatTime runs in Node as well as in a page (it touches
|
|
6
|
+
* document or navigator only when they exist).
|
|
7
|
+
*
|
|
8
|
+
* formatTime(iso, opts?) → the text ("Nov 15, 4:32 PM"); "" for a
|
|
9
|
+
* missing or invalid stamp.
|
|
10
|
+
* timeHtml(iso, opts?) → '<time datetime="ISO-UTC" title="full local
|
|
11
|
+
* date-time with zone name" data-den-time="style">text</time>'.
|
|
12
|
+
* upgradeTimes(root = document) → every time[datetime][data-den-time]
|
|
13
|
+
* under root drawn again (after the zone changes).
|
|
14
|
+
* setTimeZone(zone | null) → the zone pick; null is the browser's.
|
|
15
|
+
*
|
|
16
|
+
* opts.style "datetime" (the default), "date", "time" or "relative"
|
|
17
|
+
* ("3 min ago"; its title is the local absolute time).
|
|
18
|
+
* opts.seconds true: the seconds too.
|
|
19
|
+
* opts.zone an IANA zone. Else <html data-den-tz> (a page), else
|
|
20
|
+
* setTimeZone's value, else the browser's (or process's).
|
|
21
|
+
* opts.locale else <html lang> (in the browser's variant of it), else
|
|
22
|
+
* the browser's.
|
|
23
|
+
* opts.now ms or ISO: "now" for relative and the year rule (tests,
|
|
24
|
+
* server use).
|
|
25
|
+
*
|
|
26
|
+
* The year shows only when it isn't the current year in that zone.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
const STYLES = ["datetime", "date", "time", "relative"];
|
|
30
|
+
|
|
31
|
+
let picked = null;
|
|
32
|
+
|
|
33
|
+
const hasDoc = () =>
|
|
34
|
+
typeof document !== "undefined" && !!document.documentElement;
|
|
35
|
+
|
|
36
|
+
/** The browser's (or process's) own zone. */
|
|
37
|
+
export function browserTimeZone() {
|
|
38
|
+
try {
|
|
39
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
|
40
|
+
} catch {
|
|
41
|
+
return "UTC";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Every IANA zone this runtime knows, or [] when it can't list them. */
|
|
46
|
+
export function timeZones() {
|
|
47
|
+
try {
|
|
48
|
+
return typeof Intl.supportedValuesOf === "function"
|
|
49
|
+
? Intl.supportedValuesOf("timeZone")
|
|
50
|
+
: [];
|
|
51
|
+
} catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** True when `zone` is a zone this runtime can format in. */
|
|
57
|
+
export function isTimeZone(zone) {
|
|
58
|
+
if (typeof zone !== "string" || !zone) return false;
|
|
59
|
+
try {
|
|
60
|
+
new Intl.DateTimeFormat("en", { timeZone: zone });
|
|
61
|
+
return true;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The zone pick: an IANA zone, or null for the browser's. On a page it is
|
|
69
|
+
* also <html data-den-tz>, so the attribute is the one source there.
|
|
70
|
+
*/
|
|
71
|
+
export function setTimeZone(zone) {
|
|
72
|
+
picked = isTimeZone(zone) ? zone : null;
|
|
73
|
+
if (hasDoc()) {
|
|
74
|
+
const ds = document.documentElement.dataset;
|
|
75
|
+
if (picked) ds.denTz = picked;
|
|
76
|
+
else delete ds.denTz;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The zone a stamp is drawn in. */
|
|
81
|
+
export function currentTimeZone(zone) {
|
|
82
|
+
if (isTimeZone(zone)) return zone;
|
|
83
|
+
if (hasDoc()) {
|
|
84
|
+
const z = document.documentElement.dataset?.denTz;
|
|
85
|
+
if (isTimeZone(z)) return z;
|
|
86
|
+
}
|
|
87
|
+
return picked ?? browserTimeZone();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The page's language, in the browser's own variant of it when they share
|
|
92
|
+
* a language ("en" with an en-GB browser reads en-GB: its 24-hour clock).
|
|
93
|
+
*/
|
|
94
|
+
function currentLocale(locale) {
|
|
95
|
+
if (locale) return locale;
|
|
96
|
+
const nav =
|
|
97
|
+
typeof navigator !== "undefined" && navigator.language
|
|
98
|
+
? navigator.language
|
|
99
|
+
: undefined;
|
|
100
|
+
const lang = hasDoc() ? document.documentElement.lang : "";
|
|
101
|
+
if (!lang) return nav;
|
|
102
|
+
if (nav && !lang.includes("-") && nav.split("-")[0] === lang) return nav;
|
|
103
|
+
return lang;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A Date for an ISO string, ms or Date; null when it isn't one. */
|
|
107
|
+
function toDate(v) {
|
|
108
|
+
if (v === null || v === undefined || v === "") return null;
|
|
109
|
+
const d = v instanceof Date ? new Date(v.getTime()) : new Date(v);
|
|
110
|
+
return Number.isNaN(d.getTime()) ? null : d;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function fmt(locale, o) {
|
|
114
|
+
try {
|
|
115
|
+
return new Intl.DateTimeFormat(locale, o);
|
|
116
|
+
} catch {
|
|
117
|
+
return new Intl.DateTimeFormat("en", o);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function yearIn(d, zone) {
|
|
122
|
+
return fmt("en", { timeZone: zone, year: "numeric" }).format(d);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function absolute(d, style, o) {
|
|
126
|
+
const f = { timeZone: o.zone };
|
|
127
|
+
const withYear = yearIn(d, o.zone) !== yearIn(o.now, o.zone);
|
|
128
|
+
if (style !== "time") {
|
|
129
|
+
f.month = "short";
|
|
130
|
+
f.day = "numeric";
|
|
131
|
+
if (withYear) f.year = "numeric";
|
|
132
|
+
}
|
|
133
|
+
if (style !== "date") {
|
|
134
|
+
f.hour = "numeric";
|
|
135
|
+
f.minute = "2-digit";
|
|
136
|
+
if (o.seconds) f.second = "2-digit";
|
|
137
|
+
}
|
|
138
|
+
return fmt(o.locale, f).format(d);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The full local date-time with the zone's name: a title. */
|
|
142
|
+
function full(d, o) {
|
|
143
|
+
return fmt(o.locale, {
|
|
144
|
+
timeZone: o.zone,
|
|
145
|
+
weekday: "short",
|
|
146
|
+
year: "numeric",
|
|
147
|
+
month: "short",
|
|
148
|
+
day: "numeric",
|
|
149
|
+
hour: "numeric",
|
|
150
|
+
minute: "2-digit",
|
|
151
|
+
second: "2-digit",
|
|
152
|
+
timeZoneName: "long",
|
|
153
|
+
}).format(d);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const UNITS = [
|
|
157
|
+
["year", 365 * 86400],
|
|
158
|
+
["month", 30 * 86400],
|
|
159
|
+
["day", 86400],
|
|
160
|
+
["hour", 3600],
|
|
161
|
+
["minute", 60],
|
|
162
|
+
];
|
|
163
|
+
|
|
164
|
+
function relative(d, o) {
|
|
165
|
+
const s = Math.round((d.getTime() - o.now.getTime()) / 1000);
|
|
166
|
+
let rtf;
|
|
167
|
+
try {
|
|
168
|
+
rtf = new Intl.RelativeTimeFormat(o.locale, { numeric: "auto" });
|
|
169
|
+
} catch {
|
|
170
|
+
rtf = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
|
|
171
|
+
}
|
|
172
|
+
const abs = Math.abs(s);
|
|
173
|
+
if (abs < 45) return rtf.format(0, "second");
|
|
174
|
+
for (const [unit, size] of UNITS)
|
|
175
|
+
if (abs >= size || unit === "minute")
|
|
176
|
+
return rtf.format(Math.round(s / size), unit);
|
|
177
|
+
return "";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function resolve(opts) {
|
|
181
|
+
const o = opts || {};
|
|
182
|
+
return {
|
|
183
|
+
style: STYLES.includes(o.style) ? o.style : "datetime",
|
|
184
|
+
seconds: !!o.seconds,
|
|
185
|
+
zone: currentTimeZone(o.zone),
|
|
186
|
+
locale: currentLocale(o.locale),
|
|
187
|
+
now: toDate(o.now) ?? new Date(),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The text a person reads for `iso`; "" when it isn't a date-time. */
|
|
192
|
+
export function formatTime(iso, opts) {
|
|
193
|
+
const d = toDate(iso);
|
|
194
|
+
if (!d) return "";
|
|
195
|
+
const o = resolve(opts);
|
|
196
|
+
return o.style === "relative" ? relative(d, o) : absolute(d, o.style, o);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** The full local date-time with its zone's name, for a title. */
|
|
200
|
+
export function formatTitle(iso, opts) {
|
|
201
|
+
const d = toDate(iso);
|
|
202
|
+
return d ? full(d, resolve(opts)) : "";
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const ESC = {
|
|
206
|
+
"&": "&",
|
|
207
|
+
"<": "<",
|
|
208
|
+
">": ">",
|
|
209
|
+
'"': """,
|
|
210
|
+
"'": "'",
|
|
211
|
+
};
|
|
212
|
+
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);
|
|
213
|
+
|
|
214
|
+
/** A <time> element's HTML for `iso`; "" when it isn't a date-time. */
|
|
215
|
+
export function timeHtml(iso, opts) {
|
|
216
|
+
const d = toDate(iso);
|
|
217
|
+
if (!d) return "";
|
|
218
|
+
const o = resolve(opts);
|
|
219
|
+
const text = formatTime(d, opts);
|
|
220
|
+
return (
|
|
221
|
+
`<time datetime="${esc(d.toISOString())}" title="${esc(full(d, o))}"` +
|
|
222
|
+
` data-den-time="${esc(o.style)}"${o.seconds ? ' data-den-seconds=""' : ""}>` +
|
|
223
|
+
`${esc(text)}</time>`
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Draws every time[datetime][data-den-time] under `root` again. */
|
|
228
|
+
export function upgradeTimes(root) {
|
|
229
|
+
const r = root || (hasDoc() ? document : null);
|
|
230
|
+
if (!r || typeof r.querySelectorAll !== "function") return;
|
|
231
|
+
for (const el of r.querySelectorAll("time[datetime][data-den-time]")) {
|
|
232
|
+
const iso = el.getAttribute("datetime");
|
|
233
|
+
const opts = {
|
|
234
|
+
style: el.getAttribute("data-den-time"),
|
|
235
|
+
seconds: el.hasAttribute("data-den-seconds"),
|
|
236
|
+
};
|
|
237
|
+
const text = formatTime(iso, opts);
|
|
238
|
+
if (!text) continue;
|
|
239
|
+
el.textContent = text;
|
|
240
|
+
el.title = formatTitle(iso, opts);
|
|
241
|
+
}
|
|
242
|
+
}
|