@takazudo/zudo-sg 0.4.0 → 0.5.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 (67) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/README.md +59 -61
  3. package/dist/catalog/catalog-thumb.d.ts +1 -6
  4. package/dist/catalog/catalog-thumb.js +1 -2
  5. package/dist/catalog/component-thumb.d.ts +1 -6
  6. package/dist/catalog/component-thumb.js +8 -7
  7. package/dist/catalog/descriptor-thumb.d.ts +1 -6
  8. package/dist/catalog/descriptor-thumb.js +1 -2
  9. package/dist/chrome/_styleguide-layout.d.ts +8 -9
  10. package/dist/chrome/_styleguide-layout.js +3 -6
  11. package/dist/chrome/panel-scripts.d.ts +1 -3
  12. package/dist/chrome/panel-scripts.js +3 -3
  13. package/dist/cli/scaffold/component-scaffold.js +35 -16
  14. package/dist/code-panel/code-panel.d.ts +2 -8
  15. package/dist/code-panel/code-panel.js +34 -35
  16. package/dist/code-panel/copy-button.d.ts +1 -7
  17. package/dist/code-panel/copy-button.js +31 -48
  18. package/dist/code-panel/copy-text.d.ts +1 -0
  19. package/dist/code-panel/copy-text.js +26 -0
  20. package/dist/code-panel/editor-setup.d.ts +3 -0
  21. package/dist/code-panel/editor-setup.js +15 -3
  22. package/dist/code-panel/editor-theme.d.ts +2 -0
  23. package/dist/code-panel/editor-theme.js +54 -0
  24. package/dist/code-panel/source-editor.d.ts +10 -2
  25. package/dist/code-panel/source-editor.js +20 -19
  26. package/dist/config/index.d.ts +2 -0
  27. package/dist/config/index.js +6 -1
  28. package/dist/config/wind.d.ts +265 -0
  29. package/dist/config/wind.js +133 -0
  30. package/dist/plugins/preview-css.d.ts +9 -7
  31. package/dist/plugins/preview-css.js +58 -37
  32. package/dist/preview/detail-workbench.d.ts +2 -6
  33. package/dist/preview/detail-workbench.js +100 -65
  34. package/dist/preview/index.d.ts +4 -2
  35. package/dist/preview/index.js +7 -7
  36. package/dist/preview/preview-app.d.ts +2 -5
  37. package/dist/preview/preview-app.js +76 -46
  38. package/dist/preview/variant-frame-config.d.ts +47 -0
  39. package/dist/preview/variant-frame-config.js +50 -0
  40. package/dist/preview/variant-frame.d.ts +9 -53
  41. package/dist/preview/variant-frame.js +158 -149
  42. package/dist/preview-css/compile.d.ts +15 -29
  43. package/dist/preview-css/compile.js +183 -31
  44. package/dist/search/catalog-filter.d.ts +2 -6
  45. package/dist/search/catalog-filter.js +50 -42
  46. package/dist/stories/types.d.ts +5 -3
  47. package/dist/token-dashboard/create-token-dashboards.d.ts +3 -5
  48. package/dist/token-dashboard/create-token-dashboards.js +14 -44
  49. package/dist/token-dashboard/dashboard-inventory.d.ts +1 -1
  50. package/dist/token-dashboard/token-dashboard-adapter.d.ts +2 -0
  51. package/dist/token-dashboard/token-dashboard-adapter.js +9 -0
  52. package/dist/token-dashboard/token-dashboard-mount.d.ts +6 -0
  53. package/dist/token-dashboard/token-dashboard-mount.js +33 -0
  54. package/dist/token-tweak/preview-token-panel-bootstrap.d.ts +1 -6
  55. package/dist/token-tweak/preview-token-panel-bootstrap.js +4 -2
  56. package/dist/token-tweak/preview-tokens-button.d.ts +1 -5
  57. package/dist/token-tweak/preview-tokens-button.js +5 -4
  58. package/dist/wind.json +119 -0
  59. package/package.json +16 -19
  60. package/routes-src/_chrome.tsx +1 -3
  61. package/routes-src/_preview-app.tsx +3 -8
  62. package/routes-src/components-index.tsx +5 -10
  63. package/routes-src/components-preview.tsx +3 -9
  64. package/routes-src/components-slug.tsx +21 -24
  65. package/routes-src/tokens.tsx +4 -9
  66. package/styles.css +36 -11
  67. package/dist/safelist.css +0 -2
@@ -1,12 +1,18 @@
1
1
  "use client";
2
- import { jsx, jsxs } from "preact/jsx-runtime";
3
- import { useEffect, useState } from "preact/hooks";
4
- import VariantFrame, {
2
+ import { jsx, jsxs } from "@takazudo/zfb/zudo-react/jsx-runtime";
3
+ import {
4
+ signal,
5
+ computed,
6
+ getScope,
7
+ Show
8
+ } from "@takazudo/zfb/zudo-react";
9
+ import VariantFrame from "./variant-frame.js";
10
+ import {
5
11
  DEFAULT_THEME_MODE,
6
12
  DEFAULT_VIEWPORT_ID,
7
13
  THEME_OPTIONS,
8
14
  VIEWPORTS
9
- } from "./variant-frame.js";
15
+ } from "./variant-frame-config.js";
10
16
  import {
11
17
  ATTR_CODE_PANEL_HIDDEN,
12
18
  isCodePanelHidden,
@@ -17,19 +23,19 @@ const LAYOUT_OPTIONS = [
17
23
  { id: "stacked", label: "Stacked" },
18
24
  { id: "grid", label: "Grid" }
19
25
  ];
20
- const TRACK_CLASS = "flex flex-wrap items-center gap-hsp-2xs rounded-md border border-[color:var(--sg-border)] bg-[var(--sg-surface-2)] p-hsp-2xs";
26
+ const TRACK_CLASS = "flex flex-wrap items-center gap-hsp-2xs rounded-md border border-sg-border bg-sg-surface-2 p-hsp-2xs";
21
27
  const SEGMENT_BASE_CLASS = "rounded-sm border px-hsp-sm py-vsp-3xs text-caption leading-normal transition-colors cursor-pointer ";
22
- const SEGMENT_ON_CLASS = "border-[color:var(--sg-border-strong)] bg-[var(--sg-surface)] text-[color:var(--sg-fg)]";
23
- const SEGMENT_OFF_CLASS = "border-transparent text-[color:var(--sg-muted)] hover:text-[color:var(--sg-fg)]";
28
+ const SEGMENT_ON_CLASS = "border-sg-border-strong bg-sg-surface text-sg-fg";
29
+ const SEGMENT_OFF_CLASS = "border-transparent text-sg-muted hover:text-sg-fg";
24
30
  const PILL_BASE_CLASS = "inline-flex items-center gap-hsp-2xs rounded-md border px-hsp-sm py-vsp-3xs text-caption leading-normal transition-colors cursor-pointer ";
25
- const PILL_ON_CLASS = "border-[color:var(--sg-border-strong)] bg-[var(--sg-surface-2)] text-[color:var(--sg-fg)]";
26
- const PILL_OFF_CLASS = "border-[color:var(--sg-border)] bg-[var(--sg-surface)] text-[color:var(--sg-muted)] hover:text-[color:var(--sg-fg)]";
31
+ const PILL_ON_CLASS = "border-sg-border-strong bg-sg-surface-2 text-sg-fg";
32
+ const PILL_OFF_CLASS = "border-sg-border bg-sg-surface text-sg-muted hover:text-sg-fg";
27
33
  const STACKED_GRID_CLASS = "grid items-start gap-x-hsp-xl gap-y-vsp-xl grid-cols-1";
28
34
  const COLUMNS_GRID_CLASS = "grid items-start gap-x-hsp-xl gap-y-vsp-xl grid-cols-[repeat(auto-fit,minmax(min(18rem,100%),1fr))]";
29
- const TABLIST_CLASS = "flex flex-wrap items-center gap-hsp-2xs border-b border-[color:var(--sg-border)]";
35
+ const TABLIST_CLASS = "flex flex-wrap items-center gap-hsp-2xs border-b border-sg-border";
30
36
  const TAB_BASE_CLASS = "-mb-px rounded-t-sm border-b-2 px-hsp-sm py-vsp-2xs text-caption leading-normal transition-colors cursor-pointer ";
31
- const TAB_ON_CLASS = "border-[color:var(--sg-border-strong)] text-[color:var(--sg-fg)]";
32
- const TAB_OFF_CLASS = "border-transparent text-[color:var(--sg-muted)] hover:text-[color:var(--sg-fg)]";
37
+ const TAB_ON_CLASS = "border-sg-border-strong text-sg-fg";
38
+ const TAB_OFF_CLASS = "border-transparent text-sg-muted hover:text-sg-fg";
33
39
  function segmentClass(selected) {
34
40
  return SEGMENT_BASE_CLASS + (selected ? SEGMENT_ON_CLASS : SEGMENT_OFF_CLASS);
35
41
  }
@@ -48,13 +54,12 @@ function DetailWorkbench({
48
54
  sizing = { mode: "auto" },
49
55
  toolbar = {}
50
56
  }) {
51
- const [themeMode, setThemeMode] = useState(DEFAULT_THEME_MODE);
52
- const [viewportId, setViewportId] = useState(DEFAULT_VIEWPORT_ID);
53
- const [layout, setLayout] = useState("stacked");
54
- const [codePanelShown, setCodePanelShown] = useState(true);
55
- const [internalActive, setInternalActive] = useState(
56
- void 0
57
- );
57
+ const scope = getScope();
58
+ const themeMode = signal(DEFAULT_THEME_MODE);
59
+ const viewportId = signal(DEFAULT_VIEWPORT_ID);
60
+ const layout = signal("stacked");
61
+ const codePanelShown = signal(true);
62
+ const internalActive = signal(void 0);
58
63
  const tabsId = `sg-workbench-${slug}`;
59
64
  const showTheme = theme.mode === "toolbar";
60
65
  const showViewport = toolbar.viewport ?? true;
@@ -62,11 +67,15 @@ function DetailWorkbench({
62
67
  const showCodePanel = toolbar.codePanel ?? false;
63
68
  const showTokenPanel = toolbar.tokenPanel ?? false;
64
69
  const showToolbar = showTheme || showViewport || showLayout || showCodePanel || showTokenPanel;
65
- const frameThemeMode = showTheme ? themeMode : "none";
70
+ const frameThemeMode = computed(
71
+ () => showTheme ? themeMode.value : "none"
72
+ );
66
73
  const fixedHeight = sizing.mode === "fixed" ? sizing.height : void 0;
67
- useEffect(() => {
74
+ scope.onActivate(() => {
68
75
  if (!showCodePanel) return;
69
- const sync = () => setCodePanelShown(!isCodePanelHidden());
76
+ const sync = () => {
77
+ codePanelShown.value = !isCodePanelHidden();
78
+ };
70
79
  sync();
71
80
  const observer = new MutationObserver(sync);
72
81
  observer.observe(document.documentElement, {
@@ -74,16 +83,18 @@ function DetailWorkbench({
74
83
  attributeFilter: [ATTR_CODE_PANEL_HIDDEN]
75
84
  });
76
85
  return () => observer.disconnect();
77
- }, [showCodePanel]);
86
+ });
78
87
  function openPreviewTokenPanel() {
79
88
  window.dispatchEvent(new CustomEvent("toggle-preview-token-panel"));
80
89
  }
81
90
  const single = selection.mode === "single";
82
- const requested = single ? selection.active ?? internalActive : void 0;
83
- const activeVariant = single ? variants.find((v) => v.exportName === requested) ?? variants[0] : void 0;
91
+ const activeVariant = computed(() => {
92
+ const requested = selection.mode === "single" ? selection.active ?? internalActive.value : void 0;
93
+ return variants.find((v) => v.exportName === requested) ?? variants[0];
94
+ });
84
95
  function selectVariant(exportName) {
85
96
  if (selection.mode !== "single") return;
86
- if (selection.active === void 0) setInternalActive(exportName);
97
+ if (selection.active === void 0) internalActive.value = exportName;
87
98
  selection.onChange?.(exportName);
88
99
  }
89
100
  function onTabKeyDown(e, index) {
@@ -101,7 +112,7 @@ function DetailWorkbench({
101
112
  }
102
113
  );
103
114
  }
104
- function renderStage(variant, key) {
115
+ function renderStage(variant) {
105
116
  return /* @__PURE__ */ jsx(
106
117
  VariantFrame,
107
118
  {
@@ -116,57 +127,69 @@ function DetailWorkbench({
116
127
  frameAllow,
117
128
  previewParams,
118
129
  fixedHeight
119
- },
120
- key ?? variant.exportName
130
+ }
121
131
  );
122
132
  }
123
133
  const tabId = (index) => `${tabsId}-tab-${index}`;
124
134
  const panelId = `${tabsId}-panel`;
125
- const activeIndex = activeVariant ? variants.indexOf(activeVariant) : -1;
135
+ const activeIndex = computed(
136
+ () => activeVariant.value ? variants.indexOf(activeVariant.value) : -1
137
+ );
126
138
  return /* @__PURE__ */ jsxs("div", { children: [
127
139
  showToolbar && /* @__PURE__ */ jsxs("div", { class: "sg-workbench-toolbar", children: [
128
140
  showTheme && /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview theme", class: TRACK_CLASS, children: THEME_OPTIONS.map((option) => /* @__PURE__ */ jsx(
129
141
  "button",
130
142
  {
131
143
  type: "button",
132
- onClick: () => setThemeMode(option.id),
133
- "aria-pressed": themeMode === option.id,
134
- class: segmentClass(themeMode === option.id),
144
+ "on:click": () => {
145
+ themeMode.value = option.id;
146
+ },
147
+ "aria-pressed": computed(() => themeMode.value === option.id),
148
+ class: computed(
149
+ () => segmentClass(themeMode.value === option.id)
150
+ ),
135
151
  children: option.label
136
- },
137
- option.id
152
+ }
138
153
  )) }),
139
154
  showViewport && /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview viewport", class: TRACK_CLASS, children: VIEWPORTS.map((preset) => /* @__PURE__ */ jsx(
140
155
  "button",
141
156
  {
142
157
  type: "button",
143
- onClick: () => setViewportId(preset.id),
144
- "aria-pressed": viewportId === preset.id,
145
- class: segmentClass(viewportId === preset.id),
158
+ "on:click": () => {
159
+ viewportId.value = preset.id;
160
+ },
161
+ "aria-pressed": computed(() => viewportId.value === preset.id),
162
+ class: computed(
163
+ () => segmentClass(viewportId.value === preset.id)
164
+ ),
146
165
  children: preset.label
147
- },
148
- preset.id
166
+ }
149
167
  )) }),
150
168
  showLayout && /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview layout", class: TRACK_CLASS, children: LAYOUT_OPTIONS.map((option) => /* @__PURE__ */ jsx(
151
169
  "button",
152
170
  {
153
171
  type: "button",
154
- onClick: () => setLayout(option.id),
155
- "aria-pressed": layout === option.id,
156
- class: segmentClass(layout === option.id),
172
+ "on:click": () => {
173
+ layout.value = option.id;
174
+ },
175
+ "aria-pressed": computed(() => layout.value === option.id),
176
+ class: computed(
177
+ () => segmentClass(layout.value === option.id)
178
+ ),
157
179
  children: option.label
158
- },
159
- option.id
180
+ }
160
181
  )) }),
161
- (showCodePanel || showTokenPanel) && /* @__PURE__ */ jsxs("div", { class: "ms-auto flex flex-wrap items-center gap-hsp-sm", children: [
182
+ (showCodePanel || showTokenPanel) && /* @__PURE__ */ jsxs("div", { class: "sg-workbench-toolbar-actions flex flex-wrap items-center gap-hsp-sm", children: [
162
183
  showCodePanel && /* @__PURE__ */ jsxs(
163
184
  "button",
164
185
  {
165
186
  type: "button",
166
- onClick: () => toggleCodePanel(),
187
+ "on:click": () => toggleCodePanel(),
167
188
  "aria-pressed": codePanelShown,
168
189
  title: "Toggle code panel",
169
- class: PILL_BASE_CLASS + (codePanelShown ? PILL_ON_CLASS : PILL_OFF_CLASS),
190
+ class: computed(
191
+ () => PILL_BASE_CLASS + (codePanelShown.value ? PILL_ON_CLASS : PILL_OFF_CLASS)
192
+ ),
170
193
  children: [
171
194
  /* @__PURE__ */ jsxs(
172
195
  "svg",
@@ -195,7 +218,7 @@ function DetailWorkbench({
195
218
  "button",
196
219
  {
197
220
  type: "button",
198
- onClick: openPreviewTokenPanel,
221
+ "on:click": openPreviewTokenPanel,
199
222
  title: "Edit the design tokens used inside the preview iframes",
200
223
  class: PILL_BASE_CLASS + PILL_OFF_CLASS,
201
224
  children: [
@@ -225,7 +248,7 @@ function DetailWorkbench({
225
248
  ] })
226
249
  ] }),
227
250
  single && /* @__PURE__ */ jsx("div", { role: "tablist", "aria-label": "Variants", class: TABLIST_CLASS, children: variants.map((variant, index) => {
228
- const selected = index === activeIndex;
251
+ const selected = computed(() => index === activeIndex.value);
229
252
  return /* @__PURE__ */ jsx(
230
253
  "button",
231
254
  {
@@ -234,36 +257,48 @@ function DetailWorkbench({
234
257
  role: "tab",
235
258
  "aria-selected": selected,
236
259
  "aria-controls": panelId,
237
- tabIndex: selected ? 0 : -1,
238
- onClick: () => selectVariant(variant.exportName),
239
- onKeyDown: (e) => onTabKeyDown(e, index),
240
- class: tabClass(selected),
260
+ tabindex: computed(() => selected.value ? 0 : -1),
261
+ "on:click": () => selectVariant(variant.exportName),
262
+ "on:keydown": (e) => onTabKeyDown(e, index),
263
+ class: computed(() => tabClass(selected.value)),
241
264
  children: variant.name
242
- },
243
- variant.exportName
265
+ }
244
266
  );
245
267
  }) }),
246
268
  /* @__PURE__ */ jsx(
247
269
  "div",
248
270
  {
249
271
  "data-sg-stage-grid": layout,
250
- class: layout === "grid" ? COLUMNS_GRID_CLASS : STACKED_GRID_CLASS,
272
+ class: computed(
273
+ () => layout.value === "grid" ? COLUMNS_GRID_CLASS : STACKED_GRID_CLASS
274
+ ),
251
275
  ...single ? {
252
276
  id: panelId,
253
277
  role: "tabpanel",
254
- "aria-labelledby": activeIndex >= 0 ? tabId(activeIndex) : void 0
278
+ "aria-labelledby": computed(
279
+ () => activeIndex.value >= 0 ? tabId(activeIndex.value) : void 0
280
+ )
255
281
  } : {},
256
- children: single ? (
257
- // One constant key: switching tabs changes the stage's props, and
258
- // VariantFrame resets readiness/height on the new `src` (#880)
259
- // instead of remounting the iframe.
260
- activeVariant && renderStage(activeVariant, "sg-single-stage")
261
- ) : variants.map((variant) => renderStage(variant))
282
+ children: single ? /* @__PURE__ */ jsx(Show, { when: computed(() => !!activeVariant.value), children: () => /* @__PURE__ */ jsx(
283
+ VariantFrame,
284
+ {
285
+ slug,
286
+ exportName: computed(() => activeVariant.value.exportName),
287
+ name: computed(() => activeVariant.value.name),
288
+ controls: computed(() => activeVariant.value?.controls),
289
+ themeMode: frameThemeMode,
290
+ viewportId,
291
+ previewUrl,
292
+ frameSandbox,
293
+ frameAllow,
294
+ previewParams,
295
+ fixedHeight
296
+ }
297
+ ) }) : variants.map((variant) => renderStage(variant))
262
298
  }
263
299
  )
264
300
  ] });
265
301
  }
266
- DetailWorkbench.displayName = "DetailWorkbench";
267
302
  export {
268
303
  DetailWorkbench as default
269
304
  };
@@ -1,8 +1,10 @@
1
+ export { default as VariantFrame } from "./variant-frame.js";
2
+ export type { VariantFrameProps } from "./variant-frame.js";
1
3
  export { default as PreviewApp } from "./preview-app.js";
2
4
  export type { PreviewAppProps } from "./preview-app.js";
3
5
  export { default as DetailWorkbench } from "./detail-workbench.js";
4
6
  export type { DetailWorkbenchProps, WorkbenchSelection, WorkbenchSizing, WorkbenchTheme, WorkbenchToolbar, WorkbenchVariant, } from "./detail-workbench.js";
5
- export { default as VariantFrame, buildPreviewSrc, DEFAULT_FRAME_SANDBOX, DEFAULT_THEME_MODE, DEFAULT_VIEWPORT_ID, RESERVED_PREVIEW_PARAMS, THEME_OPTIONS, VIEWPORTS, } from "./variant-frame.js";
6
- export type { FrameThemeMode, ThemeMode, ThemeOption, VariantFrameProps, Viewport, ViewportId, } from "./variant-frame.js";
7
+ export { buildPreviewSrc, DEFAULT_FRAME_SANDBOX, DEFAULT_THEME_MODE, DEFAULT_VIEWPORT_ID, RESERVED_PREVIEW_PARAMS, THEME_OPTIONS, VIEWPORTS, } from "./variant-frame-config.js";
8
+ export type { FrameThemeMode, ThemeMode, ThemeOption, Viewport, ViewportId, } from "./variant-frame-config.js";
7
9
  export * from "./messages.js";
8
10
  export { PREVIEW_ROUTE_PATH } from "./route.js";
@@ -1,7 +1,7 @@
1
- import { default as default2 } from "./preview-app.js";
2
- import { default as default3 } from "./detail-workbench.js";
1
+ import { default as default2 } from "./variant-frame.js";
2
+ import { default as default3 } from "./preview-app.js";
3
+ import { default as default4 } from "./detail-workbench.js";
3
4
  import {
4
- default as default4,
5
5
  buildPreviewSrc,
6
6
  DEFAULT_FRAME_SANDBOX,
7
7
  DEFAULT_THEME_MODE,
@@ -9,19 +9,19 @@ import {
9
9
  RESERVED_PREVIEW_PARAMS,
10
10
  THEME_OPTIONS,
11
11
  VIEWPORTS
12
- } from "./variant-frame.js";
12
+ } from "./variant-frame-config.js";
13
13
  export * from "./messages.js";
14
14
  import { PREVIEW_ROUTE_PATH } from "./route.js";
15
15
  export {
16
16
  DEFAULT_FRAME_SANDBOX,
17
17
  DEFAULT_THEME_MODE,
18
18
  DEFAULT_VIEWPORT_ID,
19
- default3 as DetailWorkbench,
19
+ default4 as DetailWorkbench,
20
20
  PREVIEW_ROUTE_PATH,
21
- default2 as PreviewApp,
21
+ default3 as PreviewApp,
22
22
  RESERVED_PREVIEW_PARAMS,
23
23
  THEME_OPTIONS,
24
24
  VIEWPORTS,
25
- default4 as VariantFrame,
25
+ default2 as VariantFrame,
26
26
  buildPreviewSrc
27
27
  };
@@ -1,11 +1,8 @@
1
- import type { JSX } from "preact";
1
+ import { type Description } from "@takazudo/zfb/zudo-react";
2
2
  import type { Registry } from "../registry/index.js";
3
3
  export interface PreviewAppProps {
4
4
  /** The host's story registry (`createRegistry(...)` result). In-bundle only — never island props. */
5
5
  registry: Pick<Registry, "getStoryBySlug">;
6
6
  }
7
- declare function PreviewApp({ registry }: PreviewAppProps): JSX.Element;
8
- declare namespace PreviewApp {
9
- var displayName: string;
10
- }
7
+ declare function PreviewApp({ registry }: PreviewAppProps): Description;
11
8
  export default PreviewApp;
@@ -1,6 +1,11 @@
1
1
  "use client";
2
- import { jsx, jsxs } from "preact/jsx-runtime";
3
- import { useEffect, useMemo, useState } from "preact/hooks";
2
+ import { jsx, jsxs } from "@takazudo/zfb/zudo-react/jsx-runtime";
3
+ import {
4
+ signal,
5
+ computed,
6
+ getScope,
7
+ For
8
+ } from "@takazudo/zfb/zudo-react";
4
9
  import {
5
10
  MSG_HEIGHT,
6
11
  MSG_READY,
@@ -32,67 +37,92 @@ function postToParent(message) {
32
37
  function announceReady(identity) {
33
38
  postToParent({ type: MSG_READY, v: PROTOCOL_VERSION, ...identity });
34
39
  }
40
+ function PreviewStory({
41
+ render,
42
+ args
43
+ }) {
44
+ return render(args);
45
+ }
35
46
  function PreviewApp({ registry }) {
36
- const [identity] = useState(readParams);
37
- const { slug, variant } = identity;
38
- const [overrides, setOverrides] = useState({});
39
- const entry = useMemo(() => registry.getStoryBySlug(slug), [registry, slug]);
40
- const variantEntry = useMemo(
41
- () => entry?.variants.find((v) => v.exportName === variant),
42
- [entry, variant]
47
+ const scope = getScope();
48
+ const initial = readParams();
49
+ const slug = signal(initial.slug);
50
+ const variant = signal(initial.variant);
51
+ const overrides = signal({});
52
+ const version = signal(0);
53
+ const identity = () => ({
54
+ slug: slug.value,
55
+ variant: variant.value
56
+ });
57
+ const entry = computed(() => registry.getStoryBySlug(slug.value));
58
+ const variantEntry = computed(
59
+ () => entry.value?.variants.find((v) => v.exportName === variant.value)
43
60
  );
44
- useEffect(() => {
61
+ const merged = computed(() => ({
62
+ ...defaultsFromControls(variantEntry.value?.story.controls),
63
+ ...overrides.value
64
+ }));
65
+ scope.onActivate(() => {
66
+ const report = () => reportHeight(identity());
45
67
  function onMessage(e) {
46
- if (e.origin !== window.location.origin) return;
47
- if (e.source !== window.parent) return;
48
- if (isRequestReadyMessage(e.data)) {
49
- announceReady(identity);
50
- reportHeight(identity);
51
- return;
52
- }
53
- if (isUpdatePropsMessage(e.data)) {
54
- setOverrides((prev) => ({ ...prev, ...e.data.props }));
68
+ if (e.origin !== window.location.origin || e.source !== window.parent)
55
69
  return;
56
- }
57
- if (isSetThemeMessage(e.data)) {
70
+ if (isRequestReadyMessage(e.data)) {
71
+ announceReady(identity());
72
+ report();
73
+ } else if (isUpdatePropsMessage(e.data)) {
74
+ overrides.value = { ...overrides.value, ...e.data.props };
75
+ version.value++;
76
+ } else if (isSetThemeMessage(e.data)) {
58
77
  document.documentElement.dataset.theme = e.data.theme;
59
78
  }
60
79
  }
80
+ function onLocationChange() {
81
+ const next = readParams();
82
+ if (next.slug === slug.value && next.variant === variant.value) return;
83
+ slug.value = next.slug;
84
+ variant.value = next.variant;
85
+ overrides.value = {};
86
+ version.value++;
87
+ announceReady(identity());
88
+ report();
89
+ }
61
90
  window.addEventListener("message", onMessage);
91
+ window.addEventListener("popstate", onLocationChange);
62
92
  document.documentElement.dataset.sgPreviewHydrated = "1";
63
- announceReady(identity);
64
- return () => window.removeEventListener("message", onMessage);
65
- }, []);
66
- useEffect(() => {
67
- const report = () => reportHeight(identity);
93
+ announceReady(identity());
68
94
  report();
69
95
  const t1 = window.setTimeout(report, 100);
70
96
  const t2 = window.setTimeout(report, 500);
71
- const ro = new ResizeObserver(report);
72
- ro.observe(document.body);
97
+ const observer = new ResizeObserver(report);
98
+ observer.observe(document.body);
73
99
  return () => {
100
+ window.removeEventListener("message", onMessage);
101
+ window.removeEventListener("popstate", onLocationChange);
74
102
  window.clearTimeout(t1);
75
103
  window.clearTimeout(t2);
76
- ro.disconnect();
104
+ observer.disconnect();
105
+ delete document.documentElement.dataset.sgPreviewHydrated;
77
106
  };
78
- }, []);
79
- if (!entry || !variantEntry) {
80
- return /* @__PURE__ */ jsxs("div", { class: "p-hsp-md text-[color:var(--sg-muted)] text-small", children: [
81
- "Unknown preview: slug=",
82
- /* @__PURE__ */ jsx("code", { children: slug }),
83
- " variant=",
84
- /* @__PURE__ */ jsx("code", { children: variant })
85
- ] });
86
- }
87
- const variantArgs = {};
88
- const merged = {
89
- ...defaultsFromControls(variantEntry.story.controls),
90
- ...variantArgs,
91
- ...overrides
92
- };
93
- return /* @__PURE__ */ jsx("div", { class: "p-hsp-md", "data-sg-variant-root": true, children: variantEntry.story.render(merged) });
107
+ });
108
+ return /* @__PURE__ */ jsx(For, { each: computed(() => [version.value]), by: (key) => key, children: () => {
109
+ const current = variantEntry.value;
110
+ if (!current)
111
+ return /* @__PURE__ */ jsxs("div", { class: "p-hsp-md text-sg-muted text-small", children: [
112
+ "Unknown preview: slug=",
113
+ /* @__PURE__ */ jsx("code", { children: slug.value }),
114
+ " variant=",
115
+ /* @__PURE__ */ jsx("code", { children: variant.value })
116
+ ] });
117
+ return /* @__PURE__ */ jsx("div", { class: "p-hsp-md", "data-sg-variant-root": true, children: /* @__PURE__ */ jsx(
118
+ PreviewStory,
119
+ {
120
+ render: current.story.render,
121
+ args: merged.value
122
+ }
123
+ ) });
124
+ } });
94
125
  }
95
- PreviewApp.displayName = "PreviewApp";
96
126
  var preview_app_default = PreviewApp;
97
127
  export {
98
128
  preview_app_default as default
@@ -0,0 +1,47 @@
1
+ import type { PreviewTheme } from "./messages.js";
2
+ export type ViewportId = "mobile" | "tablet" | "desktop" | "full";
3
+ export interface Viewport {
4
+ id: ViewportId;
5
+ label: string;
6
+ width: string;
7
+ }
8
+ export declare const VIEWPORTS: Viewport[];
9
+ /** Fluid width — the fixed presets are opt-in. */
10
+ export declare const DEFAULT_VIEWPORT_ID: ViewportId;
11
+ export type ThemeMode = "follow" | PreviewTheme;
12
+ export interface ThemeOption {
13
+ id: ThemeMode;
14
+ label: string;
15
+ }
16
+ export declare const THEME_OPTIONS: ThemeOption[];
17
+ export declare const DEFAULT_THEME_MODE: ThemeMode;
18
+ /**
19
+ * What a stage does about theme. `"none"` hands theme to the host (#883): the
20
+ * stage never posts `sg:setTheme`, so a preview document that picks its own
21
+ * theme (e.g. from a `previewParams` entry) is never overridden.
22
+ */
23
+ export type FrameThemeMode = ThemeMode | "none";
24
+ /** Query keys the stage owns; `previewParams` entries using them are ignored. */
25
+ export declare const RESERVED_PREVIEW_PARAMS: readonly string[];
26
+ /**
27
+ * The preview document URL for one variant: `slug` and `variant` first, then
28
+ * every `previewParams` entry URL-encoded in insertion order. A reserved key
29
+ * in `previewParams` is dropped rather than thrown on — the identity pair must
30
+ * stay authoritative, and throwing from an island would blank the whole page.
31
+ */
32
+ export declare function buildPreviewSrc(previewUrl: string, slug: string, exportName: string, previewParams?: Readonly<Record<string, string>>): string;
33
+ export declare function viewportWidth(id: ViewportId): string;
34
+ export declare function readCatalogTheme(): PreviewTheme | null;
35
+ /** Initial iframe height (px) before the frame reports its own, and after a variant change. */
36
+ export declare const INITIAL_FRAME_HEIGHT = 180;
37
+ /**
38
+ * `allow-forms` is required by `packages/demo-ui/src/forms/` stories: #499 saw
39
+ * Chromium block submission without it (the submit listener did not fire and
40
+ * the frame did not navigate), while with it the listener fired and the frame
41
+ * navigated to the real action. The catalog form stories omit their enhancer
42
+ * islands, so `contact-form.stories.tsx` cannot show this: it renders
43
+ * `<ContactForm />` alone; `ContactFormEnhancer` lives at previewRoute
44
+ * `/preview/contact`, which the detail page links out to instead of rendering
45
+ * in a preview iframe.
46
+ */
47
+ export declare const DEFAULT_FRAME_SANDBOX: readonly string[];
@@ -0,0 +1,50 @@
1
+ const VIEWPORTS = [
2
+ { id: "mobile", label: "Mobile", width: "320px" },
3
+ { id: "tablet", label: "Tablet", width: "768px" },
4
+ { id: "desktop", label: "Desktop", width: "1280px" },
5
+ { id: "full", label: "Full", width: "100%" }
6
+ ];
7
+ const DEFAULT_VIEWPORT_ID = "full";
8
+ const THEME_OPTIONS = [
9
+ { id: "follow", label: "Follow catalog" },
10
+ { id: "light", label: "Light" },
11
+ { id: "dark", label: "Dark" }
12
+ ];
13
+ const DEFAULT_THEME_MODE = "follow";
14
+ const RESERVED_PREVIEW_PARAMS = ["slug", "variant"];
15
+ function buildPreviewSrc(previewUrl, slug, exportName, previewParams) {
16
+ let src = `${previewUrl}?slug=${encodeURIComponent(slug)}&variant=${encodeURIComponent(exportName)}`;
17
+ if (previewParams) {
18
+ for (const [key, value] of Object.entries(previewParams)) {
19
+ if (RESERVED_PREVIEW_PARAMS.includes(key)) continue;
20
+ src += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
21
+ }
22
+ }
23
+ return src;
24
+ }
25
+ function viewportWidth(id) {
26
+ const preset = VIEWPORTS.find((v) => v.id === id) ?? VIEWPORTS.at(-1);
27
+ return preset?.width ?? "100%";
28
+ }
29
+ function readCatalogTheme() {
30
+ const theme = document.documentElement.getAttribute("data-theme");
31
+ return theme === "light" || theme === "dark" ? theme : null;
32
+ }
33
+ const INITIAL_FRAME_HEIGHT = 180;
34
+ const DEFAULT_FRAME_SANDBOX = [
35
+ "allow-same-origin",
36
+ "allow-scripts",
37
+ "allow-forms"
38
+ ];
39
+ export {
40
+ DEFAULT_FRAME_SANDBOX,
41
+ DEFAULT_THEME_MODE,
42
+ DEFAULT_VIEWPORT_ID,
43
+ INITIAL_FRAME_HEIGHT,
44
+ RESERVED_PREVIEW_PARAMS,
45
+ THEME_OPTIONS,
46
+ VIEWPORTS,
47
+ buildPreviewSrc,
48
+ readCatalogTheme,
49
+ viewportWidth
50
+ };