@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/activity.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/* Activity timeline: a vertical list of things that happened — who did what, and when — newest at
|
|
2
|
+
the top. Each event has a round mark on a thin line: an icon in a tinted circle, the person's
|
|
3
|
+
avatar, or a small dot. How it moves: when the list first shows (by default when it scrolls into
|
|
4
|
+
view) the line draws down from mark to mark and each event rises in just after the line reaches
|
|
5
|
+
it. add() puts a new event at the top: the others slide down to make room (measured before and
|
|
6
|
+
after, then glided with GSAP), the new one pops in and its line grows down to the next mark.
|
|
7
|
+
The newest event's mark wears a soft halo. Times read “5 min ago” and keep themselves up to date
|
|
8
|
+
(Intl.RelativeTimeFormat, any language), or write your own with format(). New events are read
|
|
9
|
+
out politely. With reduced motion everything is simply there, with no movement. */
|
|
10
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
11
|
+
import { icon } from '@gecko-ui-kit/icons';
|
|
12
|
+
import { esc, av } from '../index.js';
|
|
13
|
+
|
|
14
|
+
const { g, RM, $$ } = M;
|
|
15
|
+
const WORDS = {
|
|
16
|
+
label: 'Activity',
|
|
17
|
+
now: 'Just now',
|
|
18
|
+
added: (text) => `New: ${text}`, // read out after add()
|
|
19
|
+
more: (n) => `and ${n} more`, // when add() brings several at once
|
|
20
|
+
};
|
|
21
|
+
const TONES = ['accent', 'good', 'warn', 'bad', 'plain', 'indigo', 'teal', 'rose', 'amber', 'violet', 'green', 'slate', 'sky'];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* const feed = activity(el, {
|
|
25
|
+
* items: [{
|
|
26
|
+
* id, time, // Date | ms | ISO string (default: now)
|
|
27
|
+
* who: 'Pim' | { name, ini, tone }, // shown in bold before the text; an object also gives an avatar
|
|
28
|
+
* what: 'approved the order', // plain text
|
|
29
|
+
* title: 'Delivered', // bold text instead of the name (for status events with no person)
|
|
30
|
+
* detail: 'Order #1042 · ฿450', // optional second line
|
|
31
|
+
* icon: 'check', tone: 'good', // the mark: an icon in a tinted circle (tone: accent good warn bad plain indigo teal…)
|
|
32
|
+
* html, // trusted markup instead of who + what
|
|
33
|
+
* }],
|
|
34
|
+
* marks: 'auto' | 'icon' | 'avatar' | 'dot', // auto: icon if it has one, else the avatar, else a dot
|
|
35
|
+
* latest: true, // halo on the newest mark
|
|
36
|
+
* max: 0, // keep at most this many (0 = all); the oldest fade out at the bottom
|
|
37
|
+
* intro: 'view' | true | false, // draw the line + rise in: when it scrolls into view, at once, or not at all
|
|
38
|
+
* locale: undefined, // for the relative times ('th', 'en-GB'…)
|
|
39
|
+
* format: (date) => string, // your own time text instead
|
|
40
|
+
* refresh: 30, // seconds between time updates (0 = never)
|
|
41
|
+
* label: '', // name for screen readers (default words.label)
|
|
42
|
+
* words: { label, now, added, more },
|
|
43
|
+
* })
|
|
44
|
+
* feed.add(item | items) // at the top, with a smooth push-down
|
|
45
|
+
* feed.remove(id) · feed.set(items) · feed.play() (draw it again) · feed.items · feed.root · feed.destroy()
|
|
46
|
+
*/
|
|
47
|
+
export const activity = (el, opts = {}) => {
|
|
48
|
+
el = $$(el)[0];
|
|
49
|
+
if (!el) return null;
|
|
50
|
+
const o = { items: [], marks: 'auto', latest: true, max: 0, intro: 'view', locale: undefined, format: null, refresh: 30, label: '', words: null, ...opts };
|
|
51
|
+
const W = { ...WORDS, ...o.words };
|
|
52
|
+
const max = +o.max > 0 ? Math.round(+o.max) : Infinity;
|
|
53
|
+
let seq = 0, list = [], dead = false, io = null, timer = 0;
|
|
54
|
+
|
|
55
|
+
el.innerHTML = `<div class="act${o.latest ? ' act--latest' : ''}"><ol class="act__list" role="list" aria-label="${esc(o.label || W.label)}"></ol>`
|
|
56
|
+
+ '<span class="act__sr" role="status" aria-live="polite"></span></div>';
|
|
57
|
+
const root = el.firstElementChild, ol = root.querySelector('.act__list'), live = root.querySelector('.act__sr');
|
|
58
|
+
const say = (t) => { live.textContent = ''; if (t) requestAnimationFrame(() => { if (!dead) live.textContent = t; }); };
|
|
59
|
+
|
|
60
|
+
/* ---------- time ---------- */
|
|
61
|
+
const toMs = (t) => (t == null ? Date.now() : t instanceof Date ? t.getTime() : typeof t === 'number' ? t : (Date.parse(t) || Date.now()));
|
|
62
|
+
const when = (ms) => {
|
|
63
|
+
if (o.format) return String(o.format(new Date(ms)));
|
|
64
|
+
const s = Math.max(0, (Date.now() - ms) / 1000), L = o.locale;
|
|
65
|
+
const rtf = new Intl.RelativeTimeFormat(L, { numeric: 'auto', style: 'short' });
|
|
66
|
+
if (s < 45) return W.now;
|
|
67
|
+
if (s < 3600) return rtf.format(-Math.max(1, Math.round(s / 60)), 'minute');
|
|
68
|
+
if (s < 86400) return rtf.format(-Math.round(s / 3600), 'hour');
|
|
69
|
+
if (s < 86400 * 7) return rtf.format(-Math.round(s / 86400), 'day');
|
|
70
|
+
return new Intl.DateTimeFormat(L, { day: 'numeric', month: 'short' }).format(ms);
|
|
71
|
+
};
|
|
72
|
+
const stamp = (it, li) => {
|
|
73
|
+
const t = li.querySelector('.act__time'), d = new Date(it.ms);
|
|
74
|
+
t.textContent = when(it.ms); t.dateTime = d.toISOString();
|
|
75
|
+
t.title = new Intl.DateTimeFormat(o.locale, { dateStyle: 'medium', timeStyle: 'short' }).format(d);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/* ---------- rows ---------- */
|
|
79
|
+
const norm = (it) => ({ ...it, id: it.id != null ? String(it.id) : `a${++seq}`, ms: toMs(it.time) });
|
|
80
|
+
const pub = (it) => { const { ms, li, ...rest } = it; return rest; };
|
|
81
|
+
const tone = (t) => (TONES.includes(t) ? t : 'accent');
|
|
82
|
+
const whoName = (it) => (it.title != null ? String(it.title) : it.who == null ? '' : typeof it.who === 'string' ? it.who : it.who.name || '');
|
|
83
|
+
const plain = (it) => {
|
|
84
|
+
if (it.html != null) { const d = document.createElement('div'); d.innerHTML = it.html; return d.textContent.trim(); }
|
|
85
|
+
return `${whoName(it)} ${it.what ?? ''}`.trim();
|
|
86
|
+
};
|
|
87
|
+
const mark = (it) => {
|
|
88
|
+
const kind = o.marks === 'auto' ? (it.icon ? 'icon' : it.who && typeof it.who === 'object' ? 'avatar' : 'dot') : o.marks;
|
|
89
|
+
if (kind === 'avatar' && it.who && typeof it.who === 'object') return `<span class="act__mark act__mark--av">${av(it.who, 'sm')}</span>`;
|
|
90
|
+
if (kind === 'icon' || (kind === 'avatar' && it.icon)) return `<span class="act__mark act__tone--${tone(it.tone)}">${icon(it.icon || 'check')}</span>`;
|
|
91
|
+
return `<span class="act__mark act__mark--dot act__tone--${tone(it.tone)}"></span>`;
|
|
92
|
+
};
|
|
93
|
+
const make = (it) => {
|
|
94
|
+
const li = document.createElement('li');
|
|
95
|
+
li.className = 'act__row'; li.dataset.id = it.id;
|
|
96
|
+
const text = it.html != null ? it.html : `${whoName(it) ? `<b>${esc(whoName(it))}</b> ` : ''}${esc(it.what ?? '')}`;
|
|
97
|
+
li.innerHTML = `<span class="act__rail" aria-hidden="true">${mark(it)}<i class="act__seg"></i></span>`
|
|
98
|
+
+ `<div class="act__body"><div class="act__head"><p class="act__text">${text}</p><time class="act__time"></time></div>`
|
|
99
|
+
+ `${it.detail ? `<p class="act__detail">${esc(it.detail)}</p>` : ''}</div>`;
|
|
100
|
+
stamp(it, li);
|
|
101
|
+
it.li = li;
|
|
102
|
+
return li;
|
|
103
|
+
};
|
|
104
|
+
const markLatest = () => list.forEach((it, i) => it.li.classList.toggle('is-latest', i === 0));
|
|
105
|
+
const parts = (li) => ({ seg: li.querySelector('.act__seg'), mk: li.querySelector('.act__mark'), body: li.querySelector('.act__body') });
|
|
106
|
+
|
|
107
|
+
/* ---------- the intro: line draws down, events rise in after it ---------- */
|
|
108
|
+
let introTl = null;
|
|
109
|
+
const play = () => {
|
|
110
|
+
introTl?.kill();
|
|
111
|
+
const rows = list.map((it) => it.li);
|
|
112
|
+
g.killTweensOf(rows.flatMap((li) => Object.values(parts(li))));
|
|
113
|
+
if (RM() || !rows.length) { rows.forEach((li) => g.set(Object.values(parts(li)), { clearProps: 'all' })); return null; }
|
|
114
|
+
const tl = g.timeline({ onComplete: () => rows.forEach((li) => g.set(Object.values(parts(li)), { clearProps: 'all' })) });
|
|
115
|
+
rows.forEach((li, i) => {
|
|
116
|
+
const { seg, mk, body } = parts(li), at = i * 0.16;
|
|
117
|
+
tl.fromTo(mk, { scale: 0, opacity: 0 }, { scale: 1, opacity: 1, duration: 0.45, ease: 'back.out(2.6)' }, at)
|
|
118
|
+
.fromTo(body, { opacity: 0, y: 10, x: -6 }, { opacity: 1, y: 0, x: 0, duration: 0.5, ease: 'power3.out' }, at + 0.04)
|
|
119
|
+
.fromTo(seg, { scaleY: 0 }, { scaleY: 1, duration: 0.2, ease: 'none' }, at + 0.06);
|
|
120
|
+
});
|
|
121
|
+
introTl = tl;
|
|
122
|
+
return tl;
|
|
123
|
+
};
|
|
124
|
+
const hideAll = () => list.forEach((it) => { const { seg, mk, body } = parts(it.li); g.set(mk, { scale: 0, opacity: 0 }); g.set(body, { opacity: 0 }); g.set(seg, { scaleY: 0 }); });
|
|
125
|
+
|
|
126
|
+
/* ---------- trim to max ---------- */
|
|
127
|
+
const trim = (animate) => {
|
|
128
|
+
while (list.length > max) {
|
|
129
|
+
const it = list.pop(), li = it.li;
|
|
130
|
+
g.killTweensOf([li, ...Object.values(parts(li))]);
|
|
131
|
+
if (!animate) { li.remove(); continue; }
|
|
132
|
+
li.classList.add('is-leaving'); li.setAttribute('aria-hidden', 'true');
|
|
133
|
+
g.to(li, { opacity: 0, y: 8, height: 0, paddingBottom: 0, duration: 0.4, ease: 'power2.inOut', onComplete: () => li.remove() });
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/* ---------- add at the top ---------- */
|
|
138
|
+
const add = (items) => {
|
|
139
|
+
if (dead) return api;
|
|
140
|
+
const fresh = (Array.isArray(items) ? items : [items]).filter(Boolean).map(norm).sort((a, b) => b.ms - a.ms);
|
|
141
|
+
if (!fresh.length) return api;
|
|
142
|
+
introTl?.progress(1);
|
|
143
|
+
const animate = !RM();
|
|
144
|
+
const before = new Map(list.map((it) => [it.id, it.li.getBoundingClientRect().top]));
|
|
145
|
+
const h0 = ol.offsetHeight;
|
|
146
|
+
// the same id again replaces the old row
|
|
147
|
+
fresh.forEach((it) => { const k = list.findIndex((x) => x.id === it.id); if (k > -1) { list[k].li.remove(); list.splice(k, 1); } });
|
|
148
|
+
for (let i = fresh.length - 1; i >= 0; i--) { list.unshift(fresh[i]); ol.prepend(make(fresh[i])); }
|
|
149
|
+
markLatest();
|
|
150
|
+
trim(animate);
|
|
151
|
+
say(`${W.added(plain(fresh[0]))}${fresh.length > 1 ? ` ${W.more(fresh.length - 1)}` : ''}`);
|
|
152
|
+
if (!animate) return api;
|
|
153
|
+
const h1 = ol.offsetHeight;
|
|
154
|
+
if (h1 !== h0) g.fromTo(ol, { height: h0 }, { height: h1, duration: 0.55, ease: 'power3.inOut', clearProps: 'height' });
|
|
155
|
+
// the rows that were there glide down
|
|
156
|
+
list.forEach((it) => {
|
|
157
|
+
if (!before.has(it.id)) return;
|
|
158
|
+
const dy = before.get(it.id) - it.li.getBoundingClientRect().top;
|
|
159
|
+
if (Math.abs(dy) > 0.5) g.fromTo(it.li, { y: dy }, { y: 0, duration: 0.6, ease: 'power3.inOut', clearProps: 'transform' });
|
|
160
|
+
});
|
|
161
|
+
// the new ones pop in, their line grows down to the next mark
|
|
162
|
+
fresh.forEach((it, i) => {
|
|
163
|
+
if (!it.li.isConnected) return;
|
|
164
|
+
const { seg, mk, body } = parts(it.li), at = 0.18 + i * 0.1;
|
|
165
|
+
g.fromTo(mk, { scale: 0 }, { scale: 1, duration: 0.55, delay: at, ease: 'back.out(2.8)', clearProps: 'transform' });
|
|
166
|
+
g.fromTo(body, { opacity: 0, x: -10 }, { opacity: 1, x: 0, duration: 0.5, delay: at + 0.05, ease: 'power3.out', clearProps: 'opacity,transform' });
|
|
167
|
+
g.fromTo(seg, { scaleY: 0 }, { scaleY: 1, duration: 0.45, delay: at + 0.2, ease: 'power2.out', clearProps: 'transform' });
|
|
168
|
+
});
|
|
169
|
+
return api;
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const set = (items) => {
|
|
173
|
+
introTl?.kill();
|
|
174
|
+
list.forEach((it) => g.killTweensOf([it.li, ...Object.values(parts(it.li))]));
|
|
175
|
+
ol.innerHTML = '';
|
|
176
|
+
list = (items || []).filter(Boolean).map(norm).sort((a, b) => b.ms - a.ms);
|
|
177
|
+
list.forEach((it) => ol.append(make(it)));
|
|
178
|
+
markLatest();
|
|
179
|
+
trim(false);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/* ---------- start ---------- */
|
|
183
|
+
set(o.items);
|
|
184
|
+
if (o.intro && !RM() && list.length) {
|
|
185
|
+
if (o.intro === 'view' && typeof IntersectionObserver === 'function') {
|
|
186
|
+
hideAll();
|
|
187
|
+
io = new IntersectionObserver((ents) => { if (ents.some((e) => e.isIntersecting)) { io.disconnect(); io = null; play(); } }, { threshold: 0.2 });
|
|
188
|
+
io.observe(root);
|
|
189
|
+
} else play();
|
|
190
|
+
}
|
|
191
|
+
if (+o.refresh > 0) timer = setInterval(() => list.forEach((it) => stamp(it, it.li)), +o.refresh * 1000);
|
|
192
|
+
|
|
193
|
+
const api = {
|
|
194
|
+
root,
|
|
195
|
+
/** The events, newest first. */
|
|
196
|
+
get items() { return list.map(pub); },
|
|
197
|
+
add,
|
|
198
|
+
remove(id) {
|
|
199
|
+
const k = list.findIndex((x) => x.id === String(id));
|
|
200
|
+
if (k < 0) return api;
|
|
201
|
+
const [it] = list.splice(k, 1), li = it.li;
|
|
202
|
+
markLatest();
|
|
203
|
+
if (RM()) { li.remove(); return api; }
|
|
204
|
+
g.killTweensOf([li, ...Object.values(parts(li))]);
|
|
205
|
+
li.setAttribute('aria-hidden', 'true');
|
|
206
|
+
g.to(li, { opacity: 0, x: -12, height: 0, paddingBottom: 0, duration: 0.4, ease: 'power2.inOut', onComplete: () => li.remove() });
|
|
207
|
+
return api;
|
|
208
|
+
},
|
|
209
|
+
set(items) { set(items); return api; },
|
|
210
|
+
play() { io?.disconnect(); io = null; play(); return api; },
|
|
211
|
+
destroy() {
|
|
212
|
+
dead = true; io?.disconnect(); clearInterval(timer); introTl?.kill();
|
|
213
|
+
g.killTweensOf([ol, ...root.querySelectorAll('*')]);
|
|
214
|
+
el.innerHTML = '';
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
return api;
|
|
218
|
+
};
|
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
|
+
};
|