@n1kname/components 0.18.0 → 0.20.0

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/src/facts.js CHANGED
@@ -1,31 +1,155 @@
1
- // Drives Facts: fills one from script (setFacts) and plays its two options once, as it comes into
2
- // view: the highlighter under a tile's figure, and icons that draw themselves in. Both are off
3
- // unless the Facts asks for them.
1
+ // Drives Facts: draws the facts (factsHTML, also used by Facts.astro at build time), fills them from
2
+ // script (setFacts, setFact) and plays its two options once, as it comes into view: the highlighter
3
+ // under a tile's figure, and icons that draw themselves in. Both are off unless the Facts asks.
4
+ //
5
+ // A fact: { label, value?, unit?, context?, delta?, icon?, accent?, key?, valueId?, contextId? }.
6
+ // unit small, after the figure: '638' + 'm³'.
7
+ // context the line under a tile's figure: 'against 699 in 2025'.
8
+ // delta a change, bold at the start of that line: { text: '−8.7%', tone: 'good' | 'bad' | 'neutral' },
9
+ // or a string for neutral. Tiles only, like context.
10
+ // key names a fact for setFact(); valueId and contextId put ids on the figure and the line,
11
+ // for a page that writes them itself, as with Stat.
12
+ import { settle } from '@n1kname/charts/update';
4
13
  import { ICONS } from './icons';
5
14
 
6
- const esc = (s) => String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
15
+ const esc = (s) => String(s ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
7
16
  const icon = (name, size) =>
8
17
  name && ICONS[name]
9
18
  ? `<svg width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="${size < 20 ? 1.75 : 2}" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[name]}</svg>`
10
19
  : '';
11
20
 
12
- // items: [{ label, value, icon?, accent? }], in the order they read.
13
- export const setFacts = (dl, items) => {
14
- const size = dl.dataset.variant === 'tiles' ? 16 : 14;
15
- const hl = dl.hasAttribute('data-highlight') && dl.dataset.variant === 'tiles';
16
- dl.innerHTML = items
17
- .map(
18
- (f, n) =>
19
- `<div style="--n:${n}${f.accent ? `;--accent:${f.accent}` : ''}"><dt>${f.icon ? `<span class="fact-icon">${icon(f.icon, size)}</span>` : ''}${esc(f.label)}</dt>` +
20
- `<dd>${hl ? `<mark class="highlight">${esc(f.value)}</mark>` : esc(f.value)}</dd></div>`,
21
- )
21
+ export const PLACEHOLDER = '–';
22
+ const TONES = ['good', 'bad', 'neutral'];
23
+ // Unknown is a dash, never 0 and never a unit on its own.
24
+ const known = (v) => v != null && v !== '' && v !== '–' && v !== '—';
25
+ const deltaOf = (d) => (d == null || d === '' ? null : typeof d === 'string' ? { text: d, tone: 'neutral' } : { text: d.text, tone: TONES.includes(d.tone) ? d.tone : 'neutral' });
26
+ const hasLine = (f) => f.context != null || f.contextId != null || deltaOf(f.delta) != null;
27
+ const sizeOf = (variant) => (variant === 'tiles' ? 16 : 14);
28
+
29
+ const dtHTML = (f, size) => `${f.icon ? `<span class="fact-icon">${icon(f.icon, size)}</span>` : ''}${esc(f.label)}`;
30
+ const lineHTML = (f) => {
31
+ const d = deltaOf(f.delta);
32
+ return `${d ? `<strong class="fact-delta" data-tone="${d.tone}">${esc(d.text)}</strong> ` : ''}${esc(f.context)}`;
33
+ };
34
+
35
+ /** The facts inside a Facts' <dl>, and the attributes the <dl> needs for them. */
36
+ export const factsHTML = (items = [], { variant = 'tiles', highlight = false } = {}) => {
37
+ const size = sizeOf(variant);
38
+ const tiles = variant === 'tiles';
39
+ const hl = highlight && tiles;
40
+ const html = items
41
+ .map((f, n) => {
42
+ const value = known(f.value) ? f.value : PLACEHOLDER;
43
+ const figure = hl ? `<mark class="highlight">${esc(value)}</mark>` : esc(value);
44
+ return (
45
+ `<div style="--n:${n}${f.accent ? `;--accent:${esc(f.accent)}` : ''}"${f.key != null ? ` data-key="${esc(f.key)}"` : ''}${f.icon ? ` data-icon="${esc(f.icon)}"` : ''}>` +
46
+ `<dt>${dtHTML(f, size)}</dt>` +
47
+ `<dd class="fact-value"><span class="fact-figure"${f.valueId ? ` id="${esc(f.valueId)}"` : ''}>${figure}</span><span class="fact-unit" data-unit="${esc(f.unit)}">${known(f.value) ? esc(f.unit) : ''}</span></dd>` +
48
+ (tiles ? `<dd class="fact-context"${f.contextId ? ` id="${esc(f.contextId)}"` : ''}>${lineHTML(f)}</dd>` : '') +
49
+ `</div>`
50
+ );
51
+ })
22
52
  .join('');
53
+ return { html, count: countOf(items.length), context: tiles && items.some(hasLine) };
54
+ };
55
+
56
+ // The tile strip balances its rows by how many there are; past six it simply fills.
57
+ const countOf = (n) => (n > 6 ? 'many' : String(n));
58
+
59
+ const mark = (dl, count, context) => {
60
+ dl.dataset.count = count;
61
+ if (context) dl.dataset.context = '';
62
+ };
63
+
64
+ /**
65
+ * Writes one fact's fields into its tile. Fields left out stay as they are, so setFact can change
66
+ * just the figure; setFacts passes every field.
67
+ */
68
+ const apply = (dl, div, f, all) => {
69
+ const size = sizeOf(dl.dataset.variant);
70
+ const has = (k) => all || k in f;
71
+ if (has('label') || has('icon')) {
72
+ const dt = div.querySelector('dt');
73
+ const label = has('label') ? f.label : dt.textContent;
74
+ const name = has('icon') ? f.icon : div.dataset.icon;
75
+ // Only when it changes, so icons that drew themselves in don't draw again.
76
+ if (dt.textContent !== String(label ?? '') || (div.dataset.icon ?? '') !== (name ?? '')) {
77
+ dt.innerHTML = dtHTML({ label, icon: name }, size);
78
+ if (name) div.dataset.icon = name;
79
+ else delete div.dataset.icon;
80
+ }
81
+ }
82
+ if (has('accent')) {
83
+ if (f.accent) div.style.setProperty('--accent', f.accent);
84
+ else div.style.removeProperty('--accent');
85
+ }
86
+ if (has('key')) {
87
+ if (f.key != null) div.dataset.key = f.key;
88
+ else delete div.dataset.key;
89
+ }
90
+ const fig = div.querySelector('.fact-figure');
91
+ const unit = div.querySelector('.fact-unit');
92
+ if (has('value')) {
93
+ const target = fig.querySelector('mark') ?? fig;
94
+ const value = known(f.value) ? String(f.value) : PLACEHOLDER;
95
+ const before = target.textContent;
96
+ if (before !== value) {
97
+ target.textContent = value;
98
+ // A figure that changes settles in, on a page that asks for update motion, as charts do.
99
+ if (known(before) && dl.closest('[data-chart-motion="update"]')) settle(fig.parentElement);
100
+ }
101
+ }
102
+ if (has('value') || has('unit')) {
103
+ const value = has('value') ? f.value : fig.textContent;
104
+ const u = has('unit') ? f.unit : unit.dataset.unit;
105
+ unit.dataset.unit = u ?? '';
106
+ unit.textContent = known(value) && u ? u : '';
107
+ }
108
+ const line = div.querySelector('.fact-context');
109
+ if (line && (has('context') || has('delta'))) {
110
+ const d = has('delta') ? f.delta : line.querySelector('.fact-delta') && { text: line.querySelector('.fact-delta').textContent, tone: line.querySelector('.fact-delta').dataset.tone };
111
+ const c = has('context') ? f.context : [...line.childNodes].filter((n) => n.nodeType === 3).map((n) => n.textContent).join('').trim();
112
+ line.innerHTML = lineHTML({ context: c, delta: d });
113
+ if (hasLine({ context: c || undefined, delta: d })) dl.dataset.context = '';
114
+ }
115
+ };
116
+
117
+ const tilesOf = (dl) => [...dl.children].filter((el) => el.tagName === 'DIV');
118
+
119
+ /**
120
+ * Fills a Facts from script. items: facts in the order they read. With as many facts as it has, it
121
+ * changes them in place, so a figure that changes can settle and nothing redraws; otherwise it draws
122
+ * them again and plays the options once more.
123
+ */
124
+ export const setFacts = (dl, items) => {
125
+ const tiles = tilesOf(dl);
126
+ if (tiles.length === items.length && tiles.length) {
127
+ items.forEach((f, i) => apply(dl, tiles[i], f, true));
128
+ // Every line gone: the strip drops the row it kept for them.
129
+ if (dl.dataset.variant === 'tiles' && !items.some(hasLine)) delete dl.dataset.context;
130
+ return;
131
+ }
132
+ const { html, count, context } = factsHTML(items, { variant: dl.dataset.variant, highlight: dl.hasAttribute('data-highlight') });
133
+ dl.innerHTML = html;
134
+ delete dl.dataset.context;
135
+ mark(dl, count, context);
23
136
  arrive(dl);
24
137
  };
25
138
 
139
+ /**
140
+ * Changes one fact: `which` is its key or its place (0 first). Only the fields given change:
141
+ * setFact(dl, 'gas', { value: '638', delta: { text: '−8.7%', tone: 'good' } }).
142
+ */
143
+ export const setFact = (dl, which, f) => {
144
+ const tiles = tilesOf(dl);
145
+ const div = typeof which === 'number' ? tiles[which] : tiles.find((t) => t.dataset.key === String(which));
146
+ if (div) apply(dl, div, f, false);
147
+ return div;
148
+ };
149
+
26
150
  const seen = typeof IntersectionObserver === 'undefined' ? null : new IntersectionObserver((entries) => entries.forEach((e) => e.isIntersecting && (e.target.classList.add('in'), seen.unobserve(e.target))), { threshold: 0.4 });
27
151
 
28
- // Once, as it comes into view; again after setFacts.
152
+ // Once, as it comes into view; again after setFacts draws the facts anew.
29
153
  export const arrive = (dl) => {
30
154
  dl.classList.remove('in');
31
155
  if (seen) seen.observe(dl);
@@ -0,0 +1,143 @@
1
+ // Made-up figures only.
2
+ import assert from 'node:assert/strict';
3
+ import { registerHooks } from 'node:module';
4
+ import { parseHTML } from 'linkedom';
5
+
6
+ // The icons live in a .ts module that Vite resolves without its extension; Node needs it named.
7
+ registerHooks({ resolve: (spec, ctx, next) => next(spec === './icons' ? './icons.ts' : spec, ctx) });
8
+ const { factsHTML, setFacts, setFact, PLACEHOLDER } = await import('./facts.js');
9
+
10
+ const { document, window } = parseHTML('<html><body></body></html>');
11
+ globalThis.document = document;
12
+ // What settle needs: motion allowed, the tokens unset, and a record of what moved.
13
+ const settled = [];
14
+ globalThis.matchMedia = () => ({ matches: false });
15
+ globalThis.getComputedStyle = () => ({ getPropertyValue: () => '' });
16
+ window.HTMLElement.prototype.animate = function () {
17
+ settled.push(this);
18
+ };
19
+ const strip = (attrs = '') => {
20
+ document.body.innerHTML = `<div data-chart-motion="update"><dl data-facts data-variant="tiles" ${attrs}></dl></div>`;
21
+ return document.querySelector('dl');
22
+ };
23
+ const text = (el, sel) => el.querySelector(sel)?.textContent;
24
+
25
+ // Plain, with a unit, and with a line led by a change.
26
+ {
27
+ const { html, count, context } = factsHTML([
28
+ { label: 'Avg sleep', value: '7h 40m' },
29
+ { label: 'Resting HR', value: '52', unit: 'bpm', icon: 'heart-pulse', accent: 'var(--c-chart-8)' },
30
+ { label: 'Gas', value: '612', unit: 'm³', delta: { text: '−6.1%', tone: 'good' }, context: 'against 652 last year', key: 'gas' },
31
+ ]);
32
+ assert.equal(count, '3');
33
+ assert.equal(context, true, 'one line is enough to keep the row for all');
34
+ const dl = strip();
35
+ dl.innerHTML = html;
36
+ const [a, b, c] = dl.querySelectorAll(':scope > div');
37
+ assert.equal(text(a, '.fact-figure'), '7h 40m');
38
+ assert.equal(text(a, '.fact-unit'), '');
39
+ assert.equal(text(b, '.fact-unit'), 'bpm');
40
+ assert.match(b.getAttribute('style'), /--accent:var\(--c-chart-8\)/);
41
+ assert.ok(b.querySelector('dt svg[aria-hidden="true"]'), 'icon drawn, hidden');
42
+ assert.equal(c.dataset.key, 'gas');
43
+ assert.equal(c.querySelector('.fact-delta').dataset.tone, 'good');
44
+ assert.equal(text(c, '.fact-context'), '−6.1% against 652 last year');
45
+ assert.equal(c.querySelectorAll('dd').length, 2, 'figure and line, each a dd of the label');
46
+ }
47
+
48
+ // Unknown is a dash, and a unit never stands alone.
49
+ {
50
+ const { html } = factsHTML([{ label: 'VO₂ max', unit: 'mL/kg/min' }, { label: 'Steps', value: '—' }, { label: 'Zero', value: '0', unit: 'km' }]);
51
+ const dl = strip();
52
+ dl.innerHTML = html;
53
+ const [a, b, c] = dl.querySelectorAll(':scope > div');
54
+ assert.equal(text(a, '.fact-figure'), PLACEHOLDER);
55
+ assert.equal(text(a, '.fact-unit'), '');
56
+ assert.equal(text(b, '.fact-figure'), PLACEHOLDER);
57
+ assert.equal(text(c, '.fact-figure'), '0', '0 is a figure');
58
+ assert.equal(text(c, '.fact-unit'), 'km');
59
+ }
60
+
61
+ // Pills and lists: units, but no line under the figure.
62
+ {
63
+ const { html, context } = factsHTML([{ label: 'Distance', value: '12.4', unit: 'km', context: 'ignored' }], { variant: 'pills' });
64
+ assert.equal(context, false);
65
+ assert.ok(!html.includes('fact-context'));
66
+ assert.ok(html.includes('>km<'));
67
+ }
68
+
69
+ // Text is escaped, attributes too.
70
+ {
71
+ const { html } = factsHTML([{ label: '<b>', value: '"1"', key: 'a"b', context: '<i>x</i>' }]);
72
+ assert.ok(!html.includes('<b>') && !html.includes('<i>'));
73
+ assert.ok(html.includes('data-key="a&quot;b"'));
74
+ }
75
+
76
+ // Ids for a page that writes its own figures, as with Stat.
77
+ {
78
+ const { html, context } = factsHTML([{ label: 'Costs', valueId: 'tile-total', contextId: 'sub-total' }]);
79
+ assert.equal(context, true, 'a contextId keeps the line');
80
+ const dl = strip();
81
+ dl.innerHTML = html;
82
+ assert.equal(document.getElementById('tile-total').className, 'fact-figure');
83
+ assert.equal(document.getElementById('sub-total').className, 'fact-context');
84
+ }
85
+
86
+ // setFacts: drawn anew when the count changes, changed in place when it doesn't.
87
+ {
88
+ const dl = strip();
89
+ setFacts(dl, [{ label: 'A', value: '1' }, { label: 'B', value: '2' }]);
90
+ assert.equal(dl.dataset.count, '2');
91
+ assert.equal(dl.dataset.context, undefined, 'no lines, no row for them');
92
+ const first = dl.querySelector(':scope > div');
93
+ setFacts(dl, [{ label: 'A', value: '3', unit: 'km', context: 'this week' }, { label: 'B', value: '4' }]);
94
+ assert.equal(dl.querySelector(':scope > div'), first, 'same tile, changed in place');
95
+ assert.equal(text(first, '.fact-figure'), '3');
96
+ assert.deepEqual(settled, [first.querySelector('.fact-value'), dl.querySelectorAll('.fact-value')[1]], 'changed figures settle, on a page that asks for update motion');
97
+ assert.equal(text(first, '.fact-unit'), 'km');
98
+ assert.equal(text(first, '.fact-context'), 'this week');
99
+ assert.equal(dl.dataset.context, '', 'a line arrived: the row is kept');
100
+ setFacts(dl, [{ label: 'A', value: '3' }, { label: 'B', value: '4' }]);
101
+ assert.equal(dl.dataset.context, undefined, 'every line gone: the row goes too');
102
+ setFacts(dl, Array.from({ length: 8 }, (_, i) => ({ label: `F${i}`, value: String(i) })));
103
+ assert.equal(dl.dataset.count, 'many');
104
+ assert.equal(dl.querySelectorAll(':scope > div').length, 8);
105
+ }
106
+
107
+ // The highlighter survives an update in place.
108
+ {
109
+ const dl = strip('data-highlight');
110
+ setFacts(dl, [{ label: 'A', value: '1' }]);
111
+ assert.equal(text(dl, '.fact-figure mark'), '1');
112
+ setFacts(dl, [{ label: 'A', value: '2' }]);
113
+ assert.equal(text(dl, '.fact-figure mark'), '2');
114
+ }
115
+
116
+ // setFact: only the fields given change, found by key or by place.
117
+ {
118
+ const dl = strip();
119
+ setFacts(dl, [
120
+ { label: 'Gas', key: 'gas', unit: 'm³', context: 'against 652', icon: 'flame' },
121
+ { label: 'Degree days', key: 'hdd' },
122
+ ]);
123
+ const gas = dl.querySelector('[data-key="gas"]');
124
+ const icon = gas.querySelector('dt svg');
125
+ assert.equal(text(gas, '.fact-unit'), '', 'no unit while the figure waits');
126
+ settled.length = 0;
127
+ setFact(dl, 'gas', { value: '612' });
128
+ assert.equal(settled.length, 0, 'a figure arriving in place of the dash just appears');
129
+ assert.equal(text(gas, '.fact-figure'), '612');
130
+ assert.equal(text(gas, '.fact-unit'), 'm³', 'the unit given earlier comes with the figure');
131
+ assert.equal(text(gas, '.fact-context'), 'against 652', 'the line is left as it was');
132
+ assert.equal(gas.querySelector('dt svg'), icon, 'the label is left as it was, icon and all');
133
+ setFact(dl, 'gas', { delta: { text: '+2.0%', tone: 'bad' } });
134
+ assert.equal(gas.querySelector('.fact-delta').dataset.tone, 'bad');
135
+ assert.equal(text(gas, '.fact-context'), '+2.0% against 652', 'the change leads the line it was given');
136
+ setFact(dl, 1, { label: 'Degree days, Oct–Mar' });
137
+ assert.equal(text(dl.querySelector('[data-key="hdd"]'), 'dt'), 'Degree days, Oct–Mar');
138
+ assert.equal(setFact(dl, 'nope', { value: '1' }), undefined);
139
+ setFact(dl, 'gas', { delta: 'flat' });
140
+ assert.equal(gas.querySelector('.fact-delta').dataset.tone, 'neutral', 'a string is a neutral change');
141
+ }
142
+
143
+ console.log('facts: ok');
package/src/period.js CHANGED
@@ -5,6 +5,9 @@
5
5
  // import { setPeriods } from '@n1kname/components/period.js';
6
6
  // picker.addEventListener('period-change', (e) => show(e.detail.value));
7
7
  // setPeriods(picker, ['2011', '2012', ..., '2026']); // oldest first
8
+ //
9
+ // A period is a string, shown and kept as it is, or { value, label }: the label on the button and
10
+ // in the list ("Nov 2026"), the value in the address and in `period-change` ("2026-11").
8
11
 
9
12
  const reduced = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
10
13
  const token = (name) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
@@ -16,14 +19,26 @@ const ms = (name, fallback) => {
16
19
  };
17
20
 
18
21
  // Oldest first.
19
- const periodsOf = (root) => [...root.querySelectorAll('.periods .option')].map((o) => o.dataset.value).reverse();
20
- const nameOf = (root, v) => (v === 'all' ? root.dataset.all : v);
22
+ const optionsOf = (root) => [...root.querySelectorAll('.periods .option')].reverse();
23
+ const periodsOf = (root) => optionsOf(root).map((o) => o.dataset.value);
24
+ // What a value says: its option's label, the words for all, or nothing before the periods are known.
25
+ const nameOf = (root, v) => {
26
+ if (v === undefined) return '';
27
+ if (v === 'all') return root.dataset.all ?? '';
28
+ return optionsOf(root).find((o) => o.dataset.value === v)?.textContent ?? v;
29
+ };
30
+ // A period as a string or { value, label }, made { value, label }.
31
+ const asPeriod = (p) => (p !== null && typeof p === 'object' ? { value: String(p.value), label: String(p.label ?? p.value) } : { value: String(p), label: String(p) });
21
32
 
22
- // The choice: the address's, when the picker keeps it there and it names a period or all.
33
+ // The choice: the address's, when the picker keeps it there and it names a period or all. Before
34
+ // the periods are known, a picker with no initial period has no choice (undefined), and a kept
35
+ // choice that is no longer one of the periods falls back to all or the newest.
23
36
  export const periodOf = (root) => {
24
37
  const periods = periodsOf(root);
25
38
  const all = root.dataset.all !== undefined;
26
- const fallback = root.dataset.value ?? (all ? 'all' : periods.at(-1));
39
+ const kept = root.dataset.value;
40
+ const valid = kept !== undefined && (kept === 'all' ? all : !periods.length || periods.includes(kept));
41
+ const fallback = valid ? kept : all ? 'all' : periods.at(-1);
27
42
  if (!root.dataset.param) return fallback;
28
43
  const asked = new URL(location.href).searchParams.get(root.dataset.param);
29
44
  if (asked === null) return all ? 'all' : fallback;
@@ -51,7 +66,9 @@ const sync = (root, dir = 0) => {
51
66
  const v = periodOf(root);
52
67
  const periods = periodsOf(root);
53
68
  const all = root.dataset.all !== undefined;
54
- root.dataset.value = v;
69
+ // Never the word "undefined": a picker built with no periods has no choice until they come.
70
+ if (v === undefined) delete root.dataset.value;
71
+ else root.dataset.value = v;
55
72
  roll(root, v, dir);
56
73
  const [prev, next] = root.querySelectorAll('.step');
57
74
  const at = periods.indexOf(v);
@@ -59,8 +76,8 @@ const sync = (root, dir = 0) => {
59
76
  next.disabled = !periods.length || v === 'all' || (!all && at === periods.length - 1);
60
77
  root.querySelectorAll('.option').forEach((o) => o.setAttribute('aria-pressed', String(o.dataset.value === v)));
61
78
  // The sizer holds the longest words the label may show, so the control keeps its width.
62
- const longest = [root.dataset.all ?? '', ...periods].reduce((a, b) => (b.length > a.length ? b : a), '');
63
- root.querySelector('.sizer').textContent = longest || v;
79
+ const longest = [root.dataset.all ?? '', ...optionsOf(root).map((o) => o.textContent)].reduce((a, b) => (b.length > a.length ? b : a), '');
80
+ root.querySelector('.sizer').textContent = longest || nameOf(root, v);
64
81
  };
65
82
 
66
83
  const go = (root, v, dir) => {
@@ -72,18 +89,25 @@ const go = (root, v, dir) => {
72
89
  }
73
90
  root.dataset.value = v;
74
91
  sync(root, dir);
75
- root.dispatchEvent(new CustomEvent('period-change', { bubbles: true, detail: { value: v } }));
92
+ changed(root);
93
+ };
94
+
95
+ // `period-change`: the value, and the words the button shows for it.
96
+ const changed = (root) => {
97
+ const value = root.dataset.value;
98
+ root.dispatchEvent(new CustomEvent('period-change', { bubbles: true, detail: { value, label: nameOf(root, value) } }));
76
99
  };
77
100
 
78
- // Fills a picker with its periods, oldest first, and shows it.
101
+ // Fills a picker with its periods, oldest first, and shows it. Each a string, or { value, label }.
79
102
  export const setPeriods = (root, periods) => {
80
103
  root.querySelector('.periods').replaceChildren(
81
104
  ...[...periods].reverse().map((p) => {
105
+ const { value, label } = asPeriod(p);
82
106
  const b = document.createElement('button');
83
107
  b.type = 'button';
84
108
  b.className = 'option';
85
- b.dataset.value = String(p);
86
- b.textContent = String(p);
109
+ b.dataset.value = value;
110
+ b.textContent = label;
87
111
  return b;
88
112
  }),
89
113
  );
@@ -153,7 +177,7 @@ export const wirePeriodPicker = (root) => {
153
177
  addEventListener('popstate', () => {
154
178
  const was = root.dataset.value;
155
179
  sync(root);
156
- if (root.dataset.value !== was) root.dispatchEvent(new CustomEvent('period-change', { bubbles: true, detail: { value: root.dataset.value } }));
180
+ if (root.dataset.value !== was) changed(root);
157
181
  });
158
182
  }
159
183
  sync(root);