@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/LICENSE +21 -0
- package/README.md +712 -2
- package/index.js +198 -0
- package/package.json +42 -4
- package/src/accordion.js +208 -0
- package/src/alert.js +205 -0
- package/src/authform.js +285 -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/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/rangeslider.js +266 -0
- package/src/rating.js +171 -0
- package/src/signature.js +213 -0
- package/src/skeleton.js +254 -0
- package/src/spinner.js +38 -0
- package/src/stepper.js +211 -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/upload.js +416 -0
- package/src/virtuallist.js +373 -0
- package/src/wizard.js +392 -0
- package/style.css +1761 -0
package/src/alert.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/* Alert / banner: a message in the page (not a pop-up) — saved, couldn't save, a warning, a notice
|
|
2
|
+
across the top of the app. It grows open when it first shows while its icon pops in. update()
|
|
3
|
+
changes it in place: the colours cross-fade, the icon spins out and the new one pops in, the old
|
|
4
|
+
text slides up and away while the new text slides in, and the height follows. close() folds it
|
|
5
|
+
away smoothly and empties the element; show() grows it back. shake() nudges it to say "look here".
|
|
6
|
+
With reduced motion it changes at once. */
|
|
7
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
8
|
+
import { icon } from '@gecko-ui-kit/icons';
|
|
9
|
+
import { esc } from '../index.js';
|
|
10
|
+
|
|
11
|
+
const { g, RM, $$ } = M;
|
|
12
|
+
const WORDS = { close: 'Close' };
|
|
13
|
+
const TONES = ['info', 'good', 'warn', 'bad'];
|
|
14
|
+
const LOOKS = ['soft', 'outline', 'solid', 'banner'];
|
|
15
|
+
const ICONS = { info: 'info', good: 'check', warn: 'alert', bad: 'close' };
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* const a = alert(el, {
|
|
19
|
+
* tone: 'info' | 'good' | 'warn' | 'bad',
|
|
20
|
+
* title: 'Saved',
|
|
21
|
+
* body: 'Your changes are live.', // HTML (trusted)
|
|
22
|
+
* icon: 'check', // default per tone (info · check · alert · close); false for none
|
|
23
|
+
* look: 'soft' | 'outline' | 'solid' | 'banner', // banner: a full-width bar
|
|
24
|
+
* dismissible: false, // a close button (and Esc while focus is inside)
|
|
25
|
+
* actions: [{ label: 'Try again', primary: true, run: (a, button) => … }],
|
|
26
|
+
* // run may return a Promise: the button waits (disabled) until it settles
|
|
27
|
+
* open: true, // false: starts closed, show() it later
|
|
28
|
+
* words: { close },
|
|
29
|
+
* onClose(a),
|
|
30
|
+
* })
|
|
31
|
+
* a.update({ tone, title, body, icon, actions }) · a.close() → Promise · a.show() → Promise
|
|
32
|
+
* a.shake() · a.open · a.tone · a.destroy()
|
|
33
|
+
* Screen readers: role="status" for info / good, role="alert" for warn / bad.
|
|
34
|
+
*/
|
|
35
|
+
export const alert = (el, opts = {}) => {
|
|
36
|
+
el = $$(el)[0];
|
|
37
|
+
if (!el) return null;
|
|
38
|
+
const o = { tone: 'info', title: '', body: '', icon: null, look: 'soft', dismissible: false, actions: null, open: true, words: null, onClose: null, ...opts };
|
|
39
|
+
const W = { ...WORDS, ...o.words };
|
|
40
|
+
const ac = new AbortController(), on = (t, ev, fn, x) => t.addEventListener(ev, fn, { signal: ac.signal, ...x });
|
|
41
|
+
const look = LOOKS.includes(o.look) ? o.look : 'soft';
|
|
42
|
+
const s = { tone: TONES.includes(o.tone) ? o.tone : 'info', title: o.title ?? '', body: o.body ?? '', icon: o.icon, actions: o.actions || [] };
|
|
43
|
+
let open = false, closing = false, tl = null;
|
|
44
|
+
|
|
45
|
+
el.innerHTML = '';
|
|
46
|
+
const root = document.createElement('div');
|
|
47
|
+
root.className = `al al--${look}`;
|
|
48
|
+
root.innerHTML = '<div class="al__box"><span class="al__ic" aria-hidden="true"></span>'
|
|
49
|
+
+ '<div class="al__txt" aria-atomic="true"><strong class="al__title"></strong><div class="al__body"></div></div>'
|
|
50
|
+
+ '<div class="al__acts"></div>'
|
|
51
|
+
+ `${o.dismissible ? `<button type="button" class="al__x" aria-label="${esc(W.close)}">${icon('close')}</button>` : ''}</div>`;
|
|
52
|
+
const box = root.firstElementChild, ic = box.querySelector('.al__ic'), txt = box.querySelector('.al__txt'),
|
|
53
|
+
title = txt.firstElementChild, body = txt.lastElementChild, acts = box.querySelector('.al__acts'), xb = box.querySelector('.al__x');
|
|
54
|
+
|
|
55
|
+
/* ---------- painting ---------- */
|
|
56
|
+
const iconName = () => (s.icon === false ? null : s.icon || ICONS[s.tone]);
|
|
57
|
+
const paintTone = () => {
|
|
58
|
+
TONES.forEach((t) => root.classList.toggle(`al--${t}`, t === s.tone));
|
|
59
|
+
root.classList.toggle('al--still', RM()); // no colour cross-fade with reduced motion
|
|
60
|
+
const loud = s.tone === 'warn' || s.tone === 'bad';
|
|
61
|
+
txt.setAttribute('role', loud ? 'alert' : 'status');
|
|
62
|
+
txt.setAttribute('aria-live', loud ? 'assertive' : 'polite');
|
|
63
|
+
};
|
|
64
|
+
const paintIcon = () => {
|
|
65
|
+
const n = iconName();
|
|
66
|
+
if ((ic.dataset.i || '') === (n || '')) return;
|
|
67
|
+
ic.dataset.i = n || ''; ic.innerHTML = n ? icon(n) : ''; ic.hidden = !n;
|
|
68
|
+
root.classList.toggle('al--noicon', !n);
|
|
69
|
+
};
|
|
70
|
+
const paintText = () => {
|
|
71
|
+
title.textContent = s.title; title.hidden = !s.title;
|
|
72
|
+
body.innerHTML = s.body; body.hidden = !s.body;
|
|
73
|
+
};
|
|
74
|
+
const paintActs = () => {
|
|
75
|
+
acts.innerHTML = s.actions.map((a, i) => `<button type="button" class="btn btn--sm al__act${a.primary ? ' al__act--primary' : ''}" data-i="${i}">${esc(a.label)}</button>`).join('');
|
|
76
|
+
acts.hidden = !s.actions.length;
|
|
77
|
+
};
|
|
78
|
+
paintTone(); paintIcon(); paintText(); paintActs();
|
|
79
|
+
|
|
80
|
+
/* ---------- motion ---------- */
|
|
81
|
+
/** Finish what is still running (an update or the first grow) before the next change. */
|
|
82
|
+
const settle = () => { if (tl && !closing) { const t = tl; t.progress(1); if (tl === t) tl = null; } };
|
|
83
|
+
const tidy = () => { root.classList.remove('is-moving'); g.set([root, box, ic, txt], { clearProps: 'height,opacity,transform' }); };
|
|
84
|
+
|
|
85
|
+
const show = () => {
|
|
86
|
+
if (open && !closing) return Promise.resolve();
|
|
87
|
+
const fresh = !root.isConnected;
|
|
88
|
+
tl?.kill(); tl = null;
|
|
89
|
+
if (fresh) el.replaceChildren(root);
|
|
90
|
+
open = true; closing = false;
|
|
91
|
+
paintTone();
|
|
92
|
+
if (RM()) { tidy(); return Promise.resolve(); }
|
|
93
|
+
const h0 = fresh ? 0 : root.offsetHeight;
|
|
94
|
+
root.classList.add('is-moving');
|
|
95
|
+
return new Promise((resolve) => {
|
|
96
|
+
tl = g.timeline({ onComplete: () => { tl = null; root.classList.remove('is-moving'); resolve(); } });
|
|
97
|
+
tl.fromTo(root, { height: h0 }, { height: 'auto', duration: 0.5, ease: 'power3.out', clearProps: 'height' }, 0);
|
|
98
|
+
if (fresh) {
|
|
99
|
+
tl.fromTo(box, { opacity: 0, y: look === 'banner' ? -10 : -6, scale: look === 'banner' ? 1 : 0.98 }, { opacity: 1, y: 0, scale: 1, duration: 0.42, ease: 'power3.out', clearProps: 'opacity,transform' }, 0.04)
|
|
100
|
+
.fromTo([txt, acts], { opacity: 0, x: 8 }, { opacity: 1, x: 0, duration: 0.42, stagger: 0.06, ease: 'power3.out', clearProps: 'opacity,transform' }, 0.12);
|
|
101
|
+
if (!ic.hidden) tl.fromTo(ic, { scale: 0.3, rotation: -30 }, { scale: 1, rotation: 0, duration: 0.6, ease: 'back.out(3)', clearProps: 'transform' }, 0.14);
|
|
102
|
+
} else tl.to(box, { opacity: 1, y: 0, scale: 1, duration: 0.35, ease: 'power3.out', clearProps: 'opacity,transform' }, 0);
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const close = () => {
|
|
107
|
+
if (!open) return Promise.resolve();
|
|
108
|
+
tl?.kill(); tl = null;
|
|
109
|
+
open = false; closing = true;
|
|
110
|
+
return new Promise((resolve) => {
|
|
111
|
+
const end = () => {
|
|
112
|
+
tl = null; closing = false; tidy();
|
|
113
|
+
el.replaceChildren();
|
|
114
|
+
resolve(); o.onClose?.(api);
|
|
115
|
+
};
|
|
116
|
+
if (RM() || !root.isConnected) { end(); return; }
|
|
117
|
+
root.classList.add('is-moving');
|
|
118
|
+
tl = g.timeline({ onComplete: end });
|
|
119
|
+
tl.to(box, { opacity: 0, y: look === 'banner' ? -10 : -4, scale: look === 'banner' ? 1 : 0.98, duration: 0.24, ease: 'power2.in' }, 0)
|
|
120
|
+
.fromTo(root, { height: root.offsetHeight }, { height: 0, duration: 0.38, ease: 'power3.inOut' }, 0.06);
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const update = (next = {}) => {
|
|
125
|
+
const before = { ...s }, prevIcon = iconName();
|
|
126
|
+
if (TONES.includes(next.tone)) s.tone = next.tone;
|
|
127
|
+
if ('title' in next) s.title = next.title ?? '';
|
|
128
|
+
if ('body' in next) s.body = next.body ?? '';
|
|
129
|
+
if ('icon' in next) s.icon = next.icon;
|
|
130
|
+
if ('actions' in next) s.actions = next.actions || [];
|
|
131
|
+
const toneCh = s.tone !== before.tone, textCh = s.title !== before.title || s.body !== before.body,
|
|
132
|
+
actCh = 'actions' in next, iconCh = iconName() !== prevIcon;
|
|
133
|
+
if (!toneCh && !textCh && !actCh && !iconCh) return api;
|
|
134
|
+
if (!open || closing || RM()) { paintTone(); paintIcon(); paintText(); if (actCh) paintActs(); return api; }
|
|
135
|
+
settle();
|
|
136
|
+
const h0 = root.offsetHeight;
|
|
137
|
+
let ghost = null;
|
|
138
|
+
if (textCh) { // the old text, left where it was, slides up and away
|
|
139
|
+
ghost = txt.cloneNode(true);
|
|
140
|
+
ghost.removeAttribute('role'); ghost.removeAttribute('aria-live'); ghost.setAttribute('aria-hidden', 'true');
|
|
141
|
+
ghost.className = 'al__txt al__ghost';
|
|
142
|
+
ghost.style.cssText = `left:${txt.offsetLeft}px;top:${txt.offsetTop}px;width:${txt.offsetWidth}px`;
|
|
143
|
+
}
|
|
144
|
+
paintTone(); // colours cross-fade (CSS transitions)
|
|
145
|
+
paintText(); if (actCh) paintActs();
|
|
146
|
+
if (ghost) box.append(ghost);
|
|
147
|
+
const h1 = root.offsetHeight, OUT = 0.15;
|
|
148
|
+
root.classList.add('is-moving');
|
|
149
|
+
tl = g.timeline({ onComplete: () => { tl = null; ghost?.remove(); root.classList.remove('is-moving'); if (ic.hidden) g.set(ic, { clearProps: 'transform,opacity' }); } });
|
|
150
|
+
if (Math.abs(h1 - h0) > 1) tl.fromTo(root, { height: h0 }, { height: h1, duration: 0.42, ease: 'power3.inOut', clearProps: 'height' }, 0);
|
|
151
|
+
if (ghost) {
|
|
152
|
+
tl.to(ghost, { opacity: 0, y: -10, duration: 0.2, ease: 'power2.in' }, 0)
|
|
153
|
+
.fromTo(txt, { opacity: 0, y: 10 }, { opacity: 1, y: 0, duration: 0.42, ease: 'power3.out', clearProps: 'opacity,transform' }, 0.1);
|
|
154
|
+
}
|
|
155
|
+
if (actCh && acts.children.length) tl.fromTo(acts.children, { opacity: 0, y: 6 }, { opacity: 1, y: 0, duration: 0.36, stagger: 0.05, ease: 'power3.out', clearProps: 'opacity,transform' }, 0.16);
|
|
156
|
+
if (iconCh) {
|
|
157
|
+
const had = !ic.hidden;
|
|
158
|
+
if (had) tl.to(ic, { scale: 0.4, rotation: -25, opacity: 0, duration: OUT, ease: 'power2.in' }, 0);
|
|
159
|
+
tl.add(paintIcon, had ? OUT : 0);
|
|
160
|
+
if (iconName()) tl.fromTo(ic, { scale: 0.4, rotation: 25, opacity: 0 }, { scale: 1, rotation: 0, opacity: 1, duration: 0.55, ease: 'back.out(3)', immediateRender: false, clearProps: 'transform,opacity' }, had ? OUT : 0);
|
|
161
|
+
} else if (toneCh && !ic.hidden) tl.fromTo(ic, { scale: 0.7 }, { scale: 1, duration: 0.5, ease: 'back.out(3)', clearProps: 'transform' }, 0);
|
|
162
|
+
tl.to({}, { duration: 0.01 }, 0); // never an empty timeline
|
|
163
|
+
return api;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const shake = () => {
|
|
167
|
+
if (!open || closing || RM()) return api;
|
|
168
|
+
settle();
|
|
169
|
+
M.shake(box);
|
|
170
|
+
if (!ic.hidden) g.fromTo(ic, { rotation: -18, scale: 1.12 }, { rotation: 0, scale: 1, duration: 0.6, ease: 'elastic.out(1, 0.4)', clearProps: 'transform' });
|
|
171
|
+
return api;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/* ---------- events ---------- */
|
|
175
|
+
on(acts, 'click', async (e) => {
|
|
176
|
+
const b = e.target.closest('.al__act'); if (!b) return;
|
|
177
|
+
const a = s.actions[+b.dataset.i]; if (!a?.run) return;
|
|
178
|
+
const r = a.run(api, b);
|
|
179
|
+
if (r && typeof r.then === 'function') {
|
|
180
|
+
b.disabled = true; b.setAttribute('aria-busy', 'true');
|
|
181
|
+
try { await r; } finally { b.disabled = false; b.removeAttribute('aria-busy'); }
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
if (xb) {
|
|
185
|
+
on(xb, 'click', () => close());
|
|
186
|
+
on(root, 'keydown', (e) => { if (e.key === 'Escape' && open && !closing) { e.stopPropagation(); close(); } });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const api = {
|
|
190
|
+
root,
|
|
191
|
+
get open() { return open; },
|
|
192
|
+
get tone() { return s.tone; },
|
|
193
|
+
update,
|
|
194
|
+
close,
|
|
195
|
+
show,
|
|
196
|
+
shake,
|
|
197
|
+
destroy() {
|
|
198
|
+
ac.abort(); tl?.kill(); tl = null;
|
|
199
|
+
g.killTweensOf([root, box, ic, txt, acts, ...acts.children]);
|
|
200
|
+
el.innerHTML = '';
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
if (o.open !== false) show();
|
|
204
|
+
return api;
|
|
205
|
+
};
|
package/src/authform.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/* Sign in / create account card. A two-way switch at the top slides its stretchy thumb between
|
|
2
|
+
“Sign in” and “Create account”. For a new account the name field (and, if asked for, the
|
|
3
|
+
“confirm password” field) grows open and the forgot-password link fades away; going back to
|
|
4
|
+
sign in folds them shut again. The password field has a show / hide eye, and on sign-up a
|
|
5
|
+
strength meter (passwordMeter) grows in under it. The submit label rolls to the new words.
|
|
6
|
+
Sending checks every field first: a field with a problem turns red, says why under it and the
|
|
7
|
+
card gives a small shake; the first one gets the focus. onSubmit may return a Promise: the
|
|
8
|
+
button shows a spinner until it settles, and a rejection shows its message (on a field too,
|
|
9
|
+
if the error names one). Nothing is sent anywhere by this file: call your own auth in onSubmit. */
|
|
10
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
11
|
+
import { icon } from '@gecko-ui-kit/icons';
|
|
12
|
+
import { esc } from '../index.js';
|
|
13
|
+
import { passwordMeter, passwordToggle } from './password.js';
|
|
14
|
+
|
|
15
|
+
const { g, RM, $$ } = M;
|
|
16
|
+
const WORDS = {
|
|
17
|
+
signIn: 'Sign in', signUp: 'Create account', modes: 'Sign in or create an account',
|
|
18
|
+
name: 'Full name', email: 'Email', password: 'Password', confirm: 'Confirm password',
|
|
19
|
+
emailPh: 'you@example.com', newPasswordPh: 'At least 8 characters',
|
|
20
|
+
remember: 'Remember me', forgot: 'Forgot password?',
|
|
21
|
+
nameMissing: 'Add your name.', emailMissing: 'Enter your email.', emailBad: 'Enter a full email address, like name@example.com.',
|
|
22
|
+
passwordMissing: 'Enter your password.', passwordWeak: 'Choose a stronger password (see the list below).', passwordShort: (n) => `Use ${n} or more characters.`,
|
|
23
|
+
confirmMissing: 'Type the password again.', confirmBad: 'The two passwords don’t match.',
|
|
24
|
+
invalid: (n) => (n === 1 ? 'Check the highlighted field.' : `Check the ${n} highlighted fields.`),
|
|
25
|
+
busyIn: 'Signing in…', busyUp: 'Creating your account…',
|
|
26
|
+
doneIn: 'Signed in. Welcome back!', doneUp: 'Account created. Welcome!',
|
|
27
|
+
failed: 'Something went wrong. Please try again.',
|
|
28
|
+
show: 'Show password', hide: 'Hide password',
|
|
29
|
+
meter: null, // words for the strength meter (see passwordMeter)
|
|
30
|
+
};
|
|
31
|
+
const ORDER = ['name', 'email', 'password', 'confirm'];
|
|
32
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
|
|
33
|
+
let uid = 0;
|
|
34
|
+
|
|
35
|
+
const txt = (w, ...a) => (typeof w === 'function' ? w(...a) : w);
|
|
36
|
+
const describe = (el, id, add) => {
|
|
37
|
+
const now = (el.getAttribute('aria-describedby') || '').split(/\s+/).filter((x) => x && x !== id);
|
|
38
|
+
const next = add ? [id, ...now] : now;
|
|
39
|
+
next.length ? el.setAttribute('aria-describedby', next.join(' ')) : el.removeAttribute('aria-describedby');
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* const f = authForm(el, {
|
|
44
|
+
* mode: 'in' | 'up', // start on sign in or create account
|
|
45
|
+
* fields: { name: true, confirm: false, remember: true, forgot: true }, // forgot: true (button → onForgot) or a URL
|
|
46
|
+
* meter: true, // strength meter on sign-up; or passwordMeter options { rules, min, minScore, score, list, lock }
|
|
47
|
+
* min: 8, // shortest password on sign-up when meter is false
|
|
48
|
+
* values: { name, email, remember }, // fill in to start with
|
|
49
|
+
* validate: (values, mode) => ({ email: 'message' }), // your own extra checks
|
|
50
|
+
* words: { signIn, signUp, modes, name, email, password, confirm, emailPh, newPasswordPh, remember, forgot,
|
|
51
|
+
* nameMissing, emailMissing, emailBad, passwordMissing, passwordWeak, passwordShort(n), confirmMissing, confirmBad,
|
|
52
|
+
* invalid(n), busyIn, busyUp, doneIn, doneUp, failed, show, hide, meter: { …passwordMeter words } },
|
|
53
|
+
* onSubmit: async (values, mode, f) => …, // reject (or throw) to show an error; Error.field = 'email' marks that field
|
|
54
|
+
* onModeChange(mode, f), onForgot(email, f),
|
|
55
|
+
* })
|
|
56
|
+
* f.mode · f.setMode('in' | 'up') · f.values · f.busy · f.setError(field, message) · f.reset() · f.destroy()
|
|
57
|
+
*/
|
|
58
|
+
export const authForm = (el, opts = {}) => {
|
|
59
|
+
el = $$(el)[0];
|
|
60
|
+
if (!el) return null;
|
|
61
|
+
const o = { mode: 'in', fields: null, meter: true, min: 8, values: null, validate: null, words: null, onSubmit: null, onModeChange: null, onForgot: null, ...opts };
|
|
62
|
+
const F = { name: true, confirm: false, remember: true, forgot: true, ...o.fields };
|
|
63
|
+
const W = { ...WORDS, ...o.words };
|
|
64
|
+
const V = { name: '', email: '', remember: true, ...o.values };
|
|
65
|
+
const ac = new AbortController(), on = (t, ev, fn, x) => t.addEventListener(ev, fn, { signal: ac.signal, ...x });
|
|
66
|
+
const id = `auth${++uid}`;
|
|
67
|
+
|
|
68
|
+
const field = (k, type, auto, extra = '') => `<div class="field auth__field" data-f="${k}"><label for="${id}-${k}">${esc(W[k])}</label>`
|
|
69
|
+
+ `<input class="input" id="${id}-${k}" name="${k}" type="${type}" autocomplete="${auto}"${extra}>`
|
|
70
|
+
+ `<small class="field__hint auth__err" id="${id}-${k}-err" hidden></small></div>`;
|
|
71
|
+
const grow = (inner, cls = '') => `<div class="auth__grow${cls}" data-only="up" hidden>${inner}</div>`;
|
|
72
|
+
const forgot = !F.forgot ? '' : typeof F.forgot === 'string'
|
|
73
|
+
? `<a class="auth__forgot" href="${esc(F.forgot)}">${esc(W.forgot)}</a>`
|
|
74
|
+
: `<button type="button" class="auth__forgot">${esc(W.forgot)}</button>`;
|
|
75
|
+
|
|
76
|
+
el.innerHTML = `<form class="auth" novalidate data-mode="in" aria-label="${esc(W.signIn)}">`
|
|
77
|
+
+ `<div class="seg auth__switch" role="group" aria-label="${esc(W.modes)}"><span class="seg__thumb"></span>`
|
|
78
|
+
+ `<button type="button" data-v="in" aria-pressed="true">${esc(W.signIn)}</button><button type="button" data-v="up" aria-pressed="false">${esc(W.signUp)}</button></div>`
|
|
79
|
+
+ (F.name ? grow(field('name', 'text', 'name', ` value="${esc(V.name)}"`)) : '')
|
|
80
|
+
+ field('email', 'email', 'email', ` value="${esc(V.email)}" inputmode="email" autocapitalize="off" spellcheck="false" placeholder="${esc(W.emailPh)}"`)
|
|
81
|
+
+ field('password', 'password', 'current-password')
|
|
82
|
+
+ (o.meter ? grow('', ' auth__meter') : '')
|
|
83
|
+
+ (F.confirm ? grow(field('confirm', 'password', 'new-password')) : '')
|
|
84
|
+
+ (F.remember || forgot ? `<div class="auth__row">${F.remember ? `<label class="auth__check"><button type="button" class="check" role="checkbox" aria-checked="${!!V.remember}" aria-labelledby="${id}-rem">${icon('check')}</button><span id="${id}-rem">${esc(W.remember)}</span></label>` : '<span></span>'}${forgot}</div>` : '')
|
|
85
|
+
+ `<button type="submit" class="btn btn--primary btn--lg btn--block auth__go"><span class="auth__roll"><span>${esc(W.signIn)}</span></span><span class="auth__spin" aria-hidden="true"></span></button>`
|
|
86
|
+
+ '<p class="auth__msg" role="status" aria-live="polite"></p></form>';
|
|
87
|
+
|
|
88
|
+
const form = el.firstElementChild;
|
|
89
|
+
const q = (s) => form.querySelector(s);
|
|
90
|
+
const inputs = Object.fromEntries(ORDER.map((k) => [k, q(`input[name="${k}"]`)]).filter(([, i]) => i));
|
|
91
|
+
const thumb = q('.seg__thumb'), modeBtns = $$('.auth__switch button', form), grows = $$('.auth__grow', form), meterWrap = q('.auth__meter');
|
|
92
|
+
const go = q('.auth__go'), roll = q('.auth__roll'), msg = q('.auth__msg'), check = q('.auth__check .check'), forgotEl = q('.auth__forgot');
|
|
93
|
+
const eye = passwordToggle(inputs.password, { words: { show: W.show, hide: W.hide } });
|
|
94
|
+
const stopTrack = M.trackIndicator(thumb, () => q('.auth__switch [aria-pressed="true"]'), { radius: 99 });
|
|
95
|
+
let mode = '', meter = null, busy = false, first = true;
|
|
96
|
+
const live = (k) => !!inputs[k] && (k === 'email' || k === 'password' || mode === 'up');
|
|
97
|
+
|
|
98
|
+
/* ---------- small motions ---------- */
|
|
99
|
+
const say = (t, tone = '') => {
|
|
100
|
+
msg.textContent = t;
|
|
101
|
+
msg.className = `auth__msg${tone ? ` is-${tone}` : ''}`;
|
|
102
|
+
g.killTweensOf(msg);
|
|
103
|
+
if (t && !RM()) g.fromTo(msg, { y: 6, opacity: 0 }, { y: 0, opacity: 1, duration: 0.35, ease: 'power2.out', clearProps: 'transform,opacity' });
|
|
104
|
+
};
|
|
105
|
+
const fold = (w, show, animate, done) => {
|
|
106
|
+
g.killTweensOf([w, w.firstElementChild]);
|
|
107
|
+
w.inert = !show;
|
|
108
|
+
if (!animate || RM()) { w.hidden = !show; g.set(w, { clearProps: 'height,opacity,overflow' }); if (!show) done?.(); return; }
|
|
109
|
+
if (show) {
|
|
110
|
+
const h0 = w.hidden ? 0 : w.offsetHeight;
|
|
111
|
+
w.hidden = false;
|
|
112
|
+
g.fromTo(w, { height: h0, opacity: h0 ? +g.getProperty(w, 'opacity') : 0, overflow: 'hidden' }, { height: 'auto', opacity: 1, duration: 0.46, ease: 'power3.out', clearProps: 'height,opacity,overflow' });
|
|
113
|
+
if (w.firstElementChild) g.fromTo(w.firstElementChild, { y: -10 }, { y: 0, duration: 0.55, ease: 'power3.out', clearProps: 'transform' });
|
|
114
|
+
} else if (!w.hidden) {
|
|
115
|
+
g.to(w, { height: 0, opacity: 0, overflow: 'hidden', duration: 0.34, ease: 'power2.inOut',
|
|
116
|
+
onComplete: () => { w.hidden = true; g.set(w, { clearProps: 'height,opacity,overflow' }); done?.(); } });
|
|
117
|
+
} else done?.();
|
|
118
|
+
};
|
|
119
|
+
const rollLabel = (text, up) => {
|
|
120
|
+
const old = [...roll.children], neu = document.createElement('span');
|
|
121
|
+
neu.textContent = text; roll.append(neu);
|
|
122
|
+
old.forEach((s) => s.setAttribute('aria-hidden', 'true'));
|
|
123
|
+
g.killTweensOf(old);
|
|
124
|
+
if (RM() || first) { old.forEach((s) => s.remove()); return; }
|
|
125
|
+
g.to(old, { yPercent: up ? -115 : 115, opacity: 0, duration: 0.26, ease: 'power2.in', onComplete: () => old.forEach((s) => s.remove()) });
|
|
126
|
+
g.fromTo(neu, { yPercent: up ? 115 : -115, opacity: 0 }, { yPercent: 0, opacity: 1, duration: 0.5, delay: 0.05, ease: 'back.out(1.7)', clearProps: 'transform,opacity' });
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/* ---------- errors ---------- */
|
|
130
|
+
const setErr = (k, text) => {
|
|
131
|
+
const inp = inputs[k]; if (!inp) return;
|
|
132
|
+
const f = inp.closest('.field'), e = f.querySelector('.auth__err'), was = !e.hidden;
|
|
133
|
+
if (text) {
|
|
134
|
+
inp.setAttribute('aria-invalid', 'true'); f.classList.add('is-bad');
|
|
135
|
+
e.textContent = text; e.hidden = false; describe(inp, e.id, true);
|
|
136
|
+
if (!was && !RM()) g.fromTo(e, { opacity: 0, y: -4 }, { opacity: 1, y: 0, duration: 0.3, ease: 'power2.out', clearProps: 'transform,opacity' });
|
|
137
|
+
} else {
|
|
138
|
+
inp.removeAttribute('aria-invalid'); f.classList.remove('is-bad');
|
|
139
|
+
e.textContent = ''; e.hidden = true; describe(inp, e.id, false);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
const clearErrs = () => ORDER.forEach((k) => setErr(k, ''));
|
|
143
|
+
const values = () => {
|
|
144
|
+
const v = {};
|
|
145
|
+
if (live('name')) v.name = inputs.name.value.trim();
|
|
146
|
+
v.email = inputs.email.value.trim();
|
|
147
|
+
v.password = inputs.password.value;
|
|
148
|
+
if (check) v.remember = check.getAttribute('aria-checked') === 'true';
|
|
149
|
+
return v;
|
|
150
|
+
};
|
|
151
|
+
const findErrs = () => {
|
|
152
|
+
const v = values(), errs = {};
|
|
153
|
+
if (live('name') && !v.name) errs.name = W.nameMissing;
|
|
154
|
+
if (!v.email) errs.email = W.emailMissing;
|
|
155
|
+
else if (!EMAIL.test(v.email)) errs.email = W.emailBad;
|
|
156
|
+
if (!v.password) errs.password = W.passwordMissing;
|
|
157
|
+
else if (mode === 'up') {
|
|
158
|
+
if (meter && !meter.valid) errs.password = W.passwordWeak;
|
|
159
|
+
else if (!meter && [...v.password].length < o.min) errs.password = txt(W.passwordShort, o.min);
|
|
160
|
+
}
|
|
161
|
+
if (live('confirm')) {
|
|
162
|
+
const c = inputs.confirm.value;
|
|
163
|
+
if (!c) errs.confirm = W.confirmMissing;
|
|
164
|
+
else if (c !== v.password) errs.confirm = W.confirmBad;
|
|
165
|
+
}
|
|
166
|
+
const extra = o.validate?.(v, mode) || {};
|
|
167
|
+
Object.entries(extra).forEach(([k, m]) => { if (m && !errs[k] && live(k)) errs[k] = m; });
|
|
168
|
+
return errs;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/* ---------- mode ---------- */
|
|
172
|
+
const setMode = (m, { animate = true } = {}) => {
|
|
173
|
+
m = m === 'up' ? 'up' : 'in';
|
|
174
|
+
if (busy || m === mode) return false;
|
|
175
|
+
const up = m === 'up';
|
|
176
|
+
mode = m; form.dataset.mode = m;
|
|
177
|
+
form.setAttribute('aria-label', up ? W.signUp : W.signIn);
|
|
178
|
+
modeBtns.forEach((b) => b.setAttribute('aria-pressed', b.dataset.v === m));
|
|
179
|
+
M.indicator(thumb, q(`.auth__switch [data-v="${m}"]`), { radius: 99, animate: animate && !first });
|
|
180
|
+
const pw = inputs.password;
|
|
181
|
+
pw.autocomplete = up ? 'new-password' : 'current-password';
|
|
182
|
+
pw.placeholder = up ? W.newPasswordPh : '';
|
|
183
|
+
clearErrs(); say('');
|
|
184
|
+
if (up && o.meter && !meter) {
|
|
185
|
+
const mo = typeof o.meter === 'object' ? o.meter : {};
|
|
186
|
+
meter = passwordMeter(pw, { ...mo, reveal: false, panel: meterWrap, words: { ...mo.words, ...W.meter } });
|
|
187
|
+
}
|
|
188
|
+
const anim = animate && !first;
|
|
189
|
+
grows.forEach((w) => fold(w, up, anim, w === meterWrap ? () => { meter?.destroy(); meter = null; } : null));
|
|
190
|
+
if (forgotEl) {
|
|
191
|
+
forgotEl.inert = up; g.killTweensOf(forgotEl);
|
|
192
|
+
if (!anim || RM()) g.set(forgotEl, { opacity: up ? 0 : 1, x: 0 });
|
|
193
|
+
else g.to(forgotEl, { opacity: up ? 0 : 1, x: up ? 8 : 0, duration: 0.3, ease: 'power2.out' });
|
|
194
|
+
}
|
|
195
|
+
rollLabel(up ? W.signUp : W.signIn, up);
|
|
196
|
+
if (!first) o.onModeChange?.(m, api);
|
|
197
|
+
return true;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/* ---------- busy + submit ---------- */
|
|
201
|
+
const setBusy = (v) => {
|
|
202
|
+
busy = v;
|
|
203
|
+
form.classList.toggle('is-busy', v); form.classList.toggle('auth--still', RM());
|
|
204
|
+
v ? form.setAttribute('aria-busy', 'true') : form.removeAttribute('aria-busy');
|
|
205
|
+
v ? go.setAttribute('aria-disabled', 'true') : go.removeAttribute('aria-disabled');
|
|
206
|
+
Object.values(inputs).forEach((i) => { i.readOnly = v; });
|
|
207
|
+
modeBtns.forEach((b) => { b.disabled = v; });
|
|
208
|
+
if (check) v ? check.setAttribute('aria-disabled', 'true') : check.removeAttribute('aria-disabled');
|
|
209
|
+
};
|
|
210
|
+
on(form, 'submit', async (e) => {
|
|
211
|
+
e.preventDefault();
|
|
212
|
+
if (busy) return;
|
|
213
|
+
const errs = findErrs(), bad = ORDER.filter((k) => errs[k]);
|
|
214
|
+
ORDER.forEach((k) => live(k) && setErr(k, errs[k] || ''));
|
|
215
|
+
if (bad.length) {
|
|
216
|
+
say(txt(W.invalid, bad.length), 'bad');
|
|
217
|
+
M.shake(form);
|
|
218
|
+
inputs[bad[0]].focus();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const m = mode, v = values();
|
|
222
|
+
if (!o.onSubmit) { say(m === 'up' ? W.doneUp : W.doneIn, 'good'); return; }
|
|
223
|
+
setBusy(true); say(m === 'up' ? W.busyUp : W.busyIn);
|
|
224
|
+
try {
|
|
225
|
+
const r = await o.onSubmit(v, m, api);
|
|
226
|
+
if (ac.signal.aborted) return;
|
|
227
|
+
setBusy(false);
|
|
228
|
+
if (r === false) throw new Error('');
|
|
229
|
+
say(typeof r === 'string' && r ? r : m === 'up' ? W.doneUp : W.doneIn, 'good');
|
|
230
|
+
} catch (err) {
|
|
231
|
+
if (ac.signal.aborted) return;
|
|
232
|
+
setBusy(false);
|
|
233
|
+
const text = (typeof err === 'string' ? err : err?.message) || W.failed;
|
|
234
|
+
const k = err?.field;
|
|
235
|
+
say(text, 'bad');
|
|
236
|
+
M.shake(form);
|
|
237
|
+
if (k && live(k)) { setErr(k, text); inputs[k].focus(); }
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
/* ---------- wiring ---------- */
|
|
242
|
+
on(q('.auth__switch'), 'click', (e) => { const b = e.target.closest('button[data-v]'); if (b && setMode(b.dataset.v)) M.pop(b); });
|
|
243
|
+
on(q('.auth__switch'), 'keydown', (e) => {
|
|
244
|
+
if (!['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(e.key)) return;
|
|
245
|
+
e.preventDefault();
|
|
246
|
+
const dir = document.dir === 'rtl' || getComputedStyle(form).direction === 'rtl' ? -1 : 1;
|
|
247
|
+
const next = e.key === 'Home' ? 'in' : e.key === 'End' ? 'up' : (e.key === 'ArrowRight') === (dir === 1) ? 'up' : 'in';
|
|
248
|
+
setMode(next); q(`.auth__switch [data-v="${next}"]`).focus();
|
|
249
|
+
});
|
|
250
|
+
on(form, 'input', (e) => { // a red field is checked again on every key and clears once fixed
|
|
251
|
+
const k = e.target.name;
|
|
252
|
+
if (!inputs[k]) return;
|
|
253
|
+
const bad = ORDER.filter((x) => inputs[x]?.getAttribute('aria-invalid') === 'true' && (x === k || (k === 'password' && x === 'confirm')));
|
|
254
|
+
if (!bad.length) return;
|
|
255
|
+
const errs = findErrs();
|
|
256
|
+
bad.forEach((x) => setErr(x, errs[x] || ''));
|
|
257
|
+
if (!ORDER.some((x) => inputs[x]?.getAttribute('aria-invalid') === 'true') && msg.classList.contains('is-bad')) say('');
|
|
258
|
+
});
|
|
259
|
+
if (check) on(check, 'click', () => { if (busy) return; check.setAttribute('aria-checked', check.getAttribute('aria-checked') !== 'true'); M.pop(check); });
|
|
260
|
+
if (forgotEl && forgotEl.tagName === 'BUTTON') on(forgotEl, 'click', () => o.onForgot?.(inputs.email.value.trim(), api));
|
|
261
|
+
|
|
262
|
+
const api = {
|
|
263
|
+
root: form,
|
|
264
|
+
get mode() { return mode; },
|
|
265
|
+
setMode: (m) => setMode(m),
|
|
266
|
+
get values() { return values(); },
|
|
267
|
+
get busy() { return busy; },
|
|
268
|
+
setError: (k, text) => { if (live(k)) setErr(k, text || ''); },
|
|
269
|
+
reset() {
|
|
270
|
+
if (busy) return;
|
|
271
|
+
form.reset(); clearErrs(); say('');
|
|
272
|
+
if (check) check.setAttribute('aria-checked', !!V.remember);
|
|
273
|
+
eye?.show(false); meter?.check();
|
|
274
|
+
},
|
|
275
|
+
destroy() {
|
|
276
|
+
ac.abort(); stopTrack();
|
|
277
|
+
meter?.destroy(); eye?.destroy();
|
|
278
|
+
g.killTweensOf([form, ...form.querySelectorAll('*')]); g.killTweensOf(thumb.__st ?? {});
|
|
279
|
+
el.innerHTML = '';
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
setMode(o.mode, { animate: false });
|
|
283
|
+
first = false;
|
|
284
|
+
return api;
|
|
285
|
+
};
|