@n1kname/components 0.14.0 → 0.16.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/package.json +4 -3
- package/src/AppShell.astro +412 -0
- package/src/FlowDiagram.astro +536 -0
- package/src/PeriodPicker.astro +264 -0
- package/src/SideNav.astro +188 -0
- package/src/icons.ts +34 -0
- package/src/page-change.css +41 -2
- package/src/period.js +160 -0
package/src/period.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Drives PeriodPicker. Its state is in the page: the periods are the list's buttons (newest
|
|
2
|
+
// first), the choice is data-value on the picker and, with data-param, the address too. So a
|
|
3
|
+
// picker needs no object kept anywhere, and setPeriods() can fill one at any time.
|
|
4
|
+
//
|
|
5
|
+
// import { setPeriods } from '@n1kname/components/period.js';
|
|
6
|
+
// picker.addEventListener('period-change', (e) => show(e.detail.value));
|
|
7
|
+
// setPeriods(picker, ['2011', '2012', ..., '2026']); // oldest first
|
|
8
|
+
|
|
9
|
+
const reduced = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
10
|
+
const token = (name) => getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
11
|
+
// A motion token in milliseconds; the tokens are written in seconds.
|
|
12
|
+
const ms = (name, fallback) => {
|
|
13
|
+
const v = token(name);
|
|
14
|
+
const n = parseFloat(v);
|
|
15
|
+
return Number.isNaN(n) ? fallback : v.endsWith('ms') ? n : n * 1000;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// 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);
|
|
21
|
+
|
|
22
|
+
// The choice: the address's, when the picker keeps it there and it names a period or all.
|
|
23
|
+
export const periodOf = (root) => {
|
|
24
|
+
const periods = periodsOf(root);
|
|
25
|
+
const all = root.dataset.all !== undefined;
|
|
26
|
+
const fallback = root.dataset.value ?? (all ? 'all' : periods.at(-1));
|
|
27
|
+
if (!root.dataset.param) return fallback;
|
|
28
|
+
const asked = new URL(location.href).searchParams.get(root.dataset.param);
|
|
29
|
+
if (asked === null) return all ? 'all' : fallback;
|
|
30
|
+
return !periods.length || periods.includes(asked) ? asked : fallback;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// The period rolls by a line: up when time goes forward, down when it goes back.
|
|
34
|
+
const roll = (root, to, dir) => {
|
|
35
|
+
const label = root.querySelector('.label');
|
|
36
|
+
const old = label.querySelector('.now');
|
|
37
|
+
if (old.textContent === nameOf(root, to)) return;
|
|
38
|
+
const now = document.createElement('span');
|
|
39
|
+
now.className = 'now';
|
|
40
|
+
now.textContent = nameOf(root, to);
|
|
41
|
+
label.append(now);
|
|
42
|
+
if (!dir || reduced()) return old.remove();
|
|
43
|
+
const timing = { duration: ms('--motion-spring', 340), easing: token('--ease-spring') || 'ease-out' };
|
|
44
|
+
old.classList.remove('now');
|
|
45
|
+
old.animate([{ transform: 'none', opacity: 1 }, { transform: `translateY(${-dir * 100}%)`, opacity: 0 }], { ...timing, fill: 'forwards' }).finished.then(() => old.remove());
|
|
46
|
+
now.animate([{ transform: `translateY(${dir * 100}%)`, opacity: 0 }, { transform: 'none', opacity: 1 }], timing);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Shows the choice: the label, the steps that lead somewhere, the pressed period in the list.
|
|
50
|
+
const sync = (root, dir = 0) => {
|
|
51
|
+
const v = periodOf(root);
|
|
52
|
+
const periods = periodsOf(root);
|
|
53
|
+
const all = root.dataset.all !== undefined;
|
|
54
|
+
root.dataset.value = v;
|
|
55
|
+
roll(root, v, dir);
|
|
56
|
+
const [prev, next] = root.querySelectorAll('.step');
|
|
57
|
+
const at = periods.indexOf(v);
|
|
58
|
+
prev.disabled = !periods.length || at === 0;
|
|
59
|
+
next.disabled = !periods.length || v === 'all' || (!all && at === periods.length - 1);
|
|
60
|
+
root.querySelectorAll('.option').forEach((o) => o.setAttribute('aria-pressed', String(o.dataset.value === v)));
|
|
61
|
+
// 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;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const go = (root, v, dir) => {
|
|
67
|
+
if (root.dataset.param) {
|
|
68
|
+
const url = new URL(location.href);
|
|
69
|
+
if (v === 'all') url.searchParams.delete(root.dataset.param);
|
|
70
|
+
else url.searchParams.set(root.dataset.param, v);
|
|
71
|
+
history.pushState(null, '', url);
|
|
72
|
+
}
|
|
73
|
+
root.dataset.value = v;
|
|
74
|
+
sync(root, dir);
|
|
75
|
+
root.dispatchEvent(new CustomEvent('period-change', { bubbles: true, detail: { value: v } }));
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Fills a picker with its periods, oldest first, and shows it.
|
|
79
|
+
export const setPeriods = (root, periods) => {
|
|
80
|
+
root.querySelector('.periods').replaceChildren(
|
|
81
|
+
...[...periods].reverse().map((p) => {
|
|
82
|
+
const b = document.createElement('button');
|
|
83
|
+
b.type = 'button';
|
|
84
|
+
b.className = 'option';
|
|
85
|
+
b.dataset.value = String(p);
|
|
86
|
+
b.textContent = String(p);
|
|
87
|
+
return b;
|
|
88
|
+
}),
|
|
89
|
+
);
|
|
90
|
+
root.hidden = false;
|
|
91
|
+
sync(root);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// Run once per picker, by PeriodPicker itself.
|
|
95
|
+
export const wirePeriodPicker = (root) => {
|
|
96
|
+
const list = root.querySelector('.period-list');
|
|
97
|
+
const button = root.querySelector('.current');
|
|
98
|
+
|
|
99
|
+
root.querySelectorAll('.step').forEach((step) =>
|
|
100
|
+
step.addEventListener('click', () => {
|
|
101
|
+
const dir = Number(step.dataset.step);
|
|
102
|
+
const periods = periodsOf(root);
|
|
103
|
+
const v = periodOf(root);
|
|
104
|
+
// The line runs oldest to newest, then all: back from all is the newest.
|
|
105
|
+
const to = v === 'all' ? periods.at(-1) : (periods[periods.indexOf(v) + dir] ?? 'all');
|
|
106
|
+
go(root, to, dir);
|
|
107
|
+
}),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
list.addEventListener('click', (e) => {
|
|
111
|
+
const o = e.target.closest('.option');
|
|
112
|
+
if (!o) return;
|
|
113
|
+
const periods = periodsOf(root);
|
|
114
|
+
const was = periodOf(root);
|
|
115
|
+
const to = o.dataset.value;
|
|
116
|
+
list.hidePopover();
|
|
117
|
+
button.focus();
|
|
118
|
+
if (to === was) return;
|
|
119
|
+
const later = to === 'all' || (was !== 'all' && periods.indexOf(to) > periods.indexOf(was));
|
|
120
|
+
go(root, to, later ? 1 : -1);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// The list opens under the control, kept inside the window, with focus on the chosen period.
|
|
124
|
+
list.addEventListener('toggle', (e) => {
|
|
125
|
+
const open = e.newState === 'open';
|
|
126
|
+
button.setAttribute('aria-expanded', String(open));
|
|
127
|
+
if (!open) {
|
|
128
|
+
// Closed from inside, by Escape or a choice: focus goes back to the control (Safari
|
|
129
|
+
// leaves it on the page).
|
|
130
|
+
if (list.contains(document.activeElement) || document.activeElement === document.body) button.focus();
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const r = root.getBoundingClientRect();
|
|
134
|
+
const w = list.offsetWidth;
|
|
135
|
+
list.style.top = `${r.bottom + 6}px`;
|
|
136
|
+
list.style.left = `${Math.max(16, Math.min(r.right - w, innerWidth - w - 16))}px`;
|
|
137
|
+
// After the browser has finished opening it, which would otherwise keep focus on the button.
|
|
138
|
+
requestAnimationFrame(() => (list.querySelector('[aria-pressed="true"]') ?? list.querySelector('.option'))?.focus());
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Arrow keys move through the list in reading order, all first.
|
|
142
|
+
list.addEventListener('keydown', (e) => {
|
|
143
|
+
const options = [...list.querySelectorAll('.option')];
|
|
144
|
+
const at = options.indexOf(document.activeElement);
|
|
145
|
+
const cols = getComputedStyle(list.querySelector('.periods')).gridTemplateColumns.split(' ').length;
|
|
146
|
+
const step = { ArrowRight: 1, ArrowDown: e.target.classList.contains('all') ? 1 : cols, ArrowLeft: -1, ArrowUp: -cols }[e.key];
|
|
147
|
+
if (step === undefined || at < 0) return;
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
options[Math.max(0, Math.min(options.length - 1, at + step))].focus();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (root.dataset.param) {
|
|
153
|
+
addEventListener('popstate', () => {
|
|
154
|
+
const was = root.dataset.value;
|
|
155
|
+
sync(root);
|
|
156
|
+
if (root.dataset.value !== was) root.dispatchEvent(new CustomEvent('period-change', { bubbles: true, detail: { value: root.dataset.value } }));
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
sync(root);
|
|
160
|
+
};
|