@snap-rail/ui 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/lib/charts.js +118 -0
- package/lib/contract.js +527 -0
- package/lib/fallback.js +399 -0
- package/lib/index.js +1282 -0
- package/lib/pages.js +68 -0
- package/lib/runtime.js +3003 -0
- package/lib/settings.js +2069 -0
- package/lib/skins.js +258 -0
- package/lib/slots.js +267 -0
- package/lib/types/charts.d.ts +62 -0
- package/lib/types/charts.js +41 -0
- package/lib/types/components/button.d.ts +39 -0
- package/lib/types/components/button.js +27 -0
- package/lib/types/components/controls.d.ts +66 -0
- package/lib/types/components/controls.js +66 -0
- package/lib/types/components/dialog.d.ts +58 -0
- package/lib/types/components/dialog.js +46 -0
- package/lib/types/components/feedback.d.ts +46 -0
- package/lib/types/components/feedback.js +20 -0
- package/lib/types/components/form.d.ts +31 -0
- package/lib/types/components/form.js +13 -0
- package/lib/types/components/index.d.ts +15 -0
- package/lib/types/components/index.js +15 -0
- package/lib/types/components/input.d.ts +40 -0
- package/lib/types/components/input.js +18 -0
- package/lib/types/components/layout.d.ts +103 -0
- package/lib/types/components/layout.js +80 -0
- package/lib/types/components/schema-form.d.ts +44 -0
- package/lib/types/components/schema-form.js +84 -0
- package/lib/types/components/surface.d.ts +49 -0
- package/lib/types/components/surface.js +59 -0
- package/lib/types/contract.d.ts +350 -0
- package/lib/types/contract.js +231 -0
- package/lib/types/fallback.d.ts +14 -0
- package/lib/types/fallback.js +50 -0
- package/lib/types/index.d.ts +19 -0
- package/lib/types/index.js +19 -0
- package/lib/types/pages.d.ts +40 -0
- package/lib/types/pages.js +69 -0
- package/lib/types/runtime.d.ts +93 -0
- package/lib/types/runtime.js +233 -0
- package/lib/types/settings-pages/appearance.d.ts +15 -0
- package/lib/types/settings-pages/appearance.js +128 -0
- package/lib/types/settings-pages/index.d.ts +3 -0
- package/lib/types/settings-pages/index.js +3 -0
- package/lib/types/settings-pages/plugin-feed.d.ts +17 -0
- package/lib/types/settings-pages/plugin-feed.js +66 -0
- package/lib/types/settings-pages/plugins.d.ts +21 -0
- package/lib/types/settings-pages/plugins.js +372 -0
- package/lib/types/settings.d.ts +59 -0
- package/lib/types/settings.js +176 -0
- package/lib/types/skins.d.ts +137 -0
- package/lib/types/skins.js +271 -0
- package/lib/types/slots.d.ts +69 -0
- package/lib/types/slots.js +210 -0
- package/lib/types/utils.d.ts +15 -0
- package/lib/types/utils.js +26 -0
- package/package.json +105 -3
- package/src/styles/base.css +329 -0
- package/src/styles/default.css +735 -0
- package/README.md +0 -3
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Component, useState, useSyncExternalStore } from 'react';
|
|
3
|
+
import { Button, Dialog, DialogContent, DialogTitle, EmptyState, IconButton, } from "./components/index.js";
|
|
4
|
+
import { UI_SLOT_IDS } from "./contract.js";
|
|
5
|
+
import { AppearancePage } from "./settings-pages/appearance.js";
|
|
6
|
+
import { PluginFeedDialog } from "./settings-pages/plugin-feed.js";
|
|
7
|
+
import { PluginsPage } from "./settings-pages/plugins.js";
|
|
8
|
+
import { cn, part } from "./utils.js";
|
|
9
|
+
/** Sort pages by order, then registration time. */
|
|
10
|
+
function orderedPages(pages) {
|
|
11
|
+
return [...pages]
|
|
12
|
+
.sort((a, b) => a.page.order - b.page.order || a.seq - b.seq)
|
|
13
|
+
.map(entry => entry.page);
|
|
14
|
+
}
|
|
15
|
+
/** A page-level boundary: one broken settings page must not take down the shell. */
|
|
16
|
+
class SettingsPageBoundary extends Component {
|
|
17
|
+
state = { failed: false };
|
|
18
|
+
static getDerivedStateFromError() {
|
|
19
|
+
return { failed: true };
|
|
20
|
+
}
|
|
21
|
+
componentDidCatch(error) {
|
|
22
|
+
console.error(`[snap-rail ui] settings page ${this.props.page} failed`, error);
|
|
23
|
+
}
|
|
24
|
+
render() {
|
|
25
|
+
return this.state.failed
|
|
26
|
+
? _jsx(EmptyState, { title: "\u8BBE\u7F6E\u9875\u52A0\u8F7D\u5931\u8D25", description: "\u8BE5\u9875\u9762\u629B\u51FA\u4E86\u5F02\u5E38\uFF0C\u5176\u4ED6\u8BBE\u7F6E\u9875\u4ECD\u53EF\u4F7F\u7528\u3002" })
|
|
27
|
+
: this.props.children;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** The gear glyph used by the default trigger. */
|
|
31
|
+
function GearIcon() {
|
|
32
|
+
return (_jsxs("svg", { viewBox: "0 0 24 24", width: "18", height: "18", fill: "none", stroke: "currentColor", strokeWidth: "1.8", "aria-hidden": "true", children: [_jsx("circle", { cx: "12", cy: "12", r: "3" }), _jsx("path", { d: "M19.4 15a1.7 1.7 0 0 0 .34 1.87l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.7 1.7 0 0 0-1.87-.34 1.7 1.7 0 0 0-1 1.55V21a2 2 0 1 1-4 0v-.09a1.7 1.7 0 0 0-1-1.55 1.7 1.7 0 0 0-1.87.34l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.7 1.7 0 0 0 .34-1.87 1.7 1.7 0 0 0-1.55-1H3a2 2 0 1 1 0-4h.09a1.7 1.7 0 0 0 1.55-1 1.7 1.7 0 0 0-.34-1.87l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.7 1.7 0 0 0 1.87.34h0a1.7 1.7 0 0 0 1-1.55V3a2 2 0 1 1 4 0v.09a1.7 1.7 0 0 0 1 1.55h0a1.7 1.7 0 0 0 1.87-.34l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.7 1.7 0 0 0-.34 1.87v0a1.7 1.7 0 0 0 1.55 1H21a2 2 0 1 1 0 4h-.09a1.7 1.7 0 0 0-1.55 1Z" })] }));
|
|
33
|
+
}
|
|
34
|
+
/** The close glyph used by the surface's dismiss control. */
|
|
35
|
+
function CloseIcon() {
|
|
36
|
+
return (_jsx("svg", { viewBox: "0 0 24 24", width: "18", height: "18", fill: "none", stroke: "currentColor", strokeWidth: "1.8", "aria-hidden": "true", children: _jsx("path", { d: "M6 6l12 12M18 6L6 18" }) }));
|
|
37
|
+
}
|
|
38
|
+
/** The default settings trigger; the layout decides placement/presentation. */
|
|
39
|
+
export function SettingsEntry(props) {
|
|
40
|
+
if (props.presentation === 'label') {
|
|
41
|
+
return _jsx(Button, { intent: "neutral", emphasis: "ghost", icon: _jsx(GearIcon, {}), onClick: props.open, children: "\u8BBE\u7F6E" });
|
|
42
|
+
}
|
|
43
|
+
return _jsx(IconButton, { icon: _jsx(GearIcon, {}), label: "\u8BBE\u7F6E", active: props.isOpen, onClick: props.open });
|
|
44
|
+
}
|
|
45
|
+
/** The default settings surface. */
|
|
46
|
+
export function SettingsSurface(props) {
|
|
47
|
+
const { service, ctx } = props;
|
|
48
|
+
useSyncExternalStore(service.subscribe, service.version, service.version);
|
|
49
|
+
const open = service.isOpen();
|
|
50
|
+
const pages = service.listPages();
|
|
51
|
+
const activeId = service.activePage();
|
|
52
|
+
const active = pages.find(page => page.id === activeId);
|
|
53
|
+
const [feedOpen, setFeedOpen] = useState(false);
|
|
54
|
+
const groups = new Map();
|
|
55
|
+
for (const page of pages) {
|
|
56
|
+
const group = page.group ?? '其他';
|
|
57
|
+
const bucket = groups.get(group);
|
|
58
|
+
if (bucket === undefined)
|
|
59
|
+
groups.set(group, [page]);
|
|
60
|
+
else
|
|
61
|
+
bucket.push(page);
|
|
62
|
+
}
|
|
63
|
+
return (_jsxs(_Fragment, { children: [_jsx(Dialog, { open: open, onOpenChange: next => { if (!next)
|
|
64
|
+
service.close(); }, children: _jsx(DialogContent, { presentation: "fullscreen", showClose: false, children: _jsxs("div", { ...part('settings', 'root'), className: "snap-settings", children: [_jsxs("header", { ...part('settings', 'header'), className: "snap-settings-header", children: [_jsx(DialogTitle, { children: "\u8BBE\u7F6E" }), _jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: 4 }, children: [_jsx(IconButton, { icon: _jsx(GearIcon, {}), label: "\u914D\u7F6E\u63D2\u4EF6\u6E90", onClick: () => setFeedOpen(true) }), _jsx(IconButton, { icon: _jsx(CloseIcon, {}), label: "\u5173\u95ED", onClick: () => service.close() })] })] }), _jsxs("div", { ...part('settings', 'body'), className: "snap-settings-body", children: [_jsx("nav", { ...part('settings', 'nav'), className: "snap-settings-nav", "aria-label": "\u8BBE\u7F6E\u9875", children: [...groups.entries()].map(([group, entries]) => (_jsxs("div", { ...part('settings', 'group'), className: "snap-settings-group", children: [_jsx("div", { ...part('settings', 'group-title'), className: "snap-settings-group-title", children: group }), entries.map(page => (_jsxs("button", { type: "button", ...part('settings', 'nav-item'), className: cn('snap-settings-nav-item', page.id === activeId && 'is-active'), "data-active": page.id === activeId || undefined, "aria-current": page.id === activeId ? 'page' : undefined, onClick: () => service.open(page.id), children: [page.icon !== undefined && _jsx("span", { ...part('settings', 'nav-icon'), children: page.icon }), _jsx("span", { children: page.title })] }, page.id)))] }, group))) }), _jsx("main", { ...part('settings', 'content'), className: "snap-settings-content", "data-active-page": activeId, children: active === undefined
|
|
65
|
+
? _jsx(EmptyState, { title: "\u6682\u65E0\u8BBE\u7F6E\u9875" })
|
|
66
|
+
: _jsx(SettingsPageBoundary, { page: active.id, children: active.render() }) })] })] }) }) }), _jsx(PluginFeedDialog, { ctx: ctx, open: feedOpen, onOpenChange: setFeedOpen })] }));
|
|
67
|
+
}
|
|
68
|
+
/** Mount the settings registry and its default slot contributions. */
|
|
69
|
+
const settingsPlugin = {
|
|
70
|
+
name: 'snap-rail settings',
|
|
71
|
+
inject: ['ui'],
|
|
72
|
+
apply(ctx) {
|
|
73
|
+
const pages = new Map();
|
|
74
|
+
const listeners = new Set();
|
|
75
|
+
let open = false;
|
|
76
|
+
let requestedId;
|
|
77
|
+
let revision = 0;
|
|
78
|
+
let seq = 0;
|
|
79
|
+
const notify = (kind) => {
|
|
80
|
+
revision += 1;
|
|
81
|
+
for (const listener of listeners)
|
|
82
|
+
listener();
|
|
83
|
+
ctx.emit(kind === 'pages' ? 'settings/pages-changed' : 'settings/open-changed');
|
|
84
|
+
};
|
|
85
|
+
const firstId = () => orderedPages(pages.values())[0]?.id;
|
|
86
|
+
const activeId = () => {
|
|
87
|
+
if (!open)
|
|
88
|
+
return undefined;
|
|
89
|
+
if (requestedId !== undefined && pages.has(requestedId))
|
|
90
|
+
return requestedId;
|
|
91
|
+
return firstId();
|
|
92
|
+
};
|
|
93
|
+
const service = {
|
|
94
|
+
registerPage(caller, page) {
|
|
95
|
+
pages.set(page.id, { page, seq: seq++ });
|
|
96
|
+
notify('pages');
|
|
97
|
+
const remove = () => {
|
|
98
|
+
if (pages.delete(page.id))
|
|
99
|
+
notify('pages');
|
|
100
|
+
};
|
|
101
|
+
caller.effect(() => remove);
|
|
102
|
+
return remove;
|
|
103
|
+
},
|
|
104
|
+
listPages: () => orderedPages(pages.values()),
|
|
105
|
+
open(pageId) {
|
|
106
|
+
if (pageId !== undefined)
|
|
107
|
+
requestedId = pageId;
|
|
108
|
+
if (open && pageId === undefined)
|
|
109
|
+
return;
|
|
110
|
+
open = true;
|
|
111
|
+
notify('open');
|
|
112
|
+
},
|
|
113
|
+
close() {
|
|
114
|
+
if (!open)
|
|
115
|
+
return;
|
|
116
|
+
open = false;
|
|
117
|
+
notify('open');
|
|
118
|
+
},
|
|
119
|
+
toggle() {
|
|
120
|
+
if (open)
|
|
121
|
+
service.close();
|
|
122
|
+
else
|
|
123
|
+
service.open();
|
|
124
|
+
},
|
|
125
|
+
isOpen: () => open,
|
|
126
|
+
activePage: activeId,
|
|
127
|
+
subscribe(listener) {
|
|
128
|
+
listeners.add(listener);
|
|
129
|
+
return () => { listeners.delete(listener); };
|
|
130
|
+
},
|
|
131
|
+
version: () => revision,
|
|
132
|
+
};
|
|
133
|
+
ctx.provide('settings', service);
|
|
134
|
+
// First-party pages ship with the substrate so a fresh install can manage
|
|
135
|
+
// its own plugins without any external settings-page package.
|
|
136
|
+
ctx.settings.registerPage(ctx, {
|
|
137
|
+
id: 'plugins',
|
|
138
|
+
title: '插件管理',
|
|
139
|
+
group: '系统',
|
|
140
|
+
order: 0,
|
|
141
|
+
keywords: ['安装', '启用', '卸载', '驱动', '皮肤'],
|
|
142
|
+
render: () => _jsx(PluginsPage, { ctx: ctx }),
|
|
143
|
+
});
|
|
144
|
+
ctx.settings.registerPage(ctx, {
|
|
145
|
+
id: 'appearance',
|
|
146
|
+
title: '外观与显示',
|
|
147
|
+
group: '系统',
|
|
148
|
+
order: 1,
|
|
149
|
+
keywords: ['主题', '皮肤', '窗口', '悬浮球', '尺寸'],
|
|
150
|
+
render: () => _jsx(AppearancePage, {}),
|
|
151
|
+
});
|
|
152
|
+
// The default trigger and surface are ordinary contributions: a plugin may
|
|
153
|
+
// replace either with a higher precedence, including a fully custom UI.
|
|
154
|
+
ctx.ui.contribute(ctx, UI_SLOT_IDS.settingsEntry, {
|
|
155
|
+
id: 'settings.default-entry',
|
|
156
|
+
order: 0,
|
|
157
|
+
render: (context) => (_jsx(SettingsEntry, { presentation: context.presentation, open: context.open, isOpen: context.isOpen })),
|
|
158
|
+
});
|
|
159
|
+
ctx.ui.contribute(ctx, UI_SLOT_IDS.settingsSurface, {
|
|
160
|
+
id: 'settings.default-surface',
|
|
161
|
+
order: 0,
|
|
162
|
+
render: () => _jsx(SettingsSurface, { service: service, ctx: ctx }),
|
|
163
|
+
});
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
/** The settings page info the slot context exposes. */
|
|
167
|
+
export function settingsPageInfo(page) {
|
|
168
|
+
return {
|
|
169
|
+
id: page.id,
|
|
170
|
+
title: page.title,
|
|
171
|
+
...page.group !== undefined ? { group: page.group } : {},
|
|
172
|
+
order: page.order,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
export default settingsPlugin;
|
|
176
|
+
//# sourceMappingURL=settings.js.map
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The theme registry and token bridge. A theme is a declared visual
|
|
3
|
+
* implementation of the semantic component contract: one token set plus
|
|
4
|
+
* optional part CSS — pure styling. Component definitions and slots live
|
|
5
|
+
* outside the theme; a theme never shapes behaviour. Exactly one theme is
|
|
6
|
+
* active; the two factory cobalt themes (day/night) are always registered
|
|
7
|
+
* and the dark one is the fallback.
|
|
8
|
+
*
|
|
9
|
+
* @module @snap-rail/ui/skins
|
|
10
|
+
*/
|
|
11
|
+
import { Context, type Plugin } from '@snap-rail/cordis';
|
|
12
|
+
import { type ReactNode } from 'react';
|
|
13
|
+
/** The colour/typography/spacing vocabulary a theme must populate. */
|
|
14
|
+
export interface SkinTokens {
|
|
15
|
+
colorBg: string;
|
|
16
|
+
colorSurface: string;
|
|
17
|
+
colorSurfaceRaised: string;
|
|
18
|
+
colorText: string;
|
|
19
|
+
colorMuted: string;
|
|
20
|
+
colorPrimary: string;
|
|
21
|
+
colorOnPrimary: string;
|
|
22
|
+
colorDanger: string;
|
|
23
|
+
colorSuccess: string;
|
|
24
|
+
colorWarning: string;
|
|
25
|
+
colorBorder: string;
|
|
26
|
+
colorFocus: string;
|
|
27
|
+
chart1: string;
|
|
28
|
+
chart2: string;
|
|
29
|
+
chart3: string;
|
|
30
|
+
chart4: string;
|
|
31
|
+
chart5: string;
|
|
32
|
+
chart6: string;
|
|
33
|
+
fontSans: string;
|
|
34
|
+
fontMono: string;
|
|
35
|
+
textXs: string;
|
|
36
|
+
textSm: string;
|
|
37
|
+
textMd: string;
|
|
38
|
+
textLg: string;
|
|
39
|
+
textXl: string;
|
|
40
|
+
weightNormal: string;
|
|
41
|
+
weightMedium: string;
|
|
42
|
+
weightStrong: string;
|
|
43
|
+
leadingTight: string;
|
|
44
|
+
leadingNormal: string;
|
|
45
|
+
spaceXs: string;
|
|
46
|
+
spaceSm: string;
|
|
47
|
+
spaceMd: string;
|
|
48
|
+
spaceLg: string;
|
|
49
|
+
spaceXl: string;
|
|
50
|
+
radiusSm: string;
|
|
51
|
+
radiusMd: string;
|
|
52
|
+
radiusLg: string;
|
|
53
|
+
radiusFull: string;
|
|
54
|
+
borderWidth: string;
|
|
55
|
+
shadowSm: string;
|
|
56
|
+
shadowMd: string;
|
|
57
|
+
shadowLg: string;
|
|
58
|
+
durationFast: string;
|
|
59
|
+
durationNormal: string;
|
|
60
|
+
durationSlow: string;
|
|
61
|
+
easeStandard: string;
|
|
62
|
+
zOverlay: string;
|
|
63
|
+
zModal: string;
|
|
64
|
+
zToast: string;
|
|
65
|
+
controlHeightSm: string;
|
|
66
|
+
controlHeightMd: string;
|
|
67
|
+
controlHeightLg: string;
|
|
68
|
+
density: string;
|
|
69
|
+
}
|
|
70
|
+
/** One theme's full definition: a single complete look. */
|
|
71
|
+
export interface SkinDefinition {
|
|
72
|
+
id: string;
|
|
73
|
+
title: string;
|
|
74
|
+
/** The theme's single token set; picking another theme switches the set. */
|
|
75
|
+
tokens: SkinTokens;
|
|
76
|
+
/** Optional CSS text; the registry injects it scoped by `data-snap-skin`. */
|
|
77
|
+
css?: string;
|
|
78
|
+
}
|
|
79
|
+
/** The factory day theme id (cobalt palette, light surfaces). */
|
|
80
|
+
export declare const DEFAULT_LIGHT_ID = "default-light";
|
|
81
|
+
/** The factory night theme id (cobalt palette, dark surfaces). */
|
|
82
|
+
export declare const DEFAULT_DARK_ID = "default-dark";
|
|
83
|
+
/** The factory day theme. Always registered; never the first paint though — the night theme is. */
|
|
84
|
+
export declare const DEFAULT_LIGHT: SkinDefinition;
|
|
85
|
+
/** The factory night theme and the registry's fallback. */
|
|
86
|
+
export declare const DEFAULT_DARK: SkinDefinition;
|
|
87
|
+
/** One registered theme as the registry reports it. */
|
|
88
|
+
export interface SkinRow {
|
|
89
|
+
id: string;
|
|
90
|
+
title: string;
|
|
91
|
+
/** The registering package; factory themes carry `@snap-rail/ui`. */
|
|
92
|
+
caller: string;
|
|
93
|
+
}
|
|
94
|
+
/** Turn a token key into its CSS custom property (`chart1` → `--snap-chart-1`). */
|
|
95
|
+
export declare function skinTokenVar(key: keyof SkinTokens): string;
|
|
96
|
+
/** The service exposed as `ctx.skins`. */
|
|
97
|
+
export interface SkinsService {
|
|
98
|
+
/** Register a theme; disposal removes it and falls back to the factory night theme. */
|
|
99
|
+
register(caller: Context, skin: SkinDefinition): () => void;
|
|
100
|
+
/** Activate a theme: exact id first, then the id read as a contributor
|
|
101
|
+
* package (a package's first registered theme), then nothing. */
|
|
102
|
+
activate(selection: string): boolean;
|
|
103
|
+
/** Every registered theme, factory themes first. */
|
|
104
|
+
list(): readonly SkinRow[];
|
|
105
|
+
/** The active theme. */
|
|
106
|
+
active(): SkinDefinition;
|
|
107
|
+
/** The active theme's token object. */
|
|
108
|
+
tokens(): SkinTokens;
|
|
109
|
+
/** Subscribe to theme changes. */
|
|
110
|
+
subscribe(listener: () => void): () => void;
|
|
111
|
+
/** Monotonic change version. */
|
|
112
|
+
version(): number;
|
|
113
|
+
}
|
|
114
|
+
declare module '@snap-rail/cordis' {
|
|
115
|
+
interface Context {
|
|
116
|
+
skins: SkinsService;
|
|
117
|
+
}
|
|
118
|
+
interface Events {
|
|
119
|
+
/** The active theme changed. */
|
|
120
|
+
'skins/changed'(): void;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/** Mount the `ctx.skins` registry. */
|
|
124
|
+
declare const skinsPlugin: Plugin.Object<void>;
|
|
125
|
+
/** Provide the skin service to a renderer tree. */
|
|
126
|
+
export declare function SkinsProvider(props: {
|
|
127
|
+
service: SkinsService;
|
|
128
|
+
children: ReactNode;
|
|
129
|
+
}): ReactNode;
|
|
130
|
+
/** Read the skin service. */
|
|
131
|
+
export declare function useSkins(): SkinsService;
|
|
132
|
+
/** Re-render on theme changes and return the active token set. */
|
|
133
|
+
export declare function useSkinTokens(): SkinTokens;
|
|
134
|
+
/** Re-render on theme changes and return the active theme id. */
|
|
135
|
+
export declare function useSkinId(): string;
|
|
136
|
+
export default skinsPlugin;
|
|
137
|
+
//# sourceMappingURL=skins.d.ts.map
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useSyncExternalStore } from 'react';
|
|
3
|
+
/** The factory day theme id (cobalt palette, light surfaces). */
|
|
4
|
+
export const DEFAULT_LIGHT_ID = 'default-light';
|
|
5
|
+
/** The factory night theme id (cobalt palette, dark surfaces). */
|
|
6
|
+
export const DEFAULT_DARK_ID = 'default-dark';
|
|
7
|
+
/** The contributor label for the factory themes. */
|
|
8
|
+
const FACTORY_CALLER = '@snap-rail/ui';
|
|
9
|
+
const COBALT_DARK_TOKENS = {
|
|
10
|
+
colorBg: '#0b0f14',
|
|
11
|
+
colorSurface: '#11161d',
|
|
12
|
+
colorSurfaceRaised: '#161b22',
|
|
13
|
+
colorText: '#ced8e3',
|
|
14
|
+
colorMuted: '#8b949e',
|
|
15
|
+
colorPrimary: '#4a6cff',
|
|
16
|
+
colorOnPrimary: '#ffffff',
|
|
17
|
+
colorDanger: '#f85149',
|
|
18
|
+
colorSuccess: '#3fb950',
|
|
19
|
+
colorWarning: '#d29922',
|
|
20
|
+
colorBorder: '#1f2933',
|
|
21
|
+
colorFocus: '#4a6cff',
|
|
22
|
+
chart1: '#4a6cff',
|
|
23
|
+
chart2: '#3147a1',
|
|
24
|
+
chart3: '#667fcd',
|
|
25
|
+
chart4: '#7d8fa5',
|
|
26
|
+
chart5: '#55677c',
|
|
27
|
+
chart6: '#33414f',
|
|
28
|
+
fontSans: 'system-ui, "Segoe UI", "Microsoft YaHei", sans-serif',
|
|
29
|
+
fontMono: '"Cascadia Code", "Cascadia Mono", Consolas, ui-monospace, monospace',
|
|
30
|
+
textXs: '13px',
|
|
31
|
+
textSm: '15px',
|
|
32
|
+
textMd: '17px',
|
|
33
|
+
textLg: '20px',
|
|
34
|
+
textXl: '24px',
|
|
35
|
+
weightNormal: '400',
|
|
36
|
+
weightMedium: '500',
|
|
37
|
+
weightStrong: '600',
|
|
38
|
+
leadingTight: '1.2',
|
|
39
|
+
leadingNormal: '1.55',
|
|
40
|
+
spaceXs: '4px',
|
|
41
|
+
spaceSm: '8px',
|
|
42
|
+
spaceMd: '12px',
|
|
43
|
+
spaceLg: '20px',
|
|
44
|
+
spaceXl: '32px',
|
|
45
|
+
radiusSm: '6px',
|
|
46
|
+
radiusMd: '8px',
|
|
47
|
+
radiusLg: '12px',
|
|
48
|
+
radiusFull: '999px',
|
|
49
|
+
borderWidth: '1px',
|
|
50
|
+
shadowSm: '0 1px 2px rgb(0 0 0 / 0.25)',
|
|
51
|
+
shadowMd: '0 8px 24px rgb(0 0 0 / 0.28)',
|
|
52
|
+
shadowLg: '0 24px 64px rgb(0 0 0 / 0.36)',
|
|
53
|
+
durationFast: '120ms',
|
|
54
|
+
durationNormal: '180ms',
|
|
55
|
+
durationSlow: '280ms',
|
|
56
|
+
easeStandard: 'cubic-bezier(0.2, 0, 0, 1)',
|
|
57
|
+
zOverlay: '50',
|
|
58
|
+
zModal: '60',
|
|
59
|
+
zToast: '70',
|
|
60
|
+
controlHeightSm: '40px',
|
|
61
|
+
controlHeightMd: '48px',
|
|
62
|
+
controlHeightLg: '56px',
|
|
63
|
+
density: 'comfortable',
|
|
64
|
+
};
|
|
65
|
+
const COBALT_LIGHT_TOKENS = {
|
|
66
|
+
colorBg: '#e4e9ef',
|
|
67
|
+
colorSurface: '#f3f6f9',
|
|
68
|
+
colorSurfaceRaised: '#ffffff',
|
|
69
|
+
colorText: '#26323e',
|
|
70
|
+
colorMuted: '#5d6d7e',
|
|
71
|
+
colorPrimary: '#3857e8',
|
|
72
|
+
colorOnPrimary: '#ffffff',
|
|
73
|
+
colorDanger: '#dc2626',
|
|
74
|
+
colorSuccess: '#15803d',
|
|
75
|
+
colorWarning: '#b45309',
|
|
76
|
+
colorBorder: '#c7d0da',
|
|
77
|
+
colorFocus: '#3857e8',
|
|
78
|
+
chart1: '#3857e8',
|
|
79
|
+
chart2: '#7088ee',
|
|
80
|
+
chart3: '#667dd0',
|
|
81
|
+
chart4: '#94a3b8',
|
|
82
|
+
chart5: '#b3c0cd',
|
|
83
|
+
chart6: '#ccd6e0',
|
|
84
|
+
fontSans: 'system-ui, "Segoe UI", "Microsoft YaHei", sans-serif',
|
|
85
|
+
fontMono: '"Cascadia Code", "Cascadia Mono", Consolas, ui-monospace, monospace',
|
|
86
|
+
textXs: '13px',
|
|
87
|
+
textSm: '15px',
|
|
88
|
+
textMd: '17px',
|
|
89
|
+
textLg: '20px',
|
|
90
|
+
textXl: '24px',
|
|
91
|
+
weightNormal: '400',
|
|
92
|
+
weightMedium: '500',
|
|
93
|
+
weightStrong: '600',
|
|
94
|
+
leadingTight: '1.2',
|
|
95
|
+
leadingNormal: '1.55',
|
|
96
|
+
spaceXs: '4px',
|
|
97
|
+
spaceSm: '8px',
|
|
98
|
+
spaceMd: '12px',
|
|
99
|
+
spaceLg: '20px',
|
|
100
|
+
spaceXl: '32px',
|
|
101
|
+
radiusSm: '6px',
|
|
102
|
+
radiusMd: '8px',
|
|
103
|
+
radiusLg: '12px',
|
|
104
|
+
radiusFull: '999px',
|
|
105
|
+
borderWidth: '1px',
|
|
106
|
+
shadowSm: '0 1px 2px rgb(0 0 0 / 0.08)',
|
|
107
|
+
shadowMd: '0 8px 24px rgb(0 0 0 / 0.12)',
|
|
108
|
+
shadowLg: '0 24px 64px rgb(0 0 0 / 0.18)',
|
|
109
|
+
durationFast: '120ms',
|
|
110
|
+
durationNormal: '180ms',
|
|
111
|
+
durationSlow: '280ms',
|
|
112
|
+
easeStandard: 'cubic-bezier(0.2, 0, 0, 1)',
|
|
113
|
+
zOverlay: '50',
|
|
114
|
+
zModal: '60',
|
|
115
|
+
zToast: '70',
|
|
116
|
+
controlHeightSm: '40px',
|
|
117
|
+
controlHeightMd: '48px',
|
|
118
|
+
controlHeightLg: '56px',
|
|
119
|
+
density: 'comfortable',
|
|
120
|
+
};
|
|
121
|
+
/** The factory day theme. Always registered; never the first paint though — the night theme is. */
|
|
122
|
+
export const DEFAULT_LIGHT = {
|
|
123
|
+
id: DEFAULT_LIGHT_ID,
|
|
124
|
+
title: '钴蓝·白天',
|
|
125
|
+
tokens: COBALT_LIGHT_TOKENS,
|
|
126
|
+
};
|
|
127
|
+
/** The factory night theme and the registry's fallback. */
|
|
128
|
+
export const DEFAULT_DARK = {
|
|
129
|
+
id: DEFAULT_DARK_ID,
|
|
130
|
+
title: '钴蓝·黑夜',
|
|
131
|
+
tokens: COBALT_DARK_TOKENS,
|
|
132
|
+
};
|
|
133
|
+
/** Turn a token key into its CSS custom property (`chart1` → `--snap-chart-1`). */
|
|
134
|
+
export function skinTokenVar(key) {
|
|
135
|
+
const kebab = key.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`).replace(/(\d+)$/, '-$1');
|
|
136
|
+
return `--snap-${kebab}`;
|
|
137
|
+
}
|
|
138
|
+
/** Apply the active theme to the document root. */
|
|
139
|
+
function applySkin(skin) {
|
|
140
|
+
if (typeof document === 'undefined')
|
|
141
|
+
return;
|
|
142
|
+
const root = document.documentElement;
|
|
143
|
+
root.dataset['snapSkin'] = skin.id;
|
|
144
|
+
for (const [key, value] of Object.entries(skin.tokens)) {
|
|
145
|
+
root.style.setProperty(skinTokenVar(key), value);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/** Mount the `ctx.skins` registry. */
|
|
149
|
+
const skinsPlugin = {
|
|
150
|
+
name: 'snap-rail ui skins',
|
|
151
|
+
apply(ctx) {
|
|
152
|
+
const skins = new Map();
|
|
153
|
+
const callers = new Map();
|
|
154
|
+
const listeners = new Set();
|
|
155
|
+
const styleTags = new Map();
|
|
156
|
+
let activeId = DEFAULT_DARK_ID;
|
|
157
|
+
let revision = 0;
|
|
158
|
+
skins.set(DEFAULT_LIGHT_ID, DEFAULT_LIGHT);
|
|
159
|
+
callers.set(DEFAULT_LIGHT_ID, FACTORY_CALLER);
|
|
160
|
+
skins.set(DEFAULT_DARK_ID, DEFAULT_DARK);
|
|
161
|
+
callers.set(DEFAULT_DARK_ID, FACTORY_CALLER);
|
|
162
|
+
const notify = () => {
|
|
163
|
+
revision += 1;
|
|
164
|
+
for (const listener of listeners)
|
|
165
|
+
listener();
|
|
166
|
+
ctx.emit('skins/changed');
|
|
167
|
+
};
|
|
168
|
+
const ensureStyleTag = (skin) => {
|
|
169
|
+
if (typeof document === 'undefined' || skin.css === undefined)
|
|
170
|
+
return;
|
|
171
|
+
if (styleTags.has(skin.id))
|
|
172
|
+
return;
|
|
173
|
+
const tag = document.createElement('style');
|
|
174
|
+
tag.dataset['snapSkinStyle'] = skin.id;
|
|
175
|
+
tag.textContent = skin.css;
|
|
176
|
+
document.head.append(tag);
|
|
177
|
+
styleTags.set(skin.id, tag);
|
|
178
|
+
};
|
|
179
|
+
const refresh = () => {
|
|
180
|
+
const skin = skins.get(activeId) ?? DEFAULT_DARK;
|
|
181
|
+
activeId = skin.id;
|
|
182
|
+
ensureStyleTag(skin);
|
|
183
|
+
applySkin(skin);
|
|
184
|
+
notify();
|
|
185
|
+
};
|
|
186
|
+
ctx.provide('skins', {
|
|
187
|
+
register(caller, skin) {
|
|
188
|
+
skins.set(skin.id, skin);
|
|
189
|
+
callers.set(skin.id, caller.fiber.name);
|
|
190
|
+
// Registration only makes a theme selectable; activation stays with
|
|
191
|
+
// the operator's stored selection (applied by the runtime).
|
|
192
|
+
refresh();
|
|
193
|
+
const remove = () => {
|
|
194
|
+
if (!skins.delete(skin.id))
|
|
195
|
+
return;
|
|
196
|
+
callers.delete(skin.id);
|
|
197
|
+
const tag = styleTags.get(skin.id);
|
|
198
|
+
if (tag !== undefined) {
|
|
199
|
+
tag.remove();
|
|
200
|
+
styleTags.delete(skin.id);
|
|
201
|
+
}
|
|
202
|
+
if (activeId === skin.id)
|
|
203
|
+
activeId = DEFAULT_DARK_ID;
|
|
204
|
+
refresh();
|
|
205
|
+
};
|
|
206
|
+
caller.effect(() => remove);
|
|
207
|
+
return remove;
|
|
208
|
+
},
|
|
209
|
+
activate(selection) {
|
|
210
|
+
let next = skins.has(selection) ? selection : undefined;
|
|
211
|
+
if (next === undefined) {
|
|
212
|
+
for (const [id, owner] of callers) {
|
|
213
|
+
if (id !== selection && owner === selection && skins.has(id)) {
|
|
214
|
+
next = id;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (next === undefined)
|
|
220
|
+
return false;
|
|
221
|
+
if (next === activeId)
|
|
222
|
+
return true;
|
|
223
|
+
activeId = next;
|
|
224
|
+
refresh();
|
|
225
|
+
return true;
|
|
226
|
+
},
|
|
227
|
+
list: () => [...skins.values()].map(skin => ({
|
|
228
|
+
id: skin.id,
|
|
229
|
+
title: skin.title,
|
|
230
|
+
caller: callers.get(skin.id) ?? FACTORY_CALLER,
|
|
231
|
+
})),
|
|
232
|
+
active: () => skins.get(activeId) ?? DEFAULT_DARK,
|
|
233
|
+
tokens: () => (skins.get(activeId) ?? DEFAULT_DARK).tokens,
|
|
234
|
+
subscribe(listener) {
|
|
235
|
+
listeners.add(listener);
|
|
236
|
+
return () => { listeners.delete(listener); };
|
|
237
|
+
},
|
|
238
|
+
version: () => revision,
|
|
239
|
+
});
|
|
240
|
+
// Apply the factory night theme immediately so the first paint already
|
|
241
|
+
// carries the token vocabulary, even before any plugin registers.
|
|
242
|
+
refresh();
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
/** The React context carrying the skin service. */
|
|
246
|
+
const SkinsContext = createContext(null);
|
|
247
|
+
/** Provide the skin service to a renderer tree. */
|
|
248
|
+
export function SkinsProvider(props) {
|
|
249
|
+
return _jsx(SkinsContext.Provider, { value: props.service, children: props.children });
|
|
250
|
+
}
|
|
251
|
+
/** Read the skin service. */
|
|
252
|
+
export function useSkins() {
|
|
253
|
+
const service = useContext(SkinsContext);
|
|
254
|
+
if (service === null)
|
|
255
|
+
throw new Error('useSkins: no SkinsProvider in the tree');
|
|
256
|
+
return service;
|
|
257
|
+
}
|
|
258
|
+
/** Re-render on theme changes and return the active token set. */
|
|
259
|
+
export function useSkinTokens() {
|
|
260
|
+
const service = useSkins();
|
|
261
|
+
useSyncExternalStore(service.subscribe, service.version, service.version);
|
|
262
|
+
return service.tokens();
|
|
263
|
+
}
|
|
264
|
+
/** Re-render on theme changes and return the active theme id. */
|
|
265
|
+
export function useSkinId() {
|
|
266
|
+
const service = useSkins();
|
|
267
|
+
useSyncExternalStore(service.subscribe, service.version, service.version);
|
|
268
|
+
return service.active().id;
|
|
269
|
+
}
|
|
270
|
+
export default skinsPlugin;
|
|
271
|
+
//# sourceMappingURL=skins.js.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed region-slot registry behind `ctx.ui` and the `<Slot>` outlet.
|
|
3
|
+
*
|
|
4
|
+
* A layout declares which slots it provides; features and pages contribute
|
|
5
|
+
* against stable ids with a scoped context. A contribution registered before
|
|
6
|
+
* its layout mounts stays dormant; a contribution whose context does not fit
|
|
7
|
+
* is skipped with a diagnostic instead of crashing the shell.
|
|
8
|
+
*
|
|
9
|
+
* @module @snap-rail/ui/slots
|
|
10
|
+
*/
|
|
11
|
+
import { Context, type Plugin } from '@snap-rail/cordis';
|
|
12
|
+
import { type ReactNode } from 'react';
|
|
13
|
+
import { type LayoutContribution, type SlotContribution, type SlotDefinition, type UiDiagnostic } from './contract.ts';
|
|
14
|
+
/** The service exposed as `ctx.ui`. */
|
|
15
|
+
export interface UiService {
|
|
16
|
+
/** Declare a slot (built-ins are predeclared); disposal removes the declaration. */
|
|
17
|
+
defineSlot<C>(caller: Context, def: SlotDefinition<C>): () => void;
|
|
18
|
+
/** Register the active layout candidate; disposal removes it. */
|
|
19
|
+
registerLayout(caller: Context, layout: LayoutContribution): () => void;
|
|
20
|
+
/** Contribute to a slot; disposal removes the contribution. */
|
|
21
|
+
contribute<C>(caller: Context, slot: string, contribution: SlotContribution<C>): () => void;
|
|
22
|
+
/** Current contributions of a slot in render order (one winner for `one`). */
|
|
23
|
+
list(slot: string): readonly SlotContribution<unknown>[];
|
|
24
|
+
/** The declared definition of a slot, when one exists. */
|
|
25
|
+
definition(slot: string): SlotDefinition<unknown> | undefined;
|
|
26
|
+
/** The winning layout, or `undefined` when none registered. */
|
|
27
|
+
activeLayout(): LayoutContribution | undefined;
|
|
28
|
+
/** Whether the active layout provides a slot. */
|
|
29
|
+
hasProvider(slot: string): boolean;
|
|
30
|
+
/** The non-fatal contract violations observed so far. */
|
|
31
|
+
diagnostics(): readonly UiDiagnostic[];
|
|
32
|
+
/** Subscribe to registry changes (React `useSyncExternalStore` shape). */
|
|
33
|
+
subscribe(listener: () => void): () => void;
|
|
34
|
+
/** Monotonic registry version. */
|
|
35
|
+
version(): number;
|
|
36
|
+
}
|
|
37
|
+
declare module '@snap-rail/cordis' {
|
|
38
|
+
interface Context {
|
|
39
|
+
ui: UiService;
|
|
40
|
+
}
|
|
41
|
+
interface Events {
|
|
42
|
+
/** The slot/layout registry changed. */
|
|
43
|
+
'ui/changed'(): void;
|
|
44
|
+
/** One slot's occupants changed.
|
|
45
|
+
* @param slot - the affected slot id.
|
|
46
|
+
*/
|
|
47
|
+
'ui/slot-changed'(slot: string): void;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Mount the `ctx.ui` registry. */
|
|
51
|
+
declare const slotsPlugin: Plugin.Object<void>;
|
|
52
|
+
/** Provide the UI service to a slot tree. */
|
|
53
|
+
export declare function UiProvider(props: {
|
|
54
|
+
service: UiService;
|
|
55
|
+
children: ReactNode;
|
|
56
|
+
}): ReactNode;
|
|
57
|
+
/** Read the UI service; throws when a slot tree is mounted outside the runtime. */
|
|
58
|
+
export declare function useUi(): UiService;
|
|
59
|
+
/** Re-render on every registry change. */
|
|
60
|
+
export declare function useUiVersion(service: UiService): number;
|
|
61
|
+
/** A region-slot outlet rendered by a layout. */
|
|
62
|
+
export declare function Slot<C = unknown>(props: {
|
|
63
|
+
name: string;
|
|
64
|
+
context?: C;
|
|
65
|
+
}): ReactNode;
|
|
66
|
+
/** Re-render when the active layout changes. */
|
|
67
|
+
export declare function useActiveLayout(): LayoutContribution | undefined;
|
|
68
|
+
export default slotsPlugin;
|
|
69
|
+
//# sourceMappingURL=slots.d.ts.map
|