@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,400 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { Slot } from "@radix-ui/react-slot";
3
+ import "@radix-ui/react-checkbox";
4
+ import "@radix-ui/react-label";
5
+ import "@radix-ui/react-radio-group";
6
+ import "@radix-ui/react-select";
7
+ import "@radix-ui/react-switch";
8
+ import "@radix-ui/react-tabs";
9
+ import { Component, createContext, useContext, useSyncExternalStore } from "react";
10
+ import "@radix-ui/react-dialog";
11
+ //#region lib/types/utils.js
12
+ /**
13
+ * Small helpers shared by the headless semantic components: class merging and
14
+ * the stable `data-snap-*` anatomy that skins target.
15
+ *
16
+ * @module @snap-rail/ui/utils
17
+ */
18
+ /** Join truthy class names. */
19
+ function cn(...values) {
20
+ return values.filter((value) => typeof value === "string" && value.length > 0).join(" ");
21
+ }
22
+ /** The anatomy attributes every semantic component renders. */
23
+ function part(component, name) {
24
+ return {
25
+ "data-snap-component": component,
26
+ "data-snap-part": `${component}.${name}`
27
+ };
28
+ }
29
+ //#endregion
30
+ //#region lib/types/components/button.js
31
+ /**
32
+ * Semantic buttons: one behaviour, skin-owned visuals. The DOM exposes
33
+ * `button.root`, `button.label`, `button.icon`, and `button.spinner` parts;
34
+ * intent/emphasis/size are data attributes a skin styles.
35
+ *
36
+ * @module @snap-rail/ui/components/button
37
+ */
38
+ /** A spinner that inherits the current text colour. */
39
+ function Spinner(props) {
40
+ return jsxs("svg", {
41
+ ...part("button", "spinner"),
42
+ className: cn("snap-spinner", props.className),
43
+ viewBox: "0 0 24 24",
44
+ "aria-hidden": "true",
45
+ children: [jsx("circle", {
46
+ cx: "12",
47
+ cy: "12",
48
+ r: "9",
49
+ fill: "none",
50
+ stroke: "currentColor",
51
+ strokeWidth: "3",
52
+ opacity: "0.25"
53
+ }), jsx("path", {
54
+ d: "M21 12a9 9 0 0 0-9-9",
55
+ fill: "none",
56
+ stroke: "currentColor",
57
+ strokeWidth: "3",
58
+ strokeLinecap: "round"
59
+ })]
60
+ });
61
+ }
62
+ /** A themed button; `asChild` composes the behaviour onto a child element. */
63
+ function Button({ intent = "neutral", emphasis = "solid", size = "md", loading = false, icon, iconPosition = "start", asChild = false, disabled, className, children, type, ...props }) {
64
+ const Component = asChild ? Slot : "button";
65
+ const content = asChild ? children : jsxs(Fragment, { children: [
66
+ icon !== void 0 && iconPosition === "start" && jsx("span", {
67
+ ...part("button", "icon"),
68
+ children: icon
69
+ }),
70
+ children !== void 0 && jsx("span", {
71
+ ...part("button", "label"),
72
+ children
73
+ }),
74
+ icon !== void 0 && iconPosition === "end" && jsx("span", {
75
+ ...part("button", "icon"),
76
+ children: icon
77
+ }),
78
+ loading && jsx(Spinner, {})
79
+ ] });
80
+ return jsx(Component, {
81
+ ...part("button", "root"),
82
+ ...type !== void 0 ? { type } : !asChild ? { type: "button" } : {},
83
+ className: cn("snap-button", className),
84
+ "data-intent": intent,
85
+ "data-emphasis": emphasis,
86
+ "data-size": size,
87
+ "data-loading": loading || void 0,
88
+ "data-disabled": disabled === true || void 0,
89
+ "aria-busy": loading || void 0,
90
+ disabled: !asChild ? disabled === true || loading : void 0,
91
+ ...props,
92
+ children: content
93
+ });
94
+ }
95
+ /** A square glyph button with a mandatory accessible label. */
96
+ function IconButton({ icon, label, active = false, intent = "neutral", emphasis = "ghost", size = "md", className, ...props }) {
97
+ return jsx("button", {
98
+ ...part("icon-button", "root"),
99
+ type: "button",
100
+ className: cn("snap-icon-button", className),
101
+ "data-intent": intent,
102
+ "data-emphasis": emphasis,
103
+ "data-size": size,
104
+ "data-active": active || void 0,
105
+ "aria-label": label,
106
+ "aria-pressed": active || void 0,
107
+ ...props,
108
+ children: jsx("span", {
109
+ ...part("icon-button", "icon"),
110
+ "aria-hidden": "true",
111
+ children: icon
112
+ })
113
+ });
114
+ }
115
+ //#endregion
116
+ //#region lib/types/components/feedback.js
117
+ /** A centered empty/zero-state block. */
118
+ function EmptyState({ title, description, icon, action, className }) {
119
+ return jsxs("div", {
120
+ ...part("empty-state", "root"),
121
+ className: cn("snap-empty-state", className),
122
+ children: [
123
+ icon !== void 0 && jsx("div", {
124
+ ...part("empty-state", "icon"),
125
+ children: icon
126
+ }),
127
+ jsx("div", {
128
+ ...part("empty-state", "title"),
129
+ children: title
130
+ }),
131
+ description !== void 0 && jsx("div", {
132
+ ...part("empty-state", "description"),
133
+ children: description
134
+ }),
135
+ action !== void 0 && jsx("div", {
136
+ ...part("empty-state", "action"),
137
+ children: action
138
+ })
139
+ ]
140
+ });
141
+ }
142
+ //#endregion
143
+ //#region lib/types/components/layout.js
144
+ /** The operator-window shell frame. */
145
+ function AppShell({ header, sidebar, footer, className, style, children }) {
146
+ return jsxs("div", {
147
+ ...part("app-shell", "root"),
148
+ className: cn("snap-app-shell", className),
149
+ style: {
150
+ display: "flex",
151
+ flexDirection: "column",
152
+ height: "100%",
153
+ minHeight: 0,
154
+ ...style
155
+ },
156
+ children: [
157
+ header !== void 0 && jsx("div", {
158
+ ...part("app-shell", "header"),
159
+ style: { flexShrink: 0 },
160
+ children: header
161
+ }),
162
+ jsxs("div", {
163
+ style: {
164
+ display: "flex",
165
+ flex: 1,
166
+ minHeight: 0
167
+ },
168
+ children: [sidebar !== void 0 && jsx("div", {
169
+ ...part("app-shell", "sidebar"),
170
+ style: {
171
+ flexShrink: 0,
172
+ minHeight: 0
173
+ },
174
+ children: sidebar
175
+ }), jsx("div", {
176
+ ...part("app-shell", "main"),
177
+ style: {
178
+ flex: 1,
179
+ minWidth: 0,
180
+ minHeight: 0,
181
+ overflow: "auto"
182
+ },
183
+ children
184
+ })]
185
+ }),
186
+ footer !== void 0 && jsx("div", {
187
+ ...part("app-shell", "footer"),
188
+ style: { flexShrink: 0 },
189
+ children: footer
190
+ })
191
+ ]
192
+ });
193
+ }
194
+ //#endregion
195
+ //#region lib/types/contract.js
196
+ /** The stable region slots every layout may provide. */
197
+ const UI_SLOT_IDS = {
198
+ /** The one active layout of the operator window. */
199
+ layout: "shell.layout",
200
+ /** The operator titlebar. */
201
+ titlebar: "shell.titlebar",
202
+ /** Contributed titlebar actions. */
203
+ titlebarActions: "shell.titlebar.actions",
204
+ /** Sidebar extension area. */
205
+ sidebar: "shell.sidebar",
206
+ /** Statusbar extension area. */
207
+ statusbar: "shell.statusbar",
208
+ /** The global overlay stack (dialogs, confirms). */
209
+ overlay: "shell.overlay",
210
+ /** The floating ball surface face. */
211
+ ball: "shell.ball",
212
+ /** The settings trigger. */
213
+ settingsEntry: "settings.entry",
214
+ /** The settings surface itself. */
215
+ settingsSurface: "settings.surface"
216
+ };
217
+ UI_SLOT_IDS.layout, UI_SLOT_IDS.titlebar, UI_SLOT_IDS.titlebarActions, UI_SLOT_IDS.sidebar, UI_SLOT_IDS.statusbar, UI_SLOT_IDS.overlay, UI_SLOT_IDS.ball, UI_SLOT_IDS.settingsEntry, UI_SLOT_IDS.settingsSurface;
218
+ //#endregion
219
+ //#region lib/types/pages.js
220
+ /**
221
+ * The main-page registry: business views contribute a page, a layout renders
222
+ * the active one. This is deliberately separate from settings pages and from
223
+ * the chrome slots; gating/workflows can wrap it later without changing the
224
+ * page authoring surface.
225
+ *
226
+ * @module @snap-rail/ui/pages
227
+ */
228
+ /** Re-render a layout when the page registry or active page changes. */
229
+ function usePagesVersion(ctx) {
230
+ return useSyncExternalStore(ctx.pages.subscribe, ctx.pages.version, ctx.pages.version);
231
+ }
232
+ //#endregion
233
+ //#region lib/types/slots.js
234
+ /** The React context carrying the active UI service. */
235
+ const UiContext = createContext(null);
236
+ /** Read the UI service; throws when a slot tree is mounted outside the runtime. */
237
+ function useUi() {
238
+ const service = useContext(UiContext);
239
+ if (service === null) throw new Error("useUi: no UiProvider in the tree");
240
+ return service;
241
+ }
242
+ /** Re-render on every registry change. */
243
+ function useUiVersion(service) {
244
+ return useSyncExternalStore(service.subscribe, service.version, service.version);
245
+ }
246
+ /** Keep one broken contribution from taking down the whole shell. */
247
+ var SlotBoundary = class extends Component {
248
+ state = { failed: false };
249
+ static getDerivedStateFromError() {
250
+ return { failed: true };
251
+ }
252
+ componentDidCatch(error) {
253
+ this.props.onError(error);
254
+ }
255
+ render() {
256
+ return this.state.failed ? null : this.props.children;
257
+ }
258
+ };
259
+ /** A region-slot outlet rendered by a layout. */
260
+ function Slot$1(props) {
261
+ const service = useUi();
262
+ useUiVersion(service);
263
+ const provided = service.hasProvider(props.name);
264
+ const entry = service.list(props.name);
265
+ const def = service.definition(props.name);
266
+ const context = props.context ?? {};
267
+ if (!provided) return null;
268
+ if (entry.length === 0) {
269
+ const fallback = def?.fallback;
270
+ return fallback?.(context) ?? null;
271
+ }
272
+ return jsx(Fragment, { children: entry.map((item) => jsx(SlotBoundary, {
273
+ onError: (error) => {
274
+ console.error(`[snap-rail ui] slot ${props.name} contribution ${item.id} failed`, error);
275
+ },
276
+ children: item.render(context)
277
+ }, item.id)) });
278
+ }
279
+ //#endregion
280
+ //#region lib/types/fallback.js
281
+ /** A compact glyph for the window controls. */
282
+ function Glyph(props) {
283
+ return jsx("span", {
284
+ "aria-hidden": "true",
285
+ style: { lineHeight: 1 },
286
+ children: props.children
287
+ });
288
+ }
289
+ /** The fallback body. */
290
+ function FallbackShell(props) {
291
+ useUiVersion(props.ctx.ui);
292
+ usePagesVersion(props.ctx);
293
+ const active = props.ctx.pages.active();
294
+ const control = (action) => {
295
+ props.ctx.client.link.call("window.control", { action }).catch(() => {});
296
+ };
297
+ const header = jsxs("div", {
298
+ className: "snap-fallback-titlebar",
299
+ style: {
300
+ display: "flex",
301
+ alignItems: "center",
302
+ justifyContent: "space-between",
303
+ minHeight: 48,
304
+ paddingLeft: 16
305
+ },
306
+ children: [jsxs("span", {
307
+ style: {
308
+ color: "var(--snap-color-muted)",
309
+ fontSize: "var(--snap-text-sm)"
310
+ },
311
+ children: ["snap-rail", active !== void 0 ? ` · ${active.title}` : ""]
312
+ }), jsxs("div", {
313
+ style: {
314
+ display: "flex",
315
+ alignItems: "center",
316
+ gap: 4
317
+ },
318
+ children: [
319
+ jsx(Slot$1, {
320
+ name: UI_SLOT_IDS.settingsEntry,
321
+ context: {
322
+ presentation: "icon",
323
+ open: () => props.ctx.settings.open(),
324
+ isOpen: props.ctx.settings.isOpen()
325
+ }
326
+ }),
327
+ jsx(IconButton, {
328
+ icon: jsx(Glyph, { children: "—" }),
329
+ label: "最小化",
330
+ onClick: () => control("minimize")
331
+ }),
332
+ jsx(IconButton, {
333
+ icon: jsx(Glyph, { children: "□" }),
334
+ label: "最大化切换",
335
+ onClick: () => control("toggle-maximize")
336
+ }),
337
+ jsx(IconButton, {
338
+ icon: jsx(Glyph, { children: "×" }),
339
+ label: "关闭",
340
+ onClick: () => control("close")
341
+ })
342
+ ]
343
+ })]
344
+ });
345
+ return jsx(AppShell, {
346
+ header,
347
+ children: active !== void 0 ? active.render() : jsx("div", {
348
+ style: {
349
+ display: "flex",
350
+ minHeight: "100%",
351
+ alignItems: "center",
352
+ justifyContent: "center",
353
+ padding: 24
354
+ },
355
+ children: jsx(EmptyState, {
356
+ title: "尚未激活业务套件",
357
+ description: "打开设置的「插件管理」页,安装并启用一个业务套件;驱动也在同一处安装。",
358
+ action: jsx(Button, {
359
+ intent: "primary",
360
+ onClick: () => props.ctx.settings.open("plugins"),
361
+ children: "打开设置"
362
+ })
363
+ })
364
+ })
365
+ });
366
+ }
367
+ /** The fallback layout occupant. */
368
+ const fallbackPlugin = {
369
+ name: "snap-rail fallback layout",
370
+ inject: [
371
+ "ui",
372
+ "settings",
373
+ "pages",
374
+ "client"
375
+ ],
376
+ apply(ctx) {
377
+ ctx.ui.registerLayout(ctx, {
378
+ id: "fallback",
379
+ order: 200,
380
+ precedence: 0,
381
+ provides: [
382
+ UI_SLOT_IDS.titlebar,
383
+ UI_SLOT_IDS.titlebarActions,
384
+ UI_SLOT_IDS.sidebar,
385
+ UI_SLOT_IDS.statusbar,
386
+ UI_SLOT_IDS.settingsEntry
387
+ ],
388
+ window: {
389
+ width: 1280,
390
+ height: 800,
391
+ minWidth: 960,
392
+ minHeight: 600
393
+ },
394
+ ball: { size: 128 },
395
+ render: () => jsx(FallbackShell, { ctx })
396
+ });
397
+ }
398
+ };
399
+ //#endregion
400
+ export { fallbackPlugin as default };