@larakit/react 0.0.0-stage → 0.5.1
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/package.json +30 -4
- package/src/Cell.tsx +35 -0
- package/src/Dashboard.tsx +79 -0
- package/src/Field.tsx +132 -0
- package/src/FileInput.tsx +76 -0
- package/src/Form.tsx +116 -0
- package/src/Layout.tsx +250 -0
- package/src/PanelPages.tsx +115 -0
- package/src/Repeater.tsx +46 -0
- package/src/ResourceIndex.tsx +271 -0
- package/src/action-upload.test.tsx +44 -0
- package/src/blocks.tsx +26 -0
- package/src/button-part.test.tsx +42 -0
- package/src/components.test.ts +22 -0
- package/src/components.tsx +435 -0
- package/src/confirm.test.tsx +68 -0
- package/src/context.tsx +19 -0
- package/src/custom-pages.test.tsx +177 -0
- package/src/errors.test.tsx +95 -0
- package/src/fixtures.ts +20 -0
- package/src/index.ts +29 -0
- package/src/kit/api.ts +305 -0
- package/src/kit/auth.tsx +109 -0
- package/src/kit/bridge.tsx +142 -0
- package/src/kit/button.tsx +398 -0
- package/src/kit/cell.tsx +260 -0
- package/src/kit/charts.tsx +638 -0
- package/src/kit/create.tsx +166 -0
- package/src/kit/dashboard.tsx +335 -0
- package/src/kit/dates.tsx +62 -0
- package/src/kit/entry.tsx +87 -0
- package/src/kit/field.tsx +449 -0
- package/src/kit/fields/CodeEditor.tsx +157 -0
- package/src/kit/fields/KeyValue.tsx +215 -0
- package/src/kit/fields/MarkdownEditor.tsx +380 -0
- package/src/kit/fields/OneTimeCode.tsx +192 -0
- package/src/kit/fields/Repeater.tsx +350 -0
- package/src/kit/fields/Slider.tsx +185 -0
- package/src/kit/fields/TagsInput.tsx +219 -0
- package/src/kit/filters.tsx +214 -0
- package/src/kit/form-shared.tsx +62 -0
- package/src/kit/form.tsx +253 -0
- package/src/kit/hooks.ts +119 -0
- package/src/kit/i18n.tsx +67 -0
- package/src/kit/icons.tsx +190 -0
- package/src/kit/infolist.tsx +452 -0
- package/src/kit/kit.test.tsx +2210 -0
- package/src/kit/layout.tsx +236 -0
- package/src/kit/loading.tsx +120 -0
- package/src/kit/notice.tsx +72 -0
- package/src/kit/page.tsx +103 -0
- package/src/kit/pageform.tsx +75 -0
- package/src/kit/picker.tsx +110 -0
- package/src/kit/plugins.tsx +206 -0
- package/src/kit/profile.tsx +86 -0
- package/src/kit/relations.tsx +146 -0
- package/src/kit/rich.tsx +276 -0
- package/src/kit/search.tsx +92 -0
- package/src/kit/select.tsx +244 -0
- package/src/kit/shell.tsx +401 -0
- package/src/kit/table-extras.tsx +457 -0
- package/src/kit/table.tsx +734 -0
- package/src/kit/theme.ts +57 -0
- package/src/layout-parts.test.tsx +159 -0
- package/src/pages/index.ts +7 -0
- package/src/part.test.tsx +56 -0
- package/src/plugins.test.tsx +159 -0
- package/src/plugins.tsx +240 -0
- package/src/preview.test.tsx +81 -0
- package/src/rail.test.tsx +56 -0
- package/src/repeater.test.tsx +48 -0
- package/src/resource-pages.test.tsx +119 -0
- package/src/screens.test.tsx +121 -0
- package/src/ssr.test.tsx +86 -0
- package/src/tenant.test.tsx +94 -0
- package/src/ui.tsx +100 -0
- package/theme.css +467 -0
- package/README.md +0 -3
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { Fragment, useContext, useEffect, useId, useMemo, useRef, useState, type KeyboardEvent as ReactKeyboardEvent, type ReactNode } from 'react';
|
|
2
|
+
import { api, currentPanelPath, fail, type Link, type Notification, type Panel, type TenantInfo } from './api';
|
|
3
|
+
import { LinkBtn, postLink } from './button';
|
|
4
|
+
import { ActionBtn } from './create';
|
|
5
|
+
import { Menu } from './filters';
|
|
6
|
+
import { TONES, useBrowserState } from './hooks';
|
|
7
|
+
import { useT } from './i18n';
|
|
8
|
+
import { Icon } from './icons';
|
|
9
|
+
import { bars, Guard, PanelHookContext, RenderHook, replaces, Slot, type Parts } from './plugins';
|
|
10
|
+
import type { useTheme } from './theme';
|
|
11
|
+
import { shown } from './cell';
|
|
12
|
+
import { Search, searchable } from './search';
|
|
13
|
+
import { LayoutPart } from '../components';
|
|
14
|
+
|
|
15
|
+
type Theme = ReturnType<typeof useTheme>;
|
|
16
|
+
type Place = 'topbar' | 'side';
|
|
17
|
+
|
|
18
|
+
/** A link to another site opens in a new tab. */
|
|
19
|
+
function Anchor({ link: l, className, children, title }: { link: Link; className: string; children: ReactNode; title?: string }) {
|
|
20
|
+
if (l.post) return <button type="button" className={className} title={title} onClick={() => void postLink(l.href)}>{children}</button>;
|
|
21
|
+
const out = l.href.startsWith('https://') ? { target: '_blank', rel: 'noreferrer' } : {};
|
|
22
|
+
return <a href={l.href} className={className} title={title} {...out}>{children}</a>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const initials = (name: string) => name.trim().split(/\s+/).filter(Boolean).map((w) => [...w][0]).filter((_, i, all) => i === 0 || i === all.length - 1).join('').toUpperCase();
|
|
26
|
+
|
|
27
|
+
function Avatar({ panel }: { panel: Panel }) {
|
|
28
|
+
const { name, avatar } = panel.user!, [bad, setBad] = useState(false);
|
|
29
|
+
return avatar && !bad ? <img src={avatar} alt="" className="size-8 shrink-0 rounded-full object-cover" onError={() => setBad(true)} />
|
|
30
|
+
: <span aria-hidden="true" className="avatar grid size-8 shrink-0 place-items-center rounded-full text-xs font-semibold">{initials(name)}</span>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function Modes({ theme: [, choose, choice] }: { theme: Theme }) {
|
|
34
|
+
const t = useT();
|
|
35
|
+
return <div role="group" aria-label={t('theme.label')} data-lk="mode" className="grid grid-cols-3 gap-1">
|
|
36
|
+
{([['light', t('theme.light'), 'sun'], ['dark', t('theme.dark'), 'moon'], ['system', t('theme.system'), 'screen']] as const).map(([m, label, icon]) =>
|
|
37
|
+
<button key={m} type="button" data-name={m} title={label} aria-label={label} aria-pressed={choice === m} className="tool seg mx-0 w-full" onClick={() => choose(m)}><Icon name={icon} /></button>)}
|
|
38
|
+
</div>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function UserMenu({ panel, theme, place, rail }: { panel: Panel; theme: Theme; place: Place; rail: boolean }) {
|
|
42
|
+
const t = useT();
|
|
43
|
+
const user = panel.user!, wide = place === 'side' && !rail;
|
|
44
|
+
return <Menu name="user" title={t('user.menu')} width={240} button={wide ? 'tool w-full justify-start gap-3 px-2' : 'avatar-btn rounded-full'} pad="p-2"
|
|
45
|
+
label={<><Avatar panel={panel} />{wide && <span className="min-w-0 flex-1 text-left"><span className="block truncate text-sm font-semibold">{user.name}</span>{user.detail && <span className="muted block truncate text-xs">{user.detail}</span>}</span>}</>}>
|
|
46
|
+
<div className="border-b px-2 pb-3"><p className="truncate text-sm font-semibold">{user.name}</p>{user.detail && <p className="muted truncate text-xs">{user.detail}</p>}</div>
|
|
47
|
+
{panel.theme.mode === 'system' && <div className="border-b py-2"><Modes theme={theme} /></div>}
|
|
48
|
+
{(panel.userMenu ?? []).length > 0 && <ul className="pt-2">{panel.userMenu!.map((l) => <li key={l.label}><Anchor link={l} className="menu-item">{l.icon && <Icon name={l.icon} className="muted size-5" />}{l.label}</Anchor></li>)}</ul>}
|
|
49
|
+
</Menu>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const ago = (at: string) => shown(at, { format: 'since' });
|
|
53
|
+
|
|
54
|
+
function Bell({ poll }: { poll: number }) {
|
|
55
|
+
const t = useT();
|
|
56
|
+
const [list, setList] = useState<Notification[]>(), [unread, setUnread] = useState(0), [problem, setProblem] = useState('');
|
|
57
|
+
const take = (a: { data: Notification[]; unread: number }) => { setList(a.data); setUnread(a.unread); setProblem(''); };
|
|
58
|
+
const asked = useRef(0);
|
|
59
|
+
// An older answer must not undo a newer one.
|
|
60
|
+
const ask = (work: Promise<{ data: Notification[]; unread: number }>) => {
|
|
61
|
+
const mine = ++asked.current;
|
|
62
|
+
return work.then((a) => { if (mine === asked.current) take(a); }, (e) => { if (mine === asked.current) setProblem(fail(e)); });
|
|
63
|
+
};
|
|
64
|
+
const load = () => ask(api('/notifications'));
|
|
65
|
+
const read = (body: object) => ask(api('/notifications/read', 'POST', body));
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
load();
|
|
68
|
+
if (!poll) return;
|
|
69
|
+
const t = setInterval(() => { if (!document.hidden) load(); }, poll * 1000);
|
|
70
|
+
return () => clearInterval(t);
|
|
71
|
+
}, [poll]);
|
|
72
|
+
const line = (n: Notification) => <>
|
|
73
|
+
<span className={'grid size-9 shrink-0 place-items-center rounded-full ' + (n.color && TONES.includes(n.color) ? 'tone-' + n.color : 'tone-gray')}><Icon name={n.icon || 'bell'} className="size-5" /></span>
|
|
74
|
+
<span className="min-w-0 flex-1"><span className={'block text-sm break-words ' + (n.read ? '' : 'font-semibold')}>{n.title}{!n.read && <span className="sr-only"> {t('notifications.unreadTag')}</span>}</span>{n.body && <span className="muted block text-sm break-words">{n.body}</span>}<span className="muted block text-xs">{ago(n.at)}</span></span>
|
|
75
|
+
{!n.read && <span aria-hidden="true" className="unread mt-2 size-2 shrink-0 rounded-full" />}
|
|
76
|
+
</>;
|
|
77
|
+
return <Menu name="notifications" title={unread ? t('notifications.unread', { count: unread }) : t('notifications.title')} width={380} button="tool relative" pad="p-0" onOpen={load}
|
|
78
|
+
label={<><Icon name="bell" />{unread > 0 && <span data-lk="unread" className="count">{unread > 99 ? '99+' : unread}</span>}</>}>
|
|
79
|
+
<div className="flex items-center gap-3 border-b px-4 py-3"><p className="mr-auto font-semibold">{t('notifications.title')}</p>{unread > 0 && <button type="button" data-lk="read-all" className="text-sm underline" onClick={() => read({ all: true })}>{t('notifications.markAll')}</button>}</div>
|
|
80
|
+
{problem && <p role="alert" className="danger px-4 py-3 text-sm">{problem}</p>}
|
|
81
|
+
{list?.length === 0 ? <div className="px-4 py-10 text-center"><Icon name="bell" className="muted mx-auto mb-2 size-8" /><p className="text-sm font-semibold">{t('notifications.empty')}</p><p className="muted text-sm">{t('notifications.emptyHint')}</p></div>
|
|
82
|
+
: <ul data-lk="notification-list" className="divide-y">{list?.map((n) => {
|
|
83
|
+
const seen = () => { if (!n.read) read({ ids: [n.id] }); }, look = 'menu-line flex w-full items-start gap-3 px-4 py-3 text-left';
|
|
84
|
+
return <li key={n.id} data-lk="notification" data-unread={!n.read || undefined}>{n.href ? <a href={n.href} className={look} onClick={seen} {...(n.href.startsWith('https://') ? { target: '_blank', rel: 'noreferrer' } : {})}>{line(n)}</a>
|
|
85
|
+
: n.read ? <div className={look}>{line(n)}</div> : <button type="button" className={look} title={t('notifications.markOne')} onClick={seen}>{line(n)}</button>}</li>;
|
|
86
|
+
})}</ul>}
|
|
87
|
+
</Menu>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The buttons of plugins and the panel, the bell and the user menu. */
|
|
91
|
+
function Tools({ panel, theme, place, rail = false }: { panel: Panel; theme: Theme; place: Place; rail?: boolean }) {
|
|
92
|
+
const t = useT();
|
|
93
|
+
const [mode, choose] = theme;
|
|
94
|
+
const hookCtx = useContext(PanelHookContext);
|
|
95
|
+
const buttons = <>
|
|
96
|
+
<RenderHook name="topbar.before-search" place={place} />
|
|
97
|
+
{searchable(panel) && <Search panel={panel} wide={place === 'topbar'} />}
|
|
98
|
+
<RenderHook name="topbar.after-search" place={place} />
|
|
99
|
+
{bars(panel).map(([type, Part]) => <div key={type} data-lk="bar" data-name={type}><Guard fallback={null}><Part place={place} /></Guard></div>)}
|
|
100
|
+
{panel.links?.map((l) => {
|
|
101
|
+
const styled = !!(l.color || l.variant || l.size || l.badge) && !l.iconOnly;
|
|
102
|
+
return <LinkBtn key={l.label} link={l} styled={styled} className={styled ? undefined : 'tool'} />;
|
|
103
|
+
})}
|
|
104
|
+
{panel.topbarActions?.map((a) => (
|
|
105
|
+
<ActionBtn
|
|
106
|
+
key={a.name}
|
|
107
|
+
action={a}
|
|
108
|
+
url={`/topbar/actions/${encodeURIComponent(a.name)}`}
|
|
109
|
+
dataLk={'topbar-action-' + a.name}
|
|
110
|
+
onDone={hookCtx?.reload}
|
|
111
|
+
/>
|
|
112
|
+
))}
|
|
113
|
+
{!panel.user && panel.theme.mode === 'system' && <button type="button" data-lk="mode" className="tool" title={mode === 'dark' ? t('theme.toLight') : t('theme.toDark')} aria-label={mode === 'dark' ? t('theme.toLight') : t('theme.toDark')} onClick={() => choose(mode === 'dark' ? 'light' : 'dark')}><Icon name={mode === 'dark' ? 'sun' : 'moon'} /></button>}
|
|
114
|
+
{panel.notifications && <Bell poll={panel.notifications.poll} />}
|
|
115
|
+
</>;
|
|
116
|
+
const user = panel.user && <UserMenu panel={panel} theme={theme} place={place} rail={rail} />;
|
|
117
|
+
if (place === 'side') return <div data-lk="tools" className={'mt-auto flex flex-col gap-1 border-t p-3 ' + (rail ? 'items-center' : '')}><div className={'flex flex-wrap gap-1 empty:hidden ' + (rail ? 'flex-col items-center' : 'items-center')}>{buttons}<RenderHook name="topbar.end" place={place} /></div>{user}</div>;
|
|
118
|
+
return <div data-lk="tools" className="flex shrink-0 items-center gap-1 sm:gap-2">{buttons}{user}<RenderHook name="topbar.end" place={place} /></div>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function Brand({ panel, mode, rail }: { panel: Pick<Panel, 'name' | 'logo' | 'logoDark'>; mode: string; rail?: boolean }) {
|
|
122
|
+
const logo = mode === 'dark' ? panel.logoDark || panel.logo : panel.logo;
|
|
123
|
+
return <LayoutPart part="brand" Default={KitBrand} props={{ name: panel.name, logo: logo || undefined, href: currentPanelPath() || '/', rail } as never} />;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Larakit's brand: the logo, or the name, or its first letter in the narrow menu. */
|
|
127
|
+
function KitBrand({ name, logo, href, rail }: { name: string; logo?: string; href: string; rail?: boolean }) {
|
|
128
|
+
return <a href={href} data-lk="brand" title={rail ? name : undefined} className="flex min-w-0 items-center text-lg font-bold">{rail ? <span className="avatar grid size-9 place-items-center rounded-lg text-sm">{[...name][0]}<span className="sr-only">{name}</span></span> : logo ? <img src={logo} alt={name} className="h-8 max-w-full" /> : <span className="truncate">{name}</span>}</a>;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function TenantBadge({ tenant }: { tenant: TenantInfo }) {
|
|
132
|
+
const [bad, setBad] = useState(false);
|
|
133
|
+
if (tenant.avatar && !bad) {
|
|
134
|
+
return <img src={tenant.avatar} alt="" className="size-7 shrink-0 rounded-[calc(var(--lk-radius)*0.75)] object-cover" onError={() => setBad(true)} />;
|
|
135
|
+
}
|
|
136
|
+
const isTone = !!tenant.color && TONES.includes(tenant.color);
|
|
137
|
+
const customColor = tenant.color && !isTone ? tenant.color : undefined;
|
|
138
|
+
return (
|
|
139
|
+
<span
|
|
140
|
+
aria-hidden="true"
|
|
141
|
+
className={'grid size-7 shrink-0 place-items-center rounded-[calc(var(--lk-radius)*0.75)] text-xs font-semibold ' + (isTone ? 'tone-' + tenant.color : customColor ? 'text-white' : 'avatar')}
|
|
142
|
+
style={customColor ? { backgroundColor: customColor } : undefined}
|
|
143
|
+
>
|
|
144
|
+
{initials(tenant.name) || '?'}
|
|
145
|
+
</span>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function DefaultTenantSwitcher({ tenants, active, onChange, rail = false }: Parts['tenantSwitcher']) {
|
|
150
|
+
const t = useT();
|
|
151
|
+
const [open, setOpen] = useState(false);
|
|
152
|
+
const wrapRef = useRef<HTMLDivElement>(null);
|
|
153
|
+
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
154
|
+
const listRef = useRef<HTMLDivElement>(null);
|
|
155
|
+
const listId = useId();
|
|
156
|
+
const current = active ?? tenants[0];
|
|
157
|
+
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
if (!open) return;
|
|
160
|
+
const onPointerDown = (e: PointerEvent) => {
|
|
161
|
+
if (!wrapRef.current?.contains(e.target as Node)) setOpen(false);
|
|
162
|
+
};
|
|
163
|
+
const onEscape = (e: globalThis.KeyboardEvent) => {
|
|
164
|
+
if (e.key === 'Escape') {
|
|
165
|
+
setOpen(false);
|
|
166
|
+
triggerRef.current?.focus();
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
document.addEventListener('pointerdown', onPointerDown);
|
|
170
|
+
document.addEventListener('keydown', onEscape);
|
|
171
|
+
return () => {
|
|
172
|
+
document.removeEventListener('pointerdown', onPointerDown);
|
|
173
|
+
document.removeEventListener('keydown', onEscape);
|
|
174
|
+
};
|
|
175
|
+
}, [open]);
|
|
176
|
+
|
|
177
|
+
if (!current) return null;
|
|
178
|
+
|
|
179
|
+
const moveFocus = (dir: 1 | -1 | 'first' | 'last') => {
|
|
180
|
+
const items = [...(listRef.current?.querySelectorAll<HTMLButtonElement>('[role="option"]') ?? [])];
|
|
181
|
+
if (items.length === 0) return;
|
|
182
|
+
const idx = items.indexOf(document.activeElement as HTMLButtonElement);
|
|
183
|
+
const nextIdx = dir === 'first' ? 0 : dir === 'last' ? items.length - 1 : (idx + dir + items.length) % items.length;
|
|
184
|
+
items[nextIdx]?.focus();
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const onListKeyDown = (e: ReactKeyboardEvent) => {
|
|
188
|
+
if (e.key === 'ArrowDown') { e.preventDefault(); moveFocus(1); }
|
|
189
|
+
else if (e.key === 'ArrowUp') { e.preventDefault(); moveFocus(-1); }
|
|
190
|
+
else if (e.key === 'Home') { e.preventDefault(); moveFocus('first'); }
|
|
191
|
+
else if (e.key === 'End') { e.preventDefault(); moveFocus('last'); }
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<div ref={wrapRef} data-lk="tenant-switcher" className="relative">
|
|
196
|
+
<button
|
|
197
|
+
ref={triggerRef}
|
|
198
|
+
type="button"
|
|
199
|
+
data-lk="tenant-trigger"
|
|
200
|
+
aria-haspopup="listbox"
|
|
201
|
+
aria-expanded={open}
|
|
202
|
+
aria-controls={listId}
|
|
203
|
+
aria-label={`${t('tenant.switch')}: ${current.name}`}
|
|
204
|
+
title={rail ? current.name : t('tenant.switch')}
|
|
205
|
+
className={'tenant-btn flex items-center border ' + (rail ? 'size-10 justify-center p-0' : 'w-full gap-2.5 px-2.5 py-1.5 text-start')}
|
|
206
|
+
onClick={() => setOpen((was) => !was)}
|
|
207
|
+
onKeyDown={(e) => {
|
|
208
|
+
if (e.key === 'ArrowDown') {
|
|
209
|
+
e.preventDefault();
|
|
210
|
+
setOpen(true);
|
|
211
|
+
requestAnimationFrame(() => moveFocus('first'));
|
|
212
|
+
}
|
|
213
|
+
}}
|
|
214
|
+
>
|
|
215
|
+
<TenantBadge tenant={current} />
|
|
216
|
+
{!rail && (
|
|
217
|
+
<>
|
|
218
|
+
<span className="min-w-0 flex-1">
|
|
219
|
+
<span data-lk="tenant-name" className="block truncate text-sm font-semibold leading-tight">{current.name}</span>
|
|
220
|
+
{current.slug && <span className="muted block truncate text-xs leading-tight">{current.slug}</span>}
|
|
221
|
+
</span>
|
|
222
|
+
<Icon name="chevrons-up-down" className="muted size-4 shrink-0" />
|
|
223
|
+
</>
|
|
224
|
+
)}
|
|
225
|
+
</button>
|
|
226
|
+
{open && (
|
|
227
|
+
<div
|
|
228
|
+
ref={listRef}
|
|
229
|
+
id={listId}
|
|
230
|
+
role="listbox"
|
|
231
|
+
data-lk="tenant-menu"
|
|
232
|
+
aria-label={t('tenant.label')}
|
|
233
|
+
className="surface tenant-menu absolute start-0 top-full z-50 mt-1.5 min-w-56 w-full border p-1.5 shadow-lg"
|
|
234
|
+
onKeyDown={onListKeyDown}
|
|
235
|
+
>
|
|
236
|
+
{tenants.map((tenant) => {
|
|
237
|
+
const selected = tenant.id === current.id;
|
|
238
|
+
return (
|
|
239
|
+
<button
|
|
240
|
+
key={tenant.id}
|
|
241
|
+
type="button"
|
|
242
|
+
role="option"
|
|
243
|
+
data-lk="tenant-option"
|
|
244
|
+
data-tenant-id={tenant.id}
|
|
245
|
+
aria-selected={selected}
|
|
246
|
+
className={'menu-item flex w-full items-center gap-2.5 px-2 py-1.5 text-start ' + (selected ? 'bg-[var(--lk-accent)] text-[var(--lk-on-accent)]' : '')}
|
|
247
|
+
onClick={() => {
|
|
248
|
+
setOpen(false);
|
|
249
|
+
if (tenant.id !== current.id) onChange(tenant);
|
|
250
|
+
triggerRef.current?.focus();
|
|
251
|
+
}}
|
|
252
|
+
>
|
|
253
|
+
<TenantBadge tenant={tenant} />
|
|
254
|
+
<span className="min-w-0 flex-1">
|
|
255
|
+
<span className="block truncate text-sm font-medium leading-tight">{tenant.name}</span>
|
|
256
|
+
{tenant.slug && <span className={'block truncate text-xs leading-tight ' + (selected ? 'opacity-80' : 'muted')}>{tenant.slug}</span>}
|
|
257
|
+
</span>
|
|
258
|
+
{selected && <Icon name="check" className="ms-auto size-4 shrink-0" />}
|
|
259
|
+
</button>
|
|
260
|
+
);
|
|
261
|
+
})}
|
|
262
|
+
</div>
|
|
263
|
+
)}
|
|
264
|
+
</div>
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function Nav({ name, items, layout, mode, rail = false, logo, logoDark, collapsible, tenantSwitcher }: Parts['nav']) {
|
|
269
|
+
const t = useT();
|
|
270
|
+
const top = layout === 'top', groups = [...new Set(items.map((i) => i.group ?? ''))].sort((a, b) => Number(a !== '') - Number(b !== ''));
|
|
271
|
+
const brand = <Brand panel={{ name, logo: logo ?? '', logoDark: logoDark ?? '' }} mode={mode} rail={rail} />;
|
|
272
|
+
// Groups folded away, remembered by the browser. The narrow menu and the top menu show every group.
|
|
273
|
+
const [folded, setFolded] = useBrowserState<string[]>([], () => { try { return JSON.parse(localStorage.getItem(foldedKey()) ?? '[]'); } catch { return []; } });
|
|
274
|
+
const folds = !!collapsible && !top && !rail;
|
|
275
|
+
const fold = (g: string) => {
|
|
276
|
+
const now = folded.includes(g) ? folded.filter((x) => x !== g) : [...folded, g];
|
|
277
|
+
setFolded(now);
|
|
278
|
+
try { localStorage.setItem(foldedKey(), JSON.stringify(now)); } catch { /* It is forgotten, nothing else. */ }
|
|
279
|
+
};
|
|
280
|
+
return <nav data-lk="nav" aria-label={t('nav.main')} className={top ? 'flex min-w-0 flex-1 items-center gap-4' : 'flex min-h-0 flex-col'}>
|
|
281
|
+
<div className={top ? 'max-w-40 shrink-0' : 'flex h-14 shrink-0 items-center ' + (rail ? 'justify-center' : 'px-5')}>{brand}</div>
|
|
282
|
+
{!top && <RenderHook name="sidebar.header" place="side" className={rail ? 'flex flex-col items-center gap-1 px-2 pb-2' : 'flex flex-col gap-2 px-3 pb-2'} />}
|
|
283
|
+
{tenantSwitcher && (top ? <div className="shrink-0">{tenantSwitcher}</div> : <div className={'shrink-0 pb-2 ' + (rail ? 'flex justify-center px-2' : 'px-3')}>{tenantSwitcher}</div>)}
|
|
284
|
+
<div className={top ? 'flex min-w-0 items-center gap-1 overflow-x-auto p-1' : 'min-h-0 flex-1 overflow-y-auto p-3'}>
|
|
285
|
+
{!top && <RenderHook name="sidebar.nav.start" place="side" className="mb-2 flex flex-col gap-1" />}
|
|
286
|
+
{groups.map((g, n) => <Fragment key={g}>
|
|
287
|
+
{g && !top && (rail ? n > 0 && <div className="mx-2 my-3 border-t" /> : folds
|
|
288
|
+
? <button type="button" data-lk="nav-group" aria-expanded={!folded.includes(g)} className={'nav-group muted flex w-full items-center gap-1 rounded-md px-3 pb-1.5 text-left text-[11px] font-semibold uppercase tracking-[0.06em] ' + (n ? 'pt-5' : 'pt-1')} onClick={() => fold(g)}>
|
|
289
|
+
<span className="flex-1">{g}</span><Icon name="down" className={'size-3.5 transition-transform motion-reduce:transition-none ' + (folded.includes(g) ? '-rotate-90' : '')} /></button>
|
|
290
|
+
: <p data-lk="nav-group" className={'muted px-3 pb-1.5 text-[11px] font-semibold uppercase tracking-[0.06em] ' + (n ? 'pt-5' : 'pt-1')}>{g}</p>)}
|
|
291
|
+
{items.filter((i) => (i.group ?? '') === g && !(folds && g && folded.includes(g))).map((i) => <a data-lk="nav-item" data-name={i.name} key={i.name} href={i.href} title={rail ? i.label : undefined} aria-current={i.current ? 'page' : undefined} className={'navlink flex items-center gap-2.5 text-sm font-medium whitespace-nowrap aria-[current]:font-semibold ' + (rail ? 'mb-0.5 justify-center p-2.5' : top ? 'px-3 py-1.5' : 'mb-0.5 px-3 py-1.5')}>
|
|
292
|
+
{i.icon ? <Icon name={i.icon} className="size-4.5" /> : rail ? <span aria-hidden="true" className="grid size-4.5 place-items-center font-semibold">{[...i.label][0]}</span> : null}<span className={rail ? 'sr-only' : 'truncate'}>{i.label}</span>
|
|
293
|
+
{i.badge ? <span data-lk="nav-badge" className={'badge tone-gray shrink-0 px-1.5 text-xs font-semibold tabular-nums ' + (rail ? 'sr-only' : 'ml-auto')}>{i.badge > 999 ? '999+' : i.badge}</span> : null}</a>)}
|
|
294
|
+
</Fragment>)}
|
|
295
|
+
{!top && <RenderHook name="sidebar.nav.end" place="side" className="mt-2 flex flex-col gap-1" />}
|
|
296
|
+
</div>
|
|
297
|
+
</nav>;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const Bar = ({ start, Tools }: Parts['topbar']) => <div data-lk="topbar" className="flex h-14 items-center gap-3 border-b px-4 sm:px-6"><div className="mr-auto flex min-w-0 items-center gap-2">{start}</div><Tools /></div>;
|
|
301
|
+
|
|
302
|
+
const key = () => `larakit:${currentPanelPath()}:rail`, foldedKey = () => `larakit:${currentPanelPath()}:folded`;
|
|
303
|
+
const narrow = typeof matchMedia === 'function' ? matchMedia('(width < 48rem)') : null;
|
|
304
|
+
|
|
305
|
+
/** Everything around a page. */
|
|
306
|
+
export function Shell({ panel, theme, items, name, onTenantChange, children }: { panel: Panel; theme: Theme; items: Parts['nav']['items']; name: string; onTenantChange?: (tenantId: string) => Promise<unknown> | void; children: ReactNode }) {
|
|
307
|
+
const [mode, choose] = theme, top = panel.layout.nav === 'top', bar = panel.layout.topbar !== 'off';
|
|
308
|
+
const t = useT();
|
|
309
|
+
const [small, setSmall] = useBrowserState(false, () => narrow?.matches ?? false), [open, setOpen] = useState(false);
|
|
310
|
+
const [kept, setKept] = useBrowserState(false, () => { try { return localStorage.getItem(key()) === '1'; } catch { return false; } });
|
|
311
|
+
const opener = useRef<HTMLButtonElement>(null), side = useRef<HTMLElement>(null);
|
|
312
|
+
useEffect(() => {
|
|
313
|
+
const change = () => { setSmall(narrow?.matches ?? false); setOpen(false); }, shut = () => setOpen(false);
|
|
314
|
+
narrow?.addEventListener('change', change); addEventListener('hashchange', shut);
|
|
315
|
+
return () => { narrow?.removeEventListener('change', change); removeEventListener('hashchange', shut); };
|
|
316
|
+
}, []);
|
|
317
|
+
const drawer = small && open;
|
|
318
|
+
useEffect(() => {
|
|
319
|
+
if (!drawer) return;
|
|
320
|
+
side.current?.querySelector<HTMLElement>('a, button')?.focus();
|
|
321
|
+
const esc = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false); };
|
|
322
|
+
addEventListener('keydown', esc);
|
|
323
|
+
return () => { removeEventListener('keydown', esc); opener.current?.focus(); };
|
|
324
|
+
}, [drawer]);
|
|
325
|
+
// A plugin's menu does not know about the narrow form.
|
|
326
|
+
const free = !replaces('nav', panel), rail = !small && free && kept;
|
|
327
|
+
const turn = () => { setKept(!kept); try { localStorage.setItem(key(), kept ? '' : '1'); } catch { /* It is forgotten, nothing else. */ } };
|
|
328
|
+
const tenancy = panel.tenancy;
|
|
329
|
+
const tenants = tenancy?.enabled ? tenancy.tenants : [];
|
|
330
|
+
// The server says which tenant the page shows (the session's choice), so a page drawn on the server shows the same
|
|
331
|
+
// one. A choice shows at once while the page is visited again, and is let go when the server refuses it.
|
|
332
|
+
const [chosenTenant, setChosenTenant] = useState<string | null>(null);
|
|
333
|
+
useEffect(() => setChosenTenant(null), [tenancy?.active]);
|
|
334
|
+
const activeTenant = tenants.find((item) => item.id === (chosenTenant ?? tenancy?.active)) ?? tenants[0];
|
|
335
|
+
const switchTenant = (tenant: TenantInfo) => {
|
|
336
|
+
setChosenTenant(tenant.id);
|
|
337
|
+
void Promise.resolve(onTenantChange?.(tenant.id)).catch(() => setChosenTenant(null));
|
|
338
|
+
};
|
|
339
|
+
// A menu that is not Larakit's (a plugin's, or the application's own nav part) doesn't know the switcher, so it
|
|
340
|
+
// goes above that menu.
|
|
341
|
+
const ownNav = !free || !!(panel as { parts?: Record<string, string> }).parts?.nav;
|
|
342
|
+
const tenantSwitcher = tenancy?.enabled && tenants.length > 0
|
|
343
|
+
? <Slot part="tenantSwitcher" own={DefaultTenantSwitcher} props={{ tenants, active: activeTenant, onChange: switchTenant, rail }} />
|
|
344
|
+
: undefined;
|
|
345
|
+
const nav = (layout: 'side' | 'top') => <Slot part="nav" own={Nav} props={{ name: panel.name, items, layout, mode, onMode: panel.theme.mode === 'system' ? () => choose(mode === 'dark' ? 'light' : 'dark') : undefined, rail, logo: panel.logo, logoDark: panel.logoDark, collapsible: panel.layout.groups === 'collapsible', tenantSwitcher: ownNav ? undefined : tenantSwitcher }} />;
|
|
346
|
+
const toggle = small ? <button ref={opener} type="button" data-lk="menu" className="tool -ms-2" aria-label={t('nav.open')} aria-expanded={open} onClick={() => setOpen(true)}><Icon name="menu" /></button>
|
|
347
|
+
: free && <button type="button" data-lk="rail" className="tool" title={rail ? t('nav.widen') : t('nav.narrow')} aria-label={rail ? t('nav.widen') : t('nav.narrow')} aria-expanded={!rail} onClick={turn}><Icon name="side" /></button>;
|
|
348
|
+
const outerHookCtx = useContext(PanelHookContext);
|
|
349
|
+
const hookCtx = useMemo(() => outerHookCtx ?? { panel, screen: name }, [outerHookCtx, panel, name]);
|
|
350
|
+
// While a person types in a field, the tools must not start anew.
|
|
351
|
+
const ToolsHere = useStable(() => <Tools panel={panel} theme={theme} place="topbar" />);
|
|
352
|
+
const main = (
|
|
353
|
+
<main data-lk="main" data-name={name} className={'w-full min-w-0 p-4 sm:p-8 ' + (panel.layout.width === 'narrow' ? 'mx-auto max-w-5xl' : '')}>
|
|
354
|
+
<RenderHook name="content.start" className="mb-4 flex flex-col gap-3" />
|
|
355
|
+
{children}
|
|
356
|
+
<RenderHook name="content.end" className="mt-4 flex flex-col gap-3" />
|
|
357
|
+
</main>
|
|
358
|
+
);
|
|
359
|
+
if (top && !small) return (
|
|
360
|
+
<PanelHookContext value={hookCtx}>
|
|
361
|
+
<div data-lk="panel" data-nav="top" data-topbar={panel.layout.topbar} data-width={panel.layout.width} className="flex min-h-screen flex-col">
|
|
362
|
+
<RenderHook name="body.start" className="flex flex-col gap-2" />
|
|
363
|
+
<header className="surface-fill sticky top-0 z-20"><Slot part="topbar" own={Bar} props={{ start: <>{nav('top')}{ownNav && tenantSwitcher}<RenderHook name="topbar.start" place="topbar" /></>, Tools: ToolsHere }} /></header>
|
|
364
|
+
<div className="flex-1">{main}</div>
|
|
365
|
+
<RenderHook name="body.end" className="flex flex-col gap-2" />
|
|
366
|
+
</div>
|
|
367
|
+
</PanelHookContext>
|
|
368
|
+
);
|
|
369
|
+
const start = <>{toggle}{small && <Brand panel={panel} mode={mode} />}<RenderHook name="topbar.start" place="topbar" /></>;
|
|
370
|
+
return (
|
|
371
|
+
<PanelHookContext value={hookCtx}>
|
|
372
|
+
<div data-lk="panel" data-nav={panel.layout.nav} data-topbar={panel.layout.topbar} data-width={panel.layout.width} className="flex min-h-screen flex-col">
|
|
373
|
+
<RenderHook name="body.start" className="flex flex-col gap-2" />
|
|
374
|
+
<div className="min-w-0 flex-1 md:flex">
|
|
375
|
+
{drawer && <div aria-hidden="true" className="fixed inset-0 z-30 bg-black/50" onClick={() => setOpen(false)} />}
|
|
376
|
+
<aside ref={side} data-lk="side" data-rail={rail || undefined} role={drawer ? 'dialog' : undefined} aria-modal={drawer || undefined} aria-label={drawer ? t('nav.main') : undefined} inert={small && !open}
|
|
377
|
+
className={'surface-fill z-40 flex h-dvh shrink-0 flex-col border-e ' + (small ? 'fixed inset-y-0 start-0 w-72 max-w-[85vw] transition-transform motion-reduce:transition-none ' + (open ? '' : '-translate-x-full rtl:translate-x-full') : 'sticky top-0 ' + (rail ? 'w-18' : 'w-64'))}>
|
|
378
|
+
{ownNav && tenantSwitcher && <div className="shrink-0 px-3 pt-3 pb-1">{tenantSwitcher}</div>}
|
|
379
|
+
<div className="flex min-h-0 flex-1 flex-col *:min-h-0">{nav('side')}</div>
|
|
380
|
+
{!bar && !small && free && <div className={'flex px-3 pb-2 ' + (rail ? 'justify-center' : '')}>{toggle}</div>}
|
|
381
|
+
{!bar && <Tools panel={panel} theme={theme} place="side" rail={rail} />}
|
|
382
|
+
<RenderHook name="sidebar.footer" place="side" className={rail ? 'flex flex-col items-center gap-1 border-t p-2' : 'flex flex-col gap-2 border-t p-3'} />
|
|
383
|
+
</aside>
|
|
384
|
+
<div className="min-w-0 flex-1" inert={drawer}>
|
|
385
|
+
{bar ? <header className="surface-fill sticky top-0 z-20"><Slot part="topbar" own={Bar} props={{ start, Tools: ToolsHere }} /></header>
|
|
386
|
+
: small && <header data-lk="topbar" className="surface-fill sticky top-0 z-20 flex h-14 items-center gap-2 border-b px-4">{start}</header>}
|
|
387
|
+
{main}
|
|
388
|
+
</div>
|
|
389
|
+
</div>
|
|
390
|
+
<RenderHook name="body.end" className="flex flex-col gap-2" />
|
|
391
|
+
</div>
|
|
392
|
+
</PanelHookContext>
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** A component that always draws the newest content and never starts anew. */
|
|
397
|
+
function useStable(draw: () => ReactNode) {
|
|
398
|
+
const now = useRef(draw);
|
|
399
|
+
now.current = draw;
|
|
400
|
+
return useState(() => () => <>{now.current()}</>)[0];
|
|
401
|
+
}
|