@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/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
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/* Before / after: two pictures (or any two pieces of content) laid on top of each other, with a
|
|
2
|
+
divider you drag sideways to wipe from one to the other — a renovation, a photo edit, a redesign,
|
|
3
|
+
light and dark. Drag anywhere on it with a mouse, finger or pen (an up-and-down swipe still
|
|
4
|
+
scrolls the page), click to jump there, or Tab to the handle and use ← → (Shift for bigger steps),
|
|
5
|
+
Page Up / Page Down, Home and End: the handle is an ARIA slider. With hover: true the divider
|
|
6
|
+
follows a mouse without pressing. Clicks and keys glide the divider, drags follow the finger, the
|
|
7
|
+
handle squeezes a little while held, and the “Before” / “After” tags step aside when the divider
|
|
8
|
+
comes close. With hint: true it sweeps once to each side when it first scrolls into view, so
|
|
9
|
+
people see it can move. With reduced motion the divider jumps and there is no sweep. */
|
|
10
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
11
|
+
import { icon } from '@gecko-ui-kit/icons';
|
|
12
|
+
import { esc } from '../index.js';
|
|
13
|
+
|
|
14
|
+
const { g, RM, $$, fine } = M;
|
|
15
|
+
const WORDS = {
|
|
16
|
+
before: 'Before', after: 'After',
|
|
17
|
+
label: 'Before and after divider', // the slider's name
|
|
18
|
+
value: (n) => `${n}% before`, // read out as it moves
|
|
19
|
+
};
|
|
20
|
+
const clamp = (v) => Math.max(0, Math.min(100, v));
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* const ba = beforeAfter(el, {
|
|
24
|
+
* before: '<img src="old.jpg" alt="The shop before">', // HTML string or an element
|
|
25
|
+
* after: '<img src="new.jpg" alt="The shop after">', // (leave both out to use el's first two children)
|
|
26
|
+
* start: 50, // where the divider starts, 0–100 (% of the before side shown)
|
|
27
|
+
* hover: false, // the divider follows the mouse without pressing (mouse / trackpad only)
|
|
28
|
+
* hint: false, // sweep once when it first scrolls into view
|
|
29
|
+
* labels: true, // the “Before” / “After” tags
|
|
30
|
+
* ratio: '4/3', // aspect ratio of the frame ('' = as tall as the content)
|
|
31
|
+
* step: 5, // arrow-key step in %, Shift × 4
|
|
32
|
+
* words: { before, after, label, value },
|
|
33
|
+
* onChange: (value, ba) => {}, // 0–100, while it moves
|
|
34
|
+
* })
|
|
35
|
+
* ba.set(70) · ba.set(70, false) (jump) · ba.value · ba.root · ba.destroy()
|
|
36
|
+
*/
|
|
37
|
+
export const beforeAfter = (el, opts = {}) => {
|
|
38
|
+
el = $$(el)[0];
|
|
39
|
+
if (!el) return null;
|
|
40
|
+
const o = { before: null, after: null, start: 50, hover: false, hint: false, labels: true, ratio: '4/3', step: 5, words: null, onChange: null, ...opts };
|
|
41
|
+
const W = { ...WORDS, ...o.words };
|
|
42
|
+
const ac = new AbortController(), on = (t, ev, fn, x) => t.addEventListener(ev, fn, { signal: ac.signal, ...x });
|
|
43
|
+
const own = [...el.children];
|
|
44
|
+
const take = (v, i) => (v == null ? own[i] || null : v);
|
|
45
|
+
const parts = [take(o.before, 0), take(o.after, 1)];
|
|
46
|
+
|
|
47
|
+
el.innerHTML = `<div class="ba${o.ratio ? '' : ' ba--auto'}"${o.ratio ? ` style="aspect-ratio:${esc(String(o.ratio).replace(/[^\d./ ]/g, ''))}"` : ''}>`
|
|
48
|
+
+ '<div class="ba__pane ba__after"></div><div class="ba__pane ba__before"></div>'
|
|
49
|
+
+ (o.labels ? `<span class="ba__tag ba__tag--before" aria-hidden="true">${esc(W.before)}</span><span class="ba__tag ba__tag--after" aria-hidden="true">${esc(W.after)}</span>` : '')
|
|
50
|
+
+ `<div class="ba__handle" role="slider" tabindex="0" aria-label="${esc(W.label)}" aria-orientation="horizontal" aria-valuemin="0" aria-valuemax="100">`
|
|
51
|
+
+ `<span class="ba__knob">${icon('chevL')}${icon('chevR')}</span></div></div>`;
|
|
52
|
+
const root = el.firstElementChild, handle = root.querySelector('.ba__handle'), knob = root.querySelector('.ba__knob');
|
|
53
|
+
const tagB = root.querySelector('.ba__tag--before'), tagA = root.querySelector('.ba__tag--after');
|
|
54
|
+
const fillPane = (pane, v) => { if (v == null) return; if (typeof v === 'string') pane.innerHTML = v; else pane.append(v); };
|
|
55
|
+
fillPane(root.querySelector('.ba__before'), parts[0]);
|
|
56
|
+
fillPane(root.querySelector('.ba__after'), parts[1]);
|
|
57
|
+
|
|
58
|
+
/* ---------- the value ---------- */
|
|
59
|
+
const st = { v: clamp(+o.start || 0) };
|
|
60
|
+
let shown = -1, ready = false;
|
|
61
|
+
const paint = () => {
|
|
62
|
+
const v = clamp(st.v);
|
|
63
|
+
root.style.setProperty('--ba-pos', `${v}%`);
|
|
64
|
+
const n = Math.round(v);
|
|
65
|
+
if (n !== shown) {
|
|
66
|
+
shown = n;
|
|
67
|
+
handle.setAttribute('aria-valuenow', String(n));
|
|
68
|
+
handle.setAttribute('aria-valuetext', W.value(n));
|
|
69
|
+
if (ready) o.onChange?.(n, api);
|
|
70
|
+
}
|
|
71
|
+
if (tagB) tagB.classList.toggle('is-away', v < 22);
|
|
72
|
+
if (tagA) tagA.classList.toggle('is-away', v > 78);
|
|
73
|
+
};
|
|
74
|
+
const glide = (to, duration = 0.45) => {
|
|
75
|
+
g.killTweensOf(st);
|
|
76
|
+
if (RM() || !duration) { st.v = clamp(to); paint(); return; }
|
|
77
|
+
g.to(st, { v: clamp(to), duration, ease: 'power3.out', onUpdate: paint });
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/* ---------- pointer ---------- */
|
|
81
|
+
const at = (e) => { const r = root.getBoundingClientRect(); return r.width ? ((e.clientX - r.left) / r.width) * 100 : st.v; };
|
|
82
|
+
let drag = null;
|
|
83
|
+
const stopHint = () => { hintTl?.kill(); hintTl = null; };
|
|
84
|
+
on(root, 'pointerdown', (e) => {
|
|
85
|
+
if (e.button !== 0) return;
|
|
86
|
+
stopHint();
|
|
87
|
+
drag = e.pointerId;
|
|
88
|
+
try { root.setPointerCapture(e.pointerId); } catch {}
|
|
89
|
+
root.classList.add('is-drag');
|
|
90
|
+
if (!RM()) g.to(knob, { scale: 0.9, duration: 0.2, ease: 'power2.out' });
|
|
91
|
+
glide(at(e), 0.25);
|
|
92
|
+
handle.focus({ preventScroll: true });
|
|
93
|
+
e.preventDefault();
|
|
94
|
+
});
|
|
95
|
+
on(root, 'pointermove', (e) => {
|
|
96
|
+
if (drag === e.pointerId) { g.killTweensOf(st); st.v = clamp(at(e)); paint(); return; }
|
|
97
|
+
if (o.hover && e.pointerType === 'mouse' && fine.matches && drag == null) { stopHint(); glide(at(e), 0.35); }
|
|
98
|
+
});
|
|
99
|
+
const up = (e) => {
|
|
100
|
+
if (drag !== e.pointerId) return;
|
|
101
|
+
drag = null;
|
|
102
|
+
root.classList.remove('is-drag');
|
|
103
|
+
if (!RM()) g.to(knob, { scale: 1, duration: 0.5, ease: 'back.out(3)' }); else g.set(knob, { scale: 1 });
|
|
104
|
+
};
|
|
105
|
+
on(root, 'pointerup', up);
|
|
106
|
+
on(root, 'pointercancel', up);
|
|
107
|
+
|
|
108
|
+
/* ---------- keyboard ---------- */
|
|
109
|
+
on(handle, 'keydown', (e) => {
|
|
110
|
+
const s = (+o.step || 5) * (e.shiftKey ? 4 : 1);
|
|
111
|
+
const to = { ArrowLeft: st.v - s, ArrowDown: st.v - s, ArrowRight: st.v + s, ArrowUp: st.v + s, PageDown: st.v - 20, PageUp: st.v + 20, Home: 0, End: 100 }[e.key];
|
|
112
|
+
if (to === undefined) return;
|
|
113
|
+
e.preventDefault(); stopHint();
|
|
114
|
+
glide(Math.round(to), 0.3);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
/* ---------- the hint sweep ---------- */
|
|
118
|
+
let hintTl = null, io = null;
|
|
119
|
+
const hint = () => {
|
|
120
|
+
if (RM()) return;
|
|
121
|
+
const from = st.v;
|
|
122
|
+
hintTl = g.timeline({ delay: 0.2, onUpdate: paint })
|
|
123
|
+
.to(st, { v: Math.max(12, from - 22), duration: 0.7, ease: 'power2.inOut' })
|
|
124
|
+
.to(st, { v: Math.min(88, from + 22), duration: 0.9, ease: 'power2.inOut' })
|
|
125
|
+
.to(st, { v: from, duration: 0.7, ease: 'power3.out' });
|
|
126
|
+
};
|
|
127
|
+
if (o.hint && typeof IntersectionObserver === 'function') {
|
|
128
|
+
io = new IntersectionObserver((ents) => { if (ents.some((x) => x.isIntersecting)) { io.disconnect(); io = null; hint(); } }, { threshold: 0.5 });
|
|
129
|
+
io.observe(root);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const api = {
|
|
133
|
+
root,
|
|
134
|
+
get value() { return Math.round(clamp(st.v)); },
|
|
135
|
+
set(v, animate = true) { stopHint(); glide(+v || 0, animate ? 0.45 : 0); return api; },
|
|
136
|
+
destroy() { ac.abort(); io?.disconnect(); stopHint(); g.killTweensOf([st, knob]); el.innerHTML = ''; },
|
|
137
|
+
};
|
|
138
|
+
paint();
|
|
139
|
+
ready = true;
|
|
140
|
+
return api;
|
|
141
|
+
};
|
package/src/bell.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/* Notification bell: an on / off switch for notifications that shows its state with the bell itself.
|
|
2
|
+
Turning it on fills the circle with colour and rings the bell: it swings from the top a few times,
|
|
3
|
+
each swing smaller, the clapper following a beat later, and two small sound waves flash beside it.
|
|
4
|
+
Turning it off draws a slash across the bell from corner to corner (a thin gap in the background colour
|
|
5
|
+
cuts the bell where the slash crosses it). The small line under the label rolls to "On" / "Off".
|
|
6
|
+
It is a real <button role="switch"> with aria-checked, named by its label; the state line is for the eyes
|
|
7
|
+
(screen readers already hear "on" / "off"). If your onChange returns a promise that fails, it switches
|
|
8
|
+
back and shakes. bellBtn() writes the HTML; bellSwitch() wires one up; bindAll(root) wires every
|
|
9
|
+
.nbell inside root for you. */
|
|
10
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
11
|
+
import { esc } from '../index.js';
|
|
12
|
+
|
|
13
|
+
const { g, RM, $$ } = M;
|
|
14
|
+
const WORDS = { label: 'Notifications', on: 'On', off: 'Off' };
|
|
15
|
+
const TONES = { accent: 'var(--accent)', good: 'var(--good)', warn: 'var(--warn)', bad: 'var(--bad)' };
|
|
16
|
+
const HUES = ['indigo', 'teal', 'rose', 'amber', 'violet', 'green', 'slate', 'sky'];
|
|
17
|
+
const toColor = (t) => (!t ? '' : TONES[t] || (HUES.includes(t) ? `var(--hue-${t})` : String(t)));
|
|
18
|
+
let uid = 0;
|
|
19
|
+
|
|
20
|
+
const SVG = (on) => '<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">'
|
|
21
|
+
+ '<g class="nbell__waves"><path d="M3.6 6.2a8.6 8.6 0 0 0-1 4.3"/><path d="M20.4 6.2a8.6 8.6 0 0 1 1 4.3"/></g>'
|
|
22
|
+
+ '<g class="nbell__body"><path d="M6 9.5a6 6 0 1 1 12 0c0 5.6 2.4 7.2 2.4 7.2H3.6S6 15.1 6 9.5"/>'
|
|
23
|
+
+ '<path class="nbell__clap" d="M10 20.2a2.2 2.2 0 0 0 4 0"/></g>'
|
|
24
|
+
+ `<path class="nbell__halo" d="M4 3.5 20 19.5" pathLength="1" stroke-dasharray="1 2" stroke-dashoffset="${on ? 1 : 0}"/>`
|
|
25
|
+
+ `<path class="nbell__slash" d="M4 3.5 20 19.5" pathLength="1" stroke-dasharray="1 2" stroke-dashoffset="${on ? 1 : 0}"/></svg>`;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* bellBtn({ on, label, iconOnly, tone, words, cls, attrs }) → HTML string
|
|
29
|
+
* label: the visible name (default words.label, "Notifications"). iconOnly: just the round bell (label
|
|
30
|
+
* becomes its aria-label). tone: 'accent' | 'good' | a palette name | any CSS colour.
|
|
31
|
+
*/
|
|
32
|
+
export const bellBtn = (opts = {}) => {
|
|
33
|
+
const W = { ...WORDS, ...opts.words }, on = !!opts.on, label = opts.label || W.label;
|
|
34
|
+
const id = `nbell-${++uid}`, c = toColor(opts.tone);
|
|
35
|
+
return `<button type="button" class="nbell${opts.iconOnly ? ' nbell--icon' : ''}${opts.cls ? ` ${esc(opts.cls)}` : ''}" role="switch" aria-checked="${on}"`
|
|
36
|
+
+ (opts.iconOnly ? ` aria-label="${esc(label)}"` : ` aria-labelledby="${id}"`)
|
|
37
|
+
+ ` data-on-text="${esc(W.on)}" data-off-text="${esc(W.off)}"${c ? ` style="--nb-c:${esc(c)}"` : ''}${opts.attrs ? ' ' + opts.attrs : ''}>`
|
|
38
|
+
+ `<span class="nbell__icon">${SVG(on)}</span>`
|
|
39
|
+
+ (opts.iconOnly ? '' : `<span class="nbell__text"><b class="nbell__label" id="${id}">${esc(label)}</b><small class="nbell__state" aria-hidden="true"><span>${esc(on ? W.on : W.off)}</span></small></span>`)
|
|
40
|
+
+ '</button>';
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* const bell = bellSwitch(el, {
|
|
45
|
+
* on: false, label: 'Notifications', iconOnly: false, tone: '',
|
|
46
|
+
* words: { label, on, off },
|
|
47
|
+
* onChange(on, bell), // return a promise: if it fails, the switch goes back
|
|
48
|
+
* })
|
|
49
|
+
* bell.on · bell.set(on, { silent, animate }) · bell.toggle() · bell.ring() · bell.destroy()
|
|
50
|
+
*
|
|
51
|
+
* el is a .nbell button from bellBtn() (its state is read from the markup) or an empty element to build one in.
|
|
52
|
+
* Calling it again on the same button returns the same handle with the new options merged in.
|
|
53
|
+
*/
|
|
54
|
+
export const bellSwitch = (el, opts = {}) => {
|
|
55
|
+
el = $$(el)[0];
|
|
56
|
+
if (!el) return null;
|
|
57
|
+
if (el.__nbell) { Object.assign(el.__nbell.opts, opts); return el.__nbell; }
|
|
58
|
+
let b = el, built = false;
|
|
59
|
+
if (!el.matches('.nbell')) { el.innerHTML = bellBtn(opts); b = el.firstElementChild; built = true; }
|
|
60
|
+
const o = { onChange: null, ...opts };
|
|
61
|
+
const W = { ...WORDS, ...o.words };
|
|
62
|
+
const onText = b.dataset.onText || W.on, offText = b.dataset.offText || W.off;
|
|
63
|
+
const $ = (s) => b.querySelector(s);
|
|
64
|
+
const body = $('.nbell__body'), clap = $('.nbell__clap'), waves = [...b.querySelectorAll('.nbell__waves path')];
|
|
65
|
+
const lines = [$('.nbell__halo'), $('.nbell__slash')], stateEl = $('.nbell__state > span');
|
|
66
|
+
const ac = new AbortController();
|
|
67
|
+
let on = b.getAttribute('aria-checked') === 'true';
|
|
68
|
+
|
|
69
|
+
/** Ring the bell: big swing first, each one smaller, the clapper a beat behind. */
|
|
70
|
+
const ring = () => {
|
|
71
|
+
g.killTweensOf([body, clap, ...waves]);
|
|
72
|
+
if (RM()) return g.timeline();
|
|
73
|
+
const swing = [0, 22, -18, 12, -8, 4, 0];
|
|
74
|
+
const tl = g.timeline();
|
|
75
|
+
tl.to(body, { keyframes: { rotation: swing }, svgOrigin: '12 3.5', duration: 0.9, ease: 'power1.out' }, 0)
|
|
76
|
+
.to(clap, { keyframes: { rotation: swing.map((r) => r * 0.6) }, svgOrigin: '12 3.5', duration: 0.9, ease: 'power1.out' }, 0.06);
|
|
77
|
+
waves.forEach((w, i) => tl.fromTo(w, { opacity: 0, x: i ? -2 : 2 }, { keyframes: { opacity: [0, 1, 1, 0] }, x: 0, duration: 0.55, ease: 'power1.out' }, 0.05 + i * 0.08));
|
|
78
|
+
return tl;
|
|
79
|
+
};
|
|
80
|
+
const draw = (animate) => {
|
|
81
|
+
b.setAttribute('aria-checked', String(on));
|
|
82
|
+
g.killTweensOf(lines);
|
|
83
|
+
const off = on ? 1 : 0;
|
|
84
|
+
if (!animate || RM()) lines.forEach((l) => l.setAttribute('stroke-dashoffset', String(off)));
|
|
85
|
+
else g.to(lines, { attr: { 'stroke-dashoffset': off }, duration: on ? 0.2 : 0.38, ease: on ? 'power2.in' : 'power2.out', delay: on ? 0 : 0.05 });
|
|
86
|
+
if (stateEl) {
|
|
87
|
+
const t = on ? onText : offText;
|
|
88
|
+
if (stateEl.textContent !== t) {
|
|
89
|
+
stateEl.textContent = t;
|
|
90
|
+
if (animate && !RM()) g.fromTo(stateEl, { yPercent: on ? 80 : -80, opacity: 0 }, { yPercent: 0, opacity: 1, duration: 0.3, ease: 'back.out(2)', clearProps: 'all' });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (on && animate) ring();
|
|
94
|
+
else if (!on) { g.killTweensOf([body, clap, ...waves]); g.set([body, clap], { rotation: 0, svgOrigin: '12 3.5' }); g.set(waves, { opacity: 0 }); }
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const api = {
|
|
98
|
+
el: b, opts: o,
|
|
99
|
+
get on() { return on; },
|
|
100
|
+
/** Switch it on or off. silent (default) skips onChange. */
|
|
101
|
+
set(next, { silent = true, animate = true } = {}) {
|
|
102
|
+
next = !!next;
|
|
103
|
+
if (next === on) return api;
|
|
104
|
+
on = next; draw(animate);
|
|
105
|
+
if (!silent) changed();
|
|
106
|
+
return api;
|
|
107
|
+
},
|
|
108
|
+
toggle() { return api.set(!on, { silent: false }); },
|
|
109
|
+
/** Ring it again (for example when a new notification comes in). */
|
|
110
|
+
ring,
|
|
111
|
+
destroy() {
|
|
112
|
+
ac.abort();
|
|
113
|
+
g.killTweensOf([body, clap, stateEl, ...waves, ...lines].filter(Boolean));
|
|
114
|
+
delete b.__nbell;
|
|
115
|
+
if (built) { delete el.__nbell; el.innerHTML = ''; }
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
const changed = () => {
|
|
119
|
+
const was = on;
|
|
120
|
+
let r;
|
|
121
|
+
try { r = api.opts.onChange?.(on, api); } catch { r = Promise.reject(); }
|
|
122
|
+
if (r && typeof r.then === 'function') {
|
|
123
|
+
Promise.resolve(r).catch(() => {
|
|
124
|
+
if (!b.isConnected || on !== was) return;
|
|
125
|
+
on = !was; draw(false); M.shake(b);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
b.addEventListener('click', () => api.toggle(), { signal: ac.signal });
|
|
130
|
+
b.__nbell = api;
|
|
131
|
+
if (built) el.__nbell = api;
|
|
132
|
+
return api;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/** Wires every .nbell switch inside root (bindAll does this for you). Returns the handles. */
|
|
136
|
+
export const bindBells = (root = document) =>
|
|
137
|
+
$$(root.matches?.('.nbell') ? [root] : $$('.nbell', root)).map((b) => b.__nbell || bellSwitch(b));
|