@gecko-ui-kit/react 0.0.0-stage → 0.3.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 +1859 -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 +454 -0
- package/src/overlays.js +583 -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/controls.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/* Controls from @gecko-ui-kit/components (segmented, switch, checkbox, tabs, chips, slider, colour, search,
|
|
3
|
+
dropdown, code field) as React components.
|
|
4
|
+
Each one works controlled (value + onChange) or uncontrolled (defaultValue). React owns the state;
|
|
5
|
+
the vanilla motion helpers only animate (thumb, knob, pop). */
|
|
6
|
+
import { forwardRef, useRef, useEffect, useState } from 'react';
|
|
7
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
8
|
+
import * as C from '@gecko-ui-kit/components';
|
|
9
|
+
import * as O from '@gecko-ui-kit/overlays';
|
|
10
|
+
import { h, cx, iconNode, useControllable, useIsoLayoutEffect, useLatest } from './core.js';
|
|
11
|
+
|
|
12
|
+
const assignRef = (ref, el) => { if (typeof ref === 'function') ref(el); else if (ref) ref.current = el; };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* <Switch checked={on} onChange={setOn} label="Wi-Fi" /> (or defaultChecked)
|
|
16
|
+
* Same markup as sw(on, label): a button with role="switch"; the knob springs across.
|
|
17
|
+
* onChange(checked, event)
|
|
18
|
+
*/
|
|
19
|
+
export const Switch = forwardRef(function Switch({ checked, defaultChecked = false, onChange, label, className, onClick, ...rest }, ref) {
|
|
20
|
+
const [on, setOn] = useControllable(checked, defaultChecked, onChange);
|
|
21
|
+
const el = useRef(null), first = useRef(true);
|
|
22
|
+
useIsoLayoutEffect(() => { M.switchKnob(el.current, !!on, !first.current); first.current = false; }, [on]);
|
|
23
|
+
return h('button', {
|
|
24
|
+
ref: (n) => { el.current = n; assignRef(ref, n); }, type: 'button', role: 'switch', 'aria-checked': !!on, 'aria-label': label, ...rest,
|
|
25
|
+
className: cx('gk-switch', className),
|
|
26
|
+
onClick: (e) => { onClick?.(e); if (!e.defaultPrevented) setOn(!on, e); },
|
|
27
|
+
}, h('i'));
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* <Segmented value={view} onChange={setView} options={[['list', 'List', 'list'], ['grid', 'Grid', 'cards']]} />
|
|
32
|
+
* Same as seg(name, options, value, { size }): the thumb stretches to the pressed button.
|
|
33
|
+
* options: [id, label, icon?, count?] arrays, { value, label, icon, count } objects or plain strings. onChange(value, event)
|
|
34
|
+
*/
|
|
35
|
+
export const Segmented = forwardRef(function Segmented({ options = [], value, defaultValue, onChange, size, name, className, ...rest }, ref) {
|
|
36
|
+
const list = options.map(normOption);
|
|
37
|
+
const [v, setV] = useControllable(value, defaultValue ?? list[0]?.value, onChange);
|
|
38
|
+
const root = useRef(null), thumb = useRef(null), first = useRef(true);
|
|
39
|
+
const current = () => root.current?.querySelector('button[aria-pressed="true"]');
|
|
40
|
+
useIsoLayoutEffect(() => { M.indicator(thumb.current, current(), { animate: !first.current, radius: 'auto' }); first.current = false; }, [v, list.length]);
|
|
41
|
+
useEffect(() => M.trackIndicator(thumb.current, current, { radius: 'auto' }), []);
|
|
42
|
+
return h('div', { ref: (n) => { root.current = n; assignRef(ref, n); }, role: 'group', 'data-seg': name, ...rest, className: cx('gk-seg', size && 'gk-seg--' + size, className) },
|
|
43
|
+
h('span', { className: 'gk-seg__thumb', ref: thumb }),
|
|
44
|
+
list.map((o) => h('button', {
|
|
45
|
+
key: o.value, type: 'button', 'data-v': o.value, 'aria-pressed': o.value === v, disabled: o.disabled,
|
|
46
|
+
onClick: (e) => { if (o.value === v) return; M.pop(e.currentTarget); setV(o.value, e); },
|
|
47
|
+
}, iconNode(o.icon), o.label, o.count != null && o.count !== '' && h('span', { className: 'gk-seg__count' }, o.count))));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/** [id, label, icon] | { value, label, icon, disabled } | 'id' → { value, label, icon, disabled } */
|
|
51
|
+
export const normOption = (o) => Array.isArray(o) ? { value: o[0], label: o[1] ?? o[0], icon: o[2], count: o[3] }
|
|
52
|
+
: o && typeof o === 'object' ? { label: o.value, ...o } : { value: o, label: o };
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* <Checkbox checked={done} onChange={setDone} label="Helmet returned" /> (or defaultChecked)
|
|
56
|
+
* Same markup as chk(on, label): a button with role="checkbox" and a tick; it pops when ticked.
|
|
57
|
+
* onChange(checked, event)
|
|
58
|
+
*/
|
|
59
|
+
export const Checkbox = forwardRef(function Checkbox({ checked, defaultChecked = false, onChange, label, className, onClick, ...rest }, ref) {
|
|
60
|
+
const [on, setOn] = useControllable(checked, defaultChecked, onChange);
|
|
61
|
+
return h('button', {
|
|
62
|
+
ref, type: 'button', role: 'checkbox', 'aria-checked': !!on, 'aria-label': label, ...rest,
|
|
63
|
+
className: cx('gk-check', className),
|
|
64
|
+
onClick: (e) => { onClick?.(e); if (e.defaultPrevented) return; M.pop(e.currentTarget); setOn(!on, e); },
|
|
65
|
+
}, iconNode('check'));
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
/** [id, label, count] | { value, label, count, disabled } | 'id' */
|
|
69
|
+
const normTab = (o) => Array.isArray(o) ? { value: o[0], label: o[1] ?? o[0], count: o[2] }
|
|
70
|
+
: o && typeof o === 'object' ? { label: o.value, ...o } : { value: o, label: o };
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* <Tabs items={[['all', 'All bookings'], ['late', 'Late', 3]]} value={tab} onChange={setTab} />
|
|
74
|
+
* Same as tabs(items, value, { name }): tab buttons with a line that stretches to the selected one; a count shows as a badge.
|
|
75
|
+
* onChange(value, event)
|
|
76
|
+
*/
|
|
77
|
+
export const Tabs = forwardRef(function Tabs({ items = [], value, defaultValue, onChange, name = 'tabs', className, ...rest }, ref) {
|
|
78
|
+
const list = items.map(normTab);
|
|
79
|
+
const [v, setV] = useControllable(value, defaultValue ?? list[0]?.value, onChange);
|
|
80
|
+
const root = useRef(null), line = useRef(null), first = useRef(true);
|
|
81
|
+
const current = () => root.current?.querySelector('[aria-selected="true"]');
|
|
82
|
+
useIsoLayoutEffect(() => { M.indicator(line.current, current(), { animate: !first.current, radius: 2, line: true }); first.current = false; }, [v, list.length]);
|
|
83
|
+
useEffect(() => M.trackIndicator(line.current, current, { radius: 2, line: true }), []);
|
|
84
|
+
return h('div', { ref: (n) => { root.current = n; assignRef(ref, n); }, role: 'tablist', 'data-tabs': name, ...rest, className: cx('gk-tabs', className) },
|
|
85
|
+
h('span', { className: 'gk-tabs__line', ref: line }),
|
|
86
|
+
list.map((o) => h('button', {
|
|
87
|
+
key: o.value, type: 'button', role: 'tab', 'data-v': o.value, 'aria-selected': o.value === v, disabled: o.disabled,
|
|
88
|
+
onClick: (e) => { if (o.value !== v) setV(o.value, e); },
|
|
89
|
+
}, o.label, o.count ? h('span', { className: 'gk-badge' }, o.count) : null)));
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* <Chips options={[['all', 'All'], ['scooter', 'Scooters', 'moto']]} value={kind} onChange={setKind} />
|
|
94
|
+
* <Chips multi options={…} value={['a', 'b']} onChange={setPicked} />
|
|
95
|
+
* Same as chips(items, value, { name, multi }): pill buttons; one picked, or several with multi. onChange(value | values, event)
|
|
96
|
+
*/
|
|
97
|
+
export const Chips = forwardRef(function Chips({ options = [], value, defaultValue, onChange, multi = false, name = 'chips', className, ...rest }, ref) {
|
|
98
|
+
const list = options.map(normOption);
|
|
99
|
+
const [v, setV] = useControllable(value, defaultValue ?? (multi ? [] : list[0]?.value), onChange);
|
|
100
|
+
const isOn = (id) => (multi ? (v || []).includes(id) : id === v);
|
|
101
|
+
return h('div', { ref, 'data-chips': name, ...(multi ? { 'data-multi': '' } : null), ...rest, className: cx('gk-chips', className) },
|
|
102
|
+
list.map((o) => h('button', {
|
|
103
|
+
key: o.value, type: 'button', className: 'gk-chip', 'data-v': o.value, 'aria-pressed': isOn(o.value), disabled: o.disabled,
|
|
104
|
+
onClick: (e) => {
|
|
105
|
+
if (multi) { const cur = v || []; M.pop(e.currentTarget); setV(isOn(o.value) ? cur.filter((x) => x !== o.value) : list.map((x) => x.value).filter((x) => x === o.value || cur.includes(x)), e); return; }
|
|
106
|
+
if (o.value === v) return; M.pop(e.currentTarget); setV(o.value, e);
|
|
107
|
+
},
|
|
108
|
+
}, iconNode(o.icon), o.label)));
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* <Range min={1} max={14} value={days} onChange={setDays} />
|
|
113
|
+
* Same as range({ min, max, step, value }): a slider with a filled track (it fills up from zero when first shown)
|
|
114
|
+
* and a springy thumb. onChange(value: number, event)
|
|
115
|
+
*/
|
|
116
|
+
export const Range = forwardRef(function Range({ value, defaultValue, onChange, min = 0, max = 100, step = 1, className, style, ...rest }, ref) {
|
|
117
|
+
const [v, setV] = useControllable(value, defaultValue ?? Math.round((+min + +max) / 2), onChange);
|
|
118
|
+
const el = useRef(null);
|
|
119
|
+
const p = ((+v - +min) / ((+max - +min) || 1)) * 100 + '%';
|
|
120
|
+
useIsoLayoutEffect(() => {
|
|
121
|
+
const r = el.current; if (!r || M.RM()) return;
|
|
122
|
+
const tw = M.g.fromTo(r, { '--gk-p': '0%' }, { '--gk-p': r.style.getPropertyValue('--gk-p'), duration: 0.9, ease: 'expo.out' });
|
|
123
|
+
return () => { tw.kill(); };
|
|
124
|
+
}, []);
|
|
125
|
+
useIsoLayoutEffect(() => { const r = el.current; if (!r) return; M.g.killTweensOf(r); r.style.setProperty('--gk-p', p); }, [p]);
|
|
126
|
+
return h('input', {
|
|
127
|
+
ref: (n) => { el.current = n; assignRef(ref, n); }, type: 'range', min, max, step, ...rest, value: v,
|
|
128
|
+
className: cx('gk-range', className), style: { ...style, '--gk-p': p },
|
|
129
|
+
onChange: (e) => { M.g.killTweensOf(e.currentTarget); setV(+e.currentTarget.value, e); },
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* <Swatch value={colour} onChange={setColour} aria-label="Label colour" />
|
|
135
|
+
* Same as swatch(value): a colour picker drawn as a round swatch; it pops when a colour is picked.
|
|
136
|
+
* onChange(value: '#rrggbb', event) runs while the colour changes.
|
|
137
|
+
*/
|
|
138
|
+
export const Swatch = forwardRef(function Swatch({ value, defaultValue = '#5b4cf0', onChange, className, ...rest }, ref) {
|
|
139
|
+
const [v, setV] = useControllable(value, defaultValue, onChange);
|
|
140
|
+
const el = useRef(null);
|
|
141
|
+
useEffect(() => { const s = el.current; const pop = () => M.pop(s); s.addEventListener('change', pop); return () => s.removeEventListener('change', pop); }, []);
|
|
142
|
+
return h('input', {
|
|
143
|
+
ref: (n) => { el.current = n; assignRef(ref, n); }, type: 'color', ...rest, value: v,
|
|
144
|
+
className: cx('gk-swatch', className), onChange: (e) => setV(e.currentTarget.value, e),
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* <SearchBox placeholder="Search bikes…" value={q} onChange={setQ} onSearch={run} />
|
|
150
|
+
* Same as searchbox(placeholder): a field with a magnifying glass. onSearch(text, event) runs on Enter
|
|
151
|
+
* (an empty box shakes instead). className / style go on the <label>; every other prop goes on the <input>. ref → the <input>.
|
|
152
|
+
*/
|
|
153
|
+
export const SearchBox = forwardRef(function SearchBox({ placeholder = 'Search…', value, defaultValue = '', onChange, onSearch, onKeyDown, size, className, style, ...rest }, ref) {
|
|
154
|
+
const [v, setV] = useControllable(value, defaultValue, onChange);
|
|
155
|
+
const box = useRef(null);
|
|
156
|
+
return h('label', { ref: box, className: cx('gk-searchbox', className), style },
|
|
157
|
+
iconNode('search'),
|
|
158
|
+
h('input', {
|
|
159
|
+
ref, 'aria-label': placeholder, ...rest, placeholder, value: v ?? '',
|
|
160
|
+
className: cx('gk-input', size && 'gk-input--' + size),
|
|
161
|
+
onChange: (e) => setV(e.currentTarget.value, e),
|
|
162
|
+
onKeyDown: (e) => {
|
|
163
|
+
onKeyDown?.(e);
|
|
164
|
+
if (e.defaultPrevented || e.key !== 'Enter' || !onSearch) return;
|
|
165
|
+
const text = e.currentTarget.value.trim();
|
|
166
|
+
if (text) onSearch(text, e); else M.shake(box.current);
|
|
167
|
+
},
|
|
168
|
+
}));
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/* ---------------- dropdown (Select) ---------------- */
|
|
172
|
+
const normSelect = (o) => (o && typeof o === 'object' && !Array.isArray(o) && Array.isArray(o.options) ? { group: o.group ?? o.label, options: o.options.map(normOption) } : normOption(o));
|
|
173
|
+
const firstValue = (list) => { for (const o of list) { if (o.options) { const f = o.options.find((x) => !x.disabled); if (f) return f.value; } else if (!o.disabled) return o.value; } return ''; };
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* <Select options={[['scooter', 'Scooter 125', 'moto'], ['ebike', 'E-bike', 'bike']]} value={bike} onChange={setBike} />
|
|
177
|
+
* A real <select class="gk-input"> (forms, FormData and labels keep working) that dropdown() from
|
|
178
|
+
* @gecko-ui-kit/overlays turns into the animated dropdown: the list pops out of the button, a highlight follows
|
|
179
|
+
* the mouse and ↑ ↓, and the chosen text rolls in. Options: [value, label, icon?], { value, label, icon, disabled },
|
|
180
|
+
* 'value', or a group { group: 'Bikes', options: [...] }. onChange(value, event). ref → the <select>.
|
|
181
|
+
*/
|
|
182
|
+
export const Select = forwardRef(function Select({ options = [], value, defaultValue, onChange, size, className, tabIndex, ...rest }, ref) {
|
|
183
|
+
const list = options.map(normSelect);
|
|
184
|
+
const [v, setV] = useControllable(value, defaultValue ?? firstValue(list), onChange);
|
|
185
|
+
const sel = useRef(null), dd = useRef(null);
|
|
186
|
+
const [up, setUp] = useState(false);
|
|
187
|
+
useIsoLayoutEffect(() => {
|
|
188
|
+
dd.current = O.dropdown(sel.current, { size }); setUp(!!dd.current);
|
|
189
|
+
return () => { dd.current?.close(); };
|
|
190
|
+
}, []);
|
|
191
|
+
useIsoLayoutEffect(() => { dd.current?.sync(); });
|
|
192
|
+
const opt = (o) => h('option', { key: o.value, value: o.value, disabled: o.disabled, 'data-icon': typeof o.icon === 'string' ? o.icon : undefined }, o.label);
|
|
193
|
+
return h('span', { className: 'gk-dd-host', style: { display: 'contents' } },
|
|
194
|
+
h('select', {
|
|
195
|
+
ref: (n) => { sel.current = n; assignRef(ref, n); }, ...rest, value: v ?? '',
|
|
196
|
+
tabIndex: up ? -1 : tabIndex, 'aria-hidden': up || undefined,
|
|
197
|
+
className: cx('gk-input', size && 'gk-input--' + size, up && 'gk-dd__native', className),
|
|
198
|
+
onChange: (e) => setV(e.currentTarget.value, e),
|
|
199
|
+
}, list.map((o, i) => (o.options ? h('optgroup', { key: 'g' + i, label: o.group }, o.options.map(opt)) : opt(o)))));
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
/* ---------------- code field (Otp) ---------------- */
|
|
203
|
+
const cleanCode = (s, letters) => (letters ? String(s ?? '').toUpperCase().replace(/[^A-Z0-9]/g, '') : String(s ?? '').replace(/\D/g, ''));
|
|
204
|
+
const ANSWER = { true: 'good', good: 'good', false: 'bad', bad: 'bad' };
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* <Otp length={6} split onComplete={async (code) => await verify(code)} />
|
|
208
|
+
* Same as otp({ length, split, mask, letters, label, name }) wired by bindOtp: one box per character, typing moves on,
|
|
209
|
+
* Backspace goes back, paste / the phone's code autofill fills every box. onComplete(code, el) runs when the last box
|
|
210
|
+
* is filled; return (or resolve) true / 'good' to play the green wave (and gather into a tick), false / 'bad' to shake
|
|
211
|
+
* and clear. onChange(code) runs as it is typed; value / defaultValue fill the boxes. ref → the .gk-otp element
|
|
212
|
+
* (otpState / otpClear from @gecko-ui-kit/components work on it).
|
|
213
|
+
*/
|
|
214
|
+
export const Otp = forwardRef(function Otp({ length = 6, split = false, mask = false, letters = false, label = 'Verification code', name = '', value, defaultValue, onChange, onComplete, gather = true, clear = true, disabled, autoFocus, className, ...rest }, ref) {
|
|
215
|
+
const el = useRef(null), last = useRef(''), cb = useLatest({ onChange, onComplete, gather, clear });
|
|
216
|
+
const write = (code) => {
|
|
217
|
+
const box = el.current; if (!box) return;
|
|
218
|
+
const chars = [...cleanCode(code, letters)];
|
|
219
|
+
box.querySelectorAll('.gk-otp__box').forEach((b, i) => { b.value = chars[i] ?? ''; b.classList.toggle('gk-is-filled', !!b.value); });
|
|
220
|
+
last.current = C.otpValue(box);
|
|
221
|
+
};
|
|
222
|
+
useIsoLayoutEffect(() => {
|
|
223
|
+
const box = el.current;
|
|
224
|
+
if (value === undefined && defaultValue) write(defaultValue);
|
|
225
|
+
let alive = true, tl = null;
|
|
226
|
+
const report = () => { if (!alive) return; const code = C.otpValue(box); if (code !== last.current) { last.current = code; cb.current.onChange?.(code); } };
|
|
227
|
+
const done = (code, node) => {
|
|
228
|
+
report();
|
|
229
|
+
Promise.resolve(cb.current.onComplete?.(code, node)).then((res) => {
|
|
230
|
+
const state = ANSWER[String(res)]; if (!alive || !state) return;
|
|
231
|
+
tl = C.otpState(node, state, { gather: cb.current.gather, clear: cb.current.clear });
|
|
232
|
+
tl.eventCallback('onComplete', report);
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
C.bindOtp(box, done);
|
|
236
|
+
const evs = ['input', 'paste', 'keydown'];
|
|
237
|
+
evs.forEach((t) => box.addEventListener(t, report));
|
|
238
|
+
if (autoFocus) box.querySelector('.gk-otp__box')?.focus();
|
|
239
|
+
return () => {
|
|
240
|
+
alive = false; tl?.kill();
|
|
241
|
+
const i = box.__otpDone?.indexOf(done); if (i > -1) box.__otpDone.splice(i, 1);
|
|
242
|
+
evs.forEach((t) => box.removeEventListener(t, report));
|
|
243
|
+
};
|
|
244
|
+
}, []);
|
|
245
|
+
useIsoLayoutEffect(() => { if (value !== undefined && cleanCode(value, letters) !== C.otpValue(el.current)) write(value); }, [value]);
|
|
246
|
+
const half = split ? Math.ceil(length / 2) : -1;
|
|
247
|
+
const boxes = [];
|
|
248
|
+
for (let i = 0; i < length; i++) {
|
|
249
|
+
if (i === half) boxes.push(h('span', { key: 'dash', className: 'gk-otp__dash', 'aria-hidden': true }));
|
|
250
|
+
boxes.push(h('input', {
|
|
251
|
+
key: i, className: 'gk-otp__box', type: mask ? 'password' : 'text', inputMode: letters ? 'text' : 'numeric', pattern: letters ? undefined : '[0-9]*',
|
|
252
|
+
autoComplete: i === 0 ? 'one-time-code' : 'off', autoCapitalize: 'characters', spellCheck: false, disabled,
|
|
253
|
+
'aria-label': `${label}, ${i + 1} of ${length}`,
|
|
254
|
+
}));
|
|
255
|
+
}
|
|
256
|
+
return h('div', {
|
|
257
|
+
ref: (n) => { el.current = n; assignRef(ref, n); }, role: 'group', 'aria-label': label, 'data-otp': name || 'code', 'data-len': length,
|
|
258
|
+
...(letters ? { 'data-letters': '' } : null), ...rest, className: cx('gk-otp', letters && 'gk-otp--letters', className),
|
|
259
|
+
}, boxes, h('span', { className: 'gk-otp__done', 'aria-hidden': true },
|
|
260
|
+
h('svg', { viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 2.6, strokeLinecap: 'round', strokeLinejoin: 'round' }, h('path', { d: 'M5.5 12.5l4 4L18.5 7.5' }))));
|
|
261
|
+
});
|
package/src/core.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
|
|
3
|
+
/** Join class names, skipping empty ones. */
|
|
4
|
+
export function cx(...names: unknown[]): string;
|
|
5
|
+
export function useLatest<T>(value: T): React.MutableRefObject<T>;
|
|
6
|
+
/** Controlled (value) or uncontrolled (defaultValue) state, like React's own inputs. */
|
|
7
|
+
export function useControllable<T>(value: T | undefined, defaultValue: T | undefined, onChange?: (value: T, ...extra: any[]) => void): [T, (next: T, ...extra: any[]) => void, boolean];
|
|
8
|
+
export const useIsoLayoutEffect: typeof React.useLayoutEffect;
|
|
9
|
+
/** Same data by content (functions always count as the same). */
|
|
10
|
+
export function same(a: unknown, b: unknown): boolean;
|
|
11
|
+
export function changedOnly(next: object, prev: object, keys: string[]): boolean;
|
|
12
|
+
/** Stop what a vanilla call returned: destroy(), revert(), kill(), stop() or a stop function. */
|
|
13
|
+
export function stop(handle: unknown): void;
|
|
14
|
+
export function isHostProp(name: string): boolean;
|
|
15
|
+
export function splitProps<P extends object>(props: P, extraHost?: string[]): [Record<string, unknown>, Record<string, unknown>];
|
|
16
|
+
export const h: typeof React.createElement;
|
|
17
|
+
|
|
18
|
+
/** Props every mounted component accepts besides its own options: they go on its element. */
|
|
19
|
+
export interface HostProps {
|
|
20
|
+
className?: string;
|
|
21
|
+
style?: React.CSSProperties;
|
|
22
|
+
id?: string;
|
|
23
|
+
title?: string;
|
|
24
|
+
role?: string;
|
|
25
|
+
tabIndex?: number;
|
|
26
|
+
hidden?: boolean;
|
|
27
|
+
lang?: string;
|
|
28
|
+
dir?: string;
|
|
29
|
+
[attr: `aria-${string}`]: string | number | boolean | undefined;
|
|
30
|
+
[attr: `data-${string}`]: string | number | boolean | undefined;
|
|
31
|
+
}
|
|
32
|
+
/** What a ref on a mounted component gives you: the vanilla handle's methods and values, plus `el`. */
|
|
33
|
+
export type Mounted<H> = H & { readonly el: HTMLElement };
|
|
34
|
+
|
|
35
|
+
export interface MountedSpec<O> {
|
|
36
|
+
tag?: string;
|
|
37
|
+
options?: (props: any, all: any) => O;
|
|
38
|
+
prepare?: (el: HTMLElement, options: O) => void;
|
|
39
|
+
update?: (handle: any, next: O, prev: O, el: HTMLElement) => boolean;
|
|
40
|
+
hostProps?: string[];
|
|
41
|
+
}
|
|
42
|
+
/** Turn mount(el, options) → handle into a React component. */
|
|
43
|
+
export function mounted<P extends object, H = any>(displayName: string, mount: (el: HTMLElement, options: any) => H, spec?: MountedSpec<any>):
|
|
44
|
+
React.ForwardRefExoticComponent<React.PropsWithoutRef<P & HostProps> & React.RefAttributes<Mounted<H>>>;
|
|
45
|
+
|
|
46
|
+
export interface EnhancerSpec {
|
|
47
|
+
tag?: string;
|
|
48
|
+
options?: string[];
|
|
49
|
+
defaults?: Record<string, unknown>;
|
|
50
|
+
update?: (handle: any, next: any, prev: any, el: HTMLElement) => boolean;
|
|
51
|
+
rerunOnChildren?: boolean;
|
|
52
|
+
}
|
|
53
|
+
/** Props of an enhancer component: its options, `as`, children and any prop of the element. */
|
|
54
|
+
export type EnhancerProps<O> = O & Omit<React.HTMLAttributes<HTMLElement>, keyof O> & { as?: React.ElementType; children?: React.ReactNode };
|
|
55
|
+
/** Turn a motion fn(el, options) into a wrapper component around children. */
|
|
56
|
+
export function enhancer<O extends object>(displayName: string, fn: (el: HTMLElement, options: O) => unknown, spec?: EnhancerSpec):
|
|
57
|
+
React.ForwardRefExoticComponent<React.PropsWithoutRef<EnhancerProps<O>> & React.RefAttributes<HTMLElement>>;
|
|
58
|
+
|
|
59
|
+
/** Run a motion helper on an element you render: const ref = useMotion(tilt, { max: 12 }). */
|
|
60
|
+
export function useMotion<E extends HTMLElement = HTMLElement, O = any>(fn: (el: E, options: O) => unknown, options?: O, deps?: React.DependencyList): React.MutableRefObject<E | null>;
|
|
61
|
+
export function useMotion<E extends HTMLElement = HTMLElement>(fn: (el: E) => unknown, deps: React.DependencyList): React.MutableRefObject<E | null>;
|
|
62
|
+
|
|
63
|
+
export interface IconProps extends Omit<React.SVGProps<SVGSVGElement>, 'name' | 'stroke'> {
|
|
64
|
+
/** An icon name from @gecko-ui-kit/icons (ICONS lists them). Unknown names show 'sparkle'. */
|
|
65
|
+
name: string;
|
|
66
|
+
size?: number;
|
|
67
|
+
stroke?: number;
|
|
68
|
+
}
|
|
69
|
+
/** <Icon name="search" /> — 24×24 stroke icon as an <svg class="gk-ico">. */
|
|
70
|
+
export function Icon(props: IconProps): React.ReactElement;
|
|
71
|
+
/** An icon name or any React node. */
|
|
72
|
+
export type IconLike = string | React.ReactNode;
|
|
73
|
+
export function iconNode(icon: IconLike, props?: Partial<IconProps>): React.ReactNode;
|
|
74
|
+
|
|
75
|
+
/** A date as a Date or an ISO string ('2026-12-04' or '2026-12-04T10:00'). */
|
|
76
|
+
export type DateLike = Date | string;
|
|
77
|
+
|
|
78
|
+
export {};
|
package/src/core.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/* The shared parts every React component in this package is built from.
|
|
3
|
+
|
|
4
|
+
The library's pieces come in three kinds, and each has a helper here:
|
|
5
|
+
|
|
6
|
+
1. Markup pieces (button, tag, switch, tabs…): written as real React components that draw the same HTML
|
|
7
|
+
as the vanilla builders, so the same CSS styles them. Small animations run in an effect (see Switch).
|
|
8
|
+
2. Mounted pieces (rating, data grid, calendar…): the vanilla code builds and owns everything inside one
|
|
9
|
+
element. `mounted(name, mount, spec)` turns such a function into a component: it mounts into an element
|
|
10
|
+
React never touches, passes the props as options, keeps callbacks fresh, applies prop changes in place
|
|
11
|
+
when it can (spec.update) or mounts again, and calls destroy() when the component goes away.
|
|
12
|
+
3. Enhancers (tilt, spotlight, magnetic…): a motion that adds itself to elements you already have.
|
|
13
|
+
`enhancer(name, fn, spec)` renders your children inside one element and runs the motion on it.
|
|
14
|
+
|
|
15
|
+
Nothing here touches `document` or `window` while rendering, so every component renders on the server
|
|
16
|
+
(Next.js) and wakes up in the browser. */
|
|
17
|
+
import { PATHS } from '@gecko-ui-kit/icons';
|
|
18
|
+
import { createElement as h, forwardRef, useEffect, useLayoutEffect, useRef, useImperativeHandle, useState, useCallback } from 'react';
|
|
19
|
+
|
|
20
|
+
export { h };
|
|
21
|
+
/** useLayoutEffect in the browser, useEffect on the server (no warning during server rendering). */
|
|
22
|
+
export const useIsoLayoutEffect = typeof document !== 'undefined' ? useLayoutEffect : useEffect;
|
|
23
|
+
/** Join class names, skipping empty ones: cx('btn', on && 'is-on', className). */
|
|
24
|
+
export const cx = (...a) => a.flat(Infinity).filter(Boolean).join(' ');
|
|
25
|
+
|
|
26
|
+
/** A ref that always holds the latest value (for callbacks the vanilla code calls later). */
|
|
27
|
+
export function useLatest(v) { const r = useRef(v); r.current = v; return r; }
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Controlled or uncontrolled value, like React's own inputs:
|
|
31
|
+
* pass `value` (+ onChange) to control it, or `defaultValue` to let the component keep it.
|
|
32
|
+
* const [v, setV] = useControllable(props.value, props.defaultValue, props.onChange)
|
|
33
|
+
*/
|
|
34
|
+
export function useControllable(value, defaultValue, onChange) {
|
|
35
|
+
const controlled = value !== undefined;
|
|
36
|
+
const [inner, setInner] = useState(defaultValue);
|
|
37
|
+
const cb = useLatest(onChange);
|
|
38
|
+
const set = useCallback((next, ...extra) => {
|
|
39
|
+
if (!controlled) setInner(next);
|
|
40
|
+
cb.current?.(next, ...extra);
|
|
41
|
+
}, [controlled]);
|
|
42
|
+
return [controlled ? value : inner, set, controlled];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Deep "same data" test: plain objects, arrays, dates and primitives by content; functions always count
|
|
46
|
+
as the same (they are called through the latest props); anything else (DOM nodes, Files) by identity. */
|
|
47
|
+
export function same(a, b) {
|
|
48
|
+
if (Object.is(a, b)) return true;
|
|
49
|
+
if (typeof a === 'function' && typeof b === 'function') return true;
|
|
50
|
+
if (!a || !b || typeof a !== 'object' || typeof b !== 'object') return false;
|
|
51
|
+
if (a instanceof Date || b instanceof Date) return a instanceof Date && b instanceof Date && +a === +b;
|
|
52
|
+
if (Array.isArray(a)) return Array.isArray(b) && a.length === b.length && a.every((x, i) => same(x, b[i]));
|
|
53
|
+
if (Object.getPrototypeOf(a) !== Object.prototype || Object.getPrototypeOf(b) !== Object.prototype) return false;
|
|
54
|
+
const ka = Object.keys(a), kb = Object.keys(b);
|
|
55
|
+
return ka.length === kb.length && ka.every((k) => Object.prototype.hasOwnProperty.call(b, k) && same(a[k], b[k]));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/* Props that belong on the element itself, not in the options. */
|
|
59
|
+
const HOST = new Set(['className', 'style', 'id', 'title', 'role', 'tabIndex', 'hidden', 'lang', 'dir']);
|
|
60
|
+
export const isHostProp = (k) => HOST.has(k) || k.startsWith('aria-') || k.startsWith('data-');
|
|
61
|
+
|
|
62
|
+
/** Split props into [hostProps, options]. children and key/ref are left out of both. */
|
|
63
|
+
export function splitProps(props, extraHost = []) {
|
|
64
|
+
const host = {}, opts = {};
|
|
65
|
+
for (const k in props) {
|
|
66
|
+
if (k === 'children') continue;
|
|
67
|
+
if (isHostProp(k) || extraHost.includes(k)) host[k] = props[k]; else opts[k] = props[k];
|
|
68
|
+
}
|
|
69
|
+
return [host, opts];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/* Options with every function swapped for a stable caller of the latest prop, so a new arrow function
|
|
73
|
+
on each render does not mount the piece again and the piece never calls an old one. */
|
|
74
|
+
function liveOptions(opts, latest) {
|
|
75
|
+
const out = {};
|
|
76
|
+
for (const k in opts) {
|
|
77
|
+
const v = opts[k];
|
|
78
|
+
out[k] = typeof v === 'function' ? (...a) => { const f = latest.current[k]; return typeof f === 'function' ? f(...a) : undefined; } : v;
|
|
79
|
+
}
|
|
80
|
+
return out;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Stop whatever a vanilla call returned: a handle with destroy(), a GSAP tween/timeline, a stop function. */
|
|
84
|
+
export function stop(x) {
|
|
85
|
+
if (!x) return;
|
|
86
|
+
if (typeof x === 'function') { x(); return; }
|
|
87
|
+
if (typeof x.destroy === 'function') x.destroy();
|
|
88
|
+
else if (typeof x.revert === 'function') x.revert();
|
|
89
|
+
else if (typeof x.kill === 'function') x.kill();
|
|
90
|
+
else if (typeof x.stop === 'function') x.stop();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* Keep host props (class, style, aria-…) in step on an element React does not render. */
|
|
94
|
+
function applyHost(el, next, prev = {}) {
|
|
95
|
+
const pc = (prev.className || '').split(/\s+/).filter(Boolean), nc = (next.className || '').split(/\s+/).filter(Boolean);
|
|
96
|
+
pc.filter((c) => !nc.includes(c)).forEach((c) => el.classList.remove(c));
|
|
97
|
+
nc.forEach((c) => el.classList.add(c));
|
|
98
|
+
const ps = prev.style || {}, ns = next.style || {};
|
|
99
|
+
for (const k in ps) if (!(k in ns)) el.style[k] = '';
|
|
100
|
+
for (const k in ns) { const v = ns[k]; el.style[k] = typeof v === 'number' && !/^(opacity|zIndex|flex|flexGrow|flexShrink|order|lineHeight|fontWeight)$/.test(k) ? v + 'px' : (v ?? ''); }
|
|
101
|
+
for (const k in prev) if (k !== 'className' && k !== 'style' && !(k in next)) el.removeAttribute(attrName(k));
|
|
102
|
+
for (const k in next) {
|
|
103
|
+
if (k === 'className' || k === 'style') continue;
|
|
104
|
+
const v = next[k], n = attrName(k);
|
|
105
|
+
if (v === false || v == null) el.removeAttribute(n); else el.setAttribute(n, v === true ? '' : String(v));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const attrName = (k) => (k === 'tabIndex' ? 'tabindex' : k);
|
|
109
|
+
|
|
110
|
+
/* A ref value that stays the same object for the component's life and forwards to the live handle. */
|
|
111
|
+
function handleProxy(cur) {
|
|
112
|
+
return new Proxy({}, {
|
|
113
|
+
get(_, k) {
|
|
114
|
+
const c = cur.current; if (!c) return undefined;
|
|
115
|
+
if (k === 'el') return c.el;
|
|
116
|
+
const x = c.handle; if (x == null || typeof x !== 'object' && typeof x !== 'function') return undefined;
|
|
117
|
+
const v = x[k]; return typeof v === 'function' ? v.bind(x) : v;
|
|
118
|
+
},
|
|
119
|
+
has(_, k) { const c = cur.current; return !!c && (k === 'el' || (c.handle != null && k in Object(c.handle))); },
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Turn a vanilla mount function `mount(el, options) → handle` into a React component.
|
|
125
|
+
*
|
|
126
|
+
* export const Rating = mounted('Rating', rating, {
|
|
127
|
+
* update(handle, next, prev) { // optional: apply changed props in place
|
|
128
|
+
* if (changedOnly(next, prev, ['value'])) { handle.set(next.value); return true; }
|
|
129
|
+
* return false; // anything else: mount again
|
|
130
|
+
* },
|
|
131
|
+
* });
|
|
132
|
+
* <Rating value={3} onChange={setScore} className="mb-2" ref={r} /> r.current.set(4), r.current.el
|
|
133
|
+
*
|
|
134
|
+
* spec:
|
|
135
|
+
* tag element to mount into (default 'div')
|
|
136
|
+
* options (props) → the options object to pass (default: every prop that is not a host prop)
|
|
137
|
+
* prepare (el, options) → void: fill the element before mounting (e.g. markup the piece expects)
|
|
138
|
+
* update (handle, nextOptions, prevOptions, el) → true when the change was applied in place
|
|
139
|
+
* hostProps extra prop names that go on the element instead of the options
|
|
140
|
+
* Functions in the props are always called through the latest props (no remount when they change).
|
|
141
|
+
* Functions nested inside data (columns[].render) are taken from the props of the latest mount or update.
|
|
142
|
+
*/
|
|
143
|
+
export function mounted(displayName, mount, spec = {}) {
|
|
144
|
+
const { tag = 'div', options: toOptions, prepare, update, hostProps = [] } = spec;
|
|
145
|
+
const C = forwardRef(function GeckoMounted(props, ref) {
|
|
146
|
+
const slot = useRef(null), cur = useRef(null);
|
|
147
|
+
const [host, rawOpts] = splitProps(props, hostProps);
|
|
148
|
+
const opts = toOptions ? toOptions(rawOpts, props) : rawOpts;
|
|
149
|
+
const latest = useLatest(opts);
|
|
150
|
+
const hostRef = useRef(host);
|
|
151
|
+
const proxy = useRef(null); if (!proxy.current) proxy.current = handleProxy(cur);
|
|
152
|
+
useImperativeHandle(ref, () => proxy.current, []);
|
|
153
|
+
|
|
154
|
+
const create = () => {
|
|
155
|
+
const el = document.createElement(tag);
|
|
156
|
+
applyHost(el, hostRef.current);
|
|
157
|
+
slot.current.appendChild(el);
|
|
158
|
+
const o = liveOptions(latest.current, latest);
|
|
159
|
+
prepare?.(el, o);
|
|
160
|
+
cur.current = { el, handle: null, opts: latest.current };
|
|
161
|
+
cur.current.handle = mount(el, o) ?? null;
|
|
162
|
+
};
|
|
163
|
+
const destroy = () => {
|
|
164
|
+
const c = cur.current; if (!c) return; cur.current = null;
|
|
165
|
+
try { stop(c.handle); } finally { c.el.remove(); }
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
useIsoLayoutEffect(() => { create(); return destroy; }, []);
|
|
169
|
+
useIsoLayoutEffect(() => {
|
|
170
|
+
const c = cur.current; if (!c) return;
|
|
171
|
+
if (!same(host, hostRef.current)) { applyHost(c.el, host, hostRef.current); hostRef.current = host; }
|
|
172
|
+
const prev = c.opts, next = latest.current;
|
|
173
|
+
const fnsNow = Object.keys(next).filter((k) => typeof next[k] === 'function').sort().join();
|
|
174
|
+
const fnsBefore = Object.keys(prev).filter((k) => typeof prev[k] === 'function').sort().join();
|
|
175
|
+
if (fnsNow === fnsBefore && same(next, prev)) return;
|
|
176
|
+
if (fnsNow === fnsBefore && update && update(c.handle, next, prev, c.el) === true) { c.opts = next; return; }
|
|
177
|
+
destroy(); create();
|
|
178
|
+
});
|
|
179
|
+
return h('div', { ref: slot, style: { display: 'contents' }, 'data-gecko': displayName, suppressHydrationWarning: true });
|
|
180
|
+
});
|
|
181
|
+
C.displayName = displayName;
|
|
182
|
+
return C;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** True when only these keys changed between two option objects. */
|
|
186
|
+
export function changedOnly(next, prev, keys) {
|
|
187
|
+
const all = new Set([...Object.keys(next), ...Object.keys(prev)]);
|
|
188
|
+
let any = false;
|
|
189
|
+
for (const k of all) {
|
|
190
|
+
if (same(next[k], prev[k])) continue;
|
|
191
|
+
if (!keys.includes(k)) return false;
|
|
192
|
+
any = true;
|
|
193
|
+
}
|
|
194
|
+
return any;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Turn a motion `fn(el, options) → handle | tween | stop()` into a wrapper component around children.
|
|
199
|
+
*
|
|
200
|
+
* export const Tilt = enhancer('Tilt', tilt, { options: ['max', 'scale', 'glare', 'perspective'] });
|
|
201
|
+
* <Tilt max={10} className="gk-card"><h3>Lamp</h3></Tilt>
|
|
202
|
+
*
|
|
203
|
+
* spec:
|
|
204
|
+
* tag element to render (default 'div'; the `as` prop overrides it)
|
|
205
|
+
* options the prop names that are options for fn (all other props go on the element)
|
|
206
|
+
* update (handle, next, prev, el) → true when the change was applied in place; else it runs again
|
|
207
|
+
* rerunOnChildren run fn again when the children change (for motions that read the text, e.g. split)
|
|
208
|
+
*/
|
|
209
|
+
export function enhancer(displayName, fn, spec = {}) {
|
|
210
|
+
const { tag = 'div', options: keys = [], update, rerunOnChildren = false, defaults = {} } = spec;
|
|
211
|
+
const C = forwardRef(function GeckoEnhancer({ as, children, ...props }, ref) {
|
|
212
|
+
const el = useRef(null), cur = useRef(null);
|
|
213
|
+
const opts = { ...defaults }, dom = {};
|
|
214
|
+
for (const k in props) (keys.includes(k) ? opts : dom)[k] = props[k];
|
|
215
|
+
const latest = useLatest(opts);
|
|
216
|
+
useImperativeHandle(ref, () => el.current, []);
|
|
217
|
+
const childKey = rerunOnChildren ? childrenKey(children) : '';
|
|
218
|
+
// React rewrites the whole class attribute when className changes, which would drop classes the motion
|
|
219
|
+
// added (is-lit, is-armed…). Note them before the commit and put them back after it.
|
|
220
|
+
const lastClass = useRef(dom.className), kept = useRef(null);
|
|
221
|
+
if (dom.className !== lastClass.current && el.current) {
|
|
222
|
+
const was = new Set(String(lastClass.current || '').split(/\s+/));
|
|
223
|
+
kept.current = [...el.current.classList].filter((c) => !was.has(c));
|
|
224
|
+
}
|
|
225
|
+
lastClass.current = dom.className;
|
|
226
|
+
useIsoLayoutEffect(() => { if (kept.current) { el.current?.classList.add(...kept.current); kept.current = null; } });
|
|
227
|
+
const run = () => { cur.current = { handle: fn(el.current, liveOptions(latest.current, latest)) ?? null, opts: latest.current, childKey }; };
|
|
228
|
+
useIsoLayoutEffect(() => { run(); return () => { const c = cur.current; cur.current = null; c && stop(c.handle); }; }, []);
|
|
229
|
+
useIsoLayoutEffect(() => {
|
|
230
|
+
const c = cur.current; if (!c) return;
|
|
231
|
+
const next = latest.current, prev = c.opts;
|
|
232
|
+
const kidsChanged = c.childKey !== childKey;
|
|
233
|
+
if (!kidsChanged && same(next, prev)) return;
|
|
234
|
+
if (!kidsChanged && update && update(c.handle, next, prev, el.current) === true) { c.opts = next; return; }
|
|
235
|
+
stop(c.handle); run();
|
|
236
|
+
});
|
|
237
|
+
return h(as || tag, { ref: el, ...dom }, children);
|
|
238
|
+
});
|
|
239
|
+
C.displayName = displayName;
|
|
240
|
+
return C;
|
|
241
|
+
}
|
|
242
|
+
/* A cheap fingerprint of children made of text and elements, so a motion that reads the text runs again. */
|
|
243
|
+
function childrenKey(c) {
|
|
244
|
+
if (c == null || typeof c === 'boolean') return '';
|
|
245
|
+
if (typeof c === 'string' || typeof c === 'number') return String(c);
|
|
246
|
+
if (Array.isArray(c)) return c.map(childrenKey).join('\u0001');
|
|
247
|
+
if (typeof c === 'object' && c.props) return `<${typeof c.type === 'string' ? c.type : 'C'}>${childrenKey(c.props.children)}`;
|
|
248
|
+
return '?';
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Run any motion helper on an element you render yourself.
|
|
253
|
+
* const ref = useMotion(tilt, { max: 12 }); <div ref={ref}>…</div>
|
|
254
|
+
* const ref = useMotion((el) => rise(el.children), []); // a function of the element; deps re-run it
|
|
255
|
+
* The second argument is either options (compared by content) or, with a one-argument fn, a deps array.
|
|
256
|
+
*/
|
|
257
|
+
export function useMotion(fn, optsOrDeps, deps) {
|
|
258
|
+
const ref = useRef(null);
|
|
259
|
+
const isDeps = Array.isArray(optsOrDeps) && deps === undefined;
|
|
260
|
+
const opts = isDeps ? undefined : optsOrDeps;
|
|
261
|
+
const stable = useRef(opts); if (!same(stable.current, opts)) stable.current = opts;
|
|
262
|
+
const latestFn = useLatest(fn);
|
|
263
|
+
useIsoLayoutEffect(() => {
|
|
264
|
+
if (!ref.current) return;
|
|
265
|
+
const out = isDeps ? latestFn.current(ref.current) : latestFn.current(ref.current, stable.current);
|
|
266
|
+
return () => stop(out);
|
|
267
|
+
}, isDeps ? optsOrDeps : [stable.current, ...(deps || [])]);
|
|
268
|
+
return ref;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* <Icon name="search" /> — the same 24×24 stroke icon as icon('search') from @gecko-ui-kit/icons,
|
|
273
|
+
* as a real <svg> (class "ico"), so `.gk-btn > svg` style rules keep working.
|
|
274
|
+
*/
|
|
275
|
+
export function Icon({ name, className, size, stroke = 1.7, style, ...rest }) {
|
|
276
|
+
return h('svg', {
|
|
277
|
+
className: cx('gk-ico', className), viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: stroke,
|
|
278
|
+
strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true,
|
|
279
|
+
...(size ? { width: size, height: size, style: { width: size, height: size, ...style } } : { style }),
|
|
280
|
+
...rest, dangerouslySetInnerHTML: { __html: PATHS[name] ?? PATHS.sparkle },
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
/** An icon given as a name ('check') or as any React node (<MyLogo/>). */
|
|
284
|
+
export const iconNode = (ic, props) => (ic == null || ic === false ? null : typeof ic === 'string' ? h(Icon, { name: ic, ...props }) : ic);
|