@snap-rail/ui 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.
Files changed (60) hide show
  1. package/LICENSE +202 -0
  2. package/lib/charts.js +142 -0
  3. package/lib/contract.js +543 -0
  4. package/lib/fallback.js +400 -0
  5. package/lib/index.js +3345 -0
  6. package/lib/pages.js +68 -0
  7. package/lib/runtime.js +2064 -0
  8. package/lib/settings.js +1195 -0
  9. package/lib/skins.js +284 -0
  10. package/lib/slots.js +267 -0
  11. package/lib/types/charts.d.ts +62 -0
  12. package/lib/types/charts.js +41 -0
  13. package/lib/types/components/button.d.ts +39 -0
  14. package/lib/types/components/button.js +27 -0
  15. package/lib/types/components/controls.d.ts +66 -0
  16. package/lib/types/components/controls.js +66 -0
  17. package/lib/types/components/dialog.d.ts +58 -0
  18. package/lib/types/components/dialog.js +46 -0
  19. package/lib/types/components/feedback.d.ts +46 -0
  20. package/lib/types/components/feedback.js +20 -0
  21. package/lib/types/components/form.d.ts +31 -0
  22. package/lib/types/components/form.js +13 -0
  23. package/lib/types/components/index.d.ts +15 -0
  24. package/lib/types/components/index.js +15 -0
  25. package/lib/types/components/input.d.ts +37 -0
  26. package/lib/types/components/input.js +15 -0
  27. package/lib/types/components/layout.d.ts +100 -0
  28. package/lib/types/components/layout.js +80 -0
  29. package/lib/types/components/schema-form.d.ts +34 -0
  30. package/lib/types/components/schema-form.js +61 -0
  31. package/lib/types/components/surface.d.ts +49 -0
  32. package/lib/types/components/surface.js +59 -0
  33. package/lib/types/contract.d.ts +358 -0
  34. package/lib/types/contract.js +235 -0
  35. package/lib/types/fallback.d.ts +14 -0
  36. package/lib/types/fallback.js +50 -0
  37. package/lib/types/index.d.ts +19 -0
  38. package/lib/types/index.js +19 -0
  39. package/lib/types/pages.d.ts +40 -0
  40. package/lib/types/pages.js +69 -0
  41. package/lib/types/runtime.d.ts +71 -0
  42. package/lib/types/runtime.js +177 -0
  43. package/lib/types/settings-pages/appearance.d.ts +13 -0
  44. package/lib/types/settings-pages/appearance.js +131 -0
  45. package/lib/types/settings-pages/index.d.ts +3 -0
  46. package/lib/types/settings-pages/index.js +3 -0
  47. package/lib/types/settings-pages/plugins.d.ts +16 -0
  48. package/lib/types/settings-pages/plugins.js +109 -0
  49. package/lib/types/settings.d.ts +58 -0
  50. package/lib/types/settings.js +170 -0
  51. package/lib/types/skins.d.ts +143 -0
  52. package/lib/types/skins.js +268 -0
  53. package/lib/types/slots.d.ts +69 -0
  54. package/lib/types/slots.js +210 -0
  55. package/lib/types/utils.d.ts +15 -0
  56. package/lib/types/utils.js +26 -0
  57. package/package.json +105 -3
  58. package/src/styles/base.css +321 -0
  59. package/src/styles/default.css +734 -0
  60. package/README.md +0 -3
@@ -0,0 +1,170 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Component, 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 { PluginsPage } from "./settings-pages/plugins.js";
7
+ import { cn, part } from "./utils.js";
8
+ /** Sort pages by order, then registration time. */
9
+ function orderedPages(pages) {
10
+ return [...pages]
11
+ .sort((a, b) => a.page.order - b.page.order || a.seq - b.seq)
12
+ .map(entry => entry.page);
13
+ }
14
+ /** A page-level boundary: one broken settings page must not take down the shell. */
15
+ class SettingsPageBoundary extends Component {
16
+ state = { failed: false };
17
+ static getDerivedStateFromError() {
18
+ return { failed: true };
19
+ }
20
+ componentDidCatch(error) {
21
+ console.error(`[snap-rail ui] settings page ${this.props.page} failed`, error);
22
+ }
23
+ render() {
24
+ return this.state.failed
25
+ ? _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" })
26
+ : this.props.children;
27
+ }
28
+ }
29
+ /** The gear glyph used by the default trigger. */
30
+ function GearIcon() {
31
+ 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" })] }));
32
+ }
33
+ /** The default settings trigger; the layout decides placement/presentation. */
34
+ export function SettingsEntry(props) {
35
+ if (props.presentation === 'label') {
36
+ return _jsx(Button, { intent: "neutral", emphasis: "ghost", icon: _jsx(GearIcon, {}), onClick: props.open, children: "\u8BBE\u7F6E" });
37
+ }
38
+ return _jsx(IconButton, { icon: _jsx(GearIcon, {}), label: "\u8BBE\u7F6E", active: props.isOpen, onClick: props.open });
39
+ }
40
+ /** The default settings surface. */
41
+ export function SettingsSurface(props) {
42
+ const { service } = props;
43
+ useSyncExternalStore(service.subscribe, service.version, service.version);
44
+ const open = service.isOpen();
45
+ const pages = service.listPages();
46
+ const activeId = service.activePage();
47
+ const active = pages.find(page => page.id === activeId);
48
+ const groups = new Map();
49
+ for (const page of pages) {
50
+ const group = page.group ?? '其他';
51
+ const bucket = groups.get(group);
52
+ if (bucket === undefined)
53
+ groups.set(group, [page]);
54
+ else
55
+ bucket.push(page);
56
+ }
57
+ return (_jsx(Dialog, { open: open, onOpenChange: next => { if (!next)
58
+ 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" }), _jsx(Button, { intent: "neutral", emphasis: "ghost", onClick: () => service.close(), children: "\u5173\u95ED" })] }), _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
59
+ ? _jsx(EmptyState, { title: "\u6682\u65E0\u8BBE\u7F6E\u9875", description: "\u5B89\u88C5\u6216\u542F\u7528\u4E00\u4E2A\u5E26\u8BBE\u7F6E\u9875\u7684\u63D2\u4EF6\u540E\uFF0C\u5B83\u4F1A\u51FA\u73B0\u5728\u8FD9\u91CC\u3002" })
60
+ : _jsx(SettingsPageBoundary, { page: active.id, children: active.render() }) })] })] }) }) }));
61
+ }
62
+ /** Mount the settings registry and its default slot contributions. */
63
+ const settingsPlugin = {
64
+ name: 'snap-rail settings',
65
+ inject: ['ui'],
66
+ apply(ctx) {
67
+ const pages = new Map();
68
+ const listeners = new Set();
69
+ let open = false;
70
+ let requestedId;
71
+ let revision = 0;
72
+ let seq = 0;
73
+ const notify = (kind) => {
74
+ revision += 1;
75
+ for (const listener of listeners)
76
+ listener();
77
+ ctx.emit(kind === 'pages' ? 'settings/pages-changed' : 'settings/open-changed');
78
+ };
79
+ const firstId = () => orderedPages(pages.values())[0]?.id;
80
+ const activeId = () => {
81
+ if (!open)
82
+ return undefined;
83
+ if (requestedId !== undefined && pages.has(requestedId))
84
+ return requestedId;
85
+ return firstId();
86
+ };
87
+ const service = {
88
+ registerPage(caller, page) {
89
+ pages.set(page.id, { page, seq: seq++ });
90
+ notify('pages');
91
+ const remove = () => {
92
+ if (pages.delete(page.id))
93
+ notify('pages');
94
+ };
95
+ caller.effect(() => remove);
96
+ return remove;
97
+ },
98
+ listPages: () => orderedPages(pages.values()),
99
+ open(pageId) {
100
+ if (pageId !== undefined)
101
+ requestedId = pageId;
102
+ if (open && pageId === undefined)
103
+ return;
104
+ open = true;
105
+ notify('open');
106
+ },
107
+ close() {
108
+ if (!open)
109
+ return;
110
+ open = false;
111
+ notify('open');
112
+ },
113
+ toggle() {
114
+ if (open)
115
+ service.close();
116
+ else
117
+ service.open();
118
+ },
119
+ isOpen: () => open,
120
+ activePage: activeId,
121
+ subscribe(listener) {
122
+ listeners.add(listener);
123
+ return () => { listeners.delete(listener); };
124
+ },
125
+ version: () => revision,
126
+ };
127
+ ctx.provide('settings', service);
128
+ // First-party pages ship with the substrate so a fresh install can manage
129
+ // its own plugins without any external settings-page package.
130
+ ctx.settings.registerPage(ctx, {
131
+ id: 'plugins',
132
+ title: '插件管理',
133
+ group: '系统',
134
+ order: 0,
135
+ keywords: ['安装', '启用', '卸载', '驱动', '皮肤'],
136
+ render: () => _jsx(PluginsPage, { ctx: ctx }),
137
+ });
138
+ ctx.settings.registerPage(ctx, {
139
+ id: 'appearance',
140
+ title: '外观与显示',
141
+ group: '系统',
142
+ order: 1,
143
+ keywords: ['主题', '皮肤', '窗口', '悬浮球', '尺寸'],
144
+ render: () => _jsx(AppearancePage, {}),
145
+ });
146
+ // The default trigger and surface are ordinary contributions: a plugin may
147
+ // replace either with a higher precedence, including a fully custom UI.
148
+ ctx.ui.contribute(ctx, UI_SLOT_IDS.settingsEntry, {
149
+ id: 'settings.default-entry',
150
+ order: 0,
151
+ render: (context) => (_jsx(SettingsEntry, { presentation: context.presentation, open: context.open, isOpen: context.isOpen })),
152
+ });
153
+ ctx.ui.contribute(ctx, UI_SLOT_IDS.settingsSurface, {
154
+ id: 'settings.default-surface',
155
+ order: 0,
156
+ render: () => _jsx(SettingsSurface, { service: service }),
157
+ });
158
+ },
159
+ };
160
+ /** The settings page info the slot context exposes. */
161
+ export function settingsPageInfo(page) {
162
+ return {
163
+ id: page.id,
164
+ title: page.title,
165
+ ...page.group !== undefined ? { group: page.group } : {},
166
+ order: page.order,
167
+ };
168
+ }
169
+ export default settingsPlugin;
170
+ //# sourceMappingURL=settings.js.map
@@ -0,0 +1,143 @@
1
+ /**
2
+ * The skin registry and token bridge. A skin is a declared visual
3
+ * implementation of the semantic component contract: token values plus part
4
+ * CSS. Exactly one skin is active; the built-in default is always available
5
+ * as the fallback.
6
+ *
7
+ * @module @snap-rail/ui/skins
8
+ */
9
+ import { Context, type Plugin } from '@snap-rail/cordis';
10
+ import { type ReactNode } from 'react';
11
+ import { PRESENTATION_TARGETS, type PresentationTarget, type PresentationVariant } from './contract.ts';
12
+ /** The colour/typography/spacing vocabulary a skin must populate. */
13
+ export interface SkinTokens {
14
+ colorBg: string;
15
+ colorSurface: string;
16
+ colorSurfaceRaised: string;
17
+ colorText: string;
18
+ colorMuted: string;
19
+ colorPrimary: string;
20
+ colorOnPrimary: string;
21
+ colorDanger: string;
22
+ colorSuccess: string;
23
+ colorWarning: string;
24
+ colorBorder: string;
25
+ colorFocus: string;
26
+ chart1: string;
27
+ chart2: string;
28
+ chart3: string;
29
+ chart4: string;
30
+ chart5: string;
31
+ chart6: string;
32
+ fontSans: string;
33
+ fontMono: string;
34
+ textXs: string;
35
+ textSm: string;
36
+ textMd: string;
37
+ textLg: string;
38
+ textXl: string;
39
+ weightNormal: string;
40
+ weightMedium: string;
41
+ weightStrong: string;
42
+ leadingTight: string;
43
+ leadingNormal: string;
44
+ spaceXs: string;
45
+ spaceSm: string;
46
+ spaceMd: string;
47
+ spaceLg: string;
48
+ spaceXl: string;
49
+ radiusSm: string;
50
+ radiusMd: string;
51
+ radiusLg: string;
52
+ radiusFull: string;
53
+ borderWidth: string;
54
+ shadowSm: string;
55
+ shadowMd: string;
56
+ shadowLg: string;
57
+ durationFast: string;
58
+ durationNormal: string;
59
+ durationSlow: string;
60
+ easeStandard: string;
61
+ zOverlay: string;
62
+ zModal: string;
63
+ zToast: string;
64
+ controlHeightSm: string;
65
+ controlHeightMd: string;
66
+ controlHeightLg: string;
67
+ density: string;
68
+ }
69
+ /** One skin's full definition. */
70
+ export interface SkinDefinition {
71
+ id: string;
72
+ title: string;
73
+ /** Token values for both modes; the active mode picks one set. */
74
+ tokens: {
75
+ light: SkinTokens;
76
+ dark: SkinTokens;
77
+ };
78
+ /** Presentation defaults the skin chooses for core variants. */
79
+ presentations?: Partial<Record<PresentationTarget, PresentationVariant>>;
80
+ /** Parts the skin claims to style (coverage diagnostics, optional). */
81
+ parts?: readonly string[];
82
+ /** Optional CSS text; the registry injects it scoped by `data-snap-skin`. */
83
+ css?: string;
84
+ }
85
+ /** The two colour modes. */
86
+ export declare const SKIN_MODES: readonly ["light", "dark"];
87
+ /** One skin mode. */
88
+ export type SkinMode = (typeof SKIN_MODES)[number];
89
+ /** The built-in default skin id. */
90
+ export declare const DEFAULT_SKIN_ID = "default";
91
+ /** The factory cobalt/steel skin. Extra skins override it through the registry. */
92
+ export declare const DEFAULT_SKIN: SkinDefinition;
93
+ /** Turn a token key into its CSS custom property (`chart1` → `--snap-chart-1`). */
94
+ export declare function skinTokenVar(key: keyof SkinTokens): string;
95
+ /** The service exposed as `ctx.skins`. */
96
+ export interface SkinsService {
97
+ /** Register a skin; disposal removes it and falls back to the default. */
98
+ register(caller: Context, skin: SkinDefinition): () => void;
99
+ /** Activate a registered skin by id. */
100
+ activate(skinId: string): boolean;
101
+ /** Every registered skin, default first. */
102
+ list(): readonly SkinDefinition[];
103
+ /** The active skin. */
104
+ active(): SkinDefinition;
105
+ /** Set the colour mode and re-apply the active tokens. */
106
+ setMode(mode: SkinMode): void;
107
+ /** The current colour mode. */
108
+ mode(): SkinMode;
109
+ /** The active skin's presentation default for a target, when one is set. */
110
+ presentation(target: PresentationTarget): PresentationVariant | undefined;
111
+ /** The active skin's token object for the current mode. */
112
+ tokens(): SkinTokens;
113
+ /** Subscribe to skin/mode changes. */
114
+ subscribe(listener: () => void): () => void;
115
+ /** Monotonic change version. */
116
+ version(): number;
117
+ }
118
+ declare module '@snap-rail/cordis' {
119
+ interface Context {
120
+ skins: SkinsService;
121
+ }
122
+ interface Events {
123
+ /** The active skin or colour mode changed. */
124
+ 'skins/changed'(): void;
125
+ }
126
+ }
127
+ /** Mount the `ctx.skins` registry. */
128
+ declare const skinsPlugin: Plugin.Object<void>;
129
+ /** Provide the skin service to a renderer tree. */
130
+ export declare function SkinsProvider(props: {
131
+ service: SkinsService;
132
+ children: ReactNode;
133
+ }): ReactNode;
134
+ /** Read the skin service. */
135
+ export declare function useSkins(): SkinsService;
136
+ /** Re-render on skin/mode changes and return the active token set. */
137
+ export declare function useSkinTokens(): SkinTokens;
138
+ /** Re-render on skin/mode changes and return the active presentation default. */
139
+ export declare function usePresentation(target: PresentationTarget): PresentationVariant | undefined;
140
+ /** Re-export the presentation vocabulary for skin authors. */
141
+ export { PRESENTATION_TARGETS };
142
+ export default skinsPlugin;
143
+ //# sourceMappingURL=skins.d.ts.map
@@ -0,0 +1,268 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext, useSyncExternalStore } from 'react';
3
+ import { PRESENTATION_TARGETS, } from "./contract.js";
4
+ /** The two colour modes. */
5
+ export const SKIN_MODES = ['light', 'dark'];
6
+ /** The built-in default skin id. */
7
+ export const DEFAULT_SKIN_ID = 'default';
8
+ /** The factory cobalt/steel skin. Extra skins override it through the registry. */
9
+ export const DEFAULT_SKIN = {
10
+ id: DEFAULT_SKIN_ID,
11
+ title: '工业钴蓝',
12
+ presentations: {
13
+ dialog: 'modal',
14
+ select: 'popover',
15
+ tabs: 'inline',
16
+ toast: 'inline',
17
+ },
18
+ tokens: {
19
+ dark: {
20
+ colorBg: '#0b0f14',
21
+ colorSurface: '#11161d',
22
+ colorSurfaceRaised: '#161b22',
23
+ colorText: '#ced8e3',
24
+ colorMuted: '#8b949e',
25
+ colorPrimary: '#4a6cff',
26
+ colorOnPrimary: '#ffffff',
27
+ colorDanger: '#f85149',
28
+ colorSuccess: '#3fb950',
29
+ colorWarning: '#d29922',
30
+ colorBorder: '#1f2933',
31
+ colorFocus: '#4a6cff',
32
+ chart1: '#4a6cff',
33
+ chart2: '#3147a1',
34
+ chart3: '#667fcd',
35
+ chart4: '#7d8fa5',
36
+ chart5: '#55677c',
37
+ chart6: '#33414f',
38
+ fontSans: 'system-ui, "Segoe UI", "Microsoft YaHei", sans-serif',
39
+ fontMono: '"Cascadia Code", "Cascadia Mono", Consolas, ui-monospace, monospace',
40
+ textXs: '13px',
41
+ textSm: '15px',
42
+ textMd: '17px',
43
+ textLg: '20px',
44
+ textXl: '24px',
45
+ weightNormal: '400',
46
+ weightMedium: '500',
47
+ weightStrong: '600',
48
+ leadingTight: '1.2',
49
+ leadingNormal: '1.55',
50
+ spaceXs: '4px',
51
+ spaceSm: '8px',
52
+ spaceMd: '12px',
53
+ spaceLg: '20px',
54
+ spaceXl: '32px',
55
+ radiusSm: '6px',
56
+ radiusMd: '8px',
57
+ radiusLg: '12px',
58
+ radiusFull: '999px',
59
+ borderWidth: '1px',
60
+ shadowSm: '0 1px 2px rgb(0 0 0 / 0.25)',
61
+ shadowMd: '0 8px 24px rgb(0 0 0 / 0.28)',
62
+ shadowLg: '0 24px 64px rgb(0 0 0 / 0.36)',
63
+ durationFast: '120ms',
64
+ durationNormal: '180ms',
65
+ durationSlow: '280ms',
66
+ easeStandard: 'cubic-bezier(0.2, 0, 0, 1)',
67
+ zOverlay: '50',
68
+ zModal: '60',
69
+ zToast: '70',
70
+ controlHeightSm: '40px',
71
+ controlHeightMd: '48px',
72
+ controlHeightLg: '56px',
73
+ density: 'comfortable',
74
+ },
75
+ light: {
76
+ colorBg: '#e4e9ef',
77
+ colorSurface: '#f3f6f9',
78
+ colorSurfaceRaised: '#ffffff',
79
+ colorText: '#26323e',
80
+ colorMuted: '#5d6d7e',
81
+ colorPrimary: '#3857e8',
82
+ colorOnPrimary: '#ffffff',
83
+ colorDanger: '#dc2626',
84
+ colorSuccess: '#15803d',
85
+ colorWarning: '#b45309',
86
+ colorBorder: '#c7d0da',
87
+ colorFocus: '#3857e8',
88
+ chart1: '#3857e8',
89
+ chart2: '#7088ee',
90
+ chart3: '#667dd0',
91
+ chart4: '#94a3b8',
92
+ chart5: '#b3c0cd',
93
+ chart6: '#ccd6e0',
94
+ fontSans: 'system-ui, "Segoe UI", "Microsoft YaHei", sans-serif',
95
+ fontMono: '"Cascadia Code", "Cascadia Mono", Consolas, ui-monospace, monospace',
96
+ textXs: '13px',
97
+ textSm: '15px',
98
+ textMd: '17px',
99
+ textLg: '20px',
100
+ textXl: '24px',
101
+ weightNormal: '400',
102
+ weightMedium: '500',
103
+ weightStrong: '600',
104
+ leadingTight: '1.2',
105
+ leadingNormal: '1.55',
106
+ spaceXs: '4px',
107
+ spaceSm: '8px',
108
+ spaceMd: '12px',
109
+ spaceLg: '20px',
110
+ spaceXl: '32px',
111
+ radiusSm: '6px',
112
+ radiusMd: '8px',
113
+ radiusLg: '12px',
114
+ radiusFull: '999px',
115
+ borderWidth: '1px',
116
+ shadowSm: '0 1px 2px rgb(0 0 0 / 0.08)',
117
+ shadowMd: '0 8px 24px rgb(0 0 0 / 0.12)',
118
+ shadowLg: '0 24px 64px rgb(0 0 0 / 0.18)',
119
+ durationFast: '120ms',
120
+ durationNormal: '180ms',
121
+ durationSlow: '280ms',
122
+ easeStandard: 'cubic-bezier(0.2, 0, 0, 1)',
123
+ zOverlay: '50',
124
+ zModal: '60',
125
+ zToast: '70',
126
+ controlHeightSm: '40px',
127
+ controlHeightMd: '48px',
128
+ controlHeightLg: '56px',
129
+ density: 'comfortable',
130
+ },
131
+ },
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 skin to the document root. */
139
+ function applySkin(skin, mode) {
140
+ if (typeof document === 'undefined')
141
+ return;
142
+ const root = document.documentElement;
143
+ root.dataset['snapSkin'] = skin.id;
144
+ root.dataset['snapMode'] = mode;
145
+ const tokens = skin.tokens[mode];
146
+ for (const [key, value] of Object.entries(tokens)) {
147
+ root.style.setProperty(skinTokenVar(key), value);
148
+ }
149
+ }
150
+ /** Mount the `ctx.skins` registry. */
151
+ const skinsPlugin = {
152
+ name: 'snap-rail ui skins',
153
+ apply(ctx) {
154
+ const skins = new Map();
155
+ const listeners = new Set();
156
+ const styleTags = new Map();
157
+ let activeId = DEFAULT_SKIN_ID;
158
+ let mode = 'dark';
159
+ let revision = 0;
160
+ skins.set(DEFAULT_SKIN_ID, DEFAULT_SKIN);
161
+ const notify = () => {
162
+ revision += 1;
163
+ for (const listener of listeners)
164
+ listener();
165
+ ctx.emit('skins/changed');
166
+ };
167
+ const ensureStyleTag = (skin) => {
168
+ if (typeof document === 'undefined' || skin.css === undefined)
169
+ return;
170
+ if (styleTags.has(skin.id))
171
+ return;
172
+ const tag = document.createElement('style');
173
+ tag.dataset['snapSkinStyle'] = skin.id;
174
+ tag.textContent = skin.css;
175
+ document.head.append(tag);
176
+ styleTags.set(skin.id, tag);
177
+ };
178
+ const refresh = () => {
179
+ const skin = skins.get(activeId) ?? DEFAULT_SKIN;
180
+ activeId = skin.id;
181
+ ensureStyleTag(skin);
182
+ applySkin(skin, mode);
183
+ notify();
184
+ };
185
+ ctx.provide('skins', {
186
+ register(caller, skin) {
187
+ skins.set(skin.id, skin);
188
+ // A pool skin (non-default) becomes active immediately; the default
189
+ // stays underneath as the fallback when the pool skin is removed.
190
+ if (skin.id !== DEFAULT_SKIN_ID)
191
+ activeId = skin.id;
192
+ refresh();
193
+ const remove = () => {
194
+ if (!skins.delete(skin.id))
195
+ return;
196
+ const tag = styleTags.get(skin.id);
197
+ if (tag !== undefined) {
198
+ tag.remove();
199
+ styleTags.delete(skin.id);
200
+ }
201
+ if (activeId === skin.id)
202
+ activeId = DEFAULT_SKIN_ID;
203
+ refresh();
204
+ };
205
+ caller.effect(() => remove);
206
+ return remove;
207
+ },
208
+ activate(skinId) {
209
+ if (!skins.has(skinId))
210
+ return false;
211
+ activeId = skinId;
212
+ refresh();
213
+ return true;
214
+ },
215
+ list: () => [skins.get(DEFAULT_SKIN_ID) ?? DEFAULT_SKIN, ...[...skins.values()].filter(skin => skin.id !== DEFAULT_SKIN_ID)],
216
+ active: () => skins.get(activeId) ?? DEFAULT_SKIN,
217
+ setMode(next) {
218
+ if (mode === next)
219
+ return;
220
+ mode = next;
221
+ refresh();
222
+ },
223
+ mode: () => mode,
224
+ presentation(target) {
225
+ const skin = skins.get(activeId) ?? DEFAULT_SKIN;
226
+ return skin.presentations?.[target];
227
+ },
228
+ tokens: () => (skins.get(activeId) ?? DEFAULT_SKIN).tokens[mode],
229
+ subscribe(listener) {
230
+ listeners.add(listener);
231
+ return () => { listeners.delete(listener); };
232
+ },
233
+ version: () => revision,
234
+ });
235
+ // Apply the factory skin immediately so the first paint already carries
236
+ // the token vocabulary, even before any plugin registers.
237
+ refresh();
238
+ },
239
+ };
240
+ /** The React context carrying the skin service. */
241
+ const SkinsContext = createContext(null);
242
+ /** Provide the skin service to a renderer tree. */
243
+ export function SkinsProvider(props) {
244
+ return _jsx(SkinsContext.Provider, { value: props.service, children: props.children });
245
+ }
246
+ /** Read the skin service. */
247
+ export function useSkins() {
248
+ const service = useContext(SkinsContext);
249
+ if (service === null)
250
+ throw new Error('useSkins: no SkinsProvider in the tree');
251
+ return service;
252
+ }
253
+ /** Re-render on skin/mode changes and return the active token set. */
254
+ export function useSkinTokens() {
255
+ const service = useSkins();
256
+ useSyncExternalStore(service.subscribe, service.version, service.version);
257
+ return service.tokens();
258
+ }
259
+ /** Re-render on skin/mode changes and return the active presentation default. */
260
+ export function usePresentation(target) {
261
+ const service = useSkins();
262
+ useSyncExternalStore(service.subscribe, service.version, service.version);
263
+ return service.presentation(target);
264
+ }
265
+ /** Re-export the presentation vocabulary for skin authors. */
266
+ export { PRESENTATION_TARGETS };
267
+ export default skinsPlugin;
268
+ //# 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