@snap-rail/ui 0.1.0 → 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/lib/skins.js CHANGED
@@ -1,190 +1,160 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import { createContext, useContext, useSyncExternalStore } from "react";
3
- //#region lib/types/contract.js
4
- /** The stable region slots every layout may provide. */
5
- const UI_SLOT_IDS = {
6
- /** The one active layout of the operator window. */
7
- layout: "shell.layout",
8
- /** The operator titlebar. */
9
- titlebar: "shell.titlebar",
10
- /** Contributed titlebar actions. */
11
- titlebarActions: "shell.titlebar.actions",
12
- /** Sidebar extension area. */
13
- sidebar: "shell.sidebar",
14
- /** Statusbar extension area. */
15
- statusbar: "shell.statusbar",
16
- /** The global overlay stack (dialogs, confirms). */
17
- overlay: "shell.overlay",
18
- /** The floating ball surface face. */
19
- ball: "shell.ball",
20
- /** The settings trigger. */
21
- settingsEntry: "settings.entry",
22
- /** The settings surface itself. */
23
- settingsSurface: "settings.surface"
24
- };
25
- 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;
26
- /** The presentation targets a skin may tune. */
27
- const PRESENTATION_TARGETS = [
28
- "dialog",
29
- "select",
30
- "tabs",
31
- "toast"
32
- ];
33
- //#endregion
34
3
  //#region lib/types/skins.js
35
- /** The two colour modes. */
36
- const SKIN_MODES = ["light", "dark"];
37
- /** The built-in default skin id. */
38
- const DEFAULT_SKIN_ID = "default";
39
- /** The factory cobalt/steel skin. Extra skins override it through the registry. */
40
- const DEFAULT_SKIN = {
41
- id: DEFAULT_SKIN_ID,
42
- title: "工业钴蓝",
43
- presentations: {
44
- dialog: "modal",
45
- select: "popover",
46
- tabs: "inline",
47
- toast: "inline"
48
- },
4
+ /** The factory day theme id (cobalt palette, light surfaces). */
5
+ const DEFAULT_LIGHT_ID = "default-light";
6
+ /** The factory night theme id (cobalt palette, dark surfaces). */
7
+ const DEFAULT_DARK_ID = "default-dark";
8
+ /** The contributor label for the factory themes. */
9
+ const FACTORY_CALLER = "@snap-rail/ui";
10
+ const COBALT_DARK_TOKENS = {
11
+ colorBg: "#0b0f14",
12
+ colorSurface: "#11161d",
13
+ colorSurfaceRaised: "#161b22",
14
+ colorText: "#ced8e3",
15
+ colorMuted: "#8b949e",
16
+ colorPrimary: "#4a6cff",
17
+ colorOnPrimary: "#ffffff",
18
+ colorDanger: "#f85149",
19
+ colorSuccess: "#3fb950",
20
+ colorWarning: "#d29922",
21
+ colorBorder: "#1f2933",
22
+ colorFocus: "#4a6cff",
23
+ chart1: "#4a6cff",
24
+ chart2: "#3147a1",
25
+ chart3: "#667fcd",
26
+ chart4: "#7d8fa5",
27
+ chart5: "#55677c",
28
+ chart6: "#33414f",
29
+ fontSans: "system-ui, \"Segoe UI\", \"Microsoft YaHei\", sans-serif",
30
+ fontMono: "\"Cascadia Code\", \"Cascadia Mono\", Consolas, ui-monospace, monospace",
31
+ textXs: "13px",
32
+ textSm: "15px",
33
+ textMd: "17px",
34
+ textLg: "20px",
35
+ textXl: "24px",
36
+ weightNormal: "400",
37
+ weightMedium: "500",
38
+ weightStrong: "600",
39
+ leadingTight: "1.2",
40
+ leadingNormal: "1.55",
41
+ spaceXs: "4px",
42
+ spaceSm: "8px",
43
+ spaceMd: "12px",
44
+ spaceLg: "20px",
45
+ spaceXl: "32px",
46
+ radiusSm: "6px",
47
+ radiusMd: "8px",
48
+ radiusLg: "12px",
49
+ radiusFull: "999px",
50
+ borderWidth: "1px",
51
+ shadowSm: "0 1px 2px rgb(0 0 0 / 0.25)",
52
+ shadowMd: "0 8px 24px rgb(0 0 0 / 0.28)",
53
+ shadowLg: "0 24px 64px rgb(0 0 0 / 0.36)",
54
+ durationFast: "120ms",
55
+ durationNormal: "180ms",
56
+ durationSlow: "280ms",
57
+ easeStandard: "cubic-bezier(0.2, 0, 0, 1)",
58
+ zOverlay: "50",
59
+ zModal: "60",
60
+ zToast: "70",
61
+ controlHeightSm: "40px",
62
+ controlHeightMd: "48px",
63
+ controlHeightLg: "56px",
64
+ density: "comfortable"
65
+ };
66
+ /** The factory day theme. Always registered; never the first paint though — the night theme is. */
67
+ const DEFAULT_LIGHT = {
68
+ id: DEFAULT_LIGHT_ID,
69
+ title: "钴蓝·白天",
49
70
  tokens: {
50
- dark: {
51
- colorBg: "#0b0f14",
52
- colorSurface: "#11161d",
53
- colorSurfaceRaised: "#161b22",
54
- colorText: "#ced8e3",
55
- colorMuted: "#8b949e",
56
- colorPrimary: "#4a6cff",
57
- colorOnPrimary: "#ffffff",
58
- colorDanger: "#f85149",
59
- colorSuccess: "#3fb950",
60
- colorWarning: "#d29922",
61
- colorBorder: "#1f2933",
62
- colorFocus: "#4a6cff",
63
- chart1: "#4a6cff",
64
- chart2: "#3147a1",
65
- chart3: "#667fcd",
66
- chart4: "#7d8fa5",
67
- chart5: "#55677c",
68
- chart6: "#33414f",
69
- fontSans: "system-ui, \"Segoe UI\", \"Microsoft YaHei\", sans-serif",
70
- fontMono: "\"Cascadia Code\", \"Cascadia Mono\", Consolas, ui-monospace, monospace",
71
- textXs: "13px",
72
- textSm: "15px",
73
- textMd: "17px",
74
- textLg: "20px",
75
- textXl: "24px",
76
- weightNormal: "400",
77
- weightMedium: "500",
78
- weightStrong: "600",
79
- leadingTight: "1.2",
80
- leadingNormal: "1.55",
81
- spaceXs: "4px",
82
- spaceSm: "8px",
83
- spaceMd: "12px",
84
- spaceLg: "20px",
85
- spaceXl: "32px",
86
- radiusSm: "6px",
87
- radiusMd: "8px",
88
- radiusLg: "12px",
89
- radiusFull: "999px",
90
- borderWidth: "1px",
91
- shadowSm: "0 1px 2px rgb(0 0 0 / 0.25)",
92
- shadowMd: "0 8px 24px rgb(0 0 0 / 0.28)",
93
- shadowLg: "0 24px 64px rgb(0 0 0 / 0.36)",
94
- durationFast: "120ms",
95
- durationNormal: "180ms",
96
- durationSlow: "280ms",
97
- easeStandard: "cubic-bezier(0.2, 0, 0, 1)",
98
- zOverlay: "50",
99
- zModal: "60",
100
- zToast: "70",
101
- controlHeightSm: "40px",
102
- controlHeightMd: "48px",
103
- controlHeightLg: "56px",
104
- density: "comfortable"
105
- },
106
- light: {
107
- colorBg: "#e4e9ef",
108
- colorSurface: "#f3f6f9",
109
- colorSurfaceRaised: "#ffffff",
110
- colorText: "#26323e",
111
- colorMuted: "#5d6d7e",
112
- colorPrimary: "#3857e8",
113
- colorOnPrimary: "#ffffff",
114
- colorDanger: "#dc2626",
115
- colorSuccess: "#15803d",
116
- colorWarning: "#b45309",
117
- colorBorder: "#c7d0da",
118
- colorFocus: "#3857e8",
119
- chart1: "#3857e8",
120
- chart2: "#7088ee",
121
- chart3: "#667dd0",
122
- chart4: "#94a3b8",
123
- chart5: "#b3c0cd",
124
- chart6: "#ccd6e0",
125
- fontSans: "system-ui, \"Segoe UI\", \"Microsoft YaHei\", sans-serif",
126
- fontMono: "\"Cascadia Code\", \"Cascadia Mono\", Consolas, ui-monospace, monospace",
127
- textXs: "13px",
128
- textSm: "15px",
129
- textMd: "17px",
130
- textLg: "20px",
131
- textXl: "24px",
132
- weightNormal: "400",
133
- weightMedium: "500",
134
- weightStrong: "600",
135
- leadingTight: "1.2",
136
- leadingNormal: "1.55",
137
- spaceXs: "4px",
138
- spaceSm: "8px",
139
- spaceMd: "12px",
140
- spaceLg: "20px",
141
- spaceXl: "32px",
142
- radiusSm: "6px",
143
- radiusMd: "8px",
144
- radiusLg: "12px",
145
- radiusFull: "999px",
146
- borderWidth: "1px",
147
- shadowSm: "0 1px 2px rgb(0 0 0 / 0.08)",
148
- shadowMd: "0 8px 24px rgb(0 0 0 / 0.12)",
149
- shadowLg: "0 24px 64px rgb(0 0 0 / 0.18)",
150
- durationFast: "120ms",
151
- durationNormal: "180ms",
152
- durationSlow: "280ms",
153
- easeStandard: "cubic-bezier(0.2, 0, 0, 1)",
154
- zOverlay: "50",
155
- zModal: "60",
156
- zToast: "70",
157
- controlHeightSm: "40px",
158
- controlHeightMd: "48px",
159
- controlHeightLg: "56px",
160
- density: "comfortable"
161
- }
71
+ colorBg: "#e4e9ef",
72
+ colorSurface: "#f3f6f9",
73
+ colorSurfaceRaised: "#ffffff",
74
+ colorText: "#26323e",
75
+ colorMuted: "#5d6d7e",
76
+ colorPrimary: "#3857e8",
77
+ colorOnPrimary: "#ffffff",
78
+ colorDanger: "#dc2626",
79
+ colorSuccess: "#15803d",
80
+ colorWarning: "#b45309",
81
+ colorBorder: "#c7d0da",
82
+ colorFocus: "#3857e8",
83
+ chart1: "#3857e8",
84
+ chart2: "#7088ee",
85
+ chart3: "#667dd0",
86
+ chart4: "#94a3b8",
87
+ chart5: "#b3c0cd",
88
+ chart6: "#ccd6e0",
89
+ fontSans: "system-ui, \"Segoe UI\", \"Microsoft YaHei\", sans-serif",
90
+ fontMono: "\"Cascadia Code\", \"Cascadia Mono\", Consolas, ui-monospace, monospace",
91
+ textXs: "13px",
92
+ textSm: "15px",
93
+ textMd: "17px",
94
+ textLg: "20px",
95
+ textXl: "24px",
96
+ weightNormal: "400",
97
+ weightMedium: "500",
98
+ weightStrong: "600",
99
+ leadingTight: "1.2",
100
+ leadingNormal: "1.55",
101
+ spaceXs: "4px",
102
+ spaceSm: "8px",
103
+ spaceMd: "12px",
104
+ spaceLg: "20px",
105
+ spaceXl: "32px",
106
+ radiusSm: "6px",
107
+ radiusMd: "8px",
108
+ radiusLg: "12px",
109
+ radiusFull: "999px",
110
+ borderWidth: "1px",
111
+ shadowSm: "0 1px 2px rgb(0 0 0 / 0.08)",
112
+ shadowMd: "0 8px 24px rgb(0 0 0 / 0.12)",
113
+ shadowLg: "0 24px 64px rgb(0 0 0 / 0.18)",
114
+ durationFast: "120ms",
115
+ durationNormal: "180ms",
116
+ durationSlow: "280ms",
117
+ easeStandard: "cubic-bezier(0.2, 0, 0, 1)",
118
+ zOverlay: "50",
119
+ zModal: "60",
120
+ zToast: "70",
121
+ controlHeightSm: "40px",
122
+ controlHeightMd: "48px",
123
+ controlHeightLg: "56px",
124
+ density: "comfortable"
162
125
  }
163
126
  };
127
+ /** The factory night theme and the registry's fallback. */
128
+ const DEFAULT_DARK = {
129
+ id: DEFAULT_DARK_ID,
130
+ title: "钴蓝·黑夜",
131
+ tokens: COBALT_DARK_TOKENS
132
+ };
164
133
  /** Turn a token key into its CSS custom property (`chart1` → `--snap-chart-1`). */
165
134
  function skinTokenVar(key) {
166
135
  return `--snap-${key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`).replace(/(\d+)$/, "-$1")}`;
167
136
  }
168
- /** Apply the active skin to the document root. */
169
- function applySkin(skin, mode) {
137
+ /** Apply the active theme to the document root. */
138
+ function applySkin(skin) {
170
139
  if (typeof document === "undefined") return;
171
140
  const root = document.documentElement;
172
141
  root.dataset["snapSkin"] = skin.id;
173
- root.dataset["snapMode"] = mode;
174
- const tokens = skin.tokens[mode];
175
- for (const [key, value] of Object.entries(tokens)) root.style.setProperty(skinTokenVar(key), value);
142
+ for (const [key, value] of Object.entries(skin.tokens)) root.style.setProperty(skinTokenVar(key), value);
176
143
  }
177
144
  /** Mount the `ctx.skins` registry. */
178
145
  const skinsPlugin = {
179
146
  name: "snap-rail ui skins",
180
147
  apply(ctx) {
181
148
  const skins = /* @__PURE__ */ new Map();
149
+ const callers = /* @__PURE__ */ new Map();
182
150
  const listeners = /* @__PURE__ */ new Set();
183
151
  const styleTags = /* @__PURE__ */ new Map();
184
- let activeId = DEFAULT_SKIN_ID;
185
- let mode = "dark";
152
+ let activeId = DEFAULT_DARK_ID;
186
153
  let revision = 0;
187
- skins.set(DEFAULT_SKIN_ID, DEFAULT_SKIN);
154
+ skins.set(DEFAULT_LIGHT_ID, DEFAULT_LIGHT);
155
+ callers.set(DEFAULT_LIGHT_ID, FACTORY_CALLER);
156
+ skins.set(DEFAULT_DARK_ID, DEFAULT_DARK);
157
+ callers.set(DEFAULT_DARK_ID, FACTORY_CALLER);
188
158
  const notify = () => {
189
159
  revision += 1;
190
160
  for (const listener of listeners) listener();
@@ -200,48 +170,52 @@ const skinsPlugin = {
200
170
  styleTags.set(skin.id, tag);
201
171
  };
202
172
  const refresh = () => {
203
- const skin = skins.get(activeId) ?? DEFAULT_SKIN;
173
+ const skin = skins.get(activeId) ?? DEFAULT_DARK;
204
174
  activeId = skin.id;
205
175
  ensureStyleTag(skin);
206
- applySkin(skin, mode);
176
+ applySkin(skin);
207
177
  notify();
208
178
  };
209
179
  ctx.provide("skins", {
210
180
  register(caller, skin) {
211
181
  skins.set(skin.id, skin);
212
- if (skin.id !== "default") activeId = skin.id;
182
+ callers.set(skin.id, caller.fiber.name);
213
183
  refresh();
214
184
  const remove = () => {
215
185
  if (!skins.delete(skin.id)) return;
186
+ callers.delete(skin.id);
216
187
  const tag = styleTags.get(skin.id);
217
188
  if (tag !== void 0) {
218
189
  tag.remove();
219
190
  styleTags.delete(skin.id);
220
191
  }
221
- if (activeId === skin.id) activeId = DEFAULT_SKIN_ID;
192
+ if (activeId === skin.id) activeId = DEFAULT_DARK_ID;
222
193
  refresh();
223
194
  };
224
195
  caller.effect(() => remove);
225
196
  return remove;
226
197
  },
227
- activate(skinId) {
228
- if (!skins.has(skinId)) return false;
229
- activeId = skinId;
198
+ activate(selection) {
199
+ let next = skins.has(selection) ? selection : void 0;
200
+ if (next === void 0) {
201
+ for (const [id, owner] of callers) if (id !== selection && owner === selection && skins.has(id)) {
202
+ next = id;
203
+ break;
204
+ }
205
+ }
206
+ if (next === void 0) return false;
207
+ if (next === activeId) return true;
208
+ activeId = next;
230
209
  refresh();
231
210
  return true;
232
211
  },
233
- list: () => [skins.get("default") ?? DEFAULT_SKIN, ...[...skins.values()].filter((skin) => skin.id !== DEFAULT_SKIN_ID)],
234
- active: () => skins.get(activeId) ?? DEFAULT_SKIN,
235
- setMode(next) {
236
- if (mode === next) return;
237
- mode = next;
238
- refresh();
239
- },
240
- mode: () => mode,
241
- presentation(target) {
242
- return (skins.get(activeId) ?? DEFAULT_SKIN).presentations?.[target];
243
- },
244
- tokens: () => (skins.get(activeId) ?? DEFAULT_SKIN).tokens[mode],
212
+ list: () => [...skins.values()].map((skin) => ({
213
+ id: skin.id,
214
+ title: skin.title,
215
+ caller: callers.get(skin.id) ?? FACTORY_CALLER
216
+ })),
217
+ active: () => skins.get(activeId) ?? DEFAULT_DARK,
218
+ tokens: () => (skins.get(activeId) ?? DEFAULT_DARK).tokens,
245
219
  subscribe(listener) {
246
220
  listeners.add(listener);
247
221
  return () => {
@@ -268,17 +242,17 @@ function useSkins() {
268
242
  if (service === null) throw new Error("useSkins: no SkinsProvider in the tree");
269
243
  return service;
270
244
  }
271
- /** Re-render on skin/mode changes and return the active token set. */
245
+ /** Re-render on theme changes and return the active token set. */
272
246
  function useSkinTokens() {
273
247
  const service = useSkins();
274
248
  useSyncExternalStore(service.subscribe, service.version, service.version);
275
249
  return service.tokens();
276
250
  }
277
- /** Re-render on skin/mode changes and return the active presentation default. */
278
- function usePresentation(target) {
251
+ /** Re-render on theme changes and return the active theme id. */
252
+ function useSkinId() {
279
253
  const service = useSkins();
280
254
  useSyncExternalStore(service.subscribe, service.version, service.version);
281
- return service.presentation(target);
255
+ return service.active().id;
282
256
  }
283
257
  //#endregion
284
- export { DEFAULT_SKIN, DEFAULT_SKIN_ID, PRESENTATION_TARGETS, SKIN_MODES, SkinsProvider, skinsPlugin as default, skinTokenVar, usePresentation, useSkinTokens, useSkins };
258
+ export { DEFAULT_DARK, DEFAULT_DARK_ID, DEFAULT_LIGHT, DEFAULT_LIGHT_ID, SkinsProvider, skinsPlugin as default, skinTokenVar, useSkinId, useSkinTokens, useSkins };
@@ -6,12 +6,12 @@
6
6
  * @module @snap-rail/ui/components/dialog
7
7
  */
8
8
  import * as DialogPrimitive from '@radix-ui/react-dialog';
9
- import type { ReactNode } from 'react';
9
+ import type { HTMLAttributes, ReactNode } from 'react';
10
10
  /** The presentation variants the core implements for overlays. */
11
11
  export type DialogPresentation = 'modal' | 'sheet' | 'fullscreen';
12
- /** Props of {@link DialogContent}. */
13
- export interface DialogContentProps {
14
- children?: ReactNode;
12
+ /** Props of {@link DialogContent}. Panel-level attributes (drag handlers,
13
+ * `aria-describedby`, tests hooks) ride through to the dialog panel. */
14
+ export interface DialogContentProps extends HTMLAttributes<HTMLDivElement> {
15
15
  className?: string;
16
16
  /** `auto` currently resolves to the skin default; explicit values win. */
17
17
  presentation?: DialogPresentation | 'auto';
@@ -25,7 +25,7 @@ export declare const DialogTrigger: import("react").ForwardRefExoticComponent<Di
25
25
  /** The dialog close. */
26
26
  export declare const DialogClose: import("react").ForwardRefExoticComponent<DialogPrimitive.DialogCloseProps & import("react").RefAttributes<HTMLButtonElement>>;
27
27
  /** The portalled dialog content. */
28
- export declare function DialogContent({ children, className, presentation, size, showClose, }: DialogContentProps): ReactNode;
28
+ export declare function DialogContent({ children, className, presentation, size, showClose, ...rest }: DialogContentProps): ReactNode;
29
29
  /** The dialog header. */
30
30
  export declare function DialogHeader({ children, className }: {
31
31
  children?: ReactNode;
@@ -15,9 +15,9 @@ export const DialogTrigger = DialogPrimitive.Trigger;
15
15
  /** The dialog close. */
16
16
  export const DialogClose = DialogPrimitive.Close;
17
17
  /** The portalled dialog content. */
18
- export function DialogContent({ children, className, presentation = 'auto', size = 'md', showClose = true, }) {
18
+ export function DialogContent({ children, className, presentation = 'auto', size = 'md', showClose = true, ...rest }) {
19
19
  const resolved = presentation === 'auto' ? 'modal' : presentation;
20
- return (_jsxs(DialogPrimitive.Portal, { children: [_jsx(DialogPrimitive.Overlay, { ...part('dialog', 'overlay'), className: "snap-dialog-overlay" }), _jsxs(DialogPrimitive.Content, { ...part('dialog', 'panel'), className: cn('snap-dialog-panel', className), "data-presentation": resolved, "data-size": size, children: [children, showClose && (_jsx(DialogPrimitive.Close, { ...part('dialog', 'close'), className: "snap-dialog-close", "aria-label": "\u5173\u95ED", children: "\u00D7" }))] })] }));
20
+ return (_jsxs(DialogPrimitive.Portal, { children: [_jsx(DialogPrimitive.Overlay, { ...part('dialog', 'overlay'), className: "snap-dialog-overlay" }), _jsxs(DialogPrimitive.Content, { ...part('dialog', 'panel'), className: cn('snap-dialog-panel', className), "data-presentation": resolved, "data-size": size, ...rest, children: [children, showClose && (_jsx(DialogPrimitive.Close, { ...part('dialog', 'close'), className: "snap-dialog-close", "aria-label": "\u5173\u95ED", children: "\u00D7" }))] })] }));
21
21
  }
22
22
  /** The dialog header. */
23
23
  export function DialogHeader({ children, className }) {
@@ -17,15 +17,18 @@ export interface InputProps extends Omit<ComponentPropsWithoutRef<'input'>, 'siz
17
17
  /** Internal anatomy name so NumberInput can reuse the field. */
18
18
  componentName?: 'input' | 'number-input';
19
19
  }
20
- /** A single-line text field with semantic prefix/suffix slots. */
21
- export declare function Input({ size, invalid, prefix, suffix, onClear, componentName, className, disabled, ...props }: InputProps): ReactNode;
20
+ /** A single-line text field with semantic prefix/suffix slots. Layout
21
+ * `style`/`className` dress the wrapper — the visible control; the native
22
+ * field already stretches inside it. */
23
+ export declare function Input({ size, invalid, prefix, suffix, onClear, componentName, className, style, disabled, ...props }: InputProps): ReactNode;
22
24
  /** Props of {@link Textarea}. */
23
25
  export interface TextareaProps extends Omit<ComponentPropsWithoutRef<'textarea'>, 'size'> {
24
26
  size?: InputSize;
25
27
  invalid?: boolean;
26
28
  }
27
- /** A multi-line text field. */
28
- export declare function Textarea({ size, invalid, className, disabled, ...props }: TextareaProps): ReactNode;
29
+ /** A multi-line text field. Like {@link Input}, layout `style`/`className`
30
+ * dress the wrapper; the field stretches inside it. */
31
+ export declare function Textarea({ size, invalid, className, style, disabled, ...props }: TextareaProps): ReactNode;
29
32
  /** Props of {@link NumberInput}. */
30
33
  export interface NumberInputProps extends Omit<InputProps, 'type' | 'prefix' | 'suffix' | 'onClear'> {
31
34
  min?: number;
@@ -1,12 +1,15 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { cn, part } from "../utils.js";
3
- /** A single-line text field with semantic prefix/suffix slots. */
4
- export function Input({ size = 'md', invalid = false, prefix, suffix, onClear, componentName = 'input', className, disabled, ...props }) {
5
- return (_jsxs("div", { ...part(componentName, 'root'), className: cn('snap-input', className), "data-size": size, "data-invalid": invalid || undefined, "data-disabled": disabled === true || undefined, children: [prefix !== undefined && _jsx("span", { ...part(componentName, 'prefix'), children: prefix }), _jsx("input", { ...part(componentName, 'field'), className: "snap-input-field", disabled: disabled, "aria-invalid": invalid || undefined, ...props }), onClear !== undefined && typeof props.value === 'string' && props.value !== '' && (_jsx("button", { ...part(componentName, 'clear'), type: "button", className: "snap-input-clear", "aria-label": "\u6E05\u9664", onClick: onClear, children: "\u00D7" })), suffix !== undefined && _jsx("span", { ...part(componentName, 'suffix'), children: suffix })] }));
3
+ /** A single-line text field with semantic prefix/suffix slots. Layout
4
+ * `style`/`className` dress the wrapper — the visible control; the native
5
+ * field already stretches inside it. */
6
+ export function Input({ size = 'md', invalid = false, prefix, suffix, onClear, componentName = 'input', className, style, disabled, ...props }) {
7
+ return (_jsxs("div", { ...part(componentName, 'root'), className: cn('snap-input', className), style: style, "data-size": size, "data-invalid": invalid || undefined, "data-disabled": disabled === true || undefined, children: [prefix !== undefined && _jsx("span", { ...part(componentName, 'prefix'), children: prefix }), _jsx("input", { ...part(componentName, 'field'), className: "snap-input-field", disabled: disabled, "aria-invalid": invalid || undefined, ...props }), onClear !== undefined && typeof props.value === 'string' && props.value !== '' && (_jsx("button", { ...part(componentName, 'clear'), type: "button", className: "snap-input-clear", "aria-label": "\u6E05\u9664", onClick: onClear, children: "\u00D7" })), suffix !== undefined && _jsx("span", { ...part(componentName, 'suffix'), children: suffix })] }));
6
8
  }
7
- /** A multi-line text field. */
8
- export function Textarea({ size = 'md', invalid = false, className, disabled, ...props }) {
9
- return (_jsx("div", { ...part('textarea', 'root'), className: cn('snap-textarea', className), "data-size": size, "data-invalid": invalid || undefined, "data-disabled": disabled === true || undefined, children: _jsx("textarea", { ...part('textarea', 'field'), className: "snap-textarea-field", disabled: disabled, "aria-invalid": invalid || undefined, ...props }) }));
9
+ /** A multi-line text field. Like {@link Input}, layout `style`/`className`
10
+ * dress the wrapper; the field stretches inside it. */
11
+ export function Textarea({ size = 'md', invalid = false, className, style, disabled, ...props }) {
12
+ return (_jsx("div", { ...part('textarea', 'root'), className: cn('snap-textarea', className), style: style, "data-size": size, "data-invalid": invalid || undefined, "data-disabled": disabled === true || undefined, children: _jsx("textarea", { ...part('textarea', 'field'), className: "snap-textarea-field", disabled: disabled, "aria-invalid": invalid || undefined, ...props }) }));
10
13
  }
11
14
  /** A numeric field with the numeric keyboard hint and native stepping. */
12
15
  export function NumberInput({ min, max, step, ...props }) {
@@ -65,10 +65,13 @@ export interface PageProps extends LayoutProps {
65
65
  header?: ReactNode;
66
66
  actions?: ReactNode;
67
67
  title?: ReactNode;
68
+ /** Controls that belong to the title itself; rendered right beside it,
69
+ * before the far-aligned `actions`. */
70
+ titleExtra?: ReactNode;
68
71
  description?: ReactNode;
69
72
  }
70
73
  /** A page body with its own header/actions anatomy. */
71
- export declare function Page({ header, actions, title, description, className, style, children }: PageProps): ReactNode;
74
+ export declare function Page({ header, actions, title, titleExtra, description, className, style, children }: PageProps): ReactNode;
72
75
  /** Props of {@link Section}. */
73
76
  export interface SectionProps extends LayoutProps {
74
77
  title?: ReactNode;
@@ -48,8 +48,8 @@ export function AppShell({ header, sidebar, footer, className, style, children }
48
48
  return (_jsxs("div", { ...part('app-shell', 'root'), className: cn('snap-app-shell', className), style: { display: 'flex', flexDirection: 'column', height: '100%', minHeight: 0, ...style }, children: [header !== undefined && _jsx("div", { ...part('app-shell', 'header'), style: { flexShrink: 0 }, children: header }), _jsxs("div", { style: { display: 'flex', flex: 1, minHeight: 0 }, children: [sidebar !== undefined && (_jsx("div", { ...part('app-shell', 'sidebar'), style: { flexShrink: 0, minHeight: 0 }, children: sidebar })), _jsx("div", { ...part('app-shell', 'main'), style: { flex: 1, minWidth: 0, minHeight: 0, overflow: 'auto' }, children: children })] }), footer !== undefined && _jsx("div", { ...part('app-shell', 'footer'), style: { flexShrink: 0 }, children: footer })] }));
49
49
  }
50
50
  /** A page body with its own header/actions anatomy. */
51
- export function Page({ header, actions, title, description, className, style, children }) {
52
- return (_jsxs("div", { ...part('page', 'root'), className: cn('snap-page', className), style: { display: 'flex', flexDirection: 'column', minHeight: 0, ...style }, children: [(header !== undefined || title !== undefined || description !== undefined || actions !== undefined) && (_jsxs("div", { ...part('page', 'header'), style: { display: 'flex', flexDirection: 'column', gap: space('sm'), flexShrink: 0 }, children: [_jsxs("div", { style: { display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: space('md') }, children: [title !== undefined && _jsx("div", { ...part('page', 'title'), children: title }), actions !== undefined && _jsx("div", { ...part('page', 'actions'), children: actions })] }), description !== undefined && _jsx("div", { ...part('page', 'description'), children: description }), header] })), _jsx("div", { ...part('page', 'body'), style: { display: 'flex', flexDirection: 'column', flex: 1, minHeight: 0 }, children: children })] }));
51
+ export function Page({ header, actions, title, titleExtra, description, className, style, children }) {
52
+ return (_jsxs("div", { ...part('page', 'root'), className: cn('snap-page', className), style: { display: 'flex', flexDirection: 'column', minHeight: 0, ...style }, children: [(header !== undefined || title !== undefined || description !== undefined || actions !== undefined) && (_jsxs("div", { ...part('page', 'header'), style: { display: 'flex', flexDirection: 'column', gap: space('sm'), flexShrink: 0 }, children: [_jsxs("div", { style: { display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', gap: space('md') }, children: [_jsxs("div", { style: { display: 'flex', alignItems: 'center', gap: space('sm') }, children: [title !== undefined && _jsx("div", { ...part('page', 'title'), children: title }), titleExtra !== undefined && _jsx("div", { ...part('page', 'title-extra'), children: titleExtra })] }), actions !== undefined && _jsx("div", { ...part('page', 'actions'), children: actions })] }), description !== undefined && _jsx("div", { ...part('page', 'description'), children: description }), header] })), _jsx("div", { ...part('page', 'body'), style: { display: 'flex', flexDirection: 'column', flex: 1, minHeight: 0 }, children: children })] }));
53
53
  }
54
54
  /** A titled content section. */
55
55
  export function Section({ title, description, actions, className, style, children }) {
@@ -1,9 +1,9 @@
1
1
  /**
2
2
  * A small JSON-Schema form renderer for driver dialect schemas. It covers the
3
3
  * flat object shapes drivers commonly declare: string, number/integer,
4
- * boolean, and enum. Nested objects/arrays fall back to a JSON textarea; a
5
- * driver that needs a bespoke interaction should ship a registered component
6
- * instead of growing this renderer.
4
+ * boolean, enum, and unions of literals. Nested objects/arrays fall back to a
5
+ * JSON textarea; a driver that needs a bespoke interaction should ship a
6
+ * registered component instead of growing this renderer.
7
7
  *
8
8
  * @module @snap-rail/ui/components/schema-form
9
9
  */
@@ -16,10 +16,20 @@ export interface JsonSchemaNode {
16
16
  properties?: Readonly<Record<string, JsonSchemaNode>>;
17
17
  required?: readonly string[];
18
18
  enum?: readonly unknown[];
19
+ /** `z.toJSONSchema` projects a union of literals (`z.union([z.literal(1), …])`)
20
+ * as an `anyOf` of `const` branches instead of an `enum` — see
21
+ * {@link literalValuesOf}. */
22
+ anyOf?: readonly JsonSchemaNode[];
23
+ const?: unknown;
19
24
  default?: unknown;
20
25
  minimum?: number;
21
26
  maximum?: number;
22
27
  }
28
+ /** The closed literal set a property may take: its `enum`, or — for a union of
29
+ * scalar literals — the flattened `const` values of the `anyOf` branches, in
30
+ * declaration order. `undefined` when the property is not a closed literal set
31
+ * (open unions, non-scalar consts, absent both fields). */
32
+ export declare function literalValuesOf(property: JsonSchemaNode): readonly unknown[] | undefined;
23
33
  /** Props of {@link SchemaForm}. */
24
34
  export interface SchemaFormProps {
25
35
  schema: JsonSchemaNode;
@@ -2,6 +2,29 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Field } from "./form.js";
3
3
  import { Input, NumberInput, Textarea } from "./input.js";
4
4
  import { Switch, TouchSelect } from "./controls.js";
5
+ /** The closed literal set a property may take: its `enum`, or — for a union of
6
+ * scalar literals — the flattened `const` values of the `anyOf` branches, in
7
+ * declaration order. `undefined` when the property is not a closed literal set
8
+ * (open unions, non-scalar consts, absent both fields). */
9
+ export function literalValuesOf(property) {
10
+ if (Array.isArray(property.enum))
11
+ return property.enum;
12
+ const branches = property.anyOf;
13
+ if (!Array.isArray(branches) || branches.length === 0)
14
+ return undefined;
15
+ const values = [];
16
+ for (const branch of branches) {
17
+ if (branch === null || typeof branch !== 'object' || branch.anyOf !== undefined)
18
+ return undefined;
19
+ if (!('const' in branch))
20
+ return undefined;
21
+ const value = branch.const;
22
+ if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean')
23
+ return undefined;
24
+ values.push(value);
25
+ }
26
+ return values;
27
+ }
5
28
  function asRecord(value) {
6
29
  return value !== null && typeof value === 'object' && !Array.isArray(value)
7
30
  ? value
@@ -24,8 +47,8 @@ export function SchemaForm({ schema, value, onChange, idPrefix = 'schema' }) {
24
47
  ...(property.description !== undefined ? { help: property.description } : {}),
25
48
  ...(required.has(key) ? { required: true } : {}),
26
49
  };
27
- if (Array.isArray(property.enum)) {
28
- const values = property.enum;
50
+ const values = literalValuesOf(property);
51
+ if (values !== undefined) {
29
52
  const options = values.map(item => ({ value: String(item), label: String(item) }));
30
53
  return (_jsx(Field, { ...common, children: _jsx(TouchSelect, { ...current !== undefined ? { value: String(current) } : {}, options: options, onValueChange: next => {
31
54
  update(key, values.find(item => String(item) === next) ?? next);
@@ -347,12 +347,4 @@ export declare const UI_COMPONENTS: {
347
347
  };
348
348
  /** One semantic component name. */
349
349
  export type UiComponentName = keyof typeof UI_COMPONENTS;
350
- /** The presentation targets a skin may tune. */
351
- export declare const PRESENTATION_TARGETS: readonly ["dialog", "select", "tabs", "toast"];
352
- /** One presentation target. */
353
- export type PresentationTarget = (typeof PRESENTATION_TARGETS)[number];
354
- /** The presentation variants the core implements. */
355
- export declare const PRESENTATION_VARIANTS: readonly ["modal", "sheet", "fullscreen", "popover", "segmented", "inline"];
356
- /** One presentation variant. */
357
- export type PresentationVariant = (typeof PRESENTATION_VARIANTS)[number];
358
350
  //# sourceMappingURL=contract.d.ts.map