@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/carousel.js
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/* Carousel: slides you swipe, drag or step through. A flick carries on to the next slide; past the
|
|
2
|
+
ends the track gives like a rubber band. The dots are small circles; the current one stretches
|
|
3
|
+
into a pill, and with autoplay it fills up like a progress bar until the next slide.
|
|
4
|
+
One or several slides in view (by container width), a peek of the next one, an optional
|
|
5
|
+
"focus" look where the slides beside the current one shrink back. */
|
|
6
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
7
|
+
import { icon } from '@gecko-ui-kit/icons';
|
|
8
|
+
import { esc } from '../index.js';
|
|
9
|
+
|
|
10
|
+
const { g, RM, $$ } = M;
|
|
11
|
+
const WORDS = {
|
|
12
|
+
label: 'Carousel', prev: 'Previous slide', next: 'Next slide', slide: (i, n) => `${i} of ${n}`,
|
|
13
|
+
goTo: (i) => `Go to slide ${i}`, pause: 'Pause', play: 'Play',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* const c = carousel(el, {
|
|
18
|
+
* slides: ['<img …>', node, …], // or leave out: the children of el become the slides
|
|
19
|
+
* perView: 1 | { 0: 1, 600: 2, 960: 3 }, // by the carousel's own width
|
|
20
|
+
* gap: 12, peek: 0, // peek: px of the next slide left showing
|
|
21
|
+
* focus: false, // slides beside the current one shrink back (one in view)
|
|
22
|
+
* rewind: false, // next at the end goes back to the first
|
|
23
|
+
* autoplay: 0, // ms per slide; pauses on hover, focus and drag
|
|
24
|
+
* arrows: true, dots: true, start: 0,
|
|
25
|
+
* label, words: { label, prev, next, slide, goTo, pause, play },
|
|
26
|
+
* onChange(index, c)
|
|
27
|
+
* })
|
|
28
|
+
* c.index · c.count · c.go(i) · c.next() · c.prev() · c.play() · c.pause() · c.update() · c.destroy()
|
|
29
|
+
*/
|
|
30
|
+
export const carousel = (el, opts = {}) => {
|
|
31
|
+
el = $$(el)[0];
|
|
32
|
+
if (!el) return null;
|
|
33
|
+
const o = { slides: null, perView: 1, gap: 12, peek: 0, focus: false, rewind: false, autoplay: 0, arrows: true, dots: true, start: 0, label: '', onChange: null, ...opts };
|
|
34
|
+
const W = { ...WORDS, ...o.words };
|
|
35
|
+
const ac = new AbortController(), on = (t, ev, fn, x) => t.addEventListener(ev, fn, { signal: ac.signal, ...x });
|
|
36
|
+
|
|
37
|
+
const src = o.slides ?? [...el.children];
|
|
38
|
+
el.innerHTML = `<div class="cr${o.focus ? ' cr--focus' : ''}" role="region" aria-roledescription="carousel" aria-label="${esc(o.label || W.label)}">`
|
|
39
|
+
+ `<div class="cr__stage"><div class="cr__vp" tabindex="0"><div class="cr__track"></div></div>`
|
|
40
|
+
+ (o.arrows ? `<button type="button" class="cr__arrow cr__arrow--prev" aria-label="${esc(W.prev)}">${icon('chevL')}</button><button type="button" class="cr__arrow cr__arrow--next" aria-label="${esc(W.next)}">${icon('chevR')}</button>` : '') + '</div>'
|
|
41
|
+
+ `<div class="cr__bar"${o.dots || o.autoplay ? '' : ' hidden'}><div class="cr__dots"></div>`
|
|
42
|
+
+ (o.autoplay ? `<button type="button" class="cr__play" aria-label="${esc(W.pause)}">${icon('pause')}</button>` : '') + '</div>'
|
|
43
|
+
+ `<span class="cr__sr" aria-live="polite"></span></div>`;
|
|
44
|
+
const root = el.firstElementChild, vp = root.querySelector('.cr__vp'), track = root.querySelector('.cr__track');
|
|
45
|
+
const dotsEl = root.querySelector('.cr__dots'), playBtn = root.querySelector('.cr__play');
|
|
46
|
+
const prevBtn = root.querySelector('.cr__arrow--prev'), nextBtn = root.querySelector('.cr__arrow--next');
|
|
47
|
+
const slides = src.map((s) => {
|
|
48
|
+
const d = document.createElement('div');
|
|
49
|
+
d.className = 'cr__slide'; d.setAttribute('role', 'group'); d.setAttribute('aria-roledescription', 'slide');
|
|
50
|
+
if (typeof s === 'string') d.innerHTML = s; else d.append(s);
|
|
51
|
+
track.append(d);
|
|
52
|
+
return d;
|
|
53
|
+
});
|
|
54
|
+
const n = slides.length;
|
|
55
|
+
slides.forEach((d, i) => d.setAttribute('aria-label', W.slide(i + 1, n)));
|
|
56
|
+
on(track, 'dragstart', (e) => e.preventDefault());
|
|
57
|
+
|
|
58
|
+
let pv = 1, sw = 0, minX = 0, maxI = 0, index = Math.max(0, +o.start || 0), dotKey = '';
|
|
59
|
+
const step = () => sw + o.gap;
|
|
60
|
+
const perViewFor = (w) => {
|
|
61
|
+
if (typeof o.perView === 'number') return Math.max(1, o.perView);
|
|
62
|
+
let v = 1;
|
|
63
|
+
Object.keys(o.perView).map(Number).sort((a, b) => a - b).forEach((min) => { if (w >= min) v = o.perView[min]; });
|
|
64
|
+
return Math.max(1, v);
|
|
65
|
+
};
|
|
66
|
+
const pos = (i) => Math.max(minX, -i * step());
|
|
67
|
+
const clamp = (i) => Math.min(maxI, Math.max(0, i));
|
|
68
|
+
|
|
69
|
+
/* ---------- look ---------- */
|
|
70
|
+
const look = () => {
|
|
71
|
+
if (!o.focus || pv !== 1) return;
|
|
72
|
+
const x = g.getProperty(track, 'x');
|
|
73
|
+
slides.forEach((s, i) => {
|
|
74
|
+
const d = Math.min(1, Math.abs((i * step() + x) / step()));
|
|
75
|
+
g.set(s, { scale: 1 - d * 0.08, opacity: 1 - d * 0.45 });
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
const inview = () => {
|
|
79
|
+
const x = pos(index), w = vp.clientWidth;
|
|
80
|
+
slides.forEach((s, i) => {
|
|
81
|
+
const l = i * step() + x, out = l + sw <= 1 || l >= w - 1;
|
|
82
|
+
s.inert = out; out ? s.setAttribute('aria-hidden', 'true') : s.removeAttribute('aria-hidden');
|
|
83
|
+
s.classList.toggle('is-on', i >= index && i < index + pv);
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/* ---------- dots ---------- */
|
|
88
|
+
const drawDots = () => {
|
|
89
|
+
if (!dotsEl) return;
|
|
90
|
+
const key = `${maxI}`;
|
|
91
|
+
if (key !== dotKey) {
|
|
92
|
+
dotKey = key;
|
|
93
|
+
dotsEl.innerHTML = maxI + 1 > 12
|
|
94
|
+
? '<span class="cr__count"></span>'
|
|
95
|
+
: Array.from({ length: maxI + 1 }, (_, i) => `<button type="button" class="cr__dot" aria-label="${esc(W.goTo(i + 1))}"><i><b></b></i></button>`).join('');
|
|
96
|
+
dotsEl.hidden = !o.dots || maxI === 0;
|
|
97
|
+
}
|
|
98
|
+
const c = dotsEl.querySelector('.cr__count');
|
|
99
|
+
if (c) c.textContent = `${index + 1} / ${maxI + 1}`;
|
|
100
|
+
dotsEl.querySelectorAll('.cr__dot').forEach((d, i) => {
|
|
101
|
+
const onIt = i === index;
|
|
102
|
+
d.classList.toggle('is-on', onIt);
|
|
103
|
+
onIt ? d.setAttribute('aria-current', 'true') : d.removeAttribute('aria-current');
|
|
104
|
+
if (!onIt) g.set(d.querySelector('b'), { scaleX: 0 });
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
const arrows = () => {
|
|
108
|
+
if (!prevBtn) return;
|
|
109
|
+
const one = maxI === 0;
|
|
110
|
+
prevBtn.hidden = nextBtn.hidden = one;
|
|
111
|
+
prevBtn.disabled = !o.rewind && index === 0;
|
|
112
|
+
nextBtn.disabled = !o.rewind && index === maxI;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/* ---------- moving ---------- */
|
|
116
|
+
const place = (animate, dur = 0.55) => {
|
|
117
|
+
g.killTweensOf(track);
|
|
118
|
+
if (!animate || RM()) { g.set(track, { x: pos(index) }); look(); inview(); return; }
|
|
119
|
+
g.to(track, { x: pos(index), duration: dur, ease: 'power3.out', onUpdate: look, onComplete: inview });
|
|
120
|
+
slides.forEach((s) => { s.inert = false; s.removeAttribute('aria-hidden'); });
|
|
121
|
+
};
|
|
122
|
+
const say = (t) => { const s = root.querySelector('.cr__sr'); s.textContent = t; };
|
|
123
|
+
const go = (i, { animate = true, fromAuto = false } = {}) => {
|
|
124
|
+
i = clamp(i);
|
|
125
|
+
const moved = i !== index;
|
|
126
|
+
index = i; place(animate); drawDots(); arrows();
|
|
127
|
+
if (moved) { if (!fromAuto) say(W.slide(index + 1, maxI + 1)); o.onChange?.(index, api); }
|
|
128
|
+
restart();
|
|
129
|
+
return moved;
|
|
130
|
+
};
|
|
131
|
+
/** At an end with nowhere to go: the track leans and springs back. */
|
|
132
|
+
const nudge = (dir) => {
|
|
133
|
+
if (RM()) return;
|
|
134
|
+
const x = pos(index);
|
|
135
|
+
g.killTweensOf(track);
|
|
136
|
+
g.fromTo(track, { x }, { keyframes: { x: [x, x - dir * 28, x] }, duration: 0.45, ease: 'power2.out', onUpdate: look });
|
|
137
|
+
};
|
|
138
|
+
const next = (fromAuto = false) => {
|
|
139
|
+
if (index < maxI) return go(index + 1, { fromAuto });
|
|
140
|
+
if (o.rewind || fromAuto) return go(0, { fromAuto });
|
|
141
|
+
nudge(1); return false;
|
|
142
|
+
};
|
|
143
|
+
const prev = () => {
|
|
144
|
+
if (index > 0) return go(index - 1);
|
|
145
|
+
if (o.rewind) return go(maxI);
|
|
146
|
+
nudge(-1); return false;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/* ---------- layout ---------- */
|
|
150
|
+
const measure = () => {
|
|
151
|
+
const w = vp.clientWidth; if (!w) return;
|
|
152
|
+
pv = Math.min(perViewFor(w), n || 1);
|
|
153
|
+
const peek = o.peek && pv < n ? o.peek : 0;
|
|
154
|
+
sw = (w - o.gap * (pv - 1) - (peek ? peek + o.gap : 0)) / pv;
|
|
155
|
+
track.style.setProperty('--cr-w', `${sw}px`); track.style.gap = `${o.gap}px`;
|
|
156
|
+
const total = n * sw + (n - 1) * o.gap;
|
|
157
|
+
minX = Math.min(0, w - total);
|
|
158
|
+
maxI = Math.max(0, n - pv);
|
|
159
|
+
index = clamp(index);
|
|
160
|
+
root.classList.toggle('is-multi', pv > 1);
|
|
161
|
+
if (o.focus) slides.forEach((s) => g.set(s, { clearProps: 'scale,opacity' }));
|
|
162
|
+
place(false); drawDots(); arrows();
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/* ---------- autoplay ---------- */
|
|
166
|
+
let auto = null, playing = !!o.autoplay && !RM();
|
|
167
|
+
const holds = new Set();
|
|
168
|
+
const restart = () => {
|
|
169
|
+
auto?.kill(); auto = null;
|
|
170
|
+
if (!o.autoplay || !playing || n < 2 || maxI === 0) return;
|
|
171
|
+
const fill = dotsEl?.querySelector('.cr__dot.is-on b') ?? { p: 0 };
|
|
172
|
+
auto = g.fromTo(fill, fill.nodeType ? { scaleX: 0 } : { p: 0 }, { ...(fill.nodeType ? { scaleX: 1 } : { p: 1 }), duration: o.autoplay / 1000, ease: 'none', onComplete: () => next(true) });
|
|
173
|
+
if (holds.size) auto.pause();
|
|
174
|
+
};
|
|
175
|
+
const hold = (why, on) => {
|
|
176
|
+
on ? holds.add(why) : holds.delete(why);
|
|
177
|
+
if (!auto) return;
|
|
178
|
+
holds.size ? auto.pause() : auto.resume();
|
|
179
|
+
};
|
|
180
|
+
const setPlaying = (p) => {
|
|
181
|
+
playing = !!p && !!o.autoplay;
|
|
182
|
+
root.classList.toggle('is-paused', !playing);
|
|
183
|
+
if (playBtn) { playBtn.innerHTML = icon(playing ? 'pause' : 'play'); playBtn.setAttribute('aria-label', playing ? W.pause : W.play); }
|
|
184
|
+
if (!playing) dotsEl?.querySelectorAll('.cr__dot b').forEach((b) => g.set(b, { scaleX: 0 }));
|
|
185
|
+
restart();
|
|
186
|
+
};
|
|
187
|
+
if (o.autoplay) {
|
|
188
|
+
on(root, 'pointerenter', (e) => { if (e.pointerType === 'mouse') hold('hover', true); });
|
|
189
|
+
on(root, 'pointerleave', () => hold('hover', false));
|
|
190
|
+
on(root, 'focusin', () => hold('focus', true));
|
|
191
|
+
on(root, 'focusout', (e) => { if (!root.contains(e.relatedTarget)) hold('focus', false); });
|
|
192
|
+
on(document, 'visibilitychange', () => hold('hidden', document.hidden));
|
|
193
|
+
playBtn && on(playBtn, 'click', () => { M.pop(playBtn); setPlaying(!playing); });
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/* ---------- drag / swipe ---------- */
|
|
197
|
+
let d = null, eat = false;
|
|
198
|
+
const editable = 'button, input, textarea, select, [contenteditable], .cr__arrow';
|
|
199
|
+
on(vp, 'pointerdown', (e) => {
|
|
200
|
+
if (e.button > 0 || maxI === 0 || e.target.closest(editable)) return;
|
|
201
|
+
g.killTweensOf(track);
|
|
202
|
+
d = { id: e.pointerId, x0: e.clientX, y0: e.clientY, tx: g.getProperty(track, 'x'), lock: null, pts: [[e.clientX, performance.now()]] };
|
|
203
|
+
hold('drag', true);
|
|
204
|
+
});
|
|
205
|
+
on(vp, 'pointermove', (e) => {
|
|
206
|
+
if (!d || e.pointerId !== d.id) return;
|
|
207
|
+
const dx = e.clientX - d.x0, dy = e.clientY - d.y0;
|
|
208
|
+
if (!d.lock) {
|
|
209
|
+
if (Math.abs(dx) < 6 && Math.abs(dy) < 6) return;
|
|
210
|
+
d.lock = Math.abs(dx) > Math.abs(dy) ? 'x' : 'y';
|
|
211
|
+
if (d.lock === 'y') { d = null; hold('drag', false); place(true); return; }
|
|
212
|
+
try { vp.setPointerCapture(e.pointerId); } catch {}
|
|
213
|
+
root.classList.add('is-drag');
|
|
214
|
+
}
|
|
215
|
+
let x = d.tx + dx;
|
|
216
|
+
if (x > 0) x *= 0.35; else if (x < minX) x = minX + (x - minX) * 0.35;
|
|
217
|
+
g.set(track, { x }); look();
|
|
218
|
+
d.pts.push([e.clientX, performance.now()]); if (d.pts.length > 6) d.pts.shift();
|
|
219
|
+
});
|
|
220
|
+
const end = (e) => {
|
|
221
|
+
if (!d || (e && e.pointerId !== d.id)) return;
|
|
222
|
+
const was = d; d = null; root.classList.remove('is-drag'); hold('drag', false);
|
|
223
|
+
if (!was.lock) { place(true); return; }
|
|
224
|
+
eat = true; setTimeout(() => { eat = false; }, 0);
|
|
225
|
+
const [a, b] = [was.pts[0], was.pts[was.pts.length - 1]], v = (b[0] - a[0]) / Math.max(1, b[1] - a[1]);
|
|
226
|
+
const x = g.getProperty(track, 'x'), f = -x / step();
|
|
227
|
+
let i = Math.round(f);
|
|
228
|
+
if (Math.abs(v) > 0.3) i = v < 0 ? Math.ceil(f) : Math.floor(f);
|
|
229
|
+
if (i === index && Math.abs(b[0] - was.x0) > sw * 0.18) i += b[0] < was.x0 ? 1 : -1;
|
|
230
|
+
if (!go(i)) place(true, 0.45);
|
|
231
|
+
};
|
|
232
|
+
on(vp, 'pointerup', end); on(vp, 'pointercancel', end);
|
|
233
|
+
on(vp, 'click', (e) => { if (eat) { e.preventDefault(); e.stopPropagation(); } }, { capture: true });
|
|
234
|
+
|
|
235
|
+
/* ---------- buttons and keys ---------- */
|
|
236
|
+
prevBtn && on(prevBtn, 'click', () => { M.pop(prevBtn); prev(); });
|
|
237
|
+
nextBtn && on(nextBtn, 'click', () => { M.pop(nextBtn); next(); });
|
|
238
|
+
dotsEl && on(dotsEl, 'click', (e) => { const b = e.target.closest('.cr__dot'); if (b) go([...dotsEl.children].indexOf(b)); });
|
|
239
|
+
on(vp, 'keydown', (e) => {
|
|
240
|
+
if (e.target !== vp) return;
|
|
241
|
+
if (e.key === 'ArrowRight') { e.preventDefault(); next(); }
|
|
242
|
+
else if (e.key === 'ArrowLeft') { e.preventDefault(); prev(); }
|
|
243
|
+
else if (e.key === 'Home') { e.preventDefault(); go(0); }
|
|
244
|
+
else if (e.key === 'End') { e.preventDefault(); go(maxI); }
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const ro = new ResizeObserver(() => measure());
|
|
248
|
+
ro.observe(vp);
|
|
249
|
+
measure();
|
|
250
|
+
setPlaying(playing);
|
|
251
|
+
|
|
252
|
+
const api = {
|
|
253
|
+
root,
|
|
254
|
+
get index() { return index; },
|
|
255
|
+
get count() { return n; },
|
|
256
|
+
get positions() { return maxI + 1; },
|
|
257
|
+
go: (i) => go(+i || 0),
|
|
258
|
+
next: () => next(),
|
|
259
|
+
prev: () => prev(),
|
|
260
|
+
play: () => setPlaying(true),
|
|
261
|
+
pause: () => setPlaying(false),
|
|
262
|
+
update: () => measure(),
|
|
263
|
+
destroy() { ac.abort(); ro.disconnect(); auto?.kill(); g.killTweensOf([track, ...slides]); el.innerHTML = ''; },
|
|
264
|
+
};
|
|
265
|
+
return api;
|
|
266
|
+
};
|