@gecko-ui-kit/components 0.0.0-stage → 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/wizard.js ADDED
@@ -0,0 +1,392 @@
1
+ /* Wizard: a long form split into steps (cart → delivery → payment → review, or a sign-up).
2
+ How it moves: the step panels slide sideways in the direction you travel while the box grows or
3
+ shrinks to the new panel's height. Along the top, the line between the steps fills up to the
4
+ current one, and a finished step's dot fills and draws its tick. With the “bar” header a single
5
+ progress bar fills and the “Step 2 of 4” text ticks over. “Next” checks the step first: a problem
6
+ shows a message in the panel, the field and the button give a shake, and a check that has to wait
7
+ (a server call) shows a spinner on the button. Finishing slides in a success panel whose circle
8
+ and tick draw themselves.
9
+ Each panel is a labelled region; on every step change focus moves to the new heading and a live
10
+ region says “Step 2 of 4: Delivery”. */
11
+ import * as M from '@gecko-ui-kit/motion';
12
+ import { icon } from '@gecko-ui-kit/icons';
13
+ import { esc } from '../index.js';
14
+
15
+ const { g, RM, $$ } = M;
16
+ const WORDS = {
17
+ label: 'Progress',
18
+ next: 'Next', back: 'Back', finish: 'Finish',
19
+ step: (i, n) => `Step ${i} of ${n}`,
20
+ done: 'All done', doneText: '',
21
+ optional: 'Optional',
22
+ completed: 'completed', locked: 'not available yet',
23
+ busy: 'Checking…',
24
+ invalid: 'Please check this step.',
25
+ };
26
+ let uid = 0;
27
+ const FIELDS = 'input:not([type="hidden"]):not([type="button"]):not([type="submit"]), select, textarea';
28
+
29
+ /**
30
+ * const wz = wizard(el, {
31
+ * steps: [{ id, title, sub, content: '<html>' | Node, optional: false,
32
+ * validate: (panel, wz) => true | 'Message' | Promise<true | 'Message'> }],
33
+ * // no validate: the browser's own checks on the fields in the panel (required, type="email", pattern…)
34
+ * // a validate that sets aria-invalid="true" on a field gets that field marked, shaken and focused
35
+ * // (its .field or [data-field] wrapper gets .is-bad; editing the field clears it)
36
+ * start: 0,
37
+ * linear: true, // the header cannot jump past steps that are not finished
38
+ * header: 'steps' | 'bar' | 'none',
39
+ * level: 3, // heading level of the panel titles
40
+ * words: { next, back, finish, step: (i, n) => …, done, doneText, optional, completed, locked, busy, invalid, label },
41
+ * onStep(index, wz),
42
+ * onFinish(values, wz) // may return a Promise; false (or a message) keeps the last step open
43
+ * })
44
+ * wz.index · wz.values · wz.finished · wz.go(i) · wz.next() → Promise<boolean> · wz.back() · wz.reset()
45
+ * wz.setBusy(on) · wz.destroy()
46
+ */
47
+ export const wizard = (el, opts = {}) => {
48
+ el = $$(el)[0];
49
+ if (!el) return null;
50
+ const o = { steps: [], start: 0, linear: true, header: 'steps', level: 3, label: '', words: null, onStep: null, onFinish: null, ...opts };
51
+ const W = { ...WORDS, ...o.words };
52
+ const steps = o.steps || [], n = steps.length;
53
+ if (!n) { el.innerHTML = ''; return null; }
54
+ const head = ['steps', 'bar', 'none'].includes(o.header) ? o.header : 'steps';
55
+ const hl = Math.min(6, Math.max(1, +o.level || 3));
56
+ const ac = new AbortController(), on = (t, ev, fn, x) => t.addEventListener(ev, fn, { signal: ac.signal, ...x });
57
+ const base = `wz${++uid}`;
58
+
59
+ let cur = Math.min(n - 1, Math.max(0, +o.start || 0));
60
+ const done = new Set(Array.from({ length: cur }, (_, i) => i));
61
+ let finished = false, busy = false, moving = null, leaving = null;
62
+
63
+ /* ---------- markup ---------- */
64
+ const subOf = (s) => [head === 'steps' ? s.sub : '', s.optional ? W.optional : ''].filter(Boolean).join(' · ');
65
+ const header = head === 'steps'
66
+ ? `<ol class="wz__steps" aria-label="${esc(o.label || W.label)}">${steps.map((s, i) => `<li class="wz__step" data-i="${i}">`
67
+ + `<button type="button" class="wz__sbtn"><span class="wz__dot" aria-hidden="true"><span class="wz__num">${i + 1}</span><svg class="wz__ck" viewBox="0 0 24 24"><path pathLength="1" d="m5.5 12.5 4.2 4.2 8.8-9.2"/></svg></span>`
68
+ + `<span class="wz__lbl"><b>${esc(s.title ?? '')}</b>${subOf(s) ? `<small>${esc(subOf(s))}</small>` : ''}</span><span class="wz__sr wz__st"></span></button>`
69
+ + `${i < n - 1 ? '<span class="wz__line" aria-hidden="true"><i></i></span>' : ''}</li>`).join('')}</ol>`
70
+ : head === 'bar'
71
+ ? `<div class="wz__bar"><div class="wz__meta" aria-hidden="true"><span class="wz__count"></span><b class="wz__name"></b></div><div class="wz__track" role="progressbar" aria-label="${esc(o.label || W.label)}" aria-valuemin="0" aria-valuemax="${n}"><i class="wz__fill"></i></div></div>`
72
+ : '';
73
+ const panel = (s, i) => `<section class="wz__panel" role="region" data-i="${i}" aria-labelledby="${base}-h${i}" hidden>`
74
+ + `<h${hl} class="wz__h" id="${base}-h${i}" tabindex="-1">${esc(s.title ?? '')}</h${hl}>`
75
+ + `${head !== 'steps' && s.sub ? `<p class="wz__sub">${esc(s.sub)}</p>` : ''}<div class="wz__body"></div>`
76
+ + `<p class="wz__err" id="${base}-e${i}" aria-live="polite"></p></section>`;
77
+ const fin = `<section class="wz__panel wz__fin" role="region" aria-labelledby="${base}-hf" hidden>`
78
+ + '<svg class="wz__tick" viewBox="0 0 52 52" aria-hidden="true"><circle class="wz__tick-c" cx="26" cy="26" r="23" pathLength="1"/><path class="wz__tick-p" d="m16 27 7 7 13-15" pathLength="1"/></svg>'
79
+ + `<h${hl} class="wz__h" id="${base}-hf" tabindex="-1">${esc(W.done)}</h${hl}>${W.doneText ? `<p class="wz__sub">${esc(W.doneText)}</p>` : ''}</section>`;
80
+
81
+ const root = document.createElement('div');
82
+ root.className = `wz wz--${head}`;
83
+ root.innerHTML = `${header}<div class="wz__vp">${steps.map(panel).join('')}${fin}</div>`
84
+ + `<div class="wz__foot"><button type="button" class="btn wz__back">${icon('chevL')}<span>${esc(W.back)}</span></button>`
85
+ + `<button type="button" class="btn btn--primary wz__next"><span class="wz__spin" aria-hidden="true"></span><span class="wz__nl"></span><span class="wz__ni"></span></button></div>`
86
+ + '<div class="wz__sr" role="status"></div>';
87
+ el.innerHTML = ''; el.append(root);
88
+ const vp = root.querySelector('.wz__vp'), foot = root.querySelector('.wz__foot');
89
+ const backBtn = root.querySelector('.wz__back'), nextBtn = root.querySelector('.wz__next');
90
+ const panels = [...vp.querySelectorAll('.wz__panel:not(.wz__fin)')], finEl = vp.querySelector('.wz__fin');
91
+ const items = [...root.querySelectorAll('.wz__step')];
92
+ steps.forEach((s, i) => {
93
+ const body = panels[i].querySelector('.wz__body');
94
+ if (s.content instanceof Node) body.append(s.content); else if (s.content != null) body.innerHTML = String(s.content);
95
+ });
96
+ const say = (t) => { root.querySelector('.wz__sr[role="status"]').textContent = t; };
97
+
98
+ /* ---------- values: every named field in the panels ---------- */
99
+ const values = () => {
100
+ const v = {}, many = {};
101
+ panels.forEach((p) => p.querySelectorAll('input[name][type="checkbox"]').forEach((f) => { many[f.name] = (many[f.name] || 0) + 1; }));
102
+ panels.forEach((p) => {
103
+ p.querySelectorAll('[name]').forEach((f) => {
104
+ if (!('value' in f) || f.disabled || f.type === 'button' || f.type === 'submit') return;
105
+ const k = f.name;
106
+ if (f.type === 'checkbox') { if (many[k] > 1) { v[k] ||= []; if (f.checked) v[k].push(f.value); } else v[k] = f.checked; }
107
+ else if (f.type === 'radio') { if (f.checked) v[k] = f.value; else if (!(k in v)) v[k] = null; }
108
+ else if (f.tagName === 'SELECT' && f.multiple) v[k] = [...f.selectedOptions].map((x) => x.value);
109
+ else if (f.type === 'file') v[k] = f.files;
110
+ else v[k] = f.value;
111
+ });
112
+ // the library's own switches and checkboxes: <button role="switch" data-name="news">
113
+ p.querySelectorAll('[data-name][aria-checked]').forEach((b) => { v[b.dataset.name] = b.getAttribute('aria-checked') === 'true'; });
114
+ });
115
+ return v;
116
+ };
117
+
118
+ /* ---------- checking a step ---------- */
119
+ const fieldsOf = (p) => [...p.querySelectorAll(FIELDS)].filter((f) => !f.disabled);
120
+ /** The step's answer: true, or a message. Waits (with the spinner) when validate returns a Promise. */
121
+ const check = async (i) => {
122
+ const s = steps[i], p = panels[i];
123
+ let r;
124
+ try {
125
+ if (typeof s.validate === 'function') r = s.validate(p, api);
126
+ else { const bad = fieldsOf(p).find((f) => f.willValidate && !f.checkValidity()); r = bad ? (bad.setAttribute('aria-invalid', 'true'), bad.validationMessage || W.invalid) : true; }
127
+ if (r && typeof r.then === 'function') { setBusy(true); try { r = await r; } finally { setBusy(false); } }
128
+ } catch (e) { r = e?.message || W.invalid; }
129
+ if (r === false) return W.invalid;
130
+ return typeof r === 'string' && r ? r : true;
131
+ };
132
+ const shake = (t) => { if (t && !RM()) g.fromTo(t, { x: 0 }, { keyframes: { x: [0, -7, 6, -4, 2, 0] }, duration: 0.42, ease: 'power2.out', clearProps: 'transform' }); };
133
+ const fail = (i, msg) => {
134
+ const p = panels[i], err = p.querySelector('.wz__err');
135
+ const had = !!err.textContent;
136
+ err.innerHTML = `${icon('alert')}<span>${esc(msg)}</span>`;
137
+ if (!had && !RM()) g.fromTo(err, { opacity: 0, y: -4 }, { opacity: 1, y: 0, duration: 0.3, ease: 'power3.out', clearProps: 'transform,opacity' });
138
+ const f = p.querySelector('[aria-invalid="true"]');
139
+ if (f) {
140
+ const wrap = f.closest('.field, [data-field]');
141
+ wrap?.classList.add('is-bad');
142
+ const ids = (f.getAttribute('aria-describedby') || '').split(' ').filter(Boolean);
143
+ if (!ids.includes(err.id)) f.setAttribute('aria-describedby', [...ids, err.id].join(' '));
144
+ shake(wrap || f);
145
+ // a <select> upgraded into a dropdown is hidden: focus its button instead
146
+ (f.__dd?.button ?? (f.getAttribute('aria-hidden') === 'true' && f.nextElementSibling?.matches('button') ? f.nextElementSibling : f)).focus({ preventScroll: true });
147
+ } else if (had) shake(err);
148
+ shake(nextBtn);
149
+ };
150
+ const clearBad = (f) => {
151
+ const p = f.closest('.wz__panel'); if (!p || p === finEl) return;
152
+ const err = p.querySelector('.wz__err');
153
+ // a radio group is fixed as a whole: picking any option clears the one that was marked
154
+ const group = f.type === 'radio' && f.name ? [...p.querySelectorAll('input[type="radio"]')].filter((x) => x.name === f.name) : [f];
155
+ group.forEach((x) => {
156
+ if (x.getAttribute('aria-invalid') === 'true') x.removeAttribute('aria-invalid');
157
+ x.closest('.field, [data-field]')?.classList.remove('is-bad');
158
+ const ids = (x.getAttribute('aria-describedby') || '').split(' ').filter((id) => id && id !== err.id);
159
+ if (ids.length) x.setAttribute('aria-describedby', ids.join(' ')); else x.removeAttribute('aria-describedby');
160
+ });
161
+ if (!p.querySelector('[aria-invalid="true"]')) err.textContent = '';
162
+ };
163
+ const clearAll = (p) => {
164
+ p.querySelectorAll('[aria-invalid="true"]').forEach(clearBad);
165
+ p.querySelector('.wz__err') && (p.querySelector('.wz__err').textContent = '');
166
+ };
167
+
168
+ /* ---------- painting the header and the buttons ---------- */
169
+ const reachable = (k) => {
170
+ if (!o.linear || k <= cur) return true;
171
+ for (let j = 0; j < k; j++) if (j !== cur && !done.has(j) && !steps[j].optional) return false;
172
+ return true;
173
+ };
174
+ const paintHead = (dir = 0) => {
175
+ if (head === 'steps') {
176
+ items.forEach((li, k) => {
177
+ const isDone = finished || (done.has(k) && k !== cur), now = !finished && k === cur, was = li.classList.contains('is-done');
178
+ const locked = !finished && !reachable(k);
179
+ li.classList.toggle('is-done', isDone); li.classList.toggle('is-now', now); li.classList.toggle('is-locked', locked);
180
+ const b = li.querySelector('.wz__sbtn');
181
+ if (now) b.setAttribute('aria-current', 'step'); else b.removeAttribute('aria-current');
182
+ if (locked || finished) b.setAttribute('aria-disabled', 'true'); else b.removeAttribute('aria-disabled');
183
+ li.querySelector('.wz__st').textContent = isDone ? `, ${W.completed}` : locked ? `, ${W.locked}` : '';
184
+ if (isDone && !was && dir && !RM()) {
185
+ g.fromTo(li.querySelector('.wz__dot'), { scale: 0.6 }, { scale: 1, duration: 0.55, ease: 'back.out(3)', delay: 0.05, clearProps: 'transform' });
186
+ g.fromTo(li.querySelector('.wz__ck path'), { strokeDashoffset: 1 }, { strokeDashoffset: 0, duration: 0.38, delay: 0.14, ease: 'power2.out', clearProps: 'strokeDashoffset' });
187
+ } else if (now && dir && !RM()) g.fromTo(li.querySelector('.wz__dot'), { scale: 0.82 }, { scale: 1, duration: 0.5, ease: 'back.out(3)', clearProps: 'transform' });
188
+ const line = li.querySelector('.wz__line i');
189
+ if (line) {
190
+ const to = finished || k < cur ? 1 : 0;
191
+ if (RM() || !dir) g.set(line, { scaleX: to }); else g.to(line, { scaleX: to, duration: 0.55, ease: 'power3.inOut', overwrite: true });
192
+ }
193
+ });
194
+ } else if (head === 'bar') {
195
+ const track = root.querySelector('.wz__track'), fill = root.querySelector('.wz__fill'), meta = root.querySelector('.wz__meta');
196
+ const f = finished ? 1 : (cur + 1) / n;
197
+ root.querySelector('.wz__count').textContent = finished ? W.done : W.step(cur + 1, n);
198
+ root.querySelector('.wz__name').textContent = finished ? '' : steps[cur].title ?? '';
199
+ track.setAttribute('aria-valuenow', finished ? n : cur + 1);
200
+ track.setAttribute('aria-valuetext', finished ? W.done : `${W.step(cur + 1, n)}: ${steps[cur].title ?? ''}`);
201
+ if (RM() || !dir) g.set(fill, { scaleX: f });
202
+ else {
203
+ g.to(fill, { scaleX: f, duration: 0.6, ease: 'power3.inOut', overwrite: true });
204
+ g.fromTo(meta, { y: 8 * dir, opacity: 0 }, { y: 0, opacity: 1, duration: 0.4, ease: 'back.out(1.8)', clearProps: 'transform,opacity' });
205
+ }
206
+ }
207
+ };
208
+ const paintFoot = () => {
209
+ backBtn.disabled = cur === 0;
210
+ const last = cur === n - 1;
211
+ nextBtn.querySelector('.wz__nl').textContent = last ? W.finish : W.next;
212
+ nextBtn.querySelector('.wz__ni').innerHTML = icon(last ? 'check' : 'chevR');
213
+ };
214
+
215
+ /* ---------- moving between panels ---------- */
216
+ const endMove = () => {
217
+ moving?.kill(); moving = null;
218
+ if (leaving) { leaving.hidden = true; leaving.classList.remove('is-leaving'); g.set(leaving, { clearProps: 'transform,opacity' }); leaving = null; }
219
+ vp.classList.remove('is-moving');
220
+ g.set(vp, { clearProps: 'height' });
221
+ [...panels, finEl].forEach((p) => { if (!p.hidden) g.set(p, { clearProps: 'transform,opacity' }); });
222
+ };
223
+ const slide = (from, into, dir) => {
224
+ endMove();
225
+ if (from === into) { into.hidden = false; return; }
226
+ const h0 = vp.offsetHeight;
227
+ into.hidden = false;
228
+ if (RM() || !from) { if (from) from.hidden = true; return; }
229
+ from.classList.add('is-leaving');
230
+ leaving = from;
231
+ const h1 = into.offsetHeight;
232
+ vp.classList.add('is-moving');
233
+ moving = g.timeline({ onComplete: endMove })
234
+ .to(from, { xPercent: -dir * 28, opacity: 0, duration: 0.3, ease: 'power2.in' }, 0)
235
+ .fromTo(into, { xPercent: dir * 28, opacity: 0 }, { xPercent: 0, opacity: 1, duration: 0.52, ease: 'power3.out' }, 0.1)
236
+ .fromTo(vp, { height: h0 }, { height: h1, duration: 0.5, ease: 'power3.inOut' }, 0);
237
+ };
238
+ const shown = () => (finished ? finEl : panels[cur]);
239
+ /** Focus a panel's heading without letting the browser scroll the sliding box; bring it into view if it is off screen. */
240
+ const focusHead = (p) => {
241
+ const h = p.querySelector('.wz__h');
242
+ h.focus({ preventScroll: true });
243
+ const r = root.getBoundingClientRect();
244
+ if (r.top < 0 || r.top > innerHeight - 80) root.scrollIntoView({ block: 'start', behavior: RM() ? 'auto' : 'smooth' });
245
+ };
246
+ /** Show step `to`. focus: move focus to its heading (default: when focus is already inside). */
247
+ const show = (to, { focus } = {}) => {
248
+ to = Math.min(n - 1, Math.max(0, to));
249
+ const wasFinished = finished;
250
+ if (to === cur && !wasFinished) return;
251
+ const from = shown(), dir = wasFinished ? -1 : to > cur ? 1 : -1;
252
+ const f = focus ?? root.contains(document.activeElement);
253
+ if (wasFinished) { finished = false; showFoot(); }
254
+ cur = to;
255
+ slide(from, panels[cur], dir);
256
+ paintHead(dir); paintFoot();
257
+ if (f) focusHead(panels[cur]);
258
+ say(`${W.step(cur + 1, n)}: ${steps[cur].title ?? ''}`);
259
+ o.onStep?.(cur, api);
260
+ };
261
+ const hideFoot = () => {
262
+ if (RM()) { foot.hidden = true; return; }
263
+ g.to(foot, { opacity: 0, y: 8, duration: 0.25, ease: 'power2.in', onComplete: () => { foot.hidden = true; g.set(foot, { clearProps: 'transform,opacity' }); } });
264
+ };
265
+ const showFoot = () => {
266
+ g.killTweensOf(foot); foot.hidden = false;
267
+ if (!RM()) g.fromTo(foot, { opacity: 0, y: 8 }, { opacity: 1, y: 0, duration: 0.35, ease: 'power3.out', clearProps: 'transform,opacity' });
268
+ };
269
+ const showDone = () => {
270
+ const from = panels[cur];
271
+ finished = true;
272
+ slide(from, finEl, 1);
273
+ paintHead(1);
274
+ hideFoot();
275
+ focusHead(finEl);
276
+ say(W.done);
277
+ if (!RM()) {
278
+ const svg = finEl.querySelector('.wz__tick');
279
+ g.fromTo(svg, { scale: 0.6, opacity: 0 }, { scale: 1, opacity: 1, duration: 0.5, delay: 0.2, ease: 'back.out(2)', clearProps: 'transform,opacity' });
280
+ g.fromTo(svg.querySelector('.wz__tick-c'), { strokeDashoffset: 1 }, { strokeDashoffset: 0, duration: 0.6, delay: 0.25, ease: 'power2.inOut', clearProps: 'strokeDashoffset' });
281
+ g.fromTo(svg.querySelector('.wz__tick-p'), { strokeDashoffset: 1 }, { strokeDashoffset: 0, duration: 0.35, delay: 0.75, ease: 'power2.out', clearProps: 'strokeDashoffset' });
282
+ g.fromTo(finEl.querySelectorAll('.wz__h, .wz__sub'), { opacity: 0, y: 8 }, { opacity: 1, y: 0, duration: 0.45, delay: 0.45, stagger: 0.07, ease: 'power3.out', clearProps: 'transform,opacity' });
283
+ }
284
+ };
285
+
286
+ const setBusy = (b) => {
287
+ busy = !!b;
288
+ nextBtn.classList.toggle('is-busy', busy);
289
+ nextBtn.setAttribute('aria-busy', String(busy));
290
+ root.classList.toggle('is-busy', busy);
291
+ if (busy) say(W.busy);
292
+ };
293
+ const finish = async (focus) => {
294
+ // with linear: false, steps you skipped are checked now; the first one with a problem opens
295
+ for (let k = 0; k < n; k++) {
296
+ if (k === cur || done.has(k) || steps[k].optional) continue;
297
+ const r = await check(k);
298
+ if (r === true) { done.add(k); continue; }
299
+ show(k, { focus }); fail(k, r);
300
+ return false;
301
+ }
302
+ let r = true;
303
+ if (o.onFinish) {
304
+ setBusy(true);
305
+ try { r = await o.onFinish(values(), api); } catch (e) { r = e?.message || W.invalid; } finally { setBusy(false); }
306
+ }
307
+ if (r === false) return false;
308
+ if (typeof r === 'string' && r) { fail(cur, r); return false; }
309
+ showDone();
310
+ o.onStep?.(n, api);
311
+ return true;
312
+ };
313
+ const next = async (focus) => {
314
+ if (busy || finished) return false;
315
+ const at = cur;
316
+ const r = await check(at);
317
+ if (r !== true) { if (cur === at && !finished) fail(at, r); return false; }
318
+ clearAll(panels[at]);
319
+ done.add(at);
320
+ if (at < n - 1) { show(at + 1, { focus }); return true; }
321
+ return finish(focus);
322
+ };
323
+ const jump = async (k) => {
324
+ if (busy || finished || k === cur) return;
325
+ if (k < cur || !o.linear) { show(k, { focus: true }); return; }
326
+ if (!reachable(k)) { shake(items[k]?.querySelector('.wz__sbtn')); return; }
327
+ const at = cur, r = await check(at);
328
+ if (r !== true) { fail(at, r); return; }
329
+ clearAll(panels[at]); done.add(at);
330
+ show(k, { focus: true });
331
+ };
332
+
333
+ /* ---------- input ---------- */
334
+ on(root, 'click', (ev) => {
335
+ const t = ev.target;
336
+ if (t.closest('.wz__next')) { if (!busy) next(true); return; }
337
+ if (t.closest('.wz__back')) { if (!busy && cur > 0) show(cur - 1, { focus: true }); return; }
338
+ const sb = t.closest('.wz__sbtn');
339
+ if (sb) jump(+sb.closest('.wz__step').dataset.i);
340
+ });
341
+ on(root, 'keydown', (ev) => {
342
+ if (ev.key !== 'Enter' || ev.isComposing || ev.defaultPrevented) return;
343
+ const f = ev.target;
344
+ if (!f.matches?.('input') || /^(checkbox|radio|button|submit|reset|file|range|color)$/.test(f.type)) return;
345
+ if (!f.closest('.wz__panel') || f.closest('.wz__fin')) return;
346
+ ev.preventDefault();
347
+ if (!busy) next(true);
348
+ });
349
+ const edited = (ev) => { const f = ev.target; if (f.matches?.(FIELDS) || f.matches?.('[aria-invalid]')) clearBad(f); };
350
+ on(root, 'input', edited);
351
+ on(root, 'change', edited);
352
+
353
+ const api = {
354
+ el: root, opts: o,
355
+ /** The step on show (counted from 0); steps.length once finished. */
356
+ get index() { return finished ? n : cur; },
357
+ get finished() { return finished; },
358
+ /** Every named field in the panels: { name: value }. Checkboxes give true/false (or a list when several share a name). */
359
+ get values() { return values(); },
360
+ /** Show a step without checking the one you are on. */
361
+ go(i) { show(+i || 0); return api; },
362
+ /** Check this step and move on (or finish). Resolves to true when it moved. */
363
+ next: () => next(root.contains(document.activeElement)),
364
+ back() { if (!busy && !finished && cur > 0) show(cur - 1); return api; },
365
+ /** Back to the first step with the fields as they started. */
366
+ reset() {
367
+ endMove();
368
+ panels.forEach((p) => {
369
+ p.querySelectorAll('form').forEach((f) => f.reset());
370
+ p.querySelectorAll('input, textarea').forEach((f) => { if (f.type === 'checkbox' || f.type === 'radio') f.checked = f.defaultChecked; else if (f.type !== 'file') f.value = f.defaultValue; });
371
+ p.querySelectorAll('option').forEach((x) => { x.selected = x.defaultSelected; });
372
+ p.querySelectorAll('select').forEach((x) => x.__dd?.sync?.());
373
+ clearAll(p);
374
+ });
375
+ done.clear();
376
+ if (cur === 0 && !finished) { paintHead(0); paintFoot(); return api; }
377
+ show(0);
378
+ return api;
379
+ },
380
+ setBusy(b) { setBusy(b); return api; },
381
+ destroy() {
382
+ ac.abort();
383
+ moving?.kill();
384
+ g.killTweensOf([root, vp, foot, ...root.querySelectorAll('*')]);
385
+ el.innerHTML = '';
386
+ },
387
+ };
388
+
389
+ panels[cur].hidden = false;
390
+ paintHead(0); paintFoot();
391
+ return api;
392
+ };