@gecko-ui-kit/react 0.0.0-stage → 0.1.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 +1823 -2
- package/index.d.ts +12 -0
- package/index.js +15 -0
- package/package.json +49 -4
- package/src/actions.d.ts +192 -0
- package/src/actions.js +238 -0
- package/src/basics.d.ts +268 -0
- package/src/basics.js +308 -0
- package/src/charts.d.ts +207 -0
- package/src/charts.js +79 -0
- package/src/controls.d.ts +159 -0
- package/src/controls.js +261 -0
- package/src/core.d.ts +78 -0
- package/src/core.js +284 -0
- package/src/data.d.ts +762 -0
- package/src/data.js +608 -0
- package/src/effects.d.ts +410 -0
- package/src/effects.js +580 -0
- package/src/forms.d.ts +724 -0
- package/src/forms.js +403 -0
- package/src/motion.d.ts +130 -0
- package/src/motion.js +176 -0
- package/src/overlays.d.ts +450 -0
- package/src/overlays.js +580 -0
- package/src/shell.d.ts +206 -0
- package/src/shell.js +602 -0
- package/src/text.d.ts +316 -0
- package/src/text.js +354 -0
package/src/data.js
ADDED
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/* Data display from @gecko-ui-kit/components as React components: Accordion, Carousel, BeforeAfter, Tree,
|
|
3
|
+
DataGrid, VirtualList, Timeline, Calendar, SwipeItem + SwipeActions, Kanban, Notifications, Activity,
|
|
4
|
+
Pagination, Breadcrumbs, Skeleton, Alert.
|
|
5
|
+
Most are mounted pieces: the vanilla code builds and animates them inside one element. Where the content is
|
|
6
|
+
the app's own (accordion panels, slides, pictures, kanban cards, list rows, the alert message, what a
|
|
7
|
+
skeleton covers) it is drawn by React through portals into the elements the vanilla piece made.
|
|
8
|
+
Data changes (rows, events, cards, items…) are applied in place with the piece's own methods, so its
|
|
9
|
+
motion plays (a new card grows in, a removed one folds away) instead of the whole thing being drawn again. */
|
|
10
|
+
import { forwardRef, useCallback, useImperativeHandle, useRef, useState, Children, Fragment } from 'react';
|
|
11
|
+
import { createPortal, flushSync } from 'react-dom';
|
|
12
|
+
import * as C from '@gecko-ui-kit/components';
|
|
13
|
+
import { h, cx, iconNode, mounted, changedOnly, same, useIsoLayoutEffect, useLatest } from './core.js';
|
|
14
|
+
|
|
15
|
+
/* ---------------- shared helpers ---------------- */
|
|
16
|
+
|
|
17
|
+
/** Per-handle notes (quiet flag, extra elements) without touching the vanilla handle. */
|
|
18
|
+
const notes = new WeakMap();
|
|
19
|
+
const note = (handle) => { let n = notes.get(handle); if (!n) notes.set(handle, n = {}); return n; };
|
|
20
|
+
/** Run fn with the piece's own callbacks muted (a prop change is not a user change). */
|
|
21
|
+
const quietly = (handle, fn) => { const n = note(handle); n.quiet = true; try { return fn(); } finally { n.quiet = false; } };
|
|
22
|
+
const isQuiet = (handle) => !!(handle && notes.get(handle)?.quiet);
|
|
23
|
+
/** Drop our own (React-only) keys before options reach the vanilla code. */
|
|
24
|
+
const without = (o, keys) => { const r = { ...o }; keys.forEach((k) => delete r[k]); return r; };
|
|
25
|
+
|
|
26
|
+
/* Slots: elements inside a mounted piece that React draws content into (createPortal).
|
|
27
|
+
The inner (mounted) component reports Map<key, element> after each mount; the outer one renders the portals. */
|
|
28
|
+
const sameMap = (a, b) => {
|
|
29
|
+
if (a === b) return true;
|
|
30
|
+
if (!a || !b || a.size !== b.size) return false;
|
|
31
|
+
for (const [k, v] of a) { const w = b.get(k); if (!w || w.el !== v.el || w.item !== v.item || w.index !== v.index) return false; }
|
|
32
|
+
return true;
|
|
33
|
+
};
|
|
34
|
+
function useSlots() {
|
|
35
|
+
const [slots, setSlots] = useState(null);
|
|
36
|
+
const report = useCallback((m) => setSlots((old) => (sameMap(old, m) ? old : m)), []);
|
|
37
|
+
return [slots, report];
|
|
38
|
+
}
|
|
39
|
+
/** Portals for every slot: draw(key, entry) → ReactNode. */
|
|
40
|
+
const portals = (slots, draw) => (slots ? [...slots].map(([k, s]) => { const n = draw(k, s); return n == null ? null : createPortal(n, s.el, k); }) : null);
|
|
41
|
+
/** Map of key → { el } from elements found in a root. */
|
|
42
|
+
const slotMap = (pairs) => new Map(pairs.filter(([, el]) => el).map(([k, el]) => [k, { el }]));
|
|
43
|
+
|
|
44
|
+
/* `title` is a host prop in core (it would land on the element as a tooltip); pieces whose own option is
|
|
45
|
+
called title get it through this wrapper under another name and take it back before mounting. */
|
|
46
|
+
const TITLE = '__geckoTitle';
|
|
47
|
+
const titled = (name, Inner) => {
|
|
48
|
+
const C2 = forwardRef(function Titled({ title, ...rest }, ref) { return h(Inner, { ...rest, [TITLE]: title, ref }); });
|
|
49
|
+
C2.displayName = name;
|
|
50
|
+
return C2;
|
|
51
|
+
};
|
|
52
|
+
const takeTitle = (o) => { const r = without(o, [TITLE]); if (o[TITLE] !== undefined) r.title = o[TITLE]; return r; };
|
|
53
|
+
|
|
54
|
+
/* A mounted inner component + an outer one that forwards the ref (the inner's handle proxy). */
|
|
55
|
+
function useInnerRef(ref) {
|
|
56
|
+
const inner = useRef(null);
|
|
57
|
+
useImperativeHandle(ref, () => inner.current, []);
|
|
58
|
+
return inner;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* ---------------- Accordion ---------------- */
|
|
62
|
+
|
|
63
|
+
const AccordionInner = mounted('Accordion', (el, o) => {
|
|
64
|
+
const open = new Set([].concat(o.value ?? o.defaultValue ?? []).map(String));
|
|
65
|
+
const useValue = o.value !== undefined || o.defaultValue !== undefined;
|
|
66
|
+
const items = (o.items || []).map((it, i) => ({ ...it, id: String(it.id ?? i + 1), open: useValue ? open.has(String(it.id ?? i + 1)) : it.open }));
|
|
67
|
+
let pending = false;
|
|
68
|
+
const acc = C.accordion(el, {
|
|
69
|
+
...without(o, ['slots', 'value', 'defaultValue', 'onChange', 'nodeTitles']), items,
|
|
70
|
+
onToggle: (id, isOpen, a) => {
|
|
71
|
+
if (isQuiet(a)) return;
|
|
72
|
+
o.onToggle?.(id, isOpen, a);
|
|
73
|
+
if (!pending) { pending = true; queueMicrotask(() => { pending = false; if (notes.has(a) && !note(a).dead) o.onChange?.(a.value, a); }); }
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
note(acc);
|
|
77
|
+
o.slots?.(slotMap(acc.root ? [...acc.root.querySelectorAll(':scope > .acc__item')].flatMap((it) => [
|
|
78
|
+
[`b:${it.dataset.id}`, it.querySelector('.acc__body')],
|
|
79
|
+
...(o.nodeTitles?.includes(it.dataset.id) ? [[`t:${it.dataset.id}`, it.querySelector('.acc__title')]] : []),
|
|
80
|
+
]) : []));
|
|
81
|
+
const destroy = acc.destroy;
|
|
82
|
+
return Object.assign(acc, { destroy() { note(acc).dead = true; destroy(); } });
|
|
83
|
+
}, {
|
|
84
|
+
update(acc, next, prev) {
|
|
85
|
+
if (changedOnly(next, prev, ['defaultValue'])) return true;
|
|
86
|
+
if (changedOnly(next, prev, ['value', 'defaultValue'])) {
|
|
87
|
+
const want = new Set([].concat(next.value ?? []).map(String));
|
|
88
|
+
quietly(acc, () => {
|
|
89
|
+
acc.value.filter((id) => !want.has(id)).forEach((id) => acc.close(id));
|
|
90
|
+
want.forEach((id) => { if (!acc.isOpen(id)) acc.open(id); });
|
|
91
|
+
});
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* <Accordion items={[{ id: 'pay', title: 'Payment', sub: 'Cash or card', content: <PaymentForm /> }]} defaultValue={['pay']} />
|
|
100
|
+
* Same as accordion(el, options). Each item's `content` (and a `title` that is not plain text) is React content.
|
|
101
|
+
* value / defaultValue: the open ids. onChange(openIds, acc) · onToggle(id, open, acc). ref → the handle (open, close, filter…).
|
|
102
|
+
*/
|
|
103
|
+
export const Accordion = forwardRef(function Accordion({ items = [], ...rest }, ref) {
|
|
104
|
+
const [slots, report] = useSlots();
|
|
105
|
+
const inner = useInnerRef(ref);
|
|
106
|
+
const ids = items.map((it, i) => String(it.id ?? i + 1));
|
|
107
|
+
const data = items.map(({ content, title, ...it }, i) => ({ ...it, id: ids[i], title: typeof title === 'string' || typeof title === 'number' ? String(title) : '' }));
|
|
108
|
+
const nodeTitles = items.flatMap((it, i) => (it.title != null && typeof it.title === 'object' ? [ids[i]] : []));
|
|
109
|
+
return h(Fragment, null,
|
|
110
|
+
h(AccordionInner, { ...rest, items: data, nodeTitles, slots: report, ref: inner }),
|
|
111
|
+
portals(slots, (k) => {
|
|
112
|
+
const i = ids.indexOf(k.slice(2));
|
|
113
|
+
if (i < 0) return null;
|
|
114
|
+
return k[0] === 'b' ? items[i].content ?? null : items[i].title;
|
|
115
|
+
}));
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/* ---------------- Carousel ---------------- */
|
|
119
|
+
|
|
120
|
+
const CarouselInner = mounted('Carousel', (el, o) => {
|
|
121
|
+
const c = C.carousel(el, {
|
|
122
|
+
...without(o, ['slots', 'count', 'value', 'defaultValue']),
|
|
123
|
+
slides: Array.from({ length: o.count }, () => ''),
|
|
124
|
+
start: o.value ?? o.defaultValue ?? o.start ?? 0,
|
|
125
|
+
onChange: (i, h2) => { if (!isQuiet(h2)) o.onChange?.(i, h2); },
|
|
126
|
+
});
|
|
127
|
+
note(c);
|
|
128
|
+
o.slots?.(slotMap(c ? [...c.root.querySelectorAll('.cr__slide')].map((s, i) => [String(i), s]) : []));
|
|
129
|
+
return c;
|
|
130
|
+
}, {
|
|
131
|
+
update(c, next, prev) {
|
|
132
|
+
if (changedOnly(next, prev, ['defaultValue'])) return true;
|
|
133
|
+
if (changedOnly(next, prev, ['value', 'defaultValue'])) { if (next.value != null && next.value !== c.index) quietly(c, () => c.go(next.value)); return true; }
|
|
134
|
+
return false;
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* <Carousel perView={{ 0: 1, 640: 2 }} peek={40} onChange={setSlide}>{bikes.map((b) => <BikeCard key={b.id} bike={b} />)}</Carousel>
|
|
140
|
+
* Same as carousel(el, options); each child is one slide. value / defaultValue: the slide index.
|
|
141
|
+
* onChange(index, c). ref → { index, count, go(i), next(), prev(), play(), pause(), update(), el }.
|
|
142
|
+
*/
|
|
143
|
+
export const Carousel = forwardRef(function Carousel({ children, ...rest }, ref) {
|
|
144
|
+
const [slots, report] = useSlots();
|
|
145
|
+
const inner = useInnerRef(ref);
|
|
146
|
+
const kids = Children.toArray(children);
|
|
147
|
+
return h(Fragment, null,
|
|
148
|
+
h(CarouselInner, { ...rest, count: kids.length, slots: report, ref: inner }),
|
|
149
|
+
portals(slots, (k) => kids[+k] ?? null));
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
/* ---------------- BeforeAfter ---------------- */
|
|
153
|
+
|
|
154
|
+
const BeforeAfterInner = mounted('BeforeAfter', (el, o) => {
|
|
155
|
+
const ba = C.beforeAfter(el, { ...without(o, ['slots', 'value', 'defaultValue']), before: '', after: '', start: o.value ?? o.defaultValue ?? o.start ?? 50 });
|
|
156
|
+
o.slots?.(slotMap(ba ? [['before', ba.root.querySelector('.ba__before')], ['after', ba.root.querySelector('.ba__after')]] : []));
|
|
157
|
+
return ba;
|
|
158
|
+
}, {
|
|
159
|
+
update(ba, next, prev) {
|
|
160
|
+
if (changedOnly(next, prev, ['defaultValue'])) return true;
|
|
161
|
+
if (changedOnly(next, prev, ['value', 'defaultValue'])) { if (next.value != null && Math.round(next.value) !== ba.value) ba.set(next.value); return true; }
|
|
162
|
+
return false;
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* <BeforeAfter before={<img src="/scratched.jpg" alt="Before repair" />} after={<img src="/fixed.jpg" alt="After repair" />} hint />
|
|
168
|
+
* Same as beforeAfter(el, options) with React content on each side. value / defaultValue: 0–100 (vanilla `start`).
|
|
169
|
+
* onChange(value, ba) while it moves. ref → { value, set(v, animate), el }.
|
|
170
|
+
*/
|
|
171
|
+
export const BeforeAfter = forwardRef(function BeforeAfter({ before = null, after = null, ...rest }, ref) {
|
|
172
|
+
const [slots, report] = useSlots();
|
|
173
|
+
const inner = useInnerRef(ref);
|
|
174
|
+
return h(Fragment, null,
|
|
175
|
+
h(BeforeAfterInner, { ...rest, slots: report, ref: inner }),
|
|
176
|
+
portals(slots, (k) => (k === 'before' ? before : after)));
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
/* ---------------- Tree ---------------- */
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* <Tree items={folders} value={picked} onSelect={(ids) => setPicked(ids)} checkable label="Files" />
|
|
183
|
+
* Same as tree(el, options). value (or defaultValue): selected ids, applied in place with select();
|
|
184
|
+
* checked: ticked ids, applied in place with check(). Other changes (items…) draw it again.
|
|
185
|
+
* onSelect(ids, item, t) · onCheck(ids, t) · onToggle(id, open, t) · onActivate(item, t). ref → the handle.
|
|
186
|
+
*/
|
|
187
|
+
export const Tree = mounted('Tree', (el, o) => {
|
|
188
|
+
const t = C.tree(el, {
|
|
189
|
+
...without(o, ['defaultValue']), value: o.value ?? o.defaultValue ?? null,
|
|
190
|
+
onSelect: (...a) => { if (!isQuiet(a[a.length - 1])) o.onSelect?.(...a); },
|
|
191
|
+
onCheck: (...a) => { if (!isQuiet(a[a.length - 1])) o.onCheck?.(...a); },
|
|
192
|
+
});
|
|
193
|
+
note(t);
|
|
194
|
+
return t;
|
|
195
|
+
}, {
|
|
196
|
+
update(t, next, prev) {
|
|
197
|
+
if (changedOnly(next, prev, ['value', 'defaultValue', 'checked'])) {
|
|
198
|
+
if (!same(next.value, prev.value) && next.value !== undefined && !same([].concat(next.value ?? []).map(String), t.value)) quietly(t, () => t.select(next.value));
|
|
199
|
+
if (!same(next.checked, prev.checked) && next.checked != null) {
|
|
200
|
+
const want = new Set([].concat(next.checked).map(String)), now = new Set(t.checked);
|
|
201
|
+
quietly(t, () => {
|
|
202
|
+
now.forEach((id) => { if (!want.has(id) && t.get(id)?.checked === true) t.check(id, false); });
|
|
203
|
+
want.forEach((id) => { if (t.get(id)?.checked !== true) t.check(id, true); });
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
return false;
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
/* ---------------- DataGrid ---------------- */
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* <DataGrid columns={[{ key: 'plate', label: 'Plate' }, { key: 'rate', label: 'Rate / day', type: 'number', edit: true }]}
|
|
216
|
+
* rows={bikes} onChange={(row, key, value) => save(row.id, key, value)} />
|
|
217
|
+
* Same as grid(el, options). A new `rows` is applied in place with setRows() (skipped when it already matches
|
|
218
|
+
* the grid). columns[].render(value, row) returns trusted HTML: escape app text yourself (esc from components).
|
|
219
|
+
* ref → { rows(), visible(), setRows, search, filter, sort, page, reset, add, remove, selected(), el }.
|
|
220
|
+
*/
|
|
221
|
+
export const DataGrid = mounted('DataGrid', (el, o) => C.grid(el, o), {
|
|
222
|
+
update(g, next, prev) {
|
|
223
|
+
if (changedOnly(next, prev, ['rows'])) { if (!same(next.rows ?? [], g.rows())) g.setRows(next.rows || []); return true; }
|
|
224
|
+
return false;
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
/* ---------------- VirtualList ---------------- */
|
|
229
|
+
|
|
230
|
+
const vlKey = (o, item, i) => {
|
|
231
|
+
if (o.key) return o.key(item, i);
|
|
232
|
+
if (item != null && typeof item === 'object') return item.id ?? item.key ?? i;
|
|
233
|
+
return typeof o.items === 'number' ? i : item;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const VirtualListInner = mounted('VirtualList', (el, o) => {
|
|
237
|
+
let list = null;
|
|
238
|
+
const opts = without(o, ['slots', 'react', 'defaultValue']);
|
|
239
|
+
opts.value = o.value !== undefined ? o.value : o.defaultValue;
|
|
240
|
+
opts.onSelect = (item, index, l) => { if (!isQuiet(l)) o.onSelect?.(item, index, l); };
|
|
241
|
+
if (o.react) {
|
|
242
|
+
/* Each row gets a container (kept per key, so refresh() hands back the same one); React draws
|
|
243
|
+
renderItem(item, index) into the containers that are on screen. */
|
|
244
|
+
const boxes = new Map();
|
|
245
|
+
let queued = false;
|
|
246
|
+
const flush = () => {
|
|
247
|
+
queued = false;
|
|
248
|
+
if (!list || note(list).dead) return;
|
|
249
|
+
const m = new Map();
|
|
250
|
+
boxes.forEach((b, k) => { if (b.el.isConnected) m.set(String(k), b); else boxes.delete(k); });
|
|
251
|
+
flushSync(() => o.slots?.(m));
|
|
252
|
+
};
|
|
253
|
+
opts.render = (item, index) => {
|
|
254
|
+
const k = vlKey(o, item, index);
|
|
255
|
+
let b = boxes.get(k);
|
|
256
|
+
if (!b) { const d = document.createElement('div'); d.className = 'vl__r'; d.style.display = 'contents'; b = { el: d }; }
|
|
257
|
+
boxes.set(k, { el: b.el, item, index });
|
|
258
|
+
if (!queued) { queued = true; queueMicrotask(flush); }
|
|
259
|
+
return b.el;
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
list = C.virtualList(el, opts);
|
|
263
|
+
note(list);
|
|
264
|
+
const destroy = list.destroy;
|
|
265
|
+
return Object.assign(list, { destroy() { note(list).dead = true; destroy(); } });
|
|
266
|
+
}, {
|
|
267
|
+
update(l, next, prev) {
|
|
268
|
+
if (changedOnly(next, prev, ['items', 'value', 'defaultValue'])) {
|
|
269
|
+
if (!same(next.items, prev.items)) l.update(next.items);
|
|
270
|
+
if (next.value !== undefined && !same(next.value, prev.value)) {
|
|
271
|
+
const items = typeof next.items === 'number' ? null : next.items || [];
|
|
272
|
+
const n = items ? items.length : next.items;
|
|
273
|
+
for (let i = 0; i < n; i++) if (same(vlKey(next, items ? items[i] : i, i), next.value)) { quietly(l, () => l.select(i, { scroll: false })); break; }
|
|
274
|
+
}
|
|
275
|
+
return true;
|
|
276
|
+
}
|
|
277
|
+
return false;
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* <VirtualList items={riders} rowHeight={56} height={420} renderItem={(r) => <RiderRow rider={r} />} selectable onSelect={open} />
|
|
283
|
+
* Same as virtualList(el, options): only the rows in view are in the page. Draw rows with renderItem (React)
|
|
284
|
+
* or with render(item, index) → trusted HTML / a Node. A new `items` is applied in place with update().
|
|
285
|
+
* value / defaultValue: the key of the picked row (selectable). ref → { update, scrollTo, select, refresh, range, count, el }.
|
|
286
|
+
*/
|
|
287
|
+
export const VirtualList = forwardRef(function VirtualList({ renderItem, ...rest }, ref) {
|
|
288
|
+
const [slots, report] = useSlots();
|
|
289
|
+
const inner = useInnerRef(ref);
|
|
290
|
+
const auto = rest.rowHeight === 'auto';
|
|
291
|
+
useIsoLayoutEffect(() => { if (slots && auto) inner.current?.refresh?.(); }, [slots]);
|
|
292
|
+
return h(Fragment, null,
|
|
293
|
+
h(VirtualListInner, { ...rest, react: !!renderItem, slots: renderItem ? report : undefined, ref: inner }),
|
|
294
|
+
renderItem ? portals(slots, (k, s) => renderItem(s.item, s.index)) : null);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
/* ---------------- Timeline (bookings) ---------------- */
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* <Timeline rows={bikes} items={bookings} from="2026-12-01" days={14} onChange={(b) => saveBooking(b)} />
|
|
301
|
+
* Same as timeline(el, options). A new `items` is applied in place (add / update / remove, so bars glide and grow);
|
|
302
|
+
* nothing happens when it already matches what is on screen (e.g. after you store a drag from onChange).
|
|
303
|
+
* onChange(item, before) · onOpen(item, bar) · onAdd({ row, start }) · onClash(item, other). ref → the handle (undo(), go()…).
|
|
304
|
+
*/
|
|
305
|
+
export const Timeline = titled('Timeline', mounted('Timeline', (el, o) => C.timeline(el, takeTitle(o)), {
|
|
306
|
+
update(tl, next, prev) {
|
|
307
|
+
if (!changedOnly(next, prev, ['items'])) return false;
|
|
308
|
+
const now = new Map(tl.items().map((x) => [String(x.id), x]));
|
|
309
|
+
const want = next.items || [];
|
|
310
|
+
const ids = new Set(want.map((x) => String(x.id)));
|
|
311
|
+
now.forEach((x, id) => { if (!ids.has(id)) tl.remove(x.id); });
|
|
312
|
+
want.forEach((x) => {
|
|
313
|
+
const cur = now.get(String(x.id));
|
|
314
|
+
if (!cur) { tl.add(x); return; }
|
|
315
|
+
const patch = {};
|
|
316
|
+
Object.keys(x).forEach((k) => {
|
|
317
|
+
if (k === 'id') return;
|
|
318
|
+
const a = x[k], b = cur[k];
|
|
319
|
+
const differs = k === 'start' || k === 'end' ? String(a instanceof Date ? a.toISOString() : a) !== String(b) : !same(a, b);
|
|
320
|
+
if (differs) patch[k] = a;
|
|
321
|
+
});
|
|
322
|
+
if (Object.keys(patch).length) tl.update(x.id, patch);
|
|
323
|
+
});
|
|
324
|
+
return true;
|
|
325
|
+
},
|
|
326
|
+
}));
|
|
327
|
+
|
|
328
|
+
/* ---------------- Calendar ---------------- */
|
|
329
|
+
|
|
330
|
+
const dayKey = (d) => { if (d == null || d === '') return null; if (d instanceof Date) return `${d.getFullYear()}-${d.getMonth()}-${d.getDate()}`; const [y, m, dd] = String(d).slice(0, 10).split('-').map(Number); return `${y}-${m - 1}-${dd}`; };
|
|
331
|
+
const monthKey = (d) => { const k = dayKey(d); return k && k.split('-').slice(0, 2).join('-'); };
|
|
332
|
+
/**
|
|
333
|
+
* <Calendar events={bookings} value={day} onDay={(d) => setDay(d)} draggable onMove={(ev, start, end) => move(ev.id, start, end)} />
|
|
334
|
+
* Same as calendar(el, options). value (or defaultValue): the picked day, applied in place with select();
|
|
335
|
+
* month: the month on show, applied with go(); a new `events` is applied with setEvents() (skipped when it already
|
|
336
|
+
* matches, e.g. after storing a move). onMonth(date, cal) · onDay(date, events, cal) · onEvent(event, cal) · onMove(event, start, end, cal).
|
|
337
|
+
*/
|
|
338
|
+
export const Calendar = mounted('Calendar', (el, o) => C.calendar(el, { ...without(o, ['defaultValue']), value: o.value ?? o.defaultValue ?? null }), {
|
|
339
|
+
update(cal, next, prev) {
|
|
340
|
+
if (!changedOnly(next, prev, ['events', 'value', 'defaultValue', 'month'])) return false;
|
|
341
|
+
if (!same(next.events, prev.events) && !same(next.events || [], cal.events)) cal.setEvents(next.events || []);
|
|
342
|
+
if (!same(next.value, prev.value) && next.value !== undefined && dayKey(next.value) !== dayKey(cal.value)) cal.select(next.value);
|
|
343
|
+
if (!same(next.month, prev.month) && next.month != null && monthKey(next.month) !== monthKey(cal.month)) cal.go(next.month);
|
|
344
|
+
return true;
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
/* ---------------- Swipe actions ---------------- */
|
|
349
|
+
|
|
350
|
+
const SWIPE_TONES = new Set(['good', 'bad', 'warn', 'accent', 'neutral']);
|
|
351
|
+
const swipeAct = (a) => h('button', {
|
|
352
|
+
key: a.id, type: 'button', className: `swp__act swp__act--${SWIPE_TONES.has(a.tone) ? a.tone : 'neutral'}`,
|
|
353
|
+
'data-act': a.id, 'data-remove': a.remove ? '' : undefined, tabIndex: -1,
|
|
354
|
+
}, h('span', { className: 'swp__in' }, iconNode(a.icon), h('span', { className: 'swp__lb' }, a.label ?? a.id)));
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* <SwipeItem id={b.id} end={[{ id: 'cancel', label: 'Cancel', icon: 'trash', tone: 'bad', remove: true }]}>…your row…</SwipeItem>
|
|
358
|
+
* Same markup as swipeItem({ body, start, end, id, look, label }); children are the row. Put the rows in a <SwipeActions>.
|
|
359
|
+
* start: actions shown when the row is pulled right, end: when pulled left (listed from the edge inward).
|
|
360
|
+
*/
|
|
361
|
+
export const SwipeItem = forwardRef(function SwipeItem({ id, start = [], end = [], look = 'both', label, children, className, ...rest }, ref) {
|
|
362
|
+
return h('div', {
|
|
363
|
+
ref, ...rest, 'data-id': id != null && id !== '' ? String(id) : undefined,
|
|
364
|
+
className: cx('swp', (look === 'icons' || look === 'labels') && 'swp--' + look, className),
|
|
365
|
+
},
|
|
366
|
+
h('div', { className: 'swp__main', tabIndex: 0, 'aria-label': label || undefined, 'aria-keyshortcuts': 'ArrowLeft ArrowRight' }, children),
|
|
367
|
+
h('div', { className: 'swp__acts swp__acts--start' }, start.map(swipeAct)),
|
|
368
|
+
h('div', { className: 'swp__acts swp__acts--end' }, [...end].reverse().map(swipeAct)));
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
const SWIPE_OPTS = ['full', 'fullAt', 'single', 'haptic', 'keepRows', 'onAction', 'onOpen', 'onClose'];
|
|
372
|
+
/**
|
|
373
|
+
* <SwipeActions className="card card--flush" onAction={(action, ctx) => …}>{rows.map((r) => <SwipeItem key={r.id} …/>)}</SwipeActions>
|
|
374
|
+
* Same as swipeActions(list, options) on the element it renders. A row whose action has remove: true slides off and
|
|
375
|
+
* is hidden (React still owns it: drop it from your list when you are done, or call ctx.undo() to bring it back).
|
|
376
|
+
* onAction(action, ctx, sw) — ctx: { row, id, action, undo() }; return false to keep the row. onOpen(row, side) · onClose(row).
|
|
377
|
+
* ref → the list element; the handle (open, close, peek) is on ref.current.__swp.
|
|
378
|
+
*/
|
|
379
|
+
export const SwipeActions = forwardRef(function SwipeActions({ as, children, ...props }, ref) {
|
|
380
|
+
const el = useRef(null), cur = useRef(null);
|
|
381
|
+
const opts = { keepRows: true }, dom = {};
|
|
382
|
+
for (const k in props) (SWIPE_OPTS.includes(k) ? opts : dom)[k] = props[k];
|
|
383
|
+
const latest = useLatest(opts);
|
|
384
|
+
useImperativeHandle(ref, () => el.current, []);
|
|
385
|
+
useIsoLayoutEffect(() => {
|
|
386
|
+
const live = {};
|
|
387
|
+
SWIPE_OPTS.forEach((k) => { live[k] = typeof latest.current[k] === 'function' ? (...a) => latest.current[k]?.(...a) : latest.current[k]; });
|
|
388
|
+
const sw = C.swipeActions(el.current, live);
|
|
389
|
+
cur.current = sw;
|
|
390
|
+
return () => { cur.current = null; sw?.destroy(); };
|
|
391
|
+
}, []);
|
|
392
|
+
useIsoLayoutEffect(() => {
|
|
393
|
+
const sw = cur.current; if (!sw) return;
|
|
394
|
+
SWIPE_OPTS.forEach((k) => { if (typeof opts[k] !== 'function' && sw.opts[k] !== opts[k]) sw.opts[k] = opts[k]; });
|
|
395
|
+
});
|
|
396
|
+
return h(as || 'div', { ref: el, ...dom }, children);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
/* ---------------- Kanban ---------------- */
|
|
400
|
+
|
|
401
|
+
const KanbanInner = mounted('Kanban', (el, o) => {
|
|
402
|
+
const opts = without(o, ['slots', 'react']);
|
|
403
|
+
if (o.react) opts.render = (c) => `<div class="kb__r" data-kb-r="${C.esc(String(c.id))}" style="display:contents"></div>`;
|
|
404
|
+
const kb = C.kanban(el, opts);
|
|
405
|
+
note(kb).scan = () => o.slots?.(slotMap([...kb.el.querySelectorAll('[data-kb-r]')].map((x) => [x.dataset.kbR, x])));
|
|
406
|
+
if (o.react) note(kb).scan();
|
|
407
|
+
return kb;
|
|
408
|
+
}, {
|
|
409
|
+
update(kb, next, prev) {
|
|
410
|
+
if (!changedOnly(next, prev, ['cards'])) return false;
|
|
411
|
+
const want = next.cards || [];
|
|
412
|
+
const strip = (c) => { const { column, ...r } = c; return r; };
|
|
413
|
+
const nowById = () => new Map(kb.cards().map((c) => [String(c.id), c]));
|
|
414
|
+
let now = nowById();
|
|
415
|
+
const ids = new Set(want.map((c) => String(c.id)));
|
|
416
|
+
now.forEach((c, id) => { if (!ids.has(id)) kb.remove(c.id); });
|
|
417
|
+
(next.columns || []).forEach((col) => {
|
|
418
|
+
want.filter((c) => String(c.column) === String(col.id)).forEach((c, i) => {
|
|
419
|
+
const had = now.get(String(c.id));
|
|
420
|
+
if (!had) { kb.add(c, i); now = nowById(); return; }
|
|
421
|
+
const fields = strip(c), was = strip(had);
|
|
422
|
+
if (!same(fields, Object.fromEntries(Object.keys(fields).map((k) => [k, was[k]])))) kb.update(c.id, fields);
|
|
423
|
+
const list = kb.cards().filter((x) => String(x.column) === String(col.id) && ids.has(String(x.id)));
|
|
424
|
+
const at = list.findIndex((x) => String(x.id) === String(c.id));
|
|
425
|
+
if (String(had.column) !== String(col.id) || at !== i) { kb.move(c.id, col.id, i); now = nowById(); }
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
note(kb).scan?.();
|
|
429
|
+
return true;
|
|
430
|
+
},
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* <Kanban columns={[{ id: 'todo', title: 'To do' }, { id: 'done', title: 'Done', limit: 5 }]} cards={tasks}
|
|
435
|
+
* renderCard={(t) => <TaskCard task={t} />} onMove={(card, { to, index }) => moveTask(card.id, to, index)} />
|
|
436
|
+
* Same as kanban(el, options). renderCard draws each card with React (or leave it out for the built-in card:
|
|
437
|
+
* tags, title, text; `meta` is trusted HTML). A new `cards` is applied in place: added cards grow in, removed ones
|
|
438
|
+
* fold away, moved ones slide. onMove(card, { from, to, index, fromIndex }, kb) (return false to put it back) ·
|
|
439
|
+
* onOpen(card, el, kb) · onAdd(columnId, kb). ref → { cards(), add, update, remove, move, setCards, el }.
|
|
440
|
+
*/
|
|
441
|
+
export const Kanban = forwardRef(function Kanban({ renderCard, ...rest }, ref) {
|
|
442
|
+
const [slots, report] = useSlots();
|
|
443
|
+
const inner = useInnerRef(ref);
|
|
444
|
+
const seen = useRef(new Map());
|
|
445
|
+
(rest.cards || []).forEach((c) => seen.current.set(String(c.id), c));
|
|
446
|
+
return h(Fragment, null,
|
|
447
|
+
h(KanbanInner, { ...rest, react: !!renderCard, slots: renderCard ? report : undefined, ref: inner }),
|
|
448
|
+
renderCard ? portals(slots, (k) => { const c = seen.current.get(k); return c ? renderCard(c) : null; }) : null);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
/* ---------------- Notifications ---------------- */
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* <Notifications items={notes} height={420} onOpen={(n) => markRead(n.id)} onAction={(n, id) => api.answer(n.id, id)} />
|
|
455
|
+
* Same as notifications(el, options). A new `items` is applied in place: new ones slide in at the top, removed
|
|
456
|
+
* ones fold away, changed ones are drawn again (read / unread lets the dot go). filter: 'all' | 'unread' in place.
|
|
457
|
+
* onOpen(item, nf) · onAction(item, actionId, nf) → Promise<string | false | void> · onDismiss(item, nf) · onChange(unreadCount, nf).
|
|
458
|
+
*/
|
|
459
|
+
export const Notifications = mounted('Notifications', (el, o) => C.notifications(el, o), {
|
|
460
|
+
update(nf, next, prev) {
|
|
461
|
+
if (!changedOnly(next, prev, ['items', 'filter'])) return false;
|
|
462
|
+
if (next.filter !== prev.filter && next.filter && nf.filter !== next.filter) nf.setFilter(next.filter);
|
|
463
|
+
if (same(next.items, prev.items)) return true;
|
|
464
|
+
const now = new Map(nf.items.map((x) => [x.id, x]));
|
|
465
|
+
const want = next.items || [];
|
|
466
|
+
const ids = new Set(want.map((x) => String(x.id)));
|
|
467
|
+
now.forEach((x, id) => { if (!ids.has(id)) nf.remove(id); });
|
|
468
|
+
const fresh = want.filter((x) => x.id == null || !now.has(String(x.id)));
|
|
469
|
+
want.forEach((x) => {
|
|
470
|
+
const cur = x.id != null && now.get(String(x.id)); if (!cur) return;
|
|
471
|
+
const patch = {};
|
|
472
|
+
Object.keys(x).forEach((k) => { if (k !== 'id' && k !== 'read' && !same(x[k], cur[k])) patch[k] = x[k]; });
|
|
473
|
+
if (Object.keys(patch).length) nf.update(x.id, patch);
|
|
474
|
+
if (!!x.read !== !!cur.read) nf.read(x.id, !!x.read);
|
|
475
|
+
});
|
|
476
|
+
if (fresh.length) nf.add(fresh);
|
|
477
|
+
return true;
|
|
478
|
+
},
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
/* ---------------- Activity ---------------- */
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* <Activity items={events} marks="auto" locale="th" />
|
|
485
|
+
* Same as activity(el, options). Events newer than the ones on screen are added at the top with the push-down
|
|
486
|
+
* motion; removed ones fold away; any other change sets the list again. ref → { items, add, remove, set, play(), el }.
|
|
487
|
+
*/
|
|
488
|
+
export const Activity = mounted('Activity', (el, o) => C.activity(el, o), {
|
|
489
|
+
update(a, next, prev) {
|
|
490
|
+
if (!changedOnly(next, prev, ['items'])) return false;
|
|
491
|
+
const now = a.items, want = next.items || [];
|
|
492
|
+
const nowIds = new Set(now.map((x) => x.id)), ids = new Set(want.map((x) => String(x.id)));
|
|
493
|
+
if (want.some((x) => x.id == null)) { a.set(want); return true; }
|
|
494
|
+
now.forEach((x) => { if (!ids.has(x.id)) a.remove(x.id); });
|
|
495
|
+
const kept = want.filter((x) => nowIds.has(String(x.id)));
|
|
496
|
+
const fresh = want.filter((x) => !nowIds.has(String(x.id)));
|
|
497
|
+
const changed = kept.some((x) => { const c = now.find((y) => y.id === String(x.id)); return Object.keys(x).some((k) => k !== 'id' && !same(x[k], c[k])); });
|
|
498
|
+
const topOnly = want.slice(0, fresh.length).every((x) => !nowIds.has(String(x.id)));
|
|
499
|
+
if (changed || !topOnly) { a.set(want); return true; }
|
|
500
|
+
if (fresh.length) a.add(fresh);
|
|
501
|
+
return true;
|
|
502
|
+
},
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
/* ---------------- Pagination ---------------- */
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* <Pagination total={240} pageSize={10} value={page} onChange={(p, size) => { setPage(p); setSize(size); }} />
|
|
509
|
+
* Same as pagination(el, options). value / defaultValue: the page (vanilla `page`), applied in place with go();
|
|
510
|
+
* a new `total` is applied with setTotal(). onChange(page, pageSize, p). ref → { page, pageSize, pages, total, go, next, prev, setTotal, el }.
|
|
511
|
+
*/
|
|
512
|
+
export const Pagination = mounted('Pagination', (el, o) => {
|
|
513
|
+
const p = C.pagination(el, {
|
|
514
|
+
...without(o, ['value', 'defaultValue']), page: o.value ?? o.defaultValue ?? o.page ?? 1,
|
|
515
|
+
onChange: (page, size, x) => { if (!isQuiet(x)) o.onChange?.(page, size, x); },
|
|
516
|
+
});
|
|
517
|
+
note(p);
|
|
518
|
+
return p;
|
|
519
|
+
}, {
|
|
520
|
+
update(p, next, prev) {
|
|
521
|
+
if (!changedOnly(next, prev, ['value', 'defaultValue', 'total', 'pageSize'])) return false;
|
|
522
|
+
if (!same(next.pageSize, prev.pageSize) && +next.pageSize !== p.pageSize) return false;
|
|
523
|
+
if (next.total !== prev.total) p.setTotal(next.total); // a page that no longer exists is reported
|
|
524
|
+
if (next.value != null && next.value !== p.page) quietly(p, () => p.go(next.value));
|
|
525
|
+
return true;
|
|
526
|
+
},
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
/* ---------------- Breadcrumbs ---------------- */
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* <Breadcrumbs items={[{ label: 'Fleet', href: '/fleet' }, { label: 'Honda Click' }]} onNavigate={(it, i, e) => { e.preventDefault(); router.push(it.href) }} />
|
|
533
|
+
* Same as breadcrumbs(el, options). A new `items` is set in place like a route change (shared steps stay, new ones slide in).
|
|
534
|
+
* onNavigate(item, index, event). ref → { items, set(items), expand(), el }.
|
|
535
|
+
*/
|
|
536
|
+
export const Breadcrumbs = mounted('Breadcrumbs', (el, o) => C.breadcrumbs(el, o), {
|
|
537
|
+
update(bc, next, prev) {
|
|
538
|
+
if (changedOnly(next, prev, ['items'])) { bc.set(next.items || []); return true; }
|
|
539
|
+
return false;
|
|
540
|
+
},
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
/* ---------------- Skeleton ---------------- */
|
|
544
|
+
|
|
545
|
+
const SkeletonInner = mounted('Skeleton', (el, o) => {
|
|
546
|
+
const sk = C.skeleton(el, without(o, ['slots', 'loading']));
|
|
547
|
+
o.slots?.(slotMap(sk ? [['content', sk.content]] : []));
|
|
548
|
+
if (sk && o.loading === false) sk.reveal(undefined, { stagger: false });
|
|
549
|
+
return sk;
|
|
550
|
+
}, {
|
|
551
|
+
update(sk, next, prev) {
|
|
552
|
+
if (!changedOnly(next, prev, ['loading'])) return false;
|
|
553
|
+
if (next.loading === false) sk.reveal(); else sk.loading(true);
|
|
554
|
+
return true;
|
|
555
|
+
},
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* <Skeleton look="list" rows={4} loading={!riders}>{riders && <RiderList riders={riders} />}</Skeleton>
|
|
560
|
+
* Same as skeleton(el, options): grey placeholders in the shape of what is loading. Children are the real content;
|
|
561
|
+
* when `loading` turns false the placeholders fade row by row while the content rises into place (and back with true).
|
|
562
|
+
* ref → { busy, reveal(), loading(on), content, el }.
|
|
563
|
+
*/
|
|
564
|
+
export const Skeleton = forwardRef(function Skeleton({ children, loading = true, ...rest }, ref) {
|
|
565
|
+
const [slots, report] = useSlots();
|
|
566
|
+
const inner = useInnerRef(ref);
|
|
567
|
+
return h(Fragment, null,
|
|
568
|
+
h(SkeletonInner, { ...rest, loading: !!loading, slots: report, ref: inner }),
|
|
569
|
+
portals(slots, () => children ?? null));
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
/* ---------------- Alert ---------------- */
|
|
573
|
+
|
|
574
|
+
const AlertInner = mounted('Alert', (el, o) => {
|
|
575
|
+
const box = document.createElement('div');
|
|
576
|
+
box.className = 'al__node'; box.style.display = 'contents';
|
|
577
|
+
const a = C.alert(el, { ...takeTitle(without(o, ['slots', 'hasBody'])), body: o.hasBody ? box : '' });
|
|
578
|
+
note(a).box = box;
|
|
579
|
+
o.slots?.(slotMap([['body', box]]));
|
|
580
|
+
return a;
|
|
581
|
+
}, {
|
|
582
|
+
update(a, next, prev) {
|
|
583
|
+
if (!changedOnly(next, prev, ['tone', TITLE, 'icon', 'actions', 'open', 'hasBody'])) return false;
|
|
584
|
+
const patch = {};
|
|
585
|
+
['tone', 'icon', 'actions'].forEach((k) => { if (!same(next[k], prev[k])) patch[k] = next[k]; });
|
|
586
|
+
if (next[TITLE] !== prev[TITLE]) patch.title = next[TITLE] ?? '';
|
|
587
|
+
if (next.hasBody !== prev.hasBody) patch.body = next.hasBody ? note(a).box : '';
|
|
588
|
+
if (Object.keys(patch).length) a.update(patch);
|
|
589
|
+
if (next.open !== prev.open) { if (next.open === false) { if (a.open) a.close(); } else if (!a.open) a.show(); }
|
|
590
|
+
return true;
|
|
591
|
+
},
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* <Alert tone="bad" title="Couldn't save the booking" dismissible open={show} onClose={() => setShow(false)}
|
|
596
|
+
* actions={[{ label: 'Try again', primary: true, run: () => save() }]}>The bike is already booked on those days.</Alert>
|
|
597
|
+
* Same as alert(el, options); children are the message (vanilla `body`). tone, title, icon and actions change in
|
|
598
|
+
* place with update() (colours cross-fade, the icon spins, the text slides). open: false folds it away, true grows it back.
|
|
599
|
+
* onClose(a) after it closed (✕ or Esc). ref → { open, tone, update, close, show, shake, el }.
|
|
600
|
+
*/
|
|
601
|
+
export const Alert = forwardRef(function Alert({ children, title, ...rest }, ref) {
|
|
602
|
+
const [slots, report] = useSlots();
|
|
603
|
+
const inner = useInnerRef(ref);
|
|
604
|
+
const hasBody = children != null && children !== false && children !== '';
|
|
605
|
+
return h(Fragment, null,
|
|
606
|
+
h(AlertInner, { ...rest, [TITLE]: title, hasBody, slots: report, ref: inner }),
|
|
607
|
+
portals(slots, () => (hasBody ? children : null)));
|
|
608
|
+
});
|