@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/shell.js
ADDED
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/* App shell: AppShell and its parts (Sidebar, Topbar, PageHeader, MobileTabbar) as React components.
|
|
3
|
+
|
|
4
|
+
The vanilla createShell() owns everything: it draws the frame, runs a hash router and renders pages from
|
|
5
|
+
HTML strings. In a Next.js app the router is Next's and the pages are React, so these components draw the
|
|
6
|
+
same markup themselves (the same CSS styles it, it renders on the server) and only borrow the motion:
|
|
7
|
+
the stretchy pill, the sub-menu accordion, the rail collapse with FLIP, the hover flyout, the split-letter
|
|
8
|
+
title, the tab underline, the bell wiggle, the theme reveal, the intro and the page slide.
|
|
9
|
+
|
|
10
|
+
Routing: pass the current path (usePathname()) and onNavigate (router.push). Every link is a real <a href>
|
|
11
|
+
(or your `link` component, e.g. Next's Link); a plain click calls onNavigate(href) instead of loading the page.
|
|
12
|
+
An item's address is item.href, else `${base}/${id}`; a sub-page's is `${itemHref}/${subId}` (or its own href). */
|
|
13
|
+
import { createContext, forwardRef, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState, Fragment } from 'react';
|
|
14
|
+
import { createPortal, flushSync } from 'react-dom';
|
|
15
|
+
import * as M from '@gecko-ui-kit/motion';
|
|
16
|
+
import * as O from '@gecko-ui-kit/overlays';
|
|
17
|
+
import { esc } from '@gecko-ui-kit/components';
|
|
18
|
+
import { h, cx, Icon, iconNode, useLatest, useIsoLayoutEffect } from './core.js';
|
|
19
|
+
|
|
20
|
+
const { g, D, RM, $$ } = M;
|
|
21
|
+
|
|
22
|
+
const WORDS = {
|
|
23
|
+
sidebar: 'Sidebar', main: 'Main', collapse: 'Collapse sidebar', expand: 'Expand sidebar', collapseTip: 'Collapse [', expandTip: 'Expand [',
|
|
24
|
+
account: 'account', open: 'Open', menu: 'Open menu', breadcrumb: 'Breadcrumb', search: 'Search', searchPlaceholder: 'Search…', searchKey: '⌘K',
|
|
25
|
+
theme: 'Switch theme', themeTip: 'Theme', bell: 'Notifications', primary: 'New', tabs: 'Tabs', more: 'More', sections: 'Sections',
|
|
26
|
+
shortcuts: 'Keyboard shortcuts', replay: 'Replay intro', searchPages: 'Search pages…', goTo: 'Go to', toggleRail: 'Collapse / expand sidebar',
|
|
27
|
+
thisList: 'This list', close: 'Close',
|
|
28
|
+
};
|
|
29
|
+
const wordsOf = (w) => (w ? { ...WORDS, ...w } : WORDS);
|
|
30
|
+
|
|
31
|
+
/* ---------------- routes from nav + path ---------------- */
|
|
32
|
+
const clean = (p) => (String(p ?? '/').replace(/[?#].*$/, '').replace(/\/+$/, '') || '/');
|
|
33
|
+
const normSub = (s) => (Array.isArray(s) ? { id: String(s[0]), label: s[1] ?? s[0], href: s[2] } : { ...s, id: String(s.id) });
|
|
34
|
+
|
|
35
|
+
/** nav + end → everything the parts need to know about the menu and where we are. */
|
|
36
|
+
function model({ nav = [], end = [], base = '', path, home }) {
|
|
37
|
+
const all = [...nav.flatMap((grp) => grp.items || []), ...end];
|
|
38
|
+
const hrefOf = (it, subId) => {
|
|
39
|
+
const ih = it.href ?? `${base}/${it.id}`;
|
|
40
|
+
if (!subId) return ih;
|
|
41
|
+
const s = (it.subs || []).map(normSub).find((x) => x.id === subId);
|
|
42
|
+
return s?.href ?? `${ih === '/' ? '' : ih}/${subId}`;
|
|
43
|
+
};
|
|
44
|
+
const subsOf = (it) => (it?.subs?.length ? it.subs.map(normSub).map((s) => ({ ...s, href: hrefOf(it, s.id) })) : null);
|
|
45
|
+
const p = clean(path);
|
|
46
|
+
let item = null, score = -1;
|
|
47
|
+
for (const it of all) {
|
|
48
|
+
const ih = clean(hrefOf(it));
|
|
49
|
+
if ((p === ih || (ih !== '/' && p.startsWith(ih + '/'))) && ih.length > score) { item = it; score = ih.length; }
|
|
50
|
+
}
|
|
51
|
+
if (!item && home && path == null) item = all.find((it) => it.id === home) || null;
|
|
52
|
+
let sub = '';
|
|
53
|
+
const subs = subsOf(item);
|
|
54
|
+
if (subs) sub = subs.find((s) => { const sh = clean(s.href); return p === sh || p.startsWith(sh + '/'); })?.id ?? '';
|
|
55
|
+
const order = all.map((it) => it.id);
|
|
56
|
+
const rank = item ? order.indexOf(item.id) * 100 + Math.max(0, subs?.findIndex((s) => s.id === sub) ?? 0) : -1;
|
|
57
|
+
return { all, hrefOf, subsOf, item, page: item?.id ?? null, sub, rank, itemOf: (id) => all.find((it) => it.id === id) };
|
|
58
|
+
}
|
|
59
|
+
const countOf = (it) => (typeof it.count === 'function' ? it.count() : it.count) ?? 0;
|
|
60
|
+
|
|
61
|
+
/* ---------------- navigation ---------------- */
|
|
62
|
+
const go = (onNavigate, href) => { if (onNavigate) onNavigate(href); else location.assign(href); };
|
|
63
|
+
/** A real link; a plain left click calls onNavigate(href) instead of loading the page. */
|
|
64
|
+
const NavLink = forwardRef(function NavLink({ href, onNavigate, link, onClick, ...rest }, ref) {
|
|
65
|
+
return h(link || 'a', {
|
|
66
|
+
ref, href, ...rest,
|
|
67
|
+
onClick: (e) => {
|
|
68
|
+
onClick?.(e);
|
|
69
|
+
if (e.defaultPrevented || !onNavigate || e.button || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey || rest.target) return;
|
|
70
|
+
e.preventDefault(); onNavigate(href);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
/* html.is-sideopen: the sidebar shown as a drawer on phones. Every part hears about it through one event. */
|
|
76
|
+
const SIDE_EVENT = 'gecko:sideopen';
|
|
77
|
+
const setSideOpen = (open) => {
|
|
78
|
+
const root = document.documentElement;
|
|
79
|
+
if (root.classList.contains('is-sideopen') === !!open) return;
|
|
80
|
+
root.classList.toggle('is-sideopen', !!open);
|
|
81
|
+
window.dispatchEvent(new CustomEvent(SIDE_EVENT, { detail: !!open }));
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/* menu items for O.menu: '-' · { section } heading · { icon, label, kbd, danger, run } */
|
|
85
|
+
const toMenu = (items) => (items || []).filter((it) => it != null && it !== false).map((it) => {
|
|
86
|
+
if (it === '-' || it.sep) return '-';
|
|
87
|
+
if (it.section != null) return { label: String(it.section) };
|
|
88
|
+
return { icon: typeof it.icon === 'string' ? it.icon : undefined, label: String(it.label ?? ''), kbd: it.kbd ? esc(it.kbd) : '', danger: !!it.danger, run: (a) => it.run?.(a) };
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const ShellCtx = createContext(null);
|
|
92
|
+
/**
|
|
93
|
+
* Inside <AppShell>: { route: { page, sub }, path, navigate(href), palette(), shortcuts(), intro(), setRail(b), isRail() }.
|
|
94
|
+
* Outside: null.
|
|
95
|
+
*/
|
|
96
|
+
export const useShell = () => useContext(ShellCtx);
|
|
97
|
+
|
|
98
|
+
/* ======================================================================= */
|
|
99
|
+
/* Sidebar */
|
|
100
|
+
/* ======================================================================= */
|
|
101
|
+
/* FLIP targets and the parts that fade while the sidebar collapses / expands (same as the vanilla shell) */
|
|
102
|
+
const FLIP = [['.side__head .brand', '.mark'], ['.side__head .collapse', ''], ['.snav__item', '.ico'], ['.side__me', ':scope > .av']];
|
|
103
|
+
const FADE = '.brand__text, .side__group, .snav__label, .snav__chev, .snav__count, .snav__subs, .side__me .cell__text, .side__me > .ico';
|
|
104
|
+
const RISE = ':scope > .card, :scope > .grid > .card, :scope > .kpis > .kpi, :scope > .stack > .card, [data-rise]';
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* <Sidebar brand={{ name: 'MotoRent', sub: 'Chiang Mai', icon: 'logo' }} nav={nav} end={end} user={me}
|
|
108
|
+
* path={pathname} onNavigate={router.push} />
|
|
109
|
+
* The main menu: a pill stretches to the current item, the open section unfolds its sub-pages (click it to fold),
|
|
110
|
+
* numbers flash when they change, it collapses to a rail of icons (with a flyout on hover) and saves that choice.
|
|
111
|
+
* On phones it is a drawer opened by the Topbar's menu button. ref → { setRail(b), isRail(), el }.
|
|
112
|
+
*/
|
|
113
|
+
export const Sidebar = forwardRef(function Sidebar({ brand = { name: 'App' }, nav, end, user, path, base, home, onNavigate, link, storageKey = 'gecko', railBreakpoint = 1280, mobile = 760, words }, ref) {
|
|
114
|
+
const W = wordsOf(words);
|
|
115
|
+
const m = model({ nav, end, base, path, home });
|
|
116
|
+
const L = { onNavigate, link };
|
|
117
|
+
const side = useRef(null), pill = useRef(null), flyEl = useRef(null);
|
|
118
|
+
const [rail, setRailState] = useState(false);
|
|
119
|
+
const [phone, setPhone] = useState(false); // on phones the sidebar is a full drawer, even when saved as a rail
|
|
120
|
+
const railTl = useRef(null), first = useRef(true);
|
|
121
|
+
const [subs, setSubs] = useState({ page: m.page, shut: false, hidden: false });
|
|
122
|
+
if (subs.page !== m.page) setSubs({ page: m.page, shut: false, hidden: false });
|
|
123
|
+
const [fly, setFly] = useState(null); // { id, left, top }
|
|
124
|
+
const flyTimer = useRef(null);
|
|
125
|
+
const KEY = (k) => `${storageKey}:${k}`;
|
|
126
|
+
const isMobile = () => innerWidth <= mobile;
|
|
127
|
+
const state = useLatest({ rail, m, railBreakpoint, storageKey });
|
|
128
|
+
|
|
129
|
+
/* ----- rail ----- */
|
|
130
|
+
const setRail = (r) => {
|
|
131
|
+
railTl.current?.progress(1);
|
|
132
|
+
if (state.current.rail === r) return;
|
|
133
|
+
closeFly(true);
|
|
134
|
+
const root = document.documentElement, sideEl = side.current, main = document.querySelector('.t-main');
|
|
135
|
+
const apply = () => {
|
|
136
|
+
root.classList.toggle('is-rail', r);
|
|
137
|
+
try { if (innerWidth >= state.current.railBreakpoint) localStorage.setItem(`${state.current.storageKey}:pinned`, r ? '0' : '1'); } catch {}
|
|
138
|
+
flushSync(() => setRailState(r));
|
|
139
|
+
};
|
|
140
|
+
if (RM() || isMobile() || !sideEl) { apply(); return; }
|
|
141
|
+
const collect = () => FLIP.flatMap(([s, rf]) => $$(s, sideEl).map((el) => ({ el, ref: (rf ? el.querySelector(rf) : el) ?? el, key: s + (el.dataset?.id ?? '') })));
|
|
142
|
+
const els = collect();
|
|
143
|
+
const w0 = sideEl.offsetWidth, m0 = main?.getBoundingClientRect().left ?? 0;
|
|
144
|
+
root.classList.toggle('is-rail', r); const w1 = sideEl.offsetWidth, m1 = main?.getBoundingClientRect().left ?? 0; root.classList.toggle('is-rail', !r);
|
|
145
|
+
const clip = (x) => `inset(0 ${x}px 0 0 round 22px)`;
|
|
146
|
+
const flipIcons = (dur, ease) => {
|
|
147
|
+
const before = new Map(els.map((x) => [x.key, x.ref.getBoundingClientRect()]));
|
|
148
|
+
apply();
|
|
149
|
+
const tl = g.timeline();
|
|
150
|
+
collect().forEach((x) => {
|
|
151
|
+
const b = before.get(x.key); if (!b) return;
|
|
152
|
+
const a = x.ref.getBoundingClientRect(), dx = b.left - a.left, dy = b.top - a.top;
|
|
153
|
+
if (Math.abs(dx) + Math.abs(dy) < 0.5) return;
|
|
154
|
+
tl.fromTo(x.el, { x: dx, y: dy }, { x: 0, y: 0, duration: dur, ease }, 0);
|
|
155
|
+
if (x.el.matches('.snav__item[aria-current="page"]')) tl.fromTo(pill.current, { x: dx, y: dy }, { x: 0, y: 0, duration: dur, ease }, 0);
|
|
156
|
+
});
|
|
157
|
+
return tl;
|
|
158
|
+
};
|
|
159
|
+
const cleanup = () => {
|
|
160
|
+
g.set(sideEl, { clearProps: 'clipPath' }); if (main) g.set(main, { clearProps: 'marginLeft' });
|
|
161
|
+
g.set([...$$(FADE, sideEl), ...$$('.snav__item, .brand, .collapse, .side__me', sideEl)], { clearProps: 'opacity,transform' });
|
|
162
|
+
g.set(pill.current, { clearProps: 'transform' }); railTl.current = null;
|
|
163
|
+
};
|
|
164
|
+
const tl = railTl.current = g.timeline({ onComplete: cleanup });
|
|
165
|
+
if (r) {
|
|
166
|
+
tl.to($$(FADE, sideEl), { opacity: 0, x: -6, duration: 0.16, ease: 'power2.in', stagger: 0.004 }, 0);
|
|
167
|
+
tl.fromTo(sideEl, { clipPath: clip(0) }, { clipPath: clip(w0 - w1), duration: 0.42, ease: 'power3.inOut' }, 0.05);
|
|
168
|
+
if (main) tl.fromTo(main, { marginLeft: m0 }, { marginLeft: m1, duration: 0.42, ease: 'power3.inOut' }, 0.05);
|
|
169
|
+
tl.add(() => { g.set(sideEl, { clipPath: clip(0) }); railTl.current?.add(flipIcons(0.36, 'power3.out'), '>'); });
|
|
170
|
+
} else {
|
|
171
|
+
tl.add(flipIcons(0.45, 'power3.out'), 0);
|
|
172
|
+
tl.fromTo(sideEl, { clipPath: clip(w1 - w0) }, { clipPath: clip(0), duration: 0.5, ease: 'expo.out' }, 0);
|
|
173
|
+
if (main) tl.fromTo(main, { marginLeft: m0 }, { marginLeft: m1, duration: 0.5, ease: 'expo.out' }, 0);
|
|
174
|
+
tl.fromTo($$(FADE, sideEl), { opacity: 0, x: -8 }, { opacity: 1, x: 0, duration: 0.4, ease: 'power3.out', stagger: 0.01 }, 0.12);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
useImperativeHandle(ref, () => ({ setRail: (r) => setRail(!!r), isRail: () => state.current.rail, get el() { return side.current; } }), []);
|
|
178
|
+
|
|
179
|
+
// the saved choice (or a narrow window) starts it as a rail; the classes go when the sidebar does
|
|
180
|
+
useIsoLayoutEffect(() => {
|
|
181
|
+
const root = document.documentElement;
|
|
182
|
+
let r = root.classList.contains('is-rail');
|
|
183
|
+
try { const p = localStorage.getItem(KEY('pinned')); if (p === '0' || (p === null && innerWidth < railBreakpoint)) r = true; } catch {}
|
|
184
|
+
root.classList.toggle('is-rail', r); setRailState(r);
|
|
185
|
+
O.initTips();
|
|
186
|
+
const mq = matchMedia(`(max-width: ${mobile}px)`), onMq = () => setPhone(mq.matches); onMq();
|
|
187
|
+
mq.addEventListener('change', onMq);
|
|
188
|
+
const stopTrack = M.trackIndicator(pill.current, () => side.current?.querySelector('.snav__item[aria-current="page"]'), { radius: 14 });
|
|
189
|
+
return () => {
|
|
190
|
+
stopTrack(); mq.removeEventListener('change', onMq); railTl.current?.kill(); railTl.current = null; flyTimer.current?.kill();
|
|
191
|
+
root.classList.remove('is-rail', 'is-sideopen');
|
|
192
|
+
};
|
|
193
|
+
}, []);
|
|
194
|
+
|
|
195
|
+
/* ----- pill, sub-pages, counts ----- */
|
|
196
|
+
const counts = useRef({});
|
|
197
|
+
const shown = useRef({ page: m.page, rail, phone });
|
|
198
|
+
useIsoLayoutEffect(() => {
|
|
199
|
+
const el = side.current; if (!el) return;
|
|
200
|
+
const moved = shown.current.page !== m.page && shown.current.rail === rail && shown.current.phone === phone; // a new page, not a boot / rail change
|
|
201
|
+
shown.current = { page: m.page, rail, phone };
|
|
202
|
+
M.indicator(pill.current, el.querySelector('.snav__item[aria-current="page"]'), { animate: moved && !railTl.current, radius: 14 });
|
|
203
|
+
const list = el.querySelector('.snav__subs');
|
|
204
|
+
if (moved && list && !list.hidden && !RM() && !railTl.current) {
|
|
205
|
+
g.fromTo(list, { height: 0, opacity: 0 }, { height: 'auto', opacity: 1, duration: 0.45, ease: 'power3.out', clearProps: 'height,opacity' });
|
|
206
|
+
g.fromTo(list.children, { opacity: 0, x: -8 }, { opacity: 1, x: 0, duration: 0.35, ease: 'power3.out', stagger: 0.03, delay: 0.06, clearProps: 'transform,opacity' });
|
|
207
|
+
}
|
|
208
|
+
}, [m.page, rail, phone]);
|
|
209
|
+
useIsoLayoutEffect(() => {
|
|
210
|
+
const now = {}; m.all.forEach((it) => { if (it.count !== undefined) now[it.id] = countOf(it); });
|
|
211
|
+
if (!first.current) for (const id in now) if (now[id] !== counts.current[id] && now[id] > 0) {
|
|
212
|
+
const c = side.current?.querySelector(`.snav__item[data-id="${CSS.escape(id)}"] .snav__count`); if (c) M.flash(c);
|
|
213
|
+
}
|
|
214
|
+
counts.current = now; first.current = false;
|
|
215
|
+
});
|
|
216
|
+
// moving somewhere closes the phone drawer and the flyout
|
|
217
|
+
useEffect(() => { if (!first.current) { setSideOpen(false); closeFly(true); } }, [path]);
|
|
218
|
+
|
|
219
|
+
const toggleSubs = () => {
|
|
220
|
+
const list = side.current?.querySelector('.snav__subs'); if (!list) return;
|
|
221
|
+
const open = subs.shut;
|
|
222
|
+
g.killTweensOf([list, ...list.children]);
|
|
223
|
+
if (open) {
|
|
224
|
+
flushSync(() => setSubs((s) => ({ ...s, shut: false, hidden: false })));
|
|
225
|
+
if (!RM()) {
|
|
226
|
+
g.fromTo(list, { height: 0, opacity: 0 }, { height: 'auto', opacity: 1, duration: 0.4, ease: 'power3.out', clearProps: 'height,opacity' });
|
|
227
|
+
g.fromTo(list.children, { opacity: 0, x: -8 }, { opacity: 1, x: 0, duration: 0.3, ease: 'power3.out', stagger: 0.03, delay: 0.05, clearProps: 'transform,opacity' });
|
|
228
|
+
}
|
|
229
|
+
} else if (RM()) setSubs((s) => ({ ...s, shut: true, hidden: true }));
|
|
230
|
+
else {
|
|
231
|
+
setSubs((s) => ({ ...s, shut: true }));
|
|
232
|
+
g.to(list, { height: 0, opacity: 0, duration: 0.28, ease: 'power3.inOut', onComplete: () => { flushSync(() => setSubs((s) => (s.shut ? { ...s, hidden: true } : s))); g.set(list, { clearProps: 'height,opacity' }); } });
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/* ----- rail flyout ----- */
|
|
237
|
+
const openFly = (item, a) => {
|
|
238
|
+
if (!state.current.rail || isMobile()) return;
|
|
239
|
+
flyTimer.current?.kill();
|
|
240
|
+
if (fly?.id === item.id && !fly.out) return;
|
|
241
|
+
const r = a.getBoundingClientRect();
|
|
242
|
+
setFly({ id: item.id, left: side.current.getBoundingClientRect().right + 10, y: r.top + r.height / 2 - 30 });
|
|
243
|
+
};
|
|
244
|
+
function closeFly(now = false) {
|
|
245
|
+
flyTimer.current?.kill();
|
|
246
|
+
const el = flyEl.current;
|
|
247
|
+
if (now || RM() || !el) { setFly(null); return; }
|
|
248
|
+
setFly((f) => (f ? { ...f, out: true } : f));
|
|
249
|
+
g.to(el, { opacity: 0, x: -6, duration: 0.14, ease: 'power2.in', onComplete: () => setFly((f) => (f?.out ? null : f)) });
|
|
250
|
+
}
|
|
251
|
+
const closeFlySoon = () => { flyTimer.current?.kill(); flyTimer.current = g.delayedCall(0.18, () => closeFly()); };
|
|
252
|
+
useIsoLayoutEffect(() => {
|
|
253
|
+
const el = flyEl.current; if (!el || fly?.out) return;
|
|
254
|
+
el.style.top = Math.max(8, Math.min(fly.y, innerHeight - el.offsetHeight - 8)) + 'px';
|
|
255
|
+
if (!RM()) {
|
|
256
|
+
g.fromTo(el, { opacity: 0, x: -10, scale: 0.97 }, { opacity: 1, x: 0, scale: 1, duration: 0.34, ease: 'back.out(1.6)', transformOrigin: '0 30px', clearProps: 'transform,opacity', overwrite: true });
|
|
257
|
+
g.fromTo(el.querySelectorAll('a'), { opacity: 0, x: -6 }, { opacity: 1, x: 0, duration: 0.3, ease: 'power3.out', stagger: 0.03, delay: 0.05, clearProps: 'transform,opacity' });
|
|
258
|
+
}
|
|
259
|
+
}, [fly?.id, fly?.out]);
|
|
260
|
+
|
|
261
|
+
/* ----- account menu ----- */
|
|
262
|
+
const openMe = (e) => {
|
|
263
|
+
const btn = e.currentTarget;
|
|
264
|
+
O.bindEscape();
|
|
265
|
+
O.menu(btn, toMenu([{ section: user.name }, ...(user.menu || [])]));
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/* ----- markup ----- */
|
|
269
|
+
const navItem = (n) => {
|
|
270
|
+
const on = n.id === m.page, c = countOf(n), list = m.subsOf(n), open = on && !!list && (!rail || phone);
|
|
271
|
+
return h('div', { className: 'snav__wrap', key: n.id },
|
|
272
|
+
h(NavLink, {
|
|
273
|
+
...L, className: 'snav__item', 'data-id': n.id, href: m.hrefOf(n), 'aria-current': on ? 'page' : undefined,
|
|
274
|
+
'aria-expanded': open ? !subs.shut : undefined, 'aria-controls': open ? `snav-subs-${n.id}` : undefined, 'aria-label': n.label,
|
|
275
|
+
onClick: open ? (e) => { e.preventDefault(); toggleSubs(); } : undefined,
|
|
276
|
+
onPointerEnter: (e) => { if (M.fine.matches) openFly(n, e.currentTarget); }, onPointerLeave: closeFlySoon,
|
|
277
|
+
onFocus: (e) => openFly(n, e.currentTarget), onBlur: closeFlySoon,
|
|
278
|
+
}, iconNode(n.icon), h('span', { className: 'snav__label' }, n.label), c > 0 ? h('span', { className: 'snav__count' }, c) : null,
|
|
279
|
+
list && !c ? h(Icon, { name: 'chevR', className: 'snav__chev' }) : null),
|
|
280
|
+
open ? h('div', { className: 'snav__subs', id: `snav-subs-${n.id}`, hidden: subs.hidden },
|
|
281
|
+
list.map((s) => h(NavLink, { ...L, key: s.id, href: s.href, 'aria-current': s.id === m.sub ? 'page' : undefined }, s.label))) : null);
|
|
282
|
+
};
|
|
283
|
+
const flyItem = fly && m.itemOf(fly.id);
|
|
284
|
+
const flyList = flyItem && m.subsOf(flyItem), flyCount = flyItem ? countOf(flyItem) : 0;
|
|
285
|
+
return h(Fragment, null,
|
|
286
|
+
h('aside', { className: 'side', id: 't-side', 'aria-label': W.sidebar, ref: side },
|
|
287
|
+
h('div', { className: 'side__head' },
|
|
288
|
+
h(NavLink, { ...L, className: 'brand', href: brand.href ?? (m.itemOf(home) ? m.hrefOf(m.itemOf(home)) : m.all[0] ? m.hrefOf(m.all[0]) : '/'), 'aria-label': brand.name },
|
|
289
|
+
h('span', { className: 'mark' }, iconNode(brand.icon || 'logo')),
|
|
290
|
+
h('span', { className: 'brand__text' }, h('b', null, brand.name), brand.sub ? h('small', null, brand.sub) : null)),
|
|
291
|
+
h('button', {
|
|
292
|
+
type: 'button', className: 'collapse', 'aria-label': rail ? W.expand : W.collapse, 'data-tip': rail ? W.expandTip : W.collapseTip,
|
|
293
|
+
'data-tip-side': rail ? 'right' : undefined, onClick: (e) => { M.pop(e.currentTarget); setRail(!state.current.rail); },
|
|
294
|
+
}, h(Icon, { name: 'panel' }))),
|
|
295
|
+
h('nav', { className: 'snav', 'aria-label': W.main },
|
|
296
|
+
h('span', { className: 'snav__pill', ref: pill }),
|
|
297
|
+
(nav || []).map((grp, i) => h('div', { className: 'snav__group', key: i }, grp.label ? h('span', { className: 'side__group' }, grp.label) : null, (grp.items || []).map(navItem))),
|
|
298
|
+
end?.length ? h('div', { className: 'snav__group snav__group--end' }, end.map(navItem)) : null),
|
|
299
|
+
user ? h('div', { className: 'side__foot' },
|
|
300
|
+
h('button', { type: 'button', className: 'side__me', 'aria-label': `${user.name} — ${W.account}`, 'data-tip': rail ? user.name : undefined, 'data-tip-side': rail ? 'right' : undefined, onClick: openMe },
|
|
301
|
+
h(Avatar, { person: user, online: user.online ?? true }),
|
|
302
|
+
h('span', { className: 'cell__text' }, h('b', null, user.name), user.role ? h('small', null, user.role) : null),
|
|
303
|
+
h(Icon, { name: 'sort' }))) : null),
|
|
304
|
+
h('div', { className: 'side__scrim', onClick: () => setSideOpen(false) }),
|
|
305
|
+
fly && flyItem ? createPortal(h('div', {
|
|
306
|
+
ref: flyEl, className: 'flyout', 'data-id': flyItem.id, role: 'menu', style: { left: fly.left, top: fly.y },
|
|
307
|
+
onPointerEnter: () => flyTimer.current?.kill(), onPointerLeave: closeFlySoon,
|
|
308
|
+
onClick: (e) => { if (e.target.closest('a')) closeFly(); },
|
|
309
|
+
},
|
|
310
|
+
h('div', { className: 'flyout__head' }, h('b', null, flyItem.label, flyCount ? h('span', { className: 'snav__count' }, flyCount) : null), flyItem.blurb ? h('small', null, flyItem.blurb) : null),
|
|
311
|
+
flyList
|
|
312
|
+
? flyList.map((s) => h(NavLink, { ...L, key: s.id, role: 'menuitem', href: s.href, 'aria-current': m.page === flyItem.id && m.sub === s.id ? 'page' : undefined }, h(Icon, { name: 'chevR' }), s.label))
|
|
313
|
+
: h(NavLink, { ...L, role: 'menuitem', href: m.hrefOf(flyItem), 'aria-current': m.page === flyItem.id ? 'page' : undefined }, h(Icon, { name: 'arrowR' }), `${W.open} ${String(flyItem.label).toLowerCase()}`)), document.body) : null);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const initials = (name = '') => String(name).split(/\s+/).filter(Boolean).slice(0, 2).map((w) => w[0]).join('').toUpperCase();
|
|
317
|
+
/* the same markup as av(person, '', online) */
|
|
318
|
+
const Avatar = ({ person, online }) => h('span', { className: `av av--${person.tone || 'indigo'}`, title: person.name }, person.ini ?? initials(person.name), online ? h('i', { className: 'av__on' }) : null);
|
|
319
|
+
|
|
320
|
+
/* ======================================================================= */
|
|
321
|
+
/* Topbar */
|
|
322
|
+
/* ======================================================================= */
|
|
323
|
+
/**
|
|
324
|
+
* <Topbar nav={nav} path={pathname} onNavigate={router.push} crumbsRoot="MotoRent"
|
|
325
|
+
* search={{ placeholder: 'Search bikes…', onOpen: openSearch }} bell={{ count: unread, onClick: openInbox }}
|
|
326
|
+
* primary={{ label: 'New booking', icon: 'plus', run: newBooking }}>{extra buttons}</Topbar>
|
|
327
|
+
* The bar above the page: menu button (phones), breadcrumbs from the route, a search button, your own
|
|
328
|
+
* buttons (children), a theme switch that spreads from the button, a bell whose badge pops when the number
|
|
329
|
+
* goes up, and one main action. It frosts over the page once you scroll.
|
|
330
|
+
*/
|
|
331
|
+
export const Topbar = forwardRef(function Topbar({ nav, end, path, base, home, onNavigate, link, crumbsRoot = '', search, theme = true, bell, primary, onSearch, storageKey = 'gecko', words, children }, ref) {
|
|
332
|
+
const W = wordsOf(words);
|
|
333
|
+
const m = model({ nav, end, base, path, home });
|
|
334
|
+
const L = { onNavigate, link };
|
|
335
|
+
const top = useRef(null), menuBtn = useRef(null), bellBtn = useRef(null), crumbs = useRef(null), first = useRef(true);
|
|
336
|
+
const [stuck, setStuck] = useState(false);
|
|
337
|
+
useImperativeHandle(ref, () => top.current, []);
|
|
338
|
+
useEffect(() => {
|
|
339
|
+
if (theme !== false) M.restoreTheme(`${storageKey}:theme`);
|
|
340
|
+
const onScroll = () => setStuck(scrollY > 4); onScroll();
|
|
341
|
+
addEventListener('scroll', onScroll, { passive: true });
|
|
342
|
+
const b = M.bindBurger(menuBtn.current, { controls: 't-side', escape: false, onToggle: (open) => setSideOpen(open) });
|
|
343
|
+
const sync = (e) => b?.set(e.detail, { silent: true });
|
|
344
|
+
addEventListener(SIDE_EVENT, sync);
|
|
345
|
+
M.magnetic(top.current);
|
|
346
|
+
return () => { removeEventListener('scroll', onScroll); removeEventListener(SIDE_EVENT, sync); b?.destroy(); };
|
|
347
|
+
}, []);
|
|
348
|
+
// breadcrumbs slide in after a move
|
|
349
|
+
useIsoLayoutEffect(() => { if (!first.current && crumbs.current) M.slide(crumbs.current.children, { x: -6, y: 0, stagger: 0.02 }); }, [m.page, m.sub]);
|
|
350
|
+
// the bell wiggles and its badge pops when the number goes up
|
|
351
|
+
const count = bell?.count ?? 0, prevCount = useRef(count);
|
|
352
|
+
useIsoLayoutEffect(() => {
|
|
353
|
+
const was = prevCount.current; prevCount.current = count;
|
|
354
|
+
if (first.current || count <= was || RM() || !bellBtn.current) return;
|
|
355
|
+
M.wiggle(bellBtn.current.querySelector('svg'));
|
|
356
|
+
g.fromTo(bellBtn.current.querySelector('.bell__n'), { scale: 0.4 }, { scale: 1, duration: 0.6, ease: 'back.out(3)' });
|
|
357
|
+
}, [count]);
|
|
358
|
+
useEffect(() => { first.current = false; }, []);
|
|
359
|
+
|
|
360
|
+
const subLabel = m.sub && m.subsOf(m.item)?.find((s) => s.id === m.sub)?.label;
|
|
361
|
+
const s = search === false ? null : search || {};
|
|
362
|
+
return h('header', { className: cx('top', stuck && 'is-stuck'), ref: top },
|
|
363
|
+
h('div', { className: 'top__in' },
|
|
364
|
+
h('button', { type: 'button', className: 'iconbtn top__menu', 'aria-label': W.menu, ref: menuBtn }, h('span', { className: 'burger', 'aria-hidden': true }, h('i'), h('i'), h('i'))),
|
|
365
|
+
h('nav', { className: 'crumbs', 'aria-label': W.breadcrumb, ref: crumbs },
|
|
366
|
+
crumbsRoot ? h(Fragment, null, h('span', null, crumbsRoot), h(Icon, { name: 'chevR' })) : null,
|
|
367
|
+
m.item ? h(NavLink, { ...L, href: m.hrefOf(m.item) }, m.item.label) : null,
|
|
368
|
+
subLabel ? h(Fragment, null, h(Icon, { name: 'chevR' }), h('b', null, subLabel)) : null),
|
|
369
|
+
s ? h('button', { type: 'button', className: 'search', 'aria-label': W.search, onClick: () => (s.onOpen ? s.onOpen() : onSearch?.()) },
|
|
370
|
+
h(Icon, { name: 'search' }), h('span', null, s.placeholder || W.searchPlaceholder), h('kbd', null, W.searchKey)) : null,
|
|
371
|
+
children,
|
|
372
|
+
theme !== false ? h('button', {
|
|
373
|
+
type: 'button', className: 'iconbtn top__theme', 'aria-label': W.theme, 'data-tip': W.themeTip,
|
|
374
|
+
onClick: (e) => { M.pop(e.currentTarget); M.toggleTheme(e.currentTarget, { storageKey: `${storageKey}:theme` }); },
|
|
375
|
+
}, h(Icon, { name: 'moon', className: 'i-moon' }), h(Icon, { name: 'sun', className: 'i-sun' })) : null,
|
|
376
|
+
bell ? h('button', { type: 'button', className: 'iconbtn top__bell', 'aria-label': W.bell, 'data-tip': W.bell, ref: bellBtn, onClick: (e) => bell.onClick?.(e.currentTarget) },
|
|
377
|
+
h(Icon, { name: 'bell' }), h('span', { className: 'bell__n', hidden: !count }, count)) : null,
|
|
378
|
+
primary ? h('button', { type: 'button', className: 'btn btn--primary top__new magnetic', 'data-tip': primary.tip || '', onClick: (e) => primary.run?.(e.currentTarget) },
|
|
379
|
+
iconNode(primary.icon || 'plus'), h('span', null, primary.label || W.primary)) : null));
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
/* ======================================================================= */
|
|
383
|
+
/* Mobile tab bar */
|
|
384
|
+
/* ======================================================================= */
|
|
385
|
+
/**
|
|
386
|
+
* <MobileTabbar nav={nav} end={end} path={pathname} onNavigate={router.push} tabbar={['today', 'bikes', 'bookings', 'riders']} />
|
|
387
|
+
* On phones (CSS: 760px and below) a tab bar sits at the bottom with your main sections; a soft highlight
|
|
388
|
+
* stretches to the current one and “More” lists the rest. Hidden on wider screens.
|
|
389
|
+
*/
|
|
390
|
+
export const MobileTabbar = forwardRef(function MobileTabbar({ nav, end, path, base, home, onNavigate, link, tabbar, words }, ref) {
|
|
391
|
+
const W = wordsOf(words);
|
|
392
|
+
const m = model({ nav, end, base, path, home });
|
|
393
|
+
const L = { onNavigate, link };
|
|
394
|
+
const el = useRef(null), line = useRef(null);
|
|
395
|
+
const ids = tabbar || m.all.slice(0, 4).map((it) => it.id);
|
|
396
|
+
const rest = m.all.filter((it) => !ids.includes(it.id));
|
|
397
|
+
const target = () => el.current?.querySelector('a[aria-current]') || el.current?.querySelector('[data-more]');
|
|
398
|
+
useImperativeHandle(ref, () => el.current, []);
|
|
399
|
+
useIsoLayoutEffect(() => M.trackIndicator(line.current, target, { radius: 14 }), []);
|
|
400
|
+
useIsoLayoutEffect(() => { M.indicator(line.current, target(), { radius: 14 }); }, [m.page]);
|
|
401
|
+
return h('nav', { className: 'tabbar', 'aria-label': W.tabs, ref: el },
|
|
402
|
+
h('span', { className: 'tabbar__line', ref: line }),
|
|
403
|
+
ids.map((id) => { const it = m.itemOf(id); return it ? h(NavLink, { ...L, key: id, href: m.hrefOf(it), 'data-id': id, 'aria-current': m.page === id ? 'page' : undefined }, iconNode(it.icon), h('small', null, it.label)) : null; }),
|
|
404
|
+
rest.length ? h('button', {
|
|
405
|
+
type: 'button', 'data-more': '', onClick: (e) => { O.bindEscape(); O.menu(e.currentTarget, toMenu(rest.map((it) => ({ icon: it.icon, label: it.label, run: () => go(onNavigate, m.hrefOf(it)) }))), { align: 'end' }); },
|
|
406
|
+
}, h(Icon, { name: 'menu' }), h('small', null, W.more)) : null);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
/* ======================================================================= */
|
|
410
|
+
/* Page header */
|
|
411
|
+
/* ======================================================================= */
|
|
412
|
+
/* the same markup split() from @gecko-ui-kit/motion makes, so title() animates it without touching it.
|
|
413
|
+
Letters are grapheme clusters, so Thai vowels and tone marks stay on their letter. */
|
|
414
|
+
const seg = typeof Intl !== 'undefined' && typeof Intl.Segmenter === 'function' ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null;
|
|
415
|
+
const chars = (w) => (seg ? [...seg.segment(w)].map((x) => x.segment) : [...w]);
|
|
416
|
+
const splitTitle = (t) => String(t).trim().split(' ').map((w, i) => h(Fragment, { key: i }, i ? ' ' : null,
|
|
417
|
+
h('span', { className: 'word', 'aria-hidden': true }, chars(w).map((c, j) => h('span', { className: 'ch', key: j }, c)))));
|
|
418
|
+
const normTab = (t) => (Array.isArray(t) ? { id: String(t[0]), label: t[1] ?? t[0], count: t[2], href: t[3] } : { ...t, id: String(t.id) });
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* <PageHeader eyebrow="Monday 7 December" title="Bookings" sub="Who rides what, day by day."
|
|
422
|
+
* actions={<Button variant="primary" icon="plus">New booking</Button>} counts={{ requests: 3 }} />
|
|
423
|
+
* The top of a page: an eyebrow, a big title whose letters roll up (again when it changes), a line of text,
|
|
424
|
+
* buttons on the right and tabs with an underline that stretches to the chosen one.
|
|
425
|
+
* Inside <AppShell>, an item with sub-pages gets its sub-pages as link tabs by itself (tabs={false} hides them).
|
|
426
|
+
* Or pass tabs: [{ id, label, count, href }] (or [id, label, count, href]) — with href they are links,
|
|
427
|
+
* without they are buttons: value + onTabChange(id).
|
|
428
|
+
*/
|
|
429
|
+
export const PageHeader = forwardRef(function PageHeader({ title = '', eyebrow, sub, actions, tabs, value, onTabChange, counts, className, words }, ref) {
|
|
430
|
+
const W = wordsOf(words);
|
|
431
|
+
const ctx = useContext(ShellCtx);
|
|
432
|
+
const el = useRef(null), h1 = useRef(null), line = useRef(null), tabsEl = useRef(null), first = useRef(true);
|
|
433
|
+
useImperativeHandle(ref, () => el.current, []);
|
|
434
|
+
let list = null, current = value, links = false, L = {};
|
|
435
|
+
if (Array.isArray(tabs)) { list = tabs.map(normTab); links = list.some((t) => t.href != null); if (ctx) L = ctx.linkProps; }
|
|
436
|
+
else if (tabs !== false && ctx) {
|
|
437
|
+
const r = ctx.model, it = r.item, subs = r.subsOf(it);
|
|
438
|
+
if (subs && (it.tabs ?? ctx.tabs) !== false) { list = subs.map((s) => ({ id: s.id, label: s.label, href: s.href })); current = r.sub; links = true; L = ctx.linkProps; }
|
|
439
|
+
}
|
|
440
|
+
if (list && counts) list = list.map((t) => (counts[t.id] != null ? { ...t, count: counts[t.id] } : t));
|
|
441
|
+
const cur = () => tabsEl.current?.querySelector('[aria-current="page"], [aria-selected="true"]');
|
|
442
|
+
|
|
443
|
+
useIsoLayoutEffect(() => {
|
|
444
|
+
M.title(h1.current);
|
|
445
|
+
if (first.current) M.slide(el.current.querySelectorAll('.phead__eyebrow, .phead p, .phead__actions > *'), { y: 10, stagger: 0.05, delay: 0.12 });
|
|
446
|
+
}, [title]);
|
|
447
|
+
useIsoLayoutEffect(() => { if (!first.current) { const p = el.current.querySelector('.phead p'); if (p) M.slide([p], { y: 6 }); } }, [typeof sub === 'string' ? sub : null]);
|
|
448
|
+
useIsoLayoutEffect(() => { if (line.current) M.indicator(line.current, cur(), { animate: !first.current, radius: 2, line: true }); }, [current, !!list]);
|
|
449
|
+
useIsoLayoutEffect(() => (line.current ? M.trackIndicator(line.current, cur, { radius: 2, line: true }) : undefined), [!!list]);
|
|
450
|
+
useEffect(() => { first.current = false; }, []);
|
|
451
|
+
|
|
452
|
+
const badge = (n) => (n ? h('span', { className: 'badge' }, n) : null);
|
|
453
|
+
return h(Fragment, null,
|
|
454
|
+
h('header', { className: cx('phead', className), ref: el },
|
|
455
|
+
h('div', { className: 'phead__text' },
|
|
456
|
+
eyebrow ? h('span', { className: 'phead__eyebrow' }, eyebrow) : null,
|
|
457
|
+
h('h1', { ref: h1, 'data-text': String(title), 'data-split': String(title), 'aria-label': String(title) }, splitTitle(title)),
|
|
458
|
+
sub ? h('p', null, sub) : null),
|
|
459
|
+
actions ? h('div', { className: 'phead__actions' }, actions) : null),
|
|
460
|
+
list ? (links
|
|
461
|
+
? h('nav', { className: 'tabs', 'aria-label': W.sections, ref: tabsEl }, h('span', { className: 'tabs__line', ref: line }),
|
|
462
|
+
list.map((t) => h(NavLink, { ...L, key: t.id, href: t.href ?? '#', 'aria-current': t.id === current ? 'page' : undefined }, t.label, badge(t.count))))
|
|
463
|
+
: h('div', { className: 'tabs', role: 'tablist', ref: tabsEl }, h('span', { className: 'tabs__line', ref: line }),
|
|
464
|
+
list.map((t) => h('button', {
|
|
465
|
+
key: t.id, type: 'button', role: 'tab', 'data-v': t.id, 'aria-selected': t.id === current,
|
|
466
|
+
onClick: () => { if (t.id !== current) onTabChange?.(t.id); },
|
|
467
|
+
}, t.label, badge(t.count))))) : null);
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
/* ======================================================================= */
|
|
471
|
+
/* AppShell */
|
|
472
|
+
/* ======================================================================= */
|
|
473
|
+
/**
|
|
474
|
+
* A script for <head> that puts the saved theme and rail on <html> before the first paint (no jump).
|
|
475
|
+
* <script dangerouslySetInnerHTML={{ __html: shellBootScript({ storageKey: 'motorent' }) }} />
|
|
476
|
+
* (Add suppressHydrationWarning to your <html>, since this changes it before React wakes up.)
|
|
477
|
+
*/
|
|
478
|
+
export const shellBootScript = ({ storageKey = 'gecko', railBreakpoint = 1280 } = {}) =>
|
|
479
|
+
`try{var d=document.documentElement,k=${JSON.stringify(String(storageKey)).replace(/</g, '\\u003c')},t=localStorage.getItem(k+':theme');if(t)d.dataset.theme=t;var p=localStorage.getItem(k+':pinned');if(p==='0'||(p===null&&innerWidth<${Number(railBreakpoint) || 1280}))d.classList.add('is-rail')}catch(e){}`;
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* <AppShell brand={…} nav={nav} end={end} user={me} path={usePathname()} onNavigate={(href) => router.push(href)}
|
|
483
|
+
* top={{ search: { placeholder: 'Search…' }, bell: { count, onClick }, primary: { label: 'New', run } }}>
|
|
484
|
+
* {page content}
|
|
485
|
+
* </AppShell>
|
|
486
|
+
* The whole frame: Sidebar (rail, flyout), Topbar, your page in <main class="view">, the phone tab bar,
|
|
487
|
+
* the ⌘K palette (pages + your search(q) groups), keyboard shortcuts (⌘K / search, [ rail, ? list, Esc close,
|
|
488
|
+
* your keys) and the intro. When the path changes the new page slides in from the direction of travel.
|
|
489
|
+
* Use <PageHeader> at the top of each page. useShell() inside gives { route, navigate, palette, setRail… }.
|
|
490
|
+
*/
|
|
491
|
+
export function AppShell({ brand, nav, end, user, path, base, home, onNavigate, link, top = {}, crumbsRoot, tabbar, tabs = true, intro = true, shortcuts = true,
|
|
492
|
+
keys, search, storageKey = 'gecko', railBreakpoint = 1280, mobile = 760, words, className, children }) {
|
|
493
|
+
const W = wordsOf(words);
|
|
494
|
+
const m = model({ nav, end, base, path, home });
|
|
495
|
+
const sideRef = useRef(null), view = useRef(null), root = useRef(null), pal = useRef(null), introTl = useRef(null), prev = useRef(null);
|
|
496
|
+
const latest = useLatest({ m, onNavigate, keys, search, top, W, intro, shortcuts });
|
|
497
|
+
const navigate = (href) => go(latest.current.onNavigate, href);
|
|
498
|
+
|
|
499
|
+
const palette = () => {
|
|
500
|
+
const { m: mm, W: w, search: extra, top: t } = latest.current;
|
|
501
|
+
const pages = mm.all.flatMap((n) => {
|
|
502
|
+
const list = mm.subsOf(n), ic = typeof n.icon === 'string' ? n.icon : undefined;
|
|
503
|
+
return list ? list.map((s) => ({ title: String(s.label), sub: String(n.label), icon: ic, run: () => navigate(s.href) }))
|
|
504
|
+
: [{ title: String(n.label), sub: n.blurb || '', icon: ic, run: () => navigate(mm.hrefOf(n)) }];
|
|
505
|
+
});
|
|
506
|
+
O.bindEscape();
|
|
507
|
+
const l = O.palette({
|
|
508
|
+
placeholder: t.search?.placeholder || w.searchPages,
|
|
509
|
+
search: (q) => [
|
|
510
|
+
...(extra?.(q) || []).map((gr) => ({ label: gr.label ?? '', items: (gr.items || []).map(({ html, ...it }) => it) })),
|
|
511
|
+
{ label: w.goTo, items: pages.filter((p) => !q || (p.title + ' ' + p.sub).toLowerCase().includes(q)).slice(0, q ? 12 : 8) },
|
|
512
|
+
],
|
|
513
|
+
});
|
|
514
|
+
if (l) pal.current = l;
|
|
515
|
+
};
|
|
516
|
+
const openSearch = () => { const s = latest.current.top.search; if (s === false) return; if (s?.onOpen) s.onOpen(); else palette(); };
|
|
517
|
+
const showShortcuts = () => {
|
|
518
|
+
const { W: w, keys: k } = latest.current;
|
|
519
|
+
const list = [['⌘ K or /', w.search], ['[', w.toggleRail], ['?', w.thisList], ['Esc', w.close], ...(k || []).map((x) => [x.key, x.label])];
|
|
520
|
+
O.bindEscape();
|
|
521
|
+
O.modal({
|
|
522
|
+
title: esc(w.shortcuts), size: 'sm',
|
|
523
|
+
body: `<dl class="keys">${list.map(([kk, v]) => `<dt>${String(kk).split(' ').map((x) => (x === 'or' || x === 'then' ? `<em>${x}</em>` : `<kbd>${esc(x)}</kbd>`)).join(' ')}</dt><dd>${esc(v)}</dd>`).join('')}</dl>`,
|
|
524
|
+
onMount: (el) => M.slide(el.querySelectorAll('dt, dd'), { y: 6, stagger: 0.015 }),
|
|
525
|
+
});
|
|
526
|
+
};
|
|
527
|
+
const playIntro = () => {
|
|
528
|
+
const el = root.current; if (!el) return null;
|
|
529
|
+
introTl.current?.progress(1);
|
|
530
|
+
const side = el.querySelector('.side'), pill = el.querySelector('.snav__pill'), phone = innerWidth <= mobile;
|
|
531
|
+
M.ready.v = false;
|
|
532
|
+
const tl = g.timeline({ onComplete: () => { M.ready.v = true; } });
|
|
533
|
+
if (!phone && side) {
|
|
534
|
+
tl.fromTo(side, { x: -30, opacity: 0 }, { x: 0, opacity: 1, duration: D(0.7), ease: 'expo.out', clearProps: 'transform,opacity' });
|
|
535
|
+
tl.fromTo($$('.side__head > *, .side__group, .snav__item, .snav__subs > a, .side__foot > *', side), { opacity: 0, x: -10 }, { opacity: 1, x: 0, duration: D(0.5), ease: 'power3.out', stagger: D(0.025), clearProps: 'transform,opacity' }, D(0.1));
|
|
536
|
+
tl.fromTo(pill, { scale: 0.4, opacity: 0 }, { scale: 1, opacity: 1, duration: D(0.6), ease: 'back.out(2)', transformOrigin: '50% 50%', clearProps: 'transform' }, D(0.45));
|
|
537
|
+
}
|
|
538
|
+
const bits = el.querySelectorAll('.top__in > *');
|
|
539
|
+
if (bits.length) tl.fromTo(bits, { y: -12, opacity: 0 }, { y: 0, opacity: 1, duration: D(0.6), ease: 'expo.out', stagger: D(0.05), clearProps: 'transform,opacity' }, D(0.15));
|
|
540
|
+
const tb = el.querySelector('.tabbar');
|
|
541
|
+
if (phone && tb) tl.fromTo(tb, { y: 90 }, { y: 0, duration: D(0.7), ease: 'expo.out', clearProps: 'transform' }, D(0.2));
|
|
542
|
+
if (RM()) M.ready.v = true;
|
|
543
|
+
introTl.current = tl;
|
|
544
|
+
return tl;
|
|
545
|
+
};
|
|
546
|
+
|
|
547
|
+
const ctx = useMemo(() => ({
|
|
548
|
+
model: m, tabs, linkProps: { onNavigate, link }, path, route: { page: m.page, sub: m.sub }, navigate,
|
|
549
|
+
palette: openSearch, shortcuts: showShortcuts, intro: () => { playIntro(); },
|
|
550
|
+
setRail: (r) => sideRef.current?.setRail(r), isRail: () => !!sideRef.current?.isRail(),
|
|
551
|
+
}), [path, nav, end, base, home, tabs, onNavigate, link]);
|
|
552
|
+
|
|
553
|
+
const ctxRef = useLatest(ctx);
|
|
554
|
+
// boot: overlay width, intro, keys
|
|
555
|
+
useIsoLayoutEffect(() => {
|
|
556
|
+
O.config.mobile = mobile;
|
|
557
|
+
if (latest.current.intro) playIntro();
|
|
558
|
+
const onKey = (e) => {
|
|
559
|
+
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') { e.preventDefault(); openSearch(); return; }
|
|
560
|
+
if (e.key === 'Escape') {
|
|
561
|
+
if (e.defaultPrevented) return;
|
|
562
|
+
if (O.closeTop()) { e.preventDefault(); return; }
|
|
563
|
+
if (document.documentElement.classList.contains('is-sideopen')) { setSideOpen(false); root.current?.querySelector('.top__menu')?.focus(); }
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
if (!latest.current.shortcuts || e.target.closest?.('input, textarea, select, [contenteditable]') || e.metaKey || e.ctrlKey || e.altKey || O.stack.length) return;
|
|
567
|
+
if (e.key === '/') { e.preventDefault(); openSearch(); }
|
|
568
|
+
else if (e.key === '?') showShortcuts();
|
|
569
|
+
else if (e.key === '[') sideRef.current?.setRail(!sideRef.current.isRail());
|
|
570
|
+
else (latest.current.keys || []).find((k) => String(k.key).toLowerCase() === e.key.toLowerCase())?.run?.(ctxRef.current);
|
|
571
|
+
};
|
|
572
|
+
addEventListener('keydown', onKey);
|
|
573
|
+
return () => {
|
|
574
|
+
removeEventListener('keydown', onKey);
|
|
575
|
+
introTl.current?.kill(); introTl.current = null; M.ready.v = true;
|
|
576
|
+
if (pal.current && !pal.current.closing) pal.current.close(); pal.current = null;
|
|
577
|
+
};
|
|
578
|
+
}, []);
|
|
579
|
+
|
|
580
|
+
// a new page slides in from the direction of travel; popovers and tours close
|
|
581
|
+
useIsoLayoutEffect(() => {
|
|
582
|
+
const was = prev.current; prev.current = m;
|
|
583
|
+
if (!was || (was.page === m.page && was.sub === m.sub)) return;
|
|
584
|
+
O.closeAll('pop'); O.closeAll('tour');
|
|
585
|
+
const v = view.current; if (!v || RM()) return;
|
|
586
|
+
const dir = m.rank >= was.rank ? 1 : -1;
|
|
587
|
+
g.fromTo(v, { x: dir * M.swapTiming.enter }, { x: 0, duration: M.swapTiming.in, ease: 'expo.out', overwrite: true, clearProps: 'transform' });
|
|
588
|
+
M.rise(v.querySelectorAll(RISE), { stagger: 0.05 });
|
|
589
|
+
}, [m.page, m.sub]);
|
|
590
|
+
|
|
591
|
+
const me = user ? { ...user, menu: [...(user.menu || []), ...(shortcuts || intro ? ['-'] : []),
|
|
592
|
+
...(shortcuts ? [{ icon: 'help', label: W.shortcuts, kbd: '?', run: showShortcuts }] : []),
|
|
593
|
+
...(intro ? [{ icon: 'refresh', label: W.replay, run: () => playIntro() }] : [])] } : null;
|
|
594
|
+
const bag = { nav, end, path, base, home, onNavigate, link, words };
|
|
595
|
+
return h(ShellCtx.Provider, { value: ctx },
|
|
596
|
+
h('div', { className: cx('t-app', className), ref: root },
|
|
597
|
+
h(Sidebar, { ...bag, ref: sideRef, brand, user: me, storageKey, railBreakpoint, mobile }),
|
|
598
|
+
h('div', { className: 't-main' },
|
|
599
|
+
h(Topbar, { ...bag, crumbsRoot, search: top.search, theme: top.theme, bell: top.bell, primary: top.primary, onSearch: palette, storageKey }, top.extra),
|
|
600
|
+
h('main', { className: 'view', tabIndex: -1, ref: view }, children)),
|
|
601
|
+
h(MobileTabbar, { ...bag, tabbar })));
|
|
602
|
+
}
|