@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/like.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/* Like & bookmark: an on / off icon button that celebrates when it turns on. Three looks share one API:
|
|
2
|
+
- heart (Like): the heart pops in from small with a little overshoot, a ring bursts out and thins away,
|
|
3
|
+
and eight dots fly out in a circle (two colours, a random twist each time).
|
|
4
|
+
- star: the same pop and burst with a star.
|
|
5
|
+
- bookmark (Save): the ribbon fills with colour from the top, dips down a little and throws four sparks
|
|
6
|
+
up and out. It can say "Save" / "Saved".
|
|
7
|
+
An optional count rolls up (or down when the like is taken back). Turning it off is quiet: a small dip.
|
|
8
|
+
It is a real <button> with aria-pressed. If your onChange returns a promise that fails (the server said
|
|
9
|
+
no), the button goes back to how it was and gives a small shake. likeBtn() writes the HTML; likeButton()
|
|
10
|
+
wires one up; bindAll(root) wires every .lk button inside root for you. */
|
|
11
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
12
|
+
import { esc } from '../index.js';
|
|
13
|
+
|
|
14
|
+
const { g, RM, $$ } = M;
|
|
15
|
+
const WORDS = { like: 'Like', star: 'Star', save: 'Save', saved: 'Saved', withCount: (label, count) => `${label}, ${count}` };
|
|
16
|
+
|
|
17
|
+
/* the outline shapes (24 × 24) */
|
|
18
|
+
const SHAPES = {
|
|
19
|
+
heart: 'M20.4 5.6a5 5 0 0 0-7.1 0L12 6.9l-1.3-1.3a5 5 0 0 0-7.1 7.1L12 21l8.4-8.3a5 5 0 0 0 0-7.1z',
|
|
20
|
+
star: 'm12 3 2.7 5.6 6.1.8-4.5 4.2 1.1 6.1L12 16.8l-5.4 2.9 1.1-6.1-4.5-4.2 6.1-.8z',
|
|
21
|
+
bookmark: 'M6.5 4.6c0-.9.7-1.6 1.6-1.6h7.8c.9 0 1.6.7 1.6 1.6V21L12 17.2 6.5 21z',
|
|
22
|
+
};
|
|
23
|
+
const KINDS = Object.keys(SHAPES);
|
|
24
|
+
const TONES = { accent: 'var(--accent)', good: 'var(--good)', warn: 'var(--warn)', bad: 'var(--bad)' };
|
|
25
|
+
const HUES = ['indigo', 'teal', 'rose', 'amber', 'violet', 'green', 'slate', 'sky'];
|
|
26
|
+
const toColor = (t) => (!t ? '' : TONES[t] || (HUES.includes(t) ? `var(--hue-${t})` : String(t)));
|
|
27
|
+
const kindOf = (k) => (KINDS.includes(k) ? k : 'heart');
|
|
28
|
+
const defaults = (kind, W) => (kind === 'bookmark' ? [W.save, W.saved] : [kind === 'star' ? W.star : W.like, '']);
|
|
29
|
+
const svg = (kind) => `<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false"><path d="${SHAPES[kind]}"/></svg>`;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* likeBtn({ kind, pressed, count, label, onLabel, iconOnly, size, tone, format, words, cls, attrs }) → HTML string
|
|
33
|
+
* kind: 'heart' (default) | 'star' | 'bookmark'. count: a number shows a rolling count (null = none).
|
|
34
|
+
* label / onLabel: the text (onLabel replaces it while on, e.g. Save → Saved). size: '' | 'sm' | 'lg'.
|
|
35
|
+
* iconOnly: a round icon button (the label becomes its aria-label). tone: 'accent' | 'rose' | any CSS colour.
|
|
36
|
+
*/
|
|
37
|
+
export const likeBtn = (opts = {}) => {
|
|
38
|
+
const kind = kindOf(opts.kind), W = { ...WORDS, ...opts.words };
|
|
39
|
+
const [l0, on0] = defaults(kind, W);
|
|
40
|
+
const label = opts.label ?? l0, onLabel = opts.onLabel ?? on0;
|
|
41
|
+
const pressed = !!opts.pressed, hasCount = opts.count != null && opts.count !== '';
|
|
42
|
+
const count = hasCount ? Math.max(0, Math.round(+opts.count || 0)) : null;
|
|
43
|
+
const fmt = (n) => (opts.format ? String(opts.format(n)) : n.toLocaleString());
|
|
44
|
+
const c = toColor(opts.tone);
|
|
45
|
+
const swap = !opts.iconOnly && onLabel && onLabel !== label;
|
|
46
|
+
const name = opts.iconOnly ? ` aria-label="${esc(hasCount ? W.withCount(label, fmt(count)) : label)}"` : '';
|
|
47
|
+
const bits = '<i></i>'.repeat(kind === 'bookmark' ? 4 : 8);
|
|
48
|
+
return `<button type="button" class="lk lk--${kind}${opts.iconOnly ? ' lk--icon' : ''}${opts.size ? ` lk--${esc(opts.size)}` : ''}${opts.cls ? ` ${esc(opts.cls)}` : ''}"`
|
|
49
|
+
+ ` aria-pressed="${pressed}" data-kind="${kind}" data-label="${esc(label)}"${onLabel ? ` data-on-label="${esc(onLabel)}"` : ''}${hasCount ? ` data-count="${count}"` : ''}`
|
|
50
|
+
+ `${c ? ` style="--lk-c:${esc(c)}"` : ''}${name}${opts.attrs ? ' ' + opts.attrs : ''}>`
|
|
51
|
+
+ `<span class="lk__icon" aria-hidden="true">${kind === 'bookmark' ? '' : '<span class="lk__ring"></span>'}<span class="lk__bits">${bits}</span>`
|
|
52
|
+
+ `<span class="lk__glyph"><span class="lk__line">${svg(kind)}</span><span class="lk__solid">${svg(kind)}</span></span></span>`
|
|
53
|
+
+ (opts.iconOnly ? '' : `${swap ? `<span class="lk__sr">${esc(label)}</span>` : ''}<span class="lk__label"${swap ? ' aria-hidden="true"' : ''}>${esc(pressed && swap ? onLabel : label)}</span>`
|
|
54
|
+
+ (hasCount ? `<span class="lk__count"><span class="lk__num">${esc(fmt(count))}</span></span>` : ''))
|
|
55
|
+
+ '</button>';
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* const like = likeButton(el, {
|
|
60
|
+
* kind: 'heart' | 'star' | 'bookmark',
|
|
61
|
+
* pressed: false, count: 128, // count: null = no number
|
|
62
|
+
* label: 'Like', onLabel: '', // bookmark defaults: 'Save' / 'Saved'
|
|
63
|
+
* iconOnly: false, size: '', tone: '',
|
|
64
|
+
* format: (n) => n.toLocaleString(),
|
|
65
|
+
* words: { like, star, save, saved, withCount },
|
|
66
|
+
* onChange(pressed, count, like), // return a promise: if it fails, the button goes back
|
|
67
|
+
* })
|
|
68
|
+
* like.pressed · like.count · like.set(pressed, { silent }) · like.toggle() · like.setCount(n) · like.destroy()
|
|
69
|
+
*
|
|
70
|
+
* el is a .lk button from likeBtn() (its state is read from the markup) or an empty element to build one in.
|
|
71
|
+
* Calling it again on the same button returns the same handle with the new options merged in.
|
|
72
|
+
*/
|
|
73
|
+
export const likeButton = (el, opts = {}) => {
|
|
74
|
+
el = $$(el)[0];
|
|
75
|
+
if (!el) return null;
|
|
76
|
+
if (el.__lk) { Object.assign(el.__lk.opts, opts); return el.__lk; }
|
|
77
|
+
let b = el, built = false;
|
|
78
|
+
if (!el.matches('.lk')) { el.innerHTML = likeBtn(opts); b = el.firstElementChild; built = true; }
|
|
79
|
+
const o = { onChange: null, format: null, ...opts };
|
|
80
|
+
const W = { ...WORDS, ...o.words };
|
|
81
|
+
const kind = kindOf(b.dataset.kind || o.kind);
|
|
82
|
+
const label = b.dataset.label ?? defaults(kind, W)[0], onLabel = b.dataset.onLabel || '';
|
|
83
|
+
const icon = b.querySelector('.lk__icon'), glyph = b.querySelector('.lk__glyph'), solid = b.querySelector('.lk__solid');
|
|
84
|
+
const ring = b.querySelector('.lk__ring'), bits = [...b.querySelectorAll('.lk__bits i')];
|
|
85
|
+
const labelEl = b.querySelector('.lk__label'), num = b.querySelector('.lk__num');
|
|
86
|
+
const iconOnly = b.classList.contains('lk--icon');
|
|
87
|
+
const ac = new AbortController();
|
|
88
|
+
let pressed = b.getAttribute('aria-pressed') === 'true';
|
|
89
|
+
let count = b.dataset.count != null ? Math.max(0, +b.dataset.count || 0) : null;
|
|
90
|
+
const fmt = (n) => (api.opts.format ? String(api.opts.format(n)) : n.toLocaleString());
|
|
91
|
+
|
|
92
|
+
/* ---------- the little shows ---------- */
|
|
93
|
+
const burst = () => {
|
|
94
|
+
const s = icon.offsetWidth || 20, tl = g.timeline();
|
|
95
|
+
tl.fromTo(glyph, { scale: 0.2 }, { scale: 1, duration: 0.55, ease: 'back.out(2.8)', clearProps: 'transform' }, 0);
|
|
96
|
+
if (ring) {
|
|
97
|
+
const d = s + 16;
|
|
98
|
+
tl.fromTo(ring, { scale: 0.2, opacity: 1, borderWidth: d / 2 }, { scale: 1.15, borderWidth: 0, duration: 0.45, ease: 'power2.out' }, 0)
|
|
99
|
+
.set(ring, { opacity: 0 });
|
|
100
|
+
}
|
|
101
|
+
const twist = Math.random() * 40 - 20;
|
|
102
|
+
bits.forEach((dot, i) => {
|
|
103
|
+
const a = ((i * 360) / bits.length + twist) * (Math.PI / 180);
|
|
104
|
+
const r0 = s * 0.45, r1 = s * (i % 2 ? 1 : 1.25);
|
|
105
|
+
tl.fromTo(dot, { x: Math.sin(a) * r0, y: -Math.cos(a) * r0, scale: 1, opacity: 1 },
|
|
106
|
+
{ x: Math.sin(a) * r1, y: -Math.cos(a) * r1, scale: 0.3, opacity: 0, duration: 0.6, ease: 'power2.out' }, 0.12);
|
|
107
|
+
});
|
|
108
|
+
return tl;
|
|
109
|
+
};
|
|
110
|
+
const fill = () => {
|
|
111
|
+
const s = icon.offsetWidth || 20, tl = g.timeline();
|
|
112
|
+
tl.fromTo(solid, { clipPath: 'inset(0% 0% 100% 0%)' }, { clipPath: 'inset(0% 0% 0% 0%)', duration: 0.35, ease: 'power2.inOut', clearProps: 'clipPath' }, 0)
|
|
113
|
+
.to(glyph, { keyframes: { y: [0, s * 0.18, 0], scaleY: [1, 1.12, 1] }, transformOrigin: '50% 0%', duration: 0.55, ease: 'power1.out', clearProps: 'transform' }, 0.2);
|
|
114
|
+
[-55, -20, 20, 55].forEach((deg, i) => {
|
|
115
|
+
const a = deg * (Math.PI / 180), x0 = (i < 2 ? -1 : 1) * s * (i % 3 ? 0.18 : 0.42);
|
|
116
|
+
tl.fromTo(bits[i], { x: x0, y: 0, rotation: deg, scaleY: 1, opacity: 1 },
|
|
117
|
+
{ x: x0 + Math.sin(a) * s * 0.55, y: -Math.cos(a) * s * 0.55, scaleY: 0.3, opacity: 0, duration: 0.5, ease: 'power2.out' }, 0.3);
|
|
118
|
+
});
|
|
119
|
+
return tl;
|
|
120
|
+
};
|
|
121
|
+
const quiet = () => {
|
|
122
|
+
const tl = g.timeline();
|
|
123
|
+
tl.fromTo(glyph, { scale: 1 }, { keyframes: { scale: [1, 0.75, 1] }, duration: 0.35, ease: 'power1.inOut', clearProps: 'transform' }, 0);
|
|
124
|
+
if (kind === 'bookmark' && !pressed) tl.fromTo(solid, { clipPath: 'inset(0% 0% 0% 0%)' }, { clipPath: 'inset(0% 0% 100% 0%)', duration: 0.25, ease: 'power2.in', clearProps: 'clipPath' }, 0);
|
|
125
|
+
return tl;
|
|
126
|
+
};
|
|
127
|
+
const roll = (n, up, animate) => {
|
|
128
|
+
if (!num) return;
|
|
129
|
+
g.killTweensOf(num);
|
|
130
|
+
const t = fmt(n);
|
|
131
|
+
if (!animate || RM()) { num.textContent = t; g.set(num, { clearProps: 'all' }); return; }
|
|
132
|
+
const d = up ? -1 : 1;
|
|
133
|
+
g.timeline()
|
|
134
|
+
.to(num, { yPercent: d * 100, opacity: 0, duration: 0.16, ease: 'power2.in' })
|
|
135
|
+
.add(() => { num.textContent = t; })
|
|
136
|
+
.fromTo(num, { yPercent: -d * 100, opacity: 0 }, { yPercent: 0, opacity: 1, duration: 0.3, ease: 'back.out(2.2)', clearProps: 'all' });
|
|
137
|
+
};
|
|
138
|
+
const relabel = (animate) => {
|
|
139
|
+
if (!labelEl || !onLabel || onLabel === label) return;
|
|
140
|
+
const t = pressed ? onLabel : label;
|
|
141
|
+
if (labelEl.textContent === t) return;
|
|
142
|
+
labelEl.textContent = t;
|
|
143
|
+
if (animate && !RM()) g.fromTo(labelEl, { y: pressed ? 6 : -6, opacity: 0 }, { y: 0, opacity: 1, duration: 0.28, ease: 'power2.out', clearProps: 'all' });
|
|
144
|
+
};
|
|
145
|
+
const draw = (animate, up, cheer = true) => {
|
|
146
|
+
b.setAttribute('aria-pressed', String(pressed));
|
|
147
|
+
if (count != null) b.dataset.count = String(count);
|
|
148
|
+
if (iconOnly) b.setAttribute('aria-label', count != null ? W.withCount(label, fmt(count)) : label);
|
|
149
|
+
relabel(animate);
|
|
150
|
+
g.killTweensOf([glyph, solid, ring, ...bits].filter(Boolean));
|
|
151
|
+
g.set([glyph, solid, ring, ...bits].filter(Boolean), { clearProps: 'all' });
|
|
152
|
+
if (animate && !RM()) (pressed && cheer ? (kind === 'bookmark' ? fill() : burst()) : quiet());
|
|
153
|
+
if (count != null) roll(count, up, animate);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const api = {
|
|
157
|
+
el: b, opts: o,
|
|
158
|
+
get pressed() { return pressed; },
|
|
159
|
+
get count() { return count; },
|
|
160
|
+
/** Turn it on or off (counts ±1 like a click). silent: true skips onChange. */
|
|
161
|
+
set(on, { silent = true, animate = true } = {}) {
|
|
162
|
+
on = !!on;
|
|
163
|
+
if (on === pressed) return api;
|
|
164
|
+
pressed = on;
|
|
165
|
+
if (count != null) count = Math.max(0, count + (on ? 1 : -1));
|
|
166
|
+
draw(animate, on);
|
|
167
|
+
if (!silent) changed();
|
|
168
|
+
return api;
|
|
169
|
+
},
|
|
170
|
+
toggle() { return api.set(!pressed, { silent: false }); },
|
|
171
|
+
/** Set the number (rolls up or down to it). */
|
|
172
|
+
setCount(n, animate = true) {
|
|
173
|
+
if (count == null || !num) return api;
|
|
174
|
+
const next = Math.max(0, Math.round(+n || 0)), up = next > count;
|
|
175
|
+
count = next; b.dataset.count = String(count);
|
|
176
|
+
if (iconOnly) b.setAttribute('aria-label', W.withCount(label, fmt(count)));
|
|
177
|
+
roll(count, up, animate);
|
|
178
|
+
return api;
|
|
179
|
+
},
|
|
180
|
+
destroy() {
|
|
181
|
+
ac.abort();
|
|
182
|
+
g.killTweensOf([glyph, solid, ring, num, labelEl, ...bits].filter(Boolean));
|
|
183
|
+
delete b.__lk;
|
|
184
|
+
if (built) { delete el.__lk; el.innerHTML = ''; }
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
const changed = () => {
|
|
188
|
+
const was = pressed, wasCount = count;
|
|
189
|
+
let r;
|
|
190
|
+
try { r = api.opts.onChange?.(pressed, count, api); } catch { r = Promise.reject(); }
|
|
191
|
+
if (r && typeof r.then === 'function') {
|
|
192
|
+
Promise.resolve(r).catch(() => {
|
|
193
|
+
if (!b.isConnected || pressed !== was) return; // already changed again: leave it
|
|
194
|
+
pressed = !was; count = wasCount == null ? null : Math.max(0, wasCount + (was ? -1 : 1));
|
|
195
|
+
draw(true, !was, false); // back without the celebration
|
|
196
|
+
M.shake(b);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
b.addEventListener('click', () => api.toggle(), { signal: ac.signal });
|
|
201
|
+
b.__lk = api;
|
|
202
|
+
if (built) el.__lk = api;
|
|
203
|
+
return api;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/** Wires every .lk button inside root (bindAll does this for you). Returns the handles. */
|
|
207
|
+
export const bindLikes = (root = document) =>
|
|
208
|
+
$$(root.matches?.('.lk') ? [root] : $$('.lk', root)).map((b) => b.__lk || likeButton(b));
|