@gecko-ui-kit/components 0.0.0-stage → 0.21.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/LICENSE +21 -0
- package/README.md +904 -2
- package/index.js +225 -0
- package/package.json +42 -4
- package/src/accordion.js +208 -0
- package/src/activity.js +218 -0
- package/src/alert.js +205 -0
- package/src/authform.js +285 -0
- package/src/beforeafter.js +141 -0
- package/src/bell.js +137 -0
- package/src/breadcrumbs.js +204 -0
- package/src/calendar.js +523 -0
- package/src/carousel.js +266 -0
- package/src/combobox.js +421 -0
- package/src/daterange.js +311 -0
- package/src/donut.js +346 -0
- package/src/filedrop.js +378 -0
- package/src/gauge.js +169 -0
- package/src/grid.js +298 -0
- package/src/heatmap.js +332 -0
- package/src/kanban.js +319 -0
- package/src/leaderboard.js +201 -0
- package/src/like.js +208 -0
- package/src/linechart.js +550 -0
- package/src/multiselect.js +324 -0
- package/src/notifications.js +393 -0
- package/src/otp.js +112 -0
- package/src/pagination.js +242 -0
- package/src/password.js +245 -0
- package/src/pay.js +162 -0
- package/src/pricing.js +189 -0
- package/src/radiocards.js +101 -0
- package/src/rangeslider.js +266 -0
- package/src/rating.js +171 -0
- package/src/signature.js +213 -0
- package/src/skeleton.js +254 -0
- package/src/speeddial.js +128 -0
- package/src/spinner.js +38 -0
- package/src/stepper.js +211 -0
- package/src/subscribe.js +173 -0
- package/src/swipe.js +280 -0
- package/src/tagsinput.js +369 -0
- package/src/timeline.js +340 -0
- package/src/timepicker.js +290 -0
- package/src/tree.js +490 -0
- package/src/typing.js +93 -0
- package/src/upload.js +416 -0
- package/src/virtuallist.js +373 -0
- package/src/wizard.js +392 -0
- package/style.css +2092 -0
package/src/donut.js
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/* Donut chart: parts of a whole (sales by category, hours by team) as a ring of slices, drawn as SVG.
|
|
2
|
+
When it scrolls into view the slices draw in one after another, each sweeping round from where it
|
|
3
|
+
starts, while the total in the middle counts up. Pointing at a slice or its legend row (or moving to
|
|
4
|
+
it with the keyboard) lifts that slice out from the ring, fades the others and shows its value and
|
|
5
|
+
share in the middle; clicking it keeps it picked. set() morphs the slices to their new shares instead
|
|
6
|
+
of drawing again from zero: slices that stay change size, new ones grow out of their neighbour and
|
|
7
|
+
ones that are gone shrink away. Gaps between slices keep parallel sides. Screen readers get each
|
|
8
|
+
slice as an option ("Coffee: $2,480, 43%"), the data as a hidden table and the picked slice read out. */
|
|
9
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
10
|
+
import { esc } from '../index.js';
|
|
11
|
+
|
|
12
|
+
const { g, RM, $$ } = M;
|
|
13
|
+
const NS = 'http://www.w3.org/2000/svg';
|
|
14
|
+
const TAU = Math.PI * 2;
|
|
15
|
+
const WORDS = {
|
|
16
|
+
label: 'Chart',
|
|
17
|
+
total: 'Total',
|
|
18
|
+
keys: 'Use the arrow keys to move between slices and Enter to pick one.',
|
|
19
|
+
pct: (n) => `${n}%`,
|
|
20
|
+
slice: (label, value, pct) => `${label}: ${value}, ${pct}`,
|
|
21
|
+
summary: (label, total, n) => `${label}. ${n} parts, ${total} in total.`,
|
|
22
|
+
selected: (label) => `${label} picked`,
|
|
23
|
+
cleared: 'Nothing picked',
|
|
24
|
+
part: 'Part', value: 'Value', share: 'Share', // headings of the hidden table
|
|
25
|
+
};
|
|
26
|
+
const TONES = { accent: 'var(--accent)', good: 'var(--good)', warn: 'var(--warn)', bad: 'var(--bad)', ink: 'var(--ink-2)', muted: 'var(--muted)' };
|
|
27
|
+
const HUES = ['indigo', 'teal', 'rose', 'amber', 'violet', 'green', 'slate', 'sky'];
|
|
28
|
+
const AUTO = ['var(--accent)', 'var(--hue-teal)', 'var(--hue-amber)', 'var(--hue-rose)', 'var(--hue-sky)', 'var(--hue-green)', 'var(--hue-violet)', 'var(--hue-slate)'];
|
|
29
|
+
const toColor = (t, k) => (!t ? AUTO[k % AUTO.length] : TONES[t] || (HUES.includes(t) ? `var(--hue-${t})` : String(t)));
|
|
30
|
+
let uid = 0;
|
|
31
|
+
|
|
32
|
+
const lerp = (a, b, p) => a + (b - a) * p;
|
|
33
|
+
const r2 = (v) => Math.round(v * 100) / 100;
|
|
34
|
+
const decimals = (v) => { const s = String(v), i = s.indexOf('.'); return i < 0 ? 0 : Math.min(2, s.length - i - 1); };
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* const d = donut(el, {
|
|
38
|
+
* data: [{ label: 'Coffee', value: 2480, tone: 'accent'|'good'|'warn'|'bad'|'teal'|'rose'|… or any CSS colour }],
|
|
39
|
+
* size: 200, // px across (shrinks with a narrow parent)
|
|
40
|
+
* thickness: 26, // px, width of the ring
|
|
41
|
+
* gap: 3, // px between slices (sides stay parallel)
|
|
42
|
+
* legend: 'right', // 'right' (drops below when there is no room) | 'bottom' | false
|
|
43
|
+
* sort: false, // 'desc' (or true) | 'asc' — biggest first, or smallest first
|
|
44
|
+
* format: (v) => string, // values in the middle, legend, table and read-outs
|
|
45
|
+
* centre: 'Total', // the line under the total in the middle; false = no text in the middle
|
|
46
|
+
* label: 'Sales by category', // name for screen readers
|
|
47
|
+
* words: { label, total, keys, pct, slice, summary, selected, cleared, part, value, share },
|
|
48
|
+
* onSelect(index, item, d) // a slice was picked (index -1 when cleared); item = { label, value, share }
|
|
49
|
+
* })
|
|
50
|
+
* d.set(data) // slices morph to the new shares (matched by label)
|
|
51
|
+
* d.select(index | label | -1) · d.data · d.total · d.selected · d.destroy()
|
|
52
|
+
*/
|
|
53
|
+
export const donut = (el, opts = {}) => {
|
|
54
|
+
el = $$(el)[0];
|
|
55
|
+
if (!el) return null;
|
|
56
|
+
const o = { data: [], size: 200, thickness: 26, gap: 3, legend: 'right', sort: false, format: null, centre: null, words: null, label: '', onSelect: null, ...opts };
|
|
57
|
+
const W = { ...WORDS, ...o.words };
|
|
58
|
+
const ac = new AbortController(), on = (t, ev, fn, x) => t.addEventListener(ev, fn, { signal: ac.signal, ...x });
|
|
59
|
+
const base = `dnt${++uid}`, name = o.label || W.label;
|
|
60
|
+
const SZ = Math.max(80, +o.size || 200), LIFT = Math.max(4, Math.round(SZ * 0.035));
|
|
61
|
+
const cx = SZ / 2, ro = SZ / 2 - LIFT - 2, ri = Math.max(0, ro - Math.max(4, +o.thickness || 26));
|
|
62
|
+
const gap = Math.max(0, +o.gap || 0);
|
|
63
|
+
const legendAt = o.legend === 'bottom' ? 'bottom' : o.legend ? 'right' : '';
|
|
64
|
+
const centreOn = o.centre !== false;
|
|
65
|
+
|
|
66
|
+
el.innerHTML = `<div class="dnt${legendAt ? ` dnt--${legendAt}` : ''}" style="--dnt-size:${SZ}px"><div class="dnt__body">`
|
|
67
|
+
+ `<div class="dnt__ring"><svg class="dnt__svg" viewBox="0 0 ${SZ} ${SZ}" width="${SZ}" height="${SZ}" xmlns="${NS}">`
|
|
68
|
+
+ `<circle class="dnt__track" cx="${cx}" cy="${cx}" r="${r2((ro + ri) / 2)}" stroke-width="${r2(ro - ri)}" aria-hidden="true"/>`
|
|
69
|
+
+ `<g class="dnt__slices" role="listbox" aria-label="${esc(name)}" aria-describedby="${base}-k"></g></svg>`
|
|
70
|
+
+ (centreOn ? `<div class="dnt__mid" aria-hidden="true" style="width:${r2(((ri * 2) / SZ) * 82)}%"><b class="dnt__val"></b><span class="dnt__lab"></span></div>` : '')
|
|
71
|
+
+ `</div>${legendAt ? '<ul class="dnt__legend"></ul>' : ''}</div>`
|
|
72
|
+
+ `<span class="dnt__sr" id="${base}-k">${esc(W.keys)}</span><table class="dnt__sr"></table><span class="dnt__sr" role="status"></span></div>`;
|
|
73
|
+
const root = el.firstElementChild, $ = (s) => root.querySelector(s);
|
|
74
|
+
const svg = $('.dnt__svg'), slicesG = $('.dnt__slices'), legend = $('.dnt__legend');
|
|
75
|
+
const valEl = $('.dnt__val'), labEl = $('.dnt__lab'), table = $('table.dnt__sr'), live = $('[role="status"]');
|
|
76
|
+
const say = (t) => { live.textContent = ''; live.textContent = t; };
|
|
77
|
+
|
|
78
|
+
/* ---------- model ---------- */
|
|
79
|
+
const fmt = (v) => (o.format ? String(o.format(v)) : Number(v).toLocaleString());
|
|
80
|
+
const prep = (data) => {
|
|
81
|
+
const list = (data || []).map((d, k) => ({ label: String(d.label ?? `Part ${k + 1}`), value: Math.max(0, Number.isFinite(+d.value) ? +d.value : 0), tone: d.tone ?? d.color ?? null }));
|
|
82
|
+
const dir = o.sort === 'asc' ? 1 : o.sort ? -1 : 0;
|
|
83
|
+
return dir ? list.sort((a, b) => (a.value - b.value) * dir) : list;
|
|
84
|
+
};
|
|
85
|
+
let S = [], ci = 0;
|
|
86
|
+
const make = (d) => {
|
|
87
|
+
const s = { label: d.label, value: d.value, color: toColor(d.tone, ci++), leaving: false, f0: 0, f1: 0, t0: 0, t1: 0, rev: { k: 0 }, lift: { v: 0, to: 0 } };
|
|
88
|
+
s.el = document.createElementNS(NS, 'path');
|
|
89
|
+
s.el.setAttribute('class', 'dnt__slice');
|
|
90
|
+
s.el.setAttribute('role', 'option');
|
|
91
|
+
s.el.setAttribute('tabindex', '-1');
|
|
92
|
+
s.el.setAttribute('fill-rule', 'evenodd');
|
|
93
|
+
s.el.style.setProperty('--c', s.color);
|
|
94
|
+
return s;
|
|
95
|
+
};
|
|
96
|
+
const list = () => S.filter((s) => !s.leaving);
|
|
97
|
+
const totalOf = () => list().reduce((t, s) => t + s.value, 0);
|
|
98
|
+
const dec = () => Math.max(0, ...list().map((s) => decimals(s.value)));
|
|
99
|
+
const share = (s) => { const t = totalOf(); return t > 0 ? s.value / t : 0; };
|
|
100
|
+
const pctText = (s) => { const p = share(s) * 100, n = Math.round(p); return W.pct(p > 0 && n === 0 ? '<1' : n); };
|
|
101
|
+
/* target angles (clockwise from 12 o'clock); a slice that is leaving has no width where it was */
|
|
102
|
+
const aim = () => {
|
|
103
|
+
const t = totalOf();
|
|
104
|
+
let a = 0;
|
|
105
|
+
S.forEach((s) => { const span = !s.leaving && t > 0 ? (s.value / t) * TAU : 0; s.t0 = a; s.t1 = a + span; a += span; });
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/* ---------- drawing ---------- */
|
|
109
|
+
const P = (r, a) => `${r2(cx + r * Math.sin(a))},${r2(cx - r * Math.cos(a))}`;
|
|
110
|
+
const ring = (r, sweep) => `M${P(r, 0)}A${r2(r)},${r2(r)} 0 1 ${sweep} ${P(r, Math.PI)}A${r2(r)},${r2(r)} 0 1 ${sweep} ${P(r, 0)}Z`;
|
|
111
|
+
const sector = (a0, a1, cut) => {
|
|
112
|
+
const span = a1 - a0;
|
|
113
|
+
if (span <= 1e-4) return '';
|
|
114
|
+
if (!cut && span >= TAU - 1e-4) return ri > 0.5 ? ring(ro, 1) + ring(ri, 0) : ring(ro, 1);
|
|
115
|
+
const go = cut ? gap / 2 / ro : 0, gi = cut && ri > 0.5 ? gap / 2 / ri : 0;
|
|
116
|
+
if (span <= 2 * Math.max(go, gi) + 1e-3) return '';
|
|
117
|
+
const o0 = a0 + go, o1 = a1 - go, i0 = a0 + gi, i1 = a1 - gi;
|
|
118
|
+
let d = `M${P(ro, o0)}A${r2(ro)},${r2(ro)} 0 ${o1 - o0 > Math.PI ? 1 : 0} 1 ${P(ro, o1)}`;
|
|
119
|
+
d += ri > 0.5 ? `L${P(ri, i1)}A${r2(ri)},${r2(ri)} 0 ${i1 - i0 > Math.PI ? 1 : 0} 0 ${P(ri, i0)}Z` : `L${r2(cx)},${r2(cx)}Z`;
|
|
120
|
+
return d;
|
|
121
|
+
};
|
|
122
|
+
const T = { p: 1 };
|
|
123
|
+
let tween = null;
|
|
124
|
+
const draw = () => {
|
|
125
|
+
const p = T.p;
|
|
126
|
+
const spans = S.map((s) => { const a0 = lerp(s.f0, s.t0, p), a1 = lerp(s.f1, s.t1, p); return [a0, a0 + (a1 - a0) * s.rev.k]; });
|
|
127
|
+
const many = spans.filter(([a, b]) => b - a > 1e-4).length > 1, cut = gap > 0 && many;
|
|
128
|
+
S.forEach((s, k) => {
|
|
129
|
+
const [a0, a1] = spans[k], d = many ? s.lift.v * LIFT : 0, m = (a0 + a1) / 2; // a lone full ring does not move
|
|
130
|
+
s.el.setAttribute('d', sector(a0, a1, cut) || 'M0,0');
|
|
131
|
+
if (Math.abs(d) > 0.01) s.el.setAttribute('transform', `translate(${r2(Math.sin(m) * d)},${r2(-Math.cos(m) * d)})`);
|
|
132
|
+
else s.el.removeAttribute('transform');
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/* ---------- the middle: the total (counts up) or the slice in focus ---------- */
|
|
137
|
+
const cnt = { v: 0 };
|
|
138
|
+
let counting = false, midKey = null;
|
|
139
|
+
const centreText = () => (typeof o.centre === 'string' ? o.centre : W.total);
|
|
140
|
+
const showTotal = () => { if (valEl && !counting) valEl.textContent = fmt(totalOf()); };
|
|
141
|
+
const fmtCnt = () => { const d = dec(); return fmt(Math.round(cnt.v * 10 ** d) / 10 ** d); };
|
|
142
|
+
const countTotal = (from) => {
|
|
143
|
+
g.killTweensOf(cnt);
|
|
144
|
+
const to = totalOf();
|
|
145
|
+
if (RM() || from === to) { counting = false; cnt.v = to; if (!midKey) showTotal(); return; }
|
|
146
|
+
counting = true; cnt.v = from;
|
|
147
|
+
g.to(cnt, { v: to, duration: 1.1, ease: 'power3.out',
|
|
148
|
+
onUpdate: () => { if (valEl && !midKey) valEl.textContent = fmtCnt(); },
|
|
149
|
+
onComplete: () => { counting = false; if (!midKey) showTotal(); } });
|
|
150
|
+
};
|
|
151
|
+
const paintMid = (s) => {
|
|
152
|
+
if (!valEl) return;
|
|
153
|
+
const key = s ? s.label : null, changed = key !== midKey;
|
|
154
|
+
midKey = key;
|
|
155
|
+
if (s) { valEl.textContent = fmt(s.value); labEl.textContent = `${s.label} · ${pctText(s)}`; }
|
|
156
|
+
else { labEl.textContent = centreText(); if (counting) valEl.textContent = fmtCnt(); showTotal(); }
|
|
157
|
+
if (changed && !RM()) g.fromTo([valEl, labEl], { y: 5, opacity: 0 }, { y: 0, opacity: 1, duration: 0.26, ease: 'power2.out', stagger: 0.04, overwrite: true });
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/* ---------- which slice is lifted: pointer > keyboard focus > picked ---------- */
|
|
161
|
+
let hov = null, foc = null, sel = null, rove = null;
|
|
162
|
+
const keyOf = (s) => legend?.querySelector(`[data-label="${CSS.escape(s.label)}"]`);
|
|
163
|
+
const liftTo = (s, v) => {
|
|
164
|
+
if (s.lift.to === v) return;
|
|
165
|
+
s.lift.to = v;
|
|
166
|
+
if (RM()) { g.killTweensOf(s.lift); s.lift.v = v; draw(); return; }
|
|
167
|
+
g.to(s.lift, { v, duration: v ? 0.45 : 0.3, ease: v ? 'back.out(2.6)' : 'power2.out', overwrite: true, onUpdate: draw });
|
|
168
|
+
};
|
|
169
|
+
const paint = () => {
|
|
170
|
+
const act = hov || foc || sel;
|
|
171
|
+
root.classList.toggle('is-active', !!act);
|
|
172
|
+
S.forEach((s) => {
|
|
173
|
+
const isOn = s === act && !s.leaving;
|
|
174
|
+
s.el.classList.toggle('is-on', isOn);
|
|
175
|
+
s.el.setAttribute('aria-selected', String(s === sel));
|
|
176
|
+
s.el.setAttribute('tabindex', s === rove && !s.leaving ? '0' : '-1');
|
|
177
|
+
const k = keyOf(s);
|
|
178
|
+
if (k) { k.classList.toggle('is-on', isOn); k.setAttribute('aria-pressed', String(s === sel)); }
|
|
179
|
+
liftTo(s, isOn ? 1 : 0);
|
|
180
|
+
});
|
|
181
|
+
paintMid(act && !act.leaving ? act : null);
|
|
182
|
+
};
|
|
183
|
+
const info = (s) => (s ? { label: s.label, value: s.value, share: share(s) } : null);
|
|
184
|
+
const pick = (s, tell = true) => {
|
|
185
|
+
if (s && s.leaving) return;
|
|
186
|
+
if (s === sel) return;
|
|
187
|
+
sel = s;
|
|
188
|
+
if (s) rove = s;
|
|
189
|
+
paint();
|
|
190
|
+
if (!tell) return;
|
|
191
|
+
say(s ? W.selected(s.label) : W.cleared);
|
|
192
|
+
o.onSelect?.(s ? list().indexOf(s) : -1, info(s), api);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/* ---------- text for screen readers + the legend ---------- */
|
|
196
|
+
const describe = () => {
|
|
197
|
+
const L = list(), t = totalOf();
|
|
198
|
+
L.forEach((s) => s.el.setAttribute('aria-label', W.slice(s.label, fmt(s.value), pctText(s))));
|
|
199
|
+
S.filter((s) => s.leaving).forEach((s) => { s.el.removeAttribute('aria-label'); s.el.setAttribute('aria-hidden', 'true'); });
|
|
200
|
+
L.forEach((s) => s.el.removeAttribute('aria-hidden'));
|
|
201
|
+
table.innerHTML = `<caption>${esc(W.summary(name, fmt(t), L.length))}</caption>`
|
|
202
|
+
+ `<thead><tr><th scope="col">${esc(W.part)}</th><th scope="col">${esc(W.value)}</th><th scope="col">${esc(W.share)}</th></tr></thead>`
|
|
203
|
+
+ `<tbody>${L.map((s) => `<tr><th scope="row">${esc(s.label)}</th><td>${esc(fmt(s.value))}</td><td>${esc(pctText(s))}</td></tr>`).join('')}</tbody>`
|
|
204
|
+
+ `<tfoot><tr><th scope="row">${esc(W.total)}</th><td>${esc(fmt(t))}</td><td>${esc(W.pct(t > 0 ? 100 : 0))}</td></tr></tfoot>`;
|
|
205
|
+
};
|
|
206
|
+
const paintLegend = () => {
|
|
207
|
+
if (!legend) return;
|
|
208
|
+
const had = legend.contains(document.activeElement) ? document.activeElement.dataset.label : null;
|
|
209
|
+
legend.innerHTML = list().map((s) => `<li><button type="button" class="dnt__key" aria-pressed="false" data-label="${esc(s.label)}" aria-label="${esc(W.slice(s.label, fmt(s.value), pctText(s)))}" style="--c:${esc(s.color)}">`
|
|
210
|
+
+ `<i aria-hidden="true"></i><span class="dnt__kl">${esc(s.label)}</span><span class="dnt__kv">${esc(fmt(s.value))}</span><em>${esc(pctText(s))}</em></button></li>`).join('');
|
|
211
|
+
if (had != null) legend.querySelector(`[data-label="${CSS.escape(had)}"]`)?.focus({ preventScroll: true });
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/* ---------- first show: slices draw in one after another ---------- */
|
|
215
|
+
let played = false, io = null;
|
|
216
|
+
const play = () => {
|
|
217
|
+
if (played || ac.signal.aborted) return;
|
|
218
|
+
played = true; io?.disconnect(); io = null;
|
|
219
|
+
if (RM()) { S.forEach((s) => { s.rev.k = 1; }); draw(); countTotal(totalOf()); return; }
|
|
220
|
+
const L = list(), each = Math.min(0.6, 1.6 / Math.max(1, L.length));
|
|
221
|
+
L.forEach((s, k) => g.to(s.rev, { k: 1, duration: 0.42 + each * 0.6, delay: 0.1 + k * each, ease: 'power2.inOut', onUpdate: draw }));
|
|
222
|
+
countTotal(0);
|
|
223
|
+
};
|
|
224
|
+
const finishIntro = () => {
|
|
225
|
+
if (!played) { played = true; io?.disconnect(); io = null; }
|
|
226
|
+
S.forEach((s) => { g.killTweensOf(s.rev); s.rev.k = 1; });
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/* ---------- events ---------- */
|
|
230
|
+
const byKeys = (t) => { try { return t.matches(':focus-visible'); } catch { return true; } }; // a mouse click focuses too: only lift for keyboard focus
|
|
231
|
+
const sliceOf = (t) => { const p = t?.closest?.('.dnt__slice'); return p ? S.find((s) => s.el === p && !s.leaving) || null : null; };
|
|
232
|
+
on(svg, 'pointermove', (e) => { if (e.pointerType !== 'mouse') return; const s = sliceOf(e.target); if (s !== hov) { hov = s; paint(); } });
|
|
233
|
+
on(svg, 'pointerleave', () => { if (hov) { hov = null; paint(); } });
|
|
234
|
+
on(slicesG, 'click', (e) => { const s = sliceOf(e.target); if (s) { rove = s; pick(s === sel ? null : s); } });
|
|
235
|
+
on(slicesG, 'focusin', (e) => { const s = sliceOf(e.target); if (s) { rove = s; foc = byKeys(e.target) ? s : null; paint(); } });
|
|
236
|
+
on(slicesG, 'focusout', (e) => { if (!slicesG.contains(e.relatedTarget)) { foc = null; paint(); } });
|
|
237
|
+
on(slicesG, 'keydown', (e) => {
|
|
238
|
+
const L = list();
|
|
239
|
+
if (!L.length) return;
|
|
240
|
+
let i = L.indexOf(foc || rove);
|
|
241
|
+
const k = e.key;
|
|
242
|
+
if (k === 'ArrowRight' || k === 'ArrowDown') i = (i + 1) % L.length;
|
|
243
|
+
else if (k === 'ArrowLeft' || k === 'ArrowUp') i = (i - 1 + L.length) % L.length;
|
|
244
|
+
else if (k === 'Home') i = 0;
|
|
245
|
+
else if (k === 'End') i = L.length - 1;
|
|
246
|
+
else if (k === 'Enter' || k === ' ') { e.preventDefault(); const s = L[Math.max(0, i)]; pick(s === sel ? null : s); return; }
|
|
247
|
+
else if (k === 'Escape' && sel) { e.preventDefault(); pick(null); return; }
|
|
248
|
+
else return;
|
|
249
|
+
e.preventDefault();
|
|
250
|
+
rove = L[i]; foc = rove; paint(); rove.el.focus({ preventScroll: true });
|
|
251
|
+
});
|
|
252
|
+
if (legend) {
|
|
253
|
+
const keySlice = (t) => { const b = t?.closest?.('.dnt__key'); return b ? list().find((s) => s.label === b.dataset.label) || null : null; };
|
|
254
|
+
on(legend, 'pointerover', (e) => { if (e.pointerType !== 'mouse') return; const s = keySlice(e.target); if (s !== hov) { hov = s; paint(); } });
|
|
255
|
+
on(legend, 'pointerleave', () => { if (hov) { hov = null; paint(); } });
|
|
256
|
+
on(legend, 'focusin', (e) => { const s = keySlice(e.target); if (s && byKeys(e.target)) { foc = s; paint(); } });
|
|
257
|
+
on(legend, 'focusout', (e) => { if (!legend.contains(e.relatedTarget)) { foc = null; paint(); } });
|
|
258
|
+
on(legend, 'click', (e) => { const s = keySlice(e.target); if (s) pick(s === sel ? null : s); });
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/* ---------- build ---------- */
|
|
262
|
+
S = prep(o.data).map(make);
|
|
263
|
+
S.forEach((s) => slicesG.append(s.el));
|
|
264
|
+
aim();
|
|
265
|
+
S.forEach((s) => { s.f0 = s.t0; s.f1 = s.t1; });
|
|
266
|
+
rove = S[0] || null;
|
|
267
|
+
paintLegend();
|
|
268
|
+
describe();
|
|
269
|
+
counting = true; cnt.v = 0; // the total shows 0 until the ring draws in (play() sets it)
|
|
270
|
+
draw();
|
|
271
|
+
paint();
|
|
272
|
+
if (RM() || typeof IntersectionObserver !== 'function') play();
|
|
273
|
+
else {
|
|
274
|
+
io = new IntersectionObserver((en) => { if (en.some((x) => x.isIntersecting)) play(); }, { threshold: 0.3 });
|
|
275
|
+
io.observe(root);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/* ---------- changes: slices morph from what is on screen to the new shares ---------- */
|
|
279
|
+
const finish = () => {
|
|
280
|
+
tween = null;
|
|
281
|
+
S = S.filter((s) => { if (s.leaving) { g.killTweensOf([s.lift, s.rev]); s.el.remove(); return false; } return true; });
|
|
282
|
+
S.forEach((s) => { s.f0 = s.t0; s.f1 = s.t1; });
|
|
283
|
+
T.p = 1;
|
|
284
|
+
draw();
|
|
285
|
+
};
|
|
286
|
+
const set = (data) => {
|
|
287
|
+
const items = prep(data), was = played ? totalOf() : 0;
|
|
288
|
+
const p = T.p;
|
|
289
|
+
S.forEach((s) => { s.f0 = lerp(s.f0, s.t0, p); s.f1 = lerp(s.f1, s.t1, p); });
|
|
290
|
+
tween?.kill(); tween = null;
|
|
291
|
+
const animate = played && !RM();
|
|
292
|
+
if (played) finishIntro();
|
|
293
|
+
const old = S.slice(), used = new Set(), order = [];
|
|
294
|
+
items.forEach((d) => {
|
|
295
|
+
let s = old.find((x) => x.label === d.label && !used.has(x));
|
|
296
|
+
if (s) { s.value = d.value; s.leaving = false; if (d.tone != null) { s.color = toColor(d.tone, 0); s.el.style.setProperty('--c', s.color); } }
|
|
297
|
+
else { s = make(d); s.isNew = true; if (played) s.rev.k = 1; }
|
|
298
|
+
used.add(s); order.push(s);
|
|
299
|
+
});
|
|
300
|
+
old.forEach((s, k) => { // a slice that is gone shrinks where it was
|
|
301
|
+
if (used.has(s)) return;
|
|
302
|
+
s.leaving = true;
|
|
303
|
+
let at = 0;
|
|
304
|
+
for (let j = k - 1; j >= 0; j--) { const ix = order.indexOf(old[j]); if (ix >= 0) { at = ix + 1; break; } }
|
|
305
|
+
order.splice(at, 0, s);
|
|
306
|
+
});
|
|
307
|
+
order.forEach((s, k) => { if (s.isNew) { const a = k ? order[k - 1].f1 : 0; s.f0 = s.f1 = a; delete s.isNew; } });
|
|
308
|
+
S = order;
|
|
309
|
+
S.forEach((s, k) => { if (slicesG.children[k] !== s.el) slicesG.insertBefore(s.el, slicesG.children[k] || null); });
|
|
310
|
+
aim();
|
|
311
|
+
if (hov?.leaving) hov = null;
|
|
312
|
+
if (foc?.leaving) foc = null;
|
|
313
|
+
if (sel?.leaving) { sel = null; o.onSelect?.(-1, null, api); }
|
|
314
|
+
if (!rove || rove.leaving) rove = list()[0] || null;
|
|
315
|
+
paintLegend();
|
|
316
|
+
describe();
|
|
317
|
+
if (!animate) finish();
|
|
318
|
+
else { T.p = 0; tween = g.to(T, { p: 1, duration: 0.8, ease: 'power3.inOut', onUpdate: draw, onComplete: finish }); }
|
|
319
|
+
paint();
|
|
320
|
+
if (played) countTotal(counting ? cnt.v : was);
|
|
321
|
+
return api;
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
const api = {
|
|
325
|
+
root,
|
|
326
|
+
get data() { return list().map((s) => ({ label: s.label, value: s.value, share: share(s) })); },
|
|
327
|
+
get total() { return totalOf(); },
|
|
328
|
+
get selected() { return sel ? list().indexOf(sel) : -1; },
|
|
329
|
+
set,
|
|
330
|
+
select(which) {
|
|
331
|
+
const L = list();
|
|
332
|
+
const s = typeof which === 'string' ? L.find((x) => x.label === which) : which == null || which < 0 ? null : L[which];
|
|
333
|
+
pick(s || null, false);
|
|
334
|
+
return api;
|
|
335
|
+
},
|
|
336
|
+
destroy() {
|
|
337
|
+
ac.abort(); io?.disconnect(); io = null;
|
|
338
|
+
tween?.kill();
|
|
339
|
+
g.killTweensOf([T, cnt]);
|
|
340
|
+
S.forEach((s) => g.killTweensOf([s.lift, s.rev]));
|
|
341
|
+
g.killTweensOf(root.querySelectorAll('*'));
|
|
342
|
+
el.innerHTML = '';
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
return api;
|
|
346
|
+
};
|