@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/effects.js
ADDED
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/* Interactive motion from @gecko-ui-kit/motion as React components and hooks: Indicator, PageSwap, the
|
|
3
|
+
feedback hooks, Confetti, Sortable (drag & drop), ThemeToggle + useTheme, Send / Delete / Download / Hold
|
|
4
|
+
buttons, ExpandSearch, LiquidTabs and Burger.
|
|
5
|
+
React draws the markup (the same HTML as the vanilla builders, so it renders on the server) and owns the
|
|
6
|
+
state; the vanilla code only animates it. Buttons whose label or look changes are drawn again (a new
|
|
7
|
+
element), because the vanilla motion writes into them while it plays. */
|
|
8
|
+
import { forwardRef, useEffect, useId, useMemo, useRef, useState } from 'react';
|
|
9
|
+
import { flushSync } from 'react-dom';
|
|
10
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
11
|
+
import { h, cx, iconNode, Icon, useControllable, useIsoLayoutEffect, useLatest } from './core.js';
|
|
12
|
+
import { Button } from './basics.js';
|
|
13
|
+
|
|
14
|
+
const assignRef = (ref, el) => { if (typeof ref === 'function') ref(el); else if (ref) ref.current = el; };
|
|
15
|
+
/** Drop undefined values, so a prop left out keeps the vanilla default. */
|
|
16
|
+
const defined = (o) => { const out = {}; for (const k in o) if (o[k] !== undefined) out[k] = o[k]; return out; };
|
|
17
|
+
/** An element from an element, a React event (its currentTarget) or a ref. */
|
|
18
|
+
const elOf = (x, fallback) => (x && x.nodeType === 1 ? x : x && x.currentTarget && x.currentTarget.nodeType === 1 ? x.currentTarget : x && x.current && x.current.nodeType === 1 ? x.current : fallback);
|
|
19
|
+
/** Stop every tween on an element and everything inside it. */
|
|
20
|
+
const killAll = (el) => { if (el) M.g.killTweensOf([el, ...el.querySelectorAll('*')]); };
|
|
21
|
+
/** Text of a label given as children (strings and numbers only). */
|
|
22
|
+
const textOf = (c) => (c == null || typeof c === 'boolean' ? '' : Array.isArray(c) ? c.map(textOf).join('') : typeof c === 'object' ? '' : String(c));
|
|
23
|
+
/** A wrapper that draws the inner component again (new element) whenever sig(props) changes. */
|
|
24
|
+
function keyed(name, Inner, sig) {
|
|
25
|
+
const C = forwardRef(function GeckoKeyed(p, ref) { return h(Inner, { ...p, key: sig(p), fref: ref }); });
|
|
26
|
+
C.displayName = name;
|
|
27
|
+
return C;
|
|
28
|
+
}
|
|
29
|
+
/** An <svg> from one of the library's icon strings (constant markup, never app data). */
|
|
30
|
+
const svgOf = (html, className) => {
|
|
31
|
+
const inner = html.slice(html.indexOf('>') + 1, html.lastIndexOf('</svg>'));
|
|
32
|
+
return () => h('svg', { className, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 2, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true, dangerouslySetInnerHTML: { __html: inner } });
|
|
33
|
+
};
|
|
34
|
+
const BinSvg = svgOf(M.binIcon, 'bin');
|
|
35
|
+
const DownloadSvg = svgOf(M.downloadIcon, 'dl');
|
|
36
|
+
const btnSig = (p) => [p.variant, p.size, p.block, p.className, p.type, textOf(p.children), typeof p.icon === 'string' ? p.icon : ''].join('|');
|
|
37
|
+
|
|
38
|
+
/* ------------------------------------------------------------------ indicator */
|
|
39
|
+
|
|
40
|
+
const ACTIVE = '[aria-current]:not([aria-current="false"]), [aria-selected="true"], [aria-pressed="true"], .is-active';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* const pill = useIndicator(current, { radius: 99, line: false, lead, trail, cross, selector })
|
|
44
|
+
* <nav className="menu"><span ref={pill} className="menu__pill" />{items with aria-current}</nav>
|
|
45
|
+
* Same as indicator(pill, active) + trackIndicator(): when `current` changes, the pill stretches to the
|
|
46
|
+
* element in its parent that matches `selector` (default: aria-current / aria-selected / aria-pressed / .is-active).
|
|
47
|
+
*/
|
|
48
|
+
export function useIndicator(active, { selector = ACTIVE, radius, line, lead, trail, cross } = {}) {
|
|
49
|
+
const pill = useRef(null), first = useRef(true);
|
|
50
|
+
const opts = useRef({}); opts.current = Object.assign(opts.current, defined({ radius, line, lead, trail, cross }));
|
|
51
|
+
const sel = useLatest(selector);
|
|
52
|
+
const target = () => pill.current?.parentElement?.querySelector(sel.current) ?? null;
|
|
53
|
+
useIsoLayoutEffect(() => {
|
|
54
|
+
if (!pill.current) return;
|
|
55
|
+
M.indicator(pill.current, target(), { ...opts.current, animate: !first.current });
|
|
56
|
+
first.current = false;
|
|
57
|
+
}, [active, radius, line, selector]);
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (!pill.current) return;
|
|
60
|
+
const stopTrack = M.trackIndicator(pill.current, target, opts.current);
|
|
61
|
+
const p = pill.current;
|
|
62
|
+
return () => { stopTrack(); M.g.killTweensOf(p); if (p.__st) M.g.killTweensOf(p.__st); };
|
|
63
|
+
}, []);
|
|
64
|
+
return pill;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* <Indicator as="nav" active={page} className="menu" pillClassName="menu__pill" radius={99}>…buttons with aria-current…</Indicator>
|
|
69
|
+
* A box with a stretchy pill (or underline with line) that follows the active child.
|
|
70
|
+
*/
|
|
71
|
+
export const Indicator = forwardRef(function Indicator({ as = 'div', active, selector, radius = 13, line = false, lead, trail, cross, pillClassName, pillStyle, style, children, ...rest }, ref) {
|
|
72
|
+
const pill = useIndicator(active, { selector, radius, line, lead, trail, cross });
|
|
73
|
+
const base = { position: 'absolute', inset: 0, pointerEvents: 'none', opacity: 0 };
|
|
74
|
+
if (!pillClassName) base.background = 'var(--accent)';
|
|
75
|
+
// isolation + z-index -1: the pill sits on the box's background but under the items, with no CSS needed
|
|
76
|
+
return h(as, { ref, ...rest, style: { position: 'relative', isolation: 'isolate', ...style } },
|
|
77
|
+
h('span', { ref: pill, className: cx('indicator__pill', pillClassName), 'aria-hidden': true, style: { ...base, zIndex: -1, ...pillStyle } }),
|
|
78
|
+
children);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
/* ------------------------------------------------------------------ page swap */
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* <PageSwap swapKey={pathname} dir={forward ? 1 : -1}>{page}</PageSwap>
|
|
85
|
+
* Same as swap(view, dir, render): when swapKey changes, the old page (still drawn) steps back and fades, then
|
|
86
|
+
* the new children slide in from the side you are heading to. Blocks matching `leave` drop out, `head` lifts away.
|
|
87
|
+
*/
|
|
88
|
+
export const PageSwap = forwardRef(function PageSwap({ as = 'div', swapKey, dir = 1, leave, head, timing, onSwap, children, ...rest }, ref) {
|
|
89
|
+
const view = useRef(null);
|
|
90
|
+
const [shown, setShown] = useState(swapKey);
|
|
91
|
+
const kept = useRef(children);
|
|
92
|
+
if (shown === swapKey) kept.current = children;
|
|
93
|
+
const latest = useLatest({ swapKey, onSwap });
|
|
94
|
+
const inEffect = useRef(false);
|
|
95
|
+
useIsoLayoutEffect(() => {
|
|
96
|
+
if (shown === swapKey || !view.current) return;
|
|
97
|
+
const T = M.swapTiming, before = { ...T };
|
|
98
|
+
if (timing) Object.assign(T, timing);
|
|
99
|
+
const render = () => {
|
|
100
|
+
const k = latest.current.swapKey;
|
|
101
|
+
if (inEffect.current) setShown(k); else flushSync(() => setShown(k));
|
|
102
|
+
latest.current.onSwap?.(view.current);
|
|
103
|
+
};
|
|
104
|
+
inEffect.current = true;
|
|
105
|
+
const p = M.swap(view.current, dir < 0 ? -1 : 1, render, defined({ leave, head }));
|
|
106
|
+
inEffect.current = false;
|
|
107
|
+
if (timing) p.then(() => Object.assign(T, before));
|
|
108
|
+
}, [swapKey]);
|
|
109
|
+
useEffect(() => () => { const v = view.current; if (v) { killAll(v); v.__swapping = null; v.__pending = null; } }, []);
|
|
110
|
+
return h(as, { ref: (n) => { view.current = n; assignRef(ref, n); }, ...rest }, shown === swapKey ? children : kept.current);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
/* ------------------------------------------------------------------ feedback */
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* const fb = useFeedback(); <form ref={fb.ref} onSubmit={() => bad && fb.shake()}>
|
|
117
|
+
* fb.pop(), fb.shake(), fb.wiggle(), fb.flash(), fb.tick(up): run on fb.ref, or on what you pass
|
|
118
|
+
* (an element, a ref or an event: onClick={fb.pop} pops the clicked button).
|
|
119
|
+
*/
|
|
120
|
+
export function useFeedback() {
|
|
121
|
+
const ref = useRef(null), used = useRef(new Set());
|
|
122
|
+
const api = useMemo(() => {
|
|
123
|
+
const on = (fn) => (x, ...a) => { const el = elOf(x, ref.current); if (!el) return undefined; used.current.add(el); return fn(el, ...a); };
|
|
124
|
+
return {
|
|
125
|
+
ref,
|
|
126
|
+
pop: on((el) => M.pop(el)),
|
|
127
|
+
shake: on((el) => M.shake(el)),
|
|
128
|
+
wiggle: on((el) => M.wiggle(el)),
|
|
129
|
+
flash: on((el) => M.flash(el)),
|
|
130
|
+
tick: (up, x) => { const el = elOf(x, ref.current); if (el) M.tick(el, !!up); },
|
|
131
|
+
};
|
|
132
|
+
}, []);
|
|
133
|
+
useEffect(() => () => { for (const el of used.current) { M.g.killTweensOf([el, el.querySelector?.('svg')].filter(Boolean)); el.classList?.remove('is-flash'); } used.current.clear(); }, []);
|
|
134
|
+
return api;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** const ref = useFlash(total); <b ref={ref}>{total}</b> — bounces and lights up each time the value changes. */
|
|
138
|
+
export function useFlash(value) {
|
|
139
|
+
const ref = useRef(null), first = useRef(true);
|
|
140
|
+
useIsoLayoutEffect(() => { if (first.current) { first.current = false; return; } M.flash(ref.current); }, [value]);
|
|
141
|
+
useEffect(() => () => { if (ref.current) M.g.killTweensOf(ref.current); }, []);
|
|
142
|
+
return ref;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** const ref = useTick(qty); <b ref={ref}>{qty}</b> — tints green when the number goes up, red when it goes down. */
|
|
146
|
+
export function useTick(value) {
|
|
147
|
+
const ref = useRef(null), prev = useRef(value);
|
|
148
|
+
useIsoLayoutEffect(() => {
|
|
149
|
+
const p = prev.current; prev.current = value;
|
|
150
|
+
if (p === value || typeof p !== 'number' || typeof value !== 'number') return;
|
|
151
|
+
M.tick(ref.current, value > p);
|
|
152
|
+
}, [value]);
|
|
153
|
+
return ref;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* ------------------------------------------------------------------ confetti */
|
|
157
|
+
|
|
158
|
+
/** confetti(origin, options) from @gecko-ui-kit/motion (a promise with stop(); confetti.clear() stops all). */
|
|
159
|
+
export const confetti = M.confetti;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* const fire = useConfetti({ count: 150 }); <Button onClick={(e) => fire(e)}>Pay</Button>
|
|
163
|
+
* fire(origin?, options?) shoots from an element, a ref, an event's button or { x, y } (default: the screen
|
|
164
|
+
* centre). Bursts still flying are stopped when the component unmounts.
|
|
165
|
+
*/
|
|
166
|
+
export function useConfetti(options) {
|
|
167
|
+
const opts = useLatest(options), live = useRef(new Set());
|
|
168
|
+
useEffect(() => () => { for (const p of live.current) p.stop(); live.current.clear(); }, []);
|
|
169
|
+
return useMemo(() => (origin, more) => {
|
|
170
|
+
const o = origin && typeof origin.x === 'number' && typeof origin.y === 'number' && !origin.nodeType && !origin.currentTarget ? origin : elOf(origin, undefined);
|
|
171
|
+
const p = M.confetti(o, { ...opts.current, ...more });
|
|
172
|
+
live.current.add(p);
|
|
173
|
+
p.then(() => live.current.delete(p));
|
|
174
|
+
return p;
|
|
175
|
+
}, []);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* <ConfettiButton variant="primary" icon="sparkle" count={150} onClick={…}>Celebrate</ConfettiButton>
|
|
180
|
+
* A Button that bursts confetti from itself when clicked (call event.preventDefault() in onClick to skip it).
|
|
181
|
+
*/
|
|
182
|
+
export const ConfettiButton = forwardRef(function ConfettiButton({ count, spread, angle, velocity, colors, shapes, gravity, duration, zIndex, reduced, onDone, onClick, ...rest }, ref) {
|
|
183
|
+
const fire = useConfetti(defined({ count, spread, angle, velocity, colors, shapes, gravity, duration, zIndex, reduced }));
|
|
184
|
+
const done = useLatest(onDone);
|
|
185
|
+
return h(Button, { ref, ...rest, onClick: (e) => {
|
|
186
|
+
onClick?.(e); if (e.defaultPrevented) return;
|
|
187
|
+
const b = e.currentTarget; M.pop(b.querySelector('svg') ?? b);
|
|
188
|
+
fire(b).then(() => done.current?.());
|
|
189
|
+
} });
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
/* ------------------------------------------------------------------ sortable */
|
|
193
|
+
|
|
194
|
+
const groups = new Map(); // group name → Map(zone id → live instance)
|
|
195
|
+
const pendings = new Map(); // group name → { key, rects, back, seen }
|
|
196
|
+
const sel = (v) => (typeof CSS !== 'undefined' && CSS.escape ? CSS.escape(v) : String(v).replace(/["\\]/g, '\\$&'));
|
|
197
|
+
const arrayMove = (a, from, to) => { const b = a.slice(); const [x] = b.splice(from, 1); b.splice(to, 0, x); return b; };
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* <Sortable items={tasks} getKey={(t) => t.id} onReorder={(next) => setTasks(next)} renderItem={(t) => <Card task={t} />} />
|
|
201
|
+
* A list you can reorder by dragging (mouse at once, touch after a long press), using drag() and landFrom():
|
|
202
|
+
* the card lifts and tilts in your hand, the others slide aside, and it lands with a squash.
|
|
203
|
+
* React stays in charge of the order: onReorder(nextItems, { item, from, to, fromList, toList }) gives the new list.
|
|
204
|
+
* Lists with the same `group` (a kanban board's lanes) can pass items to each other; children are drawn
|
|
205
|
+
* before the items, inside the drop zone (a lane title).
|
|
206
|
+
*/
|
|
207
|
+
export const Sortable = forwardRef(function Sortable({
|
|
208
|
+
items, defaultItems, getKey = (x) => x?.id ?? x, renderItem, onReorder, onItemClick, group, as = 'div', itemAs, itemClassName,
|
|
209
|
+
lift, squash = true, rotate = 3, handle, children, ...rest }, ref) {
|
|
210
|
+
const [list, setList] = useControllable(items, defaultItems ?? [], onReorder);
|
|
211
|
+
const zid = useId();
|
|
212
|
+
const g = group ?? zid;
|
|
213
|
+
const zone = useRef(null);
|
|
214
|
+
const live = useLatest({ list, setList, getKey, onItemClick, lift, squash, rotate, handle });
|
|
215
|
+
useIsoLayoutEffect(() => {
|
|
216
|
+
if (!groups.has(g)) groups.set(g, new Map());
|
|
217
|
+
const m = groups.get(g); m.set(zid, { zone: () => zone.current, live });
|
|
218
|
+
return () => { m.delete(zid); if (!m.size) groups.delete(g); };
|
|
219
|
+
}, [g, zid]);
|
|
220
|
+
|
|
221
|
+
// after React has drawn the new order: land the dropped card, slide the others to their new places
|
|
222
|
+
useIsoLayoutEffect(() => {
|
|
223
|
+
const p = pendings.get(g); if (!p || !zone.current) return;
|
|
224
|
+
for (const el of zone.current.querySelectorAll(':scope > [data-sort-item]')) {
|
|
225
|
+
const k = el.getAttribute('data-sort-item'), r = p.rects.get(k);
|
|
226
|
+
if (!r) continue;
|
|
227
|
+
p.rects.delete(k);
|
|
228
|
+
if (k === p.key) { p.seen = true; M.g.set(el, { clearProps: 'transform' }); M.landFrom(el, r, { squash: live.current.squash, rotate: live.current.rotate }); continue; }
|
|
229
|
+
const n = el.getBoundingClientRect(), dx = r.left - n.left, dy = r.top - n.top;
|
|
230
|
+
if (!M.RM() && (Math.abs(dx) > 1 || Math.abs(dy) > 1)) M.g.fromTo(el, { x: dx, y: dy }, { x: 0, y: 0, duration: 0.35, ease: 'power3.out', clearProps: 'transform' });
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
useEffect(() => () => { if (zone.current) killAll(zone.current); }, []);
|
|
234
|
+
|
|
235
|
+
const onPointerDown = (e) => {
|
|
236
|
+
rest.onPointerDown?.(e);
|
|
237
|
+
const L = live.current;
|
|
238
|
+
if (e.defaultPrevented) return;
|
|
239
|
+
const el = e.target.closest('[data-sort-item]');
|
|
240
|
+
if (!el || el.parentElement !== zone.current || e.target.closest('button, input, textarea, select, a, [data-no-drag]')) return;
|
|
241
|
+
if (L.handle && !e.target.closest(L.handle)) return;
|
|
242
|
+
const key = el.getAttribute('data-sort-item');
|
|
243
|
+
const scope = `[data-sort-group="${sel(g)}"]`;
|
|
244
|
+
const drag = M.drag(el, e.nativeEvent, {
|
|
245
|
+
...(L.lift ? { lift: L.lift } : {}),
|
|
246
|
+
targets: `${scope} > [data-sort-item], ${scope}`,
|
|
247
|
+
onClick: () => { const it = L.list.find((x) => String(L.getKey(x)) === key); if (it !== undefined) L.onItemClick?.(it); },
|
|
248
|
+
onDrop: (t) => {
|
|
249
|
+
const srcList = live.current.list, gk = live.current.getKey;
|
|
250
|
+
const from = srcList.findIndex((x) => String(gk(x)) === key); if (from < 0) return false;
|
|
251
|
+
const zoneEl = t.matches('[data-sort-zone]') ? t : t.parentElement;
|
|
252
|
+
const dst = groups.get(g)?.get(zoneEl?.getAttribute('data-sort-zone'));
|
|
253
|
+
if (!dst) return false;
|
|
254
|
+
const onItem = t !== zoneEl, tk = onItem ? t.getAttribute('data-sort-item') : null;
|
|
255
|
+
if (tk === key) return false;
|
|
256
|
+
// remember where every card in the group is now, so each can glide from there after React re-draws
|
|
257
|
+
const rects = new Map();
|
|
258
|
+
for (const it of document.querySelectorAll(`${scope} > [data-sort-item]`)) rects.set(it.getAttribute('data-sort-item'), it.getBoundingClientRect());
|
|
259
|
+
const item = srcList[from];
|
|
260
|
+
if (dst.live === live) {
|
|
261
|
+
if (!onItem) return false; // dropped on its own list's empty space: snap back
|
|
262
|
+
const to = srcList.findIndex((x) => String(gk(x)) === tk);
|
|
263
|
+
pendings.set(g, { key, rects, back: () => drag?.back(), seen: false });
|
|
264
|
+
live.current.setList(arrayMove(srcList, from, to), { item, from, to, fromList: srcList, toList: srcList });
|
|
265
|
+
} else {
|
|
266
|
+
const D = dst.live.current, dl = D.list;
|
|
267
|
+
const to = onItem ? Math.max(0, dl.findIndex((x) => String(D.getKey(x)) === tk)) : dl.length;
|
|
268
|
+
const nextSrc = srcList.filter((_, i) => i !== from), nextDst = dl.slice(); nextDst.splice(to, 0, item);
|
|
269
|
+
pendings.set(g, { key, rects, back: () => drag?.back(), seen: false });
|
|
270
|
+
live.current.setList(nextSrc, { item, from, to: -1, fromList: srcList, toList: nextDst });
|
|
271
|
+
D.setList(nextDst, { item, from: -1, to, fromList: srcList, toList: nextDst });
|
|
272
|
+
}
|
|
273
|
+
// nobody took the new order (a controlled list that did not change): fly home
|
|
274
|
+
requestAnimationFrame(() => requestAnimationFrame(() => {
|
|
275
|
+
const p = pendings.get(g); if (!p || p.key !== key) return;
|
|
276
|
+
pendings.delete(g);
|
|
277
|
+
if (!p.seen && el.isConnected) p.back();
|
|
278
|
+
}));
|
|
279
|
+
return true;
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const tag = itemAs ?? (as === 'ul' || as === 'ol' ? 'li' : 'div');
|
|
285
|
+
return h(as, {
|
|
286
|
+
...rest, ref: (n) => { zone.current = n; assignRef(ref, n); }, 'data-sort-zone': zid, 'data-sort-group': g, onPointerDown,
|
|
287
|
+
}, children, list.map((it, i) => {
|
|
288
|
+
const k = String(getKey(it));
|
|
289
|
+
return h(tag, { key: k, 'data-sort-item': k, 'data-drag': '', className: itemClassName }, renderItem ? renderItem(it, i) : String(k));
|
|
290
|
+
}));
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
/* ------------------------------------------------------------------ theme */
|
|
294
|
+
|
|
295
|
+
const readTheme = () => M.getTheme();
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* const [theme, setTheme] = useTheme({ storageKey: 'gecko:theme', duration: 450 })
|
|
299
|
+
* The page theme ('light' | 'dark', the <html data-theme> attribute). setTheme('dark', fromElementOrEvent)
|
|
300
|
+
* switches with the circular reveal growing from that element and saves the choice. It renders 'light' on
|
|
301
|
+
* the server and reads the real theme (and the saved one) in the browser, so nothing mismatches.
|
|
302
|
+
*/
|
|
303
|
+
export function useTheme({ storageKey = 'gecko:theme', duration, easing } = {}) {
|
|
304
|
+
const [t, setT] = useState('light');
|
|
305
|
+
const o = useLatest({ storageKey, duration, easing });
|
|
306
|
+
useIsoLayoutEffect(() => {
|
|
307
|
+
const root = document.documentElement;
|
|
308
|
+
if (!root.dataset.theme && o.current.storageKey) M.restoreTheme(o.current.storageKey);
|
|
309
|
+
setT(readTheme());
|
|
310
|
+
const sync = () => setT(readTheme());
|
|
311
|
+
addEventListener('themechange', sync);
|
|
312
|
+
const mo = new MutationObserver(sync); mo.observe(root, { attributes: true, attributeFilter: ['data-theme'] });
|
|
313
|
+
return () => { removeEventListener('themechange', sync); mo.disconnect(); };
|
|
314
|
+
}, []);
|
|
315
|
+
const set = useMemo(() => (next, from) => {
|
|
316
|
+
const cur = readTheme();
|
|
317
|
+
const v = typeof next === 'function' ? next(cur) : next;
|
|
318
|
+
return M.theme(v === 'dark' ? 'dark' : 'light', elOf(from, undefined), defined(o.current));
|
|
319
|
+
}, []);
|
|
320
|
+
return [t, set];
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* <ThemeToggle onChange={(t) => save(t)} /> — a round button (moon / sun) that switches light ↔ dark with the
|
|
325
|
+
* circular reveal from the button. Same as toggleTheme(button, { storageKey, duration, easing }).
|
|
326
|
+
*/
|
|
327
|
+
export const ThemeToggle = forwardRef(function ThemeToggle({ storageKey, duration, easing, label = 'Switch theme', size, onChange, onClick, className, ...rest }, ref) {
|
|
328
|
+
const [t, setTheme] = useTheme(defined({ storageKey, duration, easing }));
|
|
329
|
+
const dark = t === 'dark';
|
|
330
|
+
return h('button', {
|
|
331
|
+
ref, type: 'button', 'aria-label': label, ...rest, className: cx('iconbtn', size && 'iconbtn--' + size, className),
|
|
332
|
+
onClick: (e) => {
|
|
333
|
+
onClick?.(e); if (e.defaultPrevented) return;
|
|
334
|
+
const next = dark ? 'light' : 'dark', b = e.currentTarget;
|
|
335
|
+
M.pop(b); setTheme(next, b); onChange?.(next);
|
|
336
|
+
},
|
|
337
|
+
}, h(Icon, { name: dark ? 'sun' : 'moon', className: dark ? 'i-sun' : 'i-moon' }));
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
/* ------------------------------------------------------------------ send / delete */
|
|
341
|
+
|
|
342
|
+
/* Shared life of a one-shot button motion (send, delete): play on click (uncontrolled) or follow a boolean
|
|
343
|
+
prop (controlled); come back after `back` seconds when uncontrolled; stop everything on unmount. */
|
|
344
|
+
function useOneShot(el, on, { controlled, back, play, reset }) {
|
|
345
|
+
const tl = useRef(null), timer = useRef(0), prev = useRef(false), life = useLatest({ back, controlled, play, reset });
|
|
346
|
+
const go = () => {
|
|
347
|
+
clearTimeout(timer.current);
|
|
348
|
+
tl.current = life.current.play(() => { if (!life.current.controlled && life.current.back > 0) timer.current = setTimeout(undo, life.current.back * 1000); });
|
|
349
|
+
};
|
|
350
|
+
const undo = () => { clearTimeout(timer.current); tl.current?.kill(); tl.current = life.current.reset(); };
|
|
351
|
+
useIsoLayoutEffect(() => {
|
|
352
|
+
if (!controlled) return;
|
|
353
|
+
if (on && !prev.current) go(); else if (!on && prev.current) undo();
|
|
354
|
+
prev.current = !!on;
|
|
355
|
+
}, [on, controlled]);
|
|
356
|
+
useEffect(() => () => { clearTimeout(timer.current); tl.current?.kill(); killAll(el.current); }, []);
|
|
357
|
+
return go;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function SendInner({ fref, sent, done = 'Sent', path = 'up', trail = true, back = 2.4, duration, onDone, icon = 'send', children = 'Send', onClick, ...rest }) {
|
|
361
|
+
const el = useRef(null), cb = useLatest({ done, path, trail, duration, onDone });
|
|
362
|
+
const controlled = sent !== undefined;
|
|
363
|
+
const go = useOneShot(el, sent, {
|
|
364
|
+
controlled, back,
|
|
365
|
+
play: (after) => { const b = el.current; if (!b || b.__flying) return null; const c = cb.current; return M.sendOff(b, defined({ done: c.done, path: c.path, trail: c.trail, duration: c.duration, back: 0, onDone: (x) => { c.onDone?.(x); after(); } })); },
|
|
366
|
+
reset: () => (el.current?.__flying ? M.sendOff.reset(el.current) : null),
|
|
367
|
+
});
|
|
368
|
+
return h(Button, { ref: (n) => { el.current = n; assignRef(fref, n); }, icon, ...rest, onClick: (e) => { onClick?.(e); if (!e.defaultPrevented && !controlled) go(); } }, children);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* <SendButton variant="primary" onClick={send}>Send</SendButton> or <SendButton sent={status === 'sent'} type="submit" />
|
|
372
|
+
* Same as sendOff(button, options): the plane winds back and flies off, "Sent" rolls in with a tick. Click plays it
|
|
373
|
+
* (unless onClick calls preventDefault) and it comes back after `back` seconds; with `sent` it follows the prop instead.
|
|
374
|
+
*/
|
|
375
|
+
export const SendButton = keyed('SendButton', SendInner, btnSig);
|
|
376
|
+
|
|
377
|
+
function DeleteInner({ fref, deleted, done = 'Deleted', order = 'first', chew = true, back = 2.4, onDone, children = 'Delete', onClick, ...rest }) {
|
|
378
|
+
const el = useRef(null), cb = useLatest({ done, order, chew, onDone });
|
|
379
|
+
const controlled = deleted !== undefined;
|
|
380
|
+
const go = useOneShot(el, deleted, {
|
|
381
|
+
controlled, back,
|
|
382
|
+
play: (after) => { const b = el.current; if (!b || b.__eating) return null; const c = cb.current; return M.binEat(b, { done: c.done, order: c.order, chew: c.chew, back: 0, onDone: (x) => { c.onDone?.(x); after(); } }); },
|
|
383
|
+
reset: () => (el.current?.__binText ? M.binEat.reset(el.current) : null),
|
|
384
|
+
});
|
|
385
|
+
return h(Button, { ref: (n) => { el.current = n; assignRef(fref, n); }, ...rest, icon: h(BinSvg), onClick: (e) => { onClick?.(e); if (!e.defaultPrevented && !controlled) go(); } }, textOf(children));
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* <DeleteButton variant="danger" onDone={() => removeRow(id)}>Delete</DeleteButton>
|
|
389
|
+
* Same as binEat(button, options): the bin opens and eats its own label letter by letter, chews, and "Deleted" rises in.
|
|
390
|
+
* Click plays it (unless onClick calls preventDefault); with `deleted` it follows the prop instead.
|
|
391
|
+
*/
|
|
392
|
+
export const DeleteButton = keyed('DeleteButton', DeleteInner, btnSig);
|
|
393
|
+
|
|
394
|
+
/* ------------------------------------------------------------------ download */
|
|
395
|
+
|
|
396
|
+
function DownloadInner({ fref, progress, state, label, wait, done, fail, back, width, children = 'Download', ...rest }) {
|
|
397
|
+
const el = useRef(null), api = useRef(null);
|
|
398
|
+
const opts = defined({ wait, done, fail, back, width });
|
|
399
|
+
const lab = useLatest(label);
|
|
400
|
+
const fullOpts = () => ({ ...opts, ...(label ? { label: (p) => lab.current(p) } : {}) });
|
|
401
|
+
useIsoLayoutEffect(() => {
|
|
402
|
+
api.current = M.edgeProgress(el.current, fullOpts());
|
|
403
|
+
return () => { api.current?.destroy?.(); api.current = null; };
|
|
404
|
+
}, []);
|
|
405
|
+
useIsoLayoutEffect(() => { if (api.current) Object.assign(api.current.opts, opts); }, [wait, done, fail, back, width]);
|
|
406
|
+
useIsoLayoutEffect(() => {
|
|
407
|
+
const a = api.current; if (!a) return;
|
|
408
|
+
const want = state ?? (progress === undefined ? 'idle' : 'loading');
|
|
409
|
+
if (want === 'idle') { if (a.state !== 'idle') a.reset(); }
|
|
410
|
+
else if (want === 'loading') { if (a.state === 'done') a.reset(true); a.set(progress ?? null); }
|
|
411
|
+
else if (want === 'done') a.done();
|
|
412
|
+
else if (want === 'failed') { if (a.state === 'idle' || a.state === 'done') { a.reset(true); a.set(progress ?? 0); } a.fail(); }
|
|
413
|
+
}, [state, progress]);
|
|
414
|
+
return h(Button, { ref: (n) => { el.current = n; assignRef(fref, n); }, ...rest, icon: h(DownloadSvg) }, textOf(children));
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* <DownloadButton progress={p} state={failed ? 'failed' : p === 1 ? 'done' : undefined} onClick={start}>Download</DownloadButton>
|
|
418
|
+
* Same as edgeProgress(button, options): the button's own outline fills as `progress` (0–1) grows; null = size unknown
|
|
419
|
+
* (a segment chases round). state 'done' glows, fills and ticks; 'failed' turns red and shakes; 'idle' puts it back.
|
|
420
|
+
*/
|
|
421
|
+
export const DownloadButton = keyed('DownloadButton', DownloadInner, btnSig);
|
|
422
|
+
|
|
423
|
+
/* ------------------------------------------------------------------ hold to confirm */
|
|
424
|
+
|
|
425
|
+
function HoldInner({ fref, confirmed, duration, look = 'fill', tone = 'bad', hint, done, back, haptic, onConfirm, onCancel, icon = 'trash', children, variant, size, block, className, ...rest }) {
|
|
426
|
+
const el = useRef(null), api = useRef(null);
|
|
427
|
+
const cb = useLatest({ onConfirm, onCancel });
|
|
428
|
+
const opts = defined({ duration, hint, done, back, haptic });
|
|
429
|
+
useIsoLayoutEffect(() => {
|
|
430
|
+
api.current = M.holdConfirm(el.current, { ...opts, look, tone, onConfirm: (b, x) => cb.current.onConfirm?.(b, x), onCancel: (b, x, p) => cb.current.onCancel?.(b, x, p) });
|
|
431
|
+
return () => {
|
|
432
|
+
const a = api.current; api.current = null; if (!a) return;
|
|
433
|
+
if (a.state === 'done') a.reset(); // put the icon and label back (StrictMode mounts it again)
|
|
434
|
+
killAll(a.el); M.g.set(a.el, { clearProps: 'width,transform' }); a.destroy();
|
|
435
|
+
};
|
|
436
|
+
}, []);
|
|
437
|
+
useIsoLayoutEffect(() => { if (api.current) Object.assign(api.current.opts, opts); }, [duration, hint, done, back, haptic]);
|
|
438
|
+
const prev = useRef(false);
|
|
439
|
+
useIsoLayoutEffect(() => {
|
|
440
|
+
if (confirmed === undefined || !api.current) return;
|
|
441
|
+
if (confirmed && api.current.state !== 'done') api.current.confirm();
|
|
442
|
+
else if (!confirmed && prev.current && api.current.state === 'done') api.current.reset();
|
|
443
|
+
prev.current = !!confirmed;
|
|
444
|
+
}, [confirmed]);
|
|
445
|
+
const setRef = (n) => { el.current = n; assignRef(fref, n); };
|
|
446
|
+
if (look === 'ring') {
|
|
447
|
+
return h('button', { ref: setRef, type: 'button', 'aria-label': textOf(children), ...rest, className: cx('iconbtn', size && 'iconbtn--' + size, className) }, iconNode(icon));
|
|
448
|
+
}
|
|
449
|
+
return h(Button, { ref: setRef, variant, size, block, className, icon, ...rest }, textOf(children));
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* <HoldButton tone="bad" hint="Hold to delete" done="Deleted" onConfirm={() => deleteBike(id)}>Delete bike</HoldButton>
|
|
453
|
+
* Same as holdConfirm(button, options): press and hold, colour floods the button (look="ring": a circle fills round a
|
|
454
|
+
* round icon button) and only a full hold calls onConfirm. Letting go early drains it and shows the hint.
|
|
455
|
+
*/
|
|
456
|
+
export const HoldButton = keyed('HoldButton', HoldInner, (p) => [btnSig(p), p.look, p.tone].join('|'));
|
|
457
|
+
|
|
458
|
+
/* ------------------------------------------------------------------ expanding search */
|
|
459
|
+
|
|
460
|
+
const GLASS_IN = '<g class="xs__glass"><circle cx="10.5" cy="10.5" r="6"/><path d="M15 15l4.5 4.5"/></g>';
|
|
461
|
+
const CROSS_IN = '<path class="xs__x" d="M7 7l10 10" pathLength="1" stroke-dasharray="1" stroke-dashoffset="1"/><path class="xs__x" d="M17 7 7 17" pathLength="1" stroke-dasharray="1" stroke-dashoffset="1"/>';
|
|
462
|
+
const svg2 = (className, html) => h('svg', { className, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 2, strokeLinecap: 'round', 'aria-hidden': true, dangerouslySetInnerHTML: { __html: html } });
|
|
463
|
+
|
|
464
|
+
function SearchInner({ fref, placeholder = 'Search…', label = 'Search', closeLabel = 'Close search', name = 'q', look = 'outline', size = 'md', width, typing, autoClose, shortcut,
|
|
465
|
+
value, defaultValue, onChange, onSearch, open, defaultOpen = false, onOpen, onClose, className, ...rest }) {
|
|
466
|
+
const box = useRef(null), api = useRef(null), id = useId();
|
|
467
|
+
const cb = useLatest({ onChange, onSearch, onOpen, onClose, value });
|
|
468
|
+
const opts = defined({ width, typing, autoClose, placeholder, key: shortcut });
|
|
469
|
+
useIsoLayoutEffect(() => {
|
|
470
|
+
api.current = M.expandSearch(box.current, {
|
|
471
|
+
...opts,
|
|
472
|
+
onInput: (v) => { if (v !== cb.current.value) cb.current.onChange?.(v); },
|
|
473
|
+
onSearch: (v) => cb.current.onSearch?.(v),
|
|
474
|
+
onOpen: () => cb.current.onOpen?.(),
|
|
475
|
+
onClose: () => cb.current.onClose?.(),
|
|
476
|
+
});
|
|
477
|
+
if (open ?? defaultOpen) api.current.open();
|
|
478
|
+
return () => {
|
|
479
|
+
const a = api.current; api.current = null; if (!a) return;
|
|
480
|
+
killAll(a.el); a.destroy();
|
|
481
|
+
// leave the markup closed, as React drew it (StrictMode mounts it again on the same element)
|
|
482
|
+
a.el.classList.remove('is-open', 'is-closing'); M.g.set([a.el, a.input], { clearProps: 'width,transform,opacity' });
|
|
483
|
+
a.el.querySelector('.xs__btn')?.setAttribute('aria-expanded', 'false');
|
|
484
|
+
a.input.tabIndex = -1; a.input.placeholder = ''; const c = a.el.querySelector('.xs__close'); if (c) c.tabIndex = -1;
|
|
485
|
+
a.el.querySelectorAll('.xs__x').forEach((x) => x.setAttribute('stroke-dashoffset', '1'));
|
|
486
|
+
};
|
|
487
|
+
}, []);
|
|
488
|
+
useIsoLayoutEffect(() => { if (api.current) Object.assign(api.current.opts, { key: '', ...opts }); }, [width, typing, autoClose, placeholder, shortcut]);
|
|
489
|
+
useIsoLayoutEffect(() => { const a = api.current; if (a && value !== undefined && a.input.value !== value) a.input.value = value; }, [value]);
|
|
490
|
+
const first = useRef(true);
|
|
491
|
+
useIsoLayoutEffect(() => {
|
|
492
|
+
if (first.current) { first.current = false; return; }
|
|
493
|
+
const a = api.current; if (!a || open === undefined || a.isOpen === open) return;
|
|
494
|
+
open ? a.open() : a.close();
|
|
495
|
+
}, [open]);
|
|
496
|
+
return h('form', {
|
|
497
|
+
ref: (n) => { box.current = n; assignRef(fref, n); }, role: 'search', 'data-placeholder': placeholder, ...rest,
|
|
498
|
+
className: cx('xs', look === 'quiet' && 'xs--quiet', size === 'sm' && 'xs--sm', className),
|
|
499
|
+
},
|
|
500
|
+
h('button', { type: 'button', className: 'xs__btn', 'aria-label': label, 'aria-expanded': 'false', 'aria-controls': id }, svg2('xs__ic', GLASS_IN)),
|
|
501
|
+
h('input', { className: 'xs__input', id, type: 'search', name, 'aria-label': label, autoComplete: 'off', spellCheck: false, enterKeyHint: 'search', tabIndex: -1, defaultValue: value ?? defaultValue }),
|
|
502
|
+
h('button', { type: 'button', className: 'xs__close', 'aria-label': closeLabel, tabIndex: -1 }, svg2(undefined, CROSS_IN)));
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* <ExpandSearch placeholder="Search bikes…" width={280} shortcut="/" onChange={setQuery} onSearch={go} />
|
|
506
|
+
* Same as expandSearch(searchField(…)): a round magnifier that stretches into a field, types its hint in and draws a ×.
|
|
507
|
+
* Escape clears, then closes. value/defaultValue + onChange(text) for the text; open/defaultOpen + onOpen/onClose.
|
|
508
|
+
*/
|
|
509
|
+
export const ExpandSearch = keyed('ExpandSearch', SearchInner, (p) => [p.look, p.size, p.name, p.label, p.closeLabel, p.className].join('|'));
|
|
510
|
+
|
|
511
|
+
/* ------------------------------------------------------------------ liquid tab bar */
|
|
512
|
+
|
|
513
|
+
function LiquidInner({ fref, items = [], value, defaultValue = 0, onChange, mode, splash, label = 'Main', labels = true, goo = true, linkAs, className, style, ...rest }) {
|
|
514
|
+
const [v, setV] = useControllable(value, defaultValue, onChange);
|
|
515
|
+
const [first] = useState(v); // React draws the starting tab only; the vanilla code moves it after that
|
|
516
|
+
const nav = useRef(null), api = useRef(null);
|
|
517
|
+
const fid = 'lq-goo-' + useId().replace(/[^\w-]/g, '_');
|
|
518
|
+
const opts = defined({ mode, splash });
|
|
519
|
+
const live = useLatest({ setV });
|
|
520
|
+
useIsoLayoutEffect(() => {
|
|
521
|
+
api.current = M.liquidTabs(nav.current, { ...opts, onChange: (i, tab) => live.current.setV(i, tab) });
|
|
522
|
+
return () => { const a = api.current; api.current = null; if (a) { killAll(a.el); a.destroy(); } };
|
|
523
|
+
}, []);
|
|
524
|
+
useIsoLayoutEffect(() => { if (api.current) Object.assign(api.current.opts, opts); }, [mode, splash]);
|
|
525
|
+
useIsoLayoutEffect(() => { const a = api.current; if (a && typeof v === 'number' && a.index !== v) a.select(v); }, [v]);
|
|
526
|
+
return h('nav', {
|
|
527
|
+
ref: (n) => { nav.current = n; assignRef(fref, n); }, 'aria-label': label, ...rest,
|
|
528
|
+
className: cx('lq', !labels && 'lq--icons', !goo && 'lq--plain', className), style: { '--lq-goo': `url(#${fid})`, ...style },
|
|
529
|
+
},
|
|
530
|
+
h('svg', { className: 'lq__defs', width: 0, height: 0, 'aria-hidden': true, focusable: 'false' },
|
|
531
|
+
h('filter', { id: fid, x: '-10%', y: '-90%', width: '120%', height: '280%', colorInterpolationFilters: 'sRGB' },
|
|
532
|
+
h('feGaussianBlur', { in: 'SourceGraphic', stdDeviation: 7 }),
|
|
533
|
+
h('feColorMatrix', { values: '1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 24 -11' }))),
|
|
534
|
+
h('span', { className: 'lq__goo', 'aria-hidden': true }, h('span', { className: 'lq__body' }), h('span', { className: 'lq__drop' }), h('span', { className: 'lq__drop' }), h('span', { className: 'lq__bub' })),
|
|
535
|
+
h('span', { className: 'lq__dot', 'aria-hidden': true }),
|
|
536
|
+
items.map((it, i) => h(it.href ? (linkAs || 'a') : 'button', {
|
|
537
|
+
key: i, className: cx('lq__tab', i === first && 'is-on'), ...(it.href ? { href: it.href } : { type: 'button' }), 'aria-current': i === first ? 'page' : undefined,
|
|
538
|
+
}, h('span', { className: 'lq__ic', 'aria-hidden': true }, iconNode(it.icon)), h('span', { className: 'lq__lb' }, it.label))));
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* <LiquidTabs items={[{ label: 'Home', icon: 'home' }, { label: 'Bikes', icon: 'box' }]} value={tab} onChange={setTab} mode="dip" />
|
|
542
|
+
* Same as liquidTabs(liquidBar(items, …)): the bar and a bubble over the active tab move like one liquid.
|
|
543
|
+
* value is the active index; onChange(index, tabElement). Items with href are links (linkAs={Link} for Next.js).
|
|
544
|
+
*/
|
|
545
|
+
export const LiquidTabs = keyed('LiquidTabs', LiquidInner, (p) => [p.labels !== false, p.goo !== false, (p.items || []).map((it) => [textOf(it.label), typeof it.icon === 'string' ? it.icon : '', it.href ?? ''].join('~')).join('/')].join('|'));
|
|
546
|
+
|
|
547
|
+
/* ------------------------------------------------------------------ burger */
|
|
548
|
+
|
|
549
|
+
const BURGER_WORDS = { open: 'Open menu', close: 'Close menu' };
|
|
550
|
+
/**
|
|
551
|
+
* <Burger open={menuOpen} onToggle={setMenuOpen} controls="main-menu" />
|
|
552
|
+
* Same as burger(button, open): three lines that turn into an ✕ and back. React owns `open` (or defaultOpen);
|
|
553
|
+
* a click or Escape calls onToggle(next). words: { open, close } labels the button. iconClassName styles the lines.
|
|
554
|
+
*/
|
|
555
|
+
export const Burger = forwardRef(function Burger({ open, defaultOpen = false, onToggle, words, duration = 0.45, controls, escape = true, size, iconClassName, className, onClick, ...rest }, ref) {
|
|
556
|
+
const [on, set] = useControllable(open, defaultOpen, onToggle);
|
|
557
|
+
const el = useRef(null), first = useRef(true);
|
|
558
|
+
const w = { ...BURGER_WORDS, ...words };
|
|
559
|
+
useIsoLayoutEffect(() => {
|
|
560
|
+
M.burger(el.current, !!on, { duration: first.current ? 0 : duration });
|
|
561
|
+
first.current = false;
|
|
562
|
+
}, [on]);
|
|
563
|
+
useEffect(() => () => { const b = el.current; if (!b) return; b.__burger?.tl?.kill(); M.g.set(b.querySelectorAll('.burger > i'), { clearProps: 'all' }); delete b.__burger; }, []);
|
|
564
|
+
const latest = useLatest({ on, set, escape });
|
|
565
|
+
useEffect(() => {
|
|
566
|
+
if (!escape) return;
|
|
567
|
+
const key = (e) => {
|
|
568
|
+
const L = latest.current;
|
|
569
|
+
if (e.key !== 'Escape' || !L.on || e.defaultPrevented) return;
|
|
570
|
+
L.set(false, el.current); el.current?.focus();
|
|
571
|
+
};
|
|
572
|
+
document.addEventListener('keydown', key);
|
|
573
|
+
return () => document.removeEventListener('keydown', key);
|
|
574
|
+
}, [escape]);
|
|
575
|
+
return h('button', {
|
|
576
|
+
ref: (n) => { el.current = n; assignRef(ref, n); }, type: 'button', 'aria-expanded': !!on, 'aria-label': on ? w.close : w.open, 'aria-controls': controls || undefined, ...rest,
|
|
577
|
+
className: cx('iconbtn', size && 'iconbtn--' + size, on && 'is-open', className),
|
|
578
|
+
onClick: (e) => { onClick?.(e); if (!e.defaultPrevented) set(!on, e.currentTarget); },
|
|
579
|
+
}, h('span', { className: cx('burger', iconClassName), 'aria-hidden': true }, h('i'), h('i'), h('i')));
|
|
580
|
+
});
|