-
Notifications
You must be signed in to change notification settings - Fork 0
/
customElements.ts
192 lines (165 loc) · 5.19 KB
/
customElements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Displays a local date in a locale-specific way.
customElements.define('intl-date', class extends HTMLElement {
constructor() { super(); }
connectedCallback() {
if (!this.firstChild) {
const time = document.createElement('time');
time.setAttribute('role', 'time');
this.appendChild(time);
}
this.update();
}
attributeChangedCallback() { this.update(); }
static get observedAttributes() { return ['lang', 'data-year', 'data-month', 'data-day']; }
update() {
// `attributeChangedCallback` is called every time Elm adds each data attribute, i.e.
// `data-year`, `data-month` and `data-day`, even if the element is not connected to the
// DOM, which would cause the `month` and `day` variables below to be `undefined`.
if (!this.isConnected) {
return;
}
// The custom element is only created by `Elements.elm`, which always sets these properties.
const year = this.dataset.year!;
const month = this.dataset.month!;
const day = this.dataset.day!;
const date = new Date(+year, +month, +day);
const lang = this.getAttribute('lang');
const opts: Intl.DateTimeFormatOptions = {
month: 'long',
day: 'numeric',
weekday: 'short',
};
if (new Date().getFullYear() != date.getFullYear()) {
opts.year = 'numeric';
}
const fmt = new Intl.DateTimeFormat(lang ?? 'default', opts);
// The only child of the custom element is `<time>`, appended by `connectedCallback`.
const time = this.firstChild as HTMLTimeElement;
if (lang) {
time.removeAttribute('lang');
} else {
time.lang = fmt.resolvedOptions().locale;
}
time.dateTime = year.padStart(4, '0')
+ '-' + String(+month + 1).padStart(2, '0')
+ '-' + day.padStart(2, '0');
time.textContent = fmt.format(date);
}
});
// Displays a ECMAScript timestamp in a locale-specific way, converting it to local datetime.
customElements.define('intl-time', class extends HTMLElement {
constructor() { super(); }
connectedCallback() {
if (!this.firstChild) {
const time = document.createElement('time');
time.setAttribute('role', 'time');
this.appendChild(time);
}
this.update();
}
attributeChangedCallback() { this.update(); }
static get observedAttributes() { return ['lang', 'data-timestamp']; }
update() {
if (!this.isConnected) {
return;
}
const timestamp = this.dataset.timestamp!;
const date = new Date(+timestamp);
const lang = this.getAttribute('lang');
const fmt = new Intl.DateTimeFormat(lang ?? 'default', {
hour: 'numeric',
minute: 'numeric',
});
const time = this.firstChild as HTMLTimeElement;
if (lang) {
time.removeAttribute('lang');
} else {
time.lang = fmt.resolvedOptions().locale;
}
time.dateTime = date.toISOString();
// Set the inner HTML to something like `12<span class="time-separator">:</span>00 AM`.
//
// Reuse nodes from a previous `update()` call if possible, in order to:
//
// - Prevent CSS animation of the element from resetting
// - Prevent screen readers from announcing a change, which is not the case in fact
//
// ... which complicates the implementation a lot (meh).
const parts = fmt.format(date).split(':');
const nodes = Array.from(time.childNodes);
const first = parts.shift();
if (first) {
const node = nodes.shift();
if (node) {
// Note: First node is always a text node, if any.
if (node.nodeValue != first) {
node.replaceWith(first);
}
} else {
time.append(first);
}
for (const part of parts) {
const node = nodes.shift();
if (node) {
// Note: `node` is always a separator element, followed by a text node.
const text = nodes.shift()!;
if (text.nodeValue != part) {
text.replaceWith(part);
}
} else {
const separator = document.createElement('span');
separator.className = 'time-separator';
separator.textContent = ':';
time.append(separator);
time.append(part);
}
}
}
for (const node of nodes) {
time.removeChild(node);
}
}
});
// Displays a relative time in a locale-specific way.
customElements.define('intl-reltime', class extends HTMLElement {
constructor() { super(); }
connectedCallback() {
if (!this.firstChild) {
const time = document.createElement('time');
time.setAttribute('role', 'time');
this.appendChild(time);
}
this.update();
}
attributeChangedCallback() { this.update(); }
static get observedAttributes() { return ['lang', 'data-value', 'data-unit']; }
update() {
if (!this.isConnected) {
return;
}
const value = +this.dataset.value!;
const unit = this.dataset.unit as Intl.RelativeTimeFormatUnit;
const lang = this.getAttribute('lang');
const fmt = new Intl.RelativeTimeFormat(lang ?? 'default');
const time = this.firstChild as HTMLTimeElement;
if (lang) {
time.removeAttribute('lang');
} else {
time.lang = fmt.resolvedOptions().locale;
}
const absValue = value < 0 ? -value : value;
// This seems to be more UglifyJS-friendly than a `switch` statement.
time.dateTime =
unit == 'day'
? 'P' + absValue + 'D'
: 'PT' + absValue +
(unit == 'hour'
? 'H'
: unit == 'minute'
? 'M'
: // unit == 'second' ?
'S');
// Unused units: `year`, `quarter`, `month`, `week`.
time.textContent = fmt.format(value, unit);
}
});