@takazudo/zudo-sg 0.3.5 → 0.4.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 (54) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/README.md +3 -3
  3. package/dist/catalog/catalog-thumb.d.ts +19 -0
  4. package/dist/catalog/catalog-thumb.js +11 -0
  5. package/dist/catalog/descriptor-thumb.d.ts +20 -0
  6. package/dist/catalog/descriptor-thumb.js +31 -0
  7. package/dist/catalog/index.d.ts +4 -0
  8. package/dist/catalog/index.js +5 -0
  9. package/dist/code-panel/code-panel.d.ts +8 -1
  10. package/dist/code-panel/code-panel.js +76 -24
  11. package/dist/code-panel/editor-setup.d.ts +2 -0
  12. package/dist/code-panel/editor-setup.js +3 -0
  13. package/dist/code-panel/source-editor.d.ts +3 -1
  14. package/dist/code-panel/source-editor.js +10 -1
  15. package/dist/config/index.d.ts +54 -5
  16. package/dist/config/index.js +72 -12
  17. package/dist/host-paths.d.ts +2 -0
  18. package/dist/host-paths.js +5 -0
  19. package/dist/plugins/routes.d.ts +60 -11
  20. package/dist/plugins/routes.js +135 -23
  21. package/dist/preview/detail-workbench.d.ts +63 -1
  22. package/dist/preview/detail-workbench.js +112 -22
  23. package/dist/preview/index.d.ts +3 -3
  24. package/dist/preview/index.js +7 -1
  25. package/dist/preview/messages.d.ts +34 -5
  26. package/dist/preview/messages.js +34 -6
  27. package/dist/preview/preview-app.js +22 -10
  28. package/dist/preview/variant-frame.d.ts +50 -2
  29. package/dist/preview/variant-frame.js +64 -23
  30. package/dist/registry/catalog.d.ts +64 -0
  31. package/dist/registry/catalog.js +79 -0
  32. package/dist/registry/descriptors.d.ts +46 -0
  33. package/dist/registry/descriptors.js +148 -0
  34. package/dist/registry/index.d.ts +5 -1
  35. package/dist/registry/index.js +7 -1
  36. package/dist/registry/nav-nodes.d.ts +4 -1
  37. package/dist/registry/nav-nodes.js +4 -1
  38. package/dist/registry/registry.d.ts +15 -0
  39. package/dist/registry/registry.js +18 -9
  40. package/dist/safelist.css +1 -1
  41. package/dist/sg-context.d.ts +29 -0
  42. package/dist/sg-routes.d.ts +49 -1
  43. package/dist/sg-routes.js +15 -1
  44. package/dist/shared/tablist-keyboard.d.ts +14 -0
  45. package/dist/shared/tablist-keyboard.js +19 -0
  46. package/package.json +10 -5
  47. package/routes-src/_preview-app.tsx +2 -2
  48. package/routes-src/_registry.ts +31 -6
  49. package/routes-src/components-index.tsx +15 -8
  50. package/routes-src/components-preview.tsx +5 -0
  51. package/routes-src/components-slug.tsx +63 -31
  52. package/routes-src/tokens.tsx +9 -4
  53. package/styles.css +10 -0
  54. package/virtual-modules.d.ts +6 -0
@@ -12,6 +12,7 @@ import {
12
12
  isCodePanelHidden,
13
13
  toggleCodePanel
14
14
  } from "../chrome/panel-contract.js";
15
+ import { handleTablistKeyDown } from "../shared/tablist-keyboard.js";
15
16
  const LAYOUT_OPTIONS = [
16
17
  { id: "stacked", label: "Stacked" },
17
18
  { id: "grid", label: "Grid" }
@@ -25,19 +26,46 @@ const PILL_ON_CLASS = "border-[color:var(--sg-border-strong)] bg-[var(--sg-surfa
25
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)]";
26
27
  const STACKED_GRID_CLASS = "grid items-start gap-x-hsp-xl gap-y-vsp-xl grid-cols-1";
27
28
  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)]";
30
+ 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)]";
28
33
  function segmentClass(selected) {
29
34
  return SEGMENT_BASE_CLASS + (selected ? SEGMENT_ON_CLASS : SEGMENT_OFF_CLASS);
30
35
  }
36
+ function tabClass(selected) {
37
+ return TAB_BASE_CLASS + (selected ? TAB_ON_CLASS : TAB_OFF_CLASS);
38
+ }
31
39
  function DetailWorkbench({
32
40
  slug,
33
41
  variants,
34
- previewUrl
42
+ previewUrl,
43
+ frameSandbox,
44
+ frameAllow,
45
+ selection = { mode: "all" },
46
+ theme = { mode: "toolbar" },
47
+ previewParams,
48
+ sizing = { mode: "auto" },
49
+ toolbar = {}
35
50
  }) {
36
51
  const [themeMode, setThemeMode] = useState(DEFAULT_THEME_MODE);
37
52
  const [viewportId, setViewportId] = useState(DEFAULT_VIEWPORT_ID);
38
53
  const [layout, setLayout] = useState("stacked");
39
54
  const [codePanelShown, setCodePanelShown] = useState(true);
55
+ const [internalActive, setInternalActive] = useState(
56
+ void 0
57
+ );
58
+ const tabsId = `sg-workbench-${slug}`;
59
+ const showTheme = theme.mode === "toolbar";
60
+ const showViewport = toolbar.viewport ?? true;
61
+ const showLayout = toolbar.layout ?? true;
62
+ const showCodePanel = toolbar.codePanel ?? false;
63
+ const showTokenPanel = toolbar.tokenPanel ?? false;
64
+ const showToolbar = showTheme || showViewport || showLayout || showCodePanel || showTokenPanel;
65
+ const frameThemeMode = showTheme ? themeMode : "none";
66
+ const fixedHeight = sizing.mode === "fixed" ? sizing.height : void 0;
40
67
  useEffect(() => {
68
+ if (!showCodePanel) return;
41
69
  const sync = () => setCodePanelShown(!isCodePanelHidden());
42
70
  sync();
43
71
  const observer = new MutationObserver(sync);
@@ -46,13 +74,58 @@ function DetailWorkbench({
46
74
  attributeFilter: [ATTR_CODE_PANEL_HIDDEN]
47
75
  });
48
76
  return () => observer.disconnect();
49
- }, []);
77
+ }, [showCodePanel]);
50
78
  function openPreviewTokenPanel() {
51
79
  window.dispatchEvent(new CustomEvent("toggle-preview-token-panel"));
52
80
  }
81
+ 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;
84
+ function selectVariant(exportName) {
85
+ if (selection.mode !== "single") return;
86
+ if (selection.active === void 0) setInternalActive(exportName);
87
+ selection.onChange?.(exportName);
88
+ }
89
+ function onTabKeyDown(e, index) {
90
+ handleTablistKeyDown(
91
+ e,
92
+ index,
93
+ variants.length,
94
+ (next) => {
95
+ const target = variants[next];
96
+ if (target) selectVariant(target.exportName);
97
+ },
98
+ (next) => {
99
+ const tablist = e.currentTarget.parentElement;
100
+ tablist?.children[next]?.focus();
101
+ }
102
+ );
103
+ }
104
+ function renderStage(variant, key) {
105
+ return /* @__PURE__ */ jsx(
106
+ VariantFrame,
107
+ {
108
+ slug,
109
+ exportName: variant.exportName,
110
+ name: variant.name,
111
+ controls: variant.controls,
112
+ themeMode: frameThemeMode,
113
+ viewportId,
114
+ previewUrl,
115
+ frameSandbox,
116
+ frameAllow,
117
+ previewParams,
118
+ fixedHeight
119
+ },
120
+ key ?? variant.exportName
121
+ );
122
+ }
123
+ const tabId = (index) => `${tabsId}-tab-${index}`;
124
+ const panelId = `${tabsId}-panel`;
125
+ const activeIndex = activeVariant ? variants.indexOf(activeVariant) : -1;
53
126
  return /* @__PURE__ */ jsxs("div", { children: [
54
- /* @__PURE__ */ jsxs("div", { class: "sg-workbench-toolbar", children: [
55
- /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview theme", class: TRACK_CLASS, children: THEME_OPTIONS.map((option) => /* @__PURE__ */ jsx(
127
+ showToolbar && /* @__PURE__ */ jsxs("div", { class: "sg-workbench-toolbar", children: [
128
+ showTheme && /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview theme", class: TRACK_CLASS, children: THEME_OPTIONS.map((option) => /* @__PURE__ */ jsx(
56
129
  "button",
57
130
  {
58
131
  type: "button",
@@ -63,7 +136,7 @@ function DetailWorkbench({
63
136
  },
64
137
  option.id
65
138
  )) }),
66
- /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview viewport", class: TRACK_CLASS, children: VIEWPORTS.map((preset) => /* @__PURE__ */ jsx(
139
+ showViewport && /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview viewport", class: TRACK_CLASS, children: VIEWPORTS.map((preset) => /* @__PURE__ */ jsx(
67
140
  "button",
68
141
  {
69
142
  type: "button",
@@ -74,7 +147,7 @@ function DetailWorkbench({
74
147
  },
75
148
  preset.id
76
149
  )) }),
77
- /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview layout", class: TRACK_CLASS, children: LAYOUT_OPTIONS.map((option) => /* @__PURE__ */ jsx(
150
+ showLayout && /* @__PURE__ */ jsx("div", { role: "group", "aria-label": "Preview layout", class: TRACK_CLASS, children: LAYOUT_OPTIONS.map((option) => /* @__PURE__ */ jsx(
78
151
  "button",
79
152
  {
80
153
  type: "button",
@@ -85,8 +158,8 @@ function DetailWorkbench({
85
158
  },
86
159
  option.id
87
160
  )) }),
88
- /* @__PURE__ */ jsxs("div", { class: "ms-auto flex flex-wrap items-center gap-hsp-sm", children: [
89
- /* @__PURE__ */ jsxs(
161
+ (showCodePanel || showTokenPanel) && /* @__PURE__ */ jsxs("div", { class: "ms-auto flex flex-wrap items-center gap-hsp-sm", children: [
162
+ showCodePanel && /* @__PURE__ */ jsxs(
90
163
  "button",
91
164
  {
92
165
  type: "button",
@@ -118,7 +191,7 @@ function DetailWorkbench({
118
191
  ]
119
192
  }
120
193
  ),
121
- /* @__PURE__ */ jsxs(
194
+ showTokenPanel && /* @__PURE__ */ jsxs(
122
195
  "button",
123
196
  {
124
197
  type: "button",
@@ -151,24 +224,41 @@ function DetailWorkbench({
151
224
  )
152
225
  ] })
153
226
  ] }),
227
+ single && /* @__PURE__ */ jsx("div", { role: "tablist", "aria-label": "Variants", class: TABLIST_CLASS, children: variants.map((variant, index) => {
228
+ const selected = index === activeIndex;
229
+ return /* @__PURE__ */ jsx(
230
+ "button",
231
+ {
232
+ id: tabId(index),
233
+ type: "button",
234
+ role: "tab",
235
+ "aria-selected": selected,
236
+ "aria-controls": panelId,
237
+ tabIndex: selected ? 0 : -1,
238
+ onClick: () => selectVariant(variant.exportName),
239
+ onKeyDown: (e) => onTabKeyDown(e, index),
240
+ class: tabClass(selected),
241
+ children: variant.name
242
+ },
243
+ variant.exportName
244
+ );
245
+ }) }),
154
246
  /* @__PURE__ */ jsx(
155
247
  "div",
156
248
  {
157
249
  "data-sg-stage-grid": layout,
158
250
  class: layout === "grid" ? COLUMNS_GRID_CLASS : STACKED_GRID_CLASS,
159
- children: variants.map((variant) => /* @__PURE__ */ jsx(
160
- VariantFrame,
161
- {
162
- slug,
163
- exportName: variant.exportName,
164
- name: variant.name,
165
- controls: variant.controls,
166
- themeMode,
167
- viewportId,
168
- previewUrl
169
- },
170
- variant.exportName
171
- ))
251
+ ...single ? {
252
+ id: panelId,
253
+ role: "tabpanel",
254
+ "aria-labelledby": activeIndex >= 0 ? tabId(activeIndex) : void 0
255
+ } : {},
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))
172
262
  }
173
263
  )
174
264
  ] });
@@ -1,8 +1,8 @@
1
1
  export { default as PreviewApp } from "./preview-app.js";
2
2
  export type { PreviewAppProps } from "./preview-app.js";
3
3
  export { default as DetailWorkbench } from "./detail-workbench.js";
4
- export type { DetailWorkbenchProps, WorkbenchVariant } from "./detail-workbench.js";
5
- export { default as VariantFrame, DEFAULT_THEME_MODE, DEFAULT_VIEWPORT_ID, THEME_OPTIONS, VIEWPORTS, } from "./variant-frame.js";
6
- export type { ThemeMode, ThemeOption, VariantFrameProps, Viewport, ViewportId, } from "./variant-frame.js";
4
+ 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
7
  export * from "./messages.js";
8
8
  export { PREVIEW_ROUTE_PATH } from "./route.js";
@@ -2,20 +2,26 @@ import { default as default2 } from "./preview-app.js";
2
2
  import { default as default3 } from "./detail-workbench.js";
3
3
  import {
4
4
  default as default4,
5
+ buildPreviewSrc,
6
+ DEFAULT_FRAME_SANDBOX,
5
7
  DEFAULT_THEME_MODE,
6
8
  DEFAULT_VIEWPORT_ID,
9
+ RESERVED_PREVIEW_PARAMS,
7
10
  THEME_OPTIONS,
8
11
  VIEWPORTS
9
12
  } from "./variant-frame.js";
10
13
  export * from "./messages.js";
11
14
  import { PREVIEW_ROUTE_PATH } from "./route.js";
12
15
  export {
16
+ DEFAULT_FRAME_SANDBOX,
13
17
  DEFAULT_THEME_MODE,
14
18
  DEFAULT_VIEWPORT_ID,
15
19
  default3 as DetailWorkbench,
16
20
  PREVIEW_ROUTE_PATH,
17
21
  default2 as PreviewApp,
22
+ RESERVED_PREVIEW_PARAMS,
18
23
  THEME_OPTIONS,
19
24
  VIEWPORTS,
20
- default4 as VariantFrame
25
+ default4 as VariantFrame,
26
+ buildPreviewSrc
21
27
  };
@@ -1,32 +1,61 @@
1
+ /** The preview postMessage protocol version this engine speaks. */
2
+ export declare const PROTOCOL_VERSION: 1;
3
+ export type ProtocolVersion = typeof PROTOCOL_VERSION;
1
4
  export declare const MSG_UPDATE_PROPS: "sg:updateProps";
2
5
  export declare const MSG_SET_THEME: "sg:setTheme";
3
6
  export declare const MSG_REQUEST_READY: "sg:requestReady";
4
7
  export declare const MSG_READY: "sg:ready";
5
8
  export declare const MSG_HEIGHT: "sg:height";
6
9
  export type PreviewTheme = "light" | "dark";
7
- export interface UpdatePropsMessage {
10
+ /** Fields every protocol message may carry. Absent `v` means legacy v1. */
11
+ export interface VersionedMessage {
12
+ v?: ProtocolVersion;
13
+ }
14
+ /**
15
+ * The frame's identity, read from its `?slug=…&variant=…` query. Optional on
16
+ * the wire: a legacy frame omits it, and the parent then relies on the origin
17
+ * and source checks alone.
18
+ */
19
+ export interface PreviewIdentity {
20
+ slug?: string;
21
+ variant?: string;
22
+ }
23
+ export interface UpdatePropsMessage extends VersionedMessage {
8
24
  type: typeof MSG_UPDATE_PROPS;
9
25
  /** Prop name → value, merged over the variant's static props on re-render. */
10
26
  props: Record<string, unknown>;
11
27
  }
12
- export interface RequestReadyMessage {
28
+ export interface RequestReadyMessage extends VersionedMessage {
13
29
  type: typeof MSG_REQUEST_READY;
14
30
  }
15
- export interface HeightMessage {
31
+ export interface HeightMessage extends VersionedMessage, PreviewIdentity {
16
32
  type: typeof MSG_HEIGHT;
17
33
  height: number;
18
34
  }
19
- export interface SetThemeMessage {
35
+ export interface SetThemeMessage extends VersionedMessage {
20
36
  type: typeof MSG_SET_THEME;
21
37
  /** A resolved theme. The frame must never receive the unresolved `auto`. */
22
38
  theme: PreviewTheme;
23
39
  }
24
- export interface ReadyMessage {
40
+ export interface ReadyMessage extends VersionedMessage, PreviewIdentity {
25
41
  type: typeof MSG_READY;
26
42
  }
27
43
  export type ParentToPreviewMessage = RequestReadyMessage | UpdatePropsMessage | SetThemeMessage;
28
44
  export type PreviewToParentMessage = ReadyMessage | HeightMessage;
29
45
  export type PreviewMessage = ParentToPreviewMessage | PreviewToParentMessage;
46
+ /**
47
+ * True when `value` is an object whose `v` is this protocol's version or is
48
+ * absent (legacy v1). Any other `v` — including a future `2` — is rejected.
49
+ */
50
+ export declare function isSupportedProtocolVersion(value: unknown): boolean;
51
+ /**
52
+ * True unless the message names a `slug` or `variant` that differs from
53
+ * `current`. A message without identity (a legacy frame) matches.
54
+ */
55
+ export declare function matchesPreviewIdentity(message: PreviewIdentity, current: {
56
+ slug: string;
57
+ variant: string;
58
+ }): boolean;
30
59
  export declare function isRequestReadyMessage(value: unknown): value is RequestReadyMessage;
31
60
  export declare function isUpdatePropsMessage(value: unknown): value is UpdatePropsMessage;
32
61
  export declare function isHeightMessage(value: unknown): value is HeightMessage;
@@ -1,22 +1,47 @@
1
+ const PROTOCOL_VERSION = 1;
1
2
  const MSG_UPDATE_PROPS = "sg:updateProps";
2
3
  const MSG_SET_THEME = "sg:setTheme";
3
4
  const MSG_REQUEST_READY = "sg:requestReady";
4
5
  const MSG_READY = "sg:ready";
5
6
  const MSG_HEIGHT = "sg:height";
7
+ function isSupportedProtocolVersion(value) {
8
+ if (typeof value !== "object" || value === null) return false;
9
+ const v = value.v;
10
+ return v === void 0 || v === PROTOCOL_VERSION;
11
+ }
12
+ function hasType(value, type) {
13
+ return isSupportedProtocolVersion(value) && value.type === type;
14
+ }
15
+ function isOptionalString(value) {
16
+ return value === void 0 || typeof value === "string";
17
+ }
18
+ function hasWellFormedIdentity(value) {
19
+ const { slug, variant } = value;
20
+ return isOptionalString(slug) && isOptionalString(variant);
21
+ }
22
+ function matchesPreviewIdentity(message, current) {
23
+ if (message.slug !== void 0 && message.slug !== current.slug) return false;
24
+ if (message.variant !== void 0 && message.variant !== current.variant) {
25
+ return false;
26
+ }
27
+ return true;
28
+ }
6
29
  function isRequestReadyMessage(value) {
7
- return typeof value === "object" && value !== null && value.type === MSG_REQUEST_READY;
30
+ return hasType(value, MSG_REQUEST_READY);
8
31
  }
9
32
  function isUpdatePropsMessage(value) {
10
- return typeof value === "object" && value !== null && value.type === MSG_UPDATE_PROPS && typeof value.props === "object";
33
+ const props = value?.props;
34
+ return hasType(value, MSG_UPDATE_PROPS) && typeof props === "object" && props !== null;
11
35
  }
12
36
  function isHeightMessage(value) {
13
- return typeof value === "object" && value !== null && value.type === MSG_HEIGHT && typeof value.height === "number";
37
+ return hasType(value, MSG_HEIGHT) && typeof value.height === "number" && hasWellFormedIdentity(value);
14
38
  }
15
39
  function isSetThemeMessage(value) {
16
- return typeof value === "object" && value !== null && value.type === MSG_SET_THEME && (value.theme === "light" || value.theme === "dark");
40
+ const theme = value?.theme;
41
+ return hasType(value, MSG_SET_THEME) && (theme === "light" || theme === "dark");
17
42
  }
18
43
  function isReadyMessage(value) {
19
- return typeof value === "object" && value !== null && value.type === MSG_READY;
44
+ return hasType(value, MSG_READY) && hasWellFormedIdentity(value);
20
45
  }
21
46
  export {
22
47
  MSG_HEIGHT,
@@ -24,9 +49,12 @@ export {
24
49
  MSG_REQUEST_READY,
25
50
  MSG_SET_THEME,
26
51
  MSG_UPDATE_PROPS,
52
+ PROTOCOL_VERSION,
27
53
  isHeightMessage,
28
54
  isReadyMessage,
29
55
  isRequestReadyMessage,
30
56
  isSetThemeMessage,
31
- isUpdatePropsMessage
57
+ isSupportedProtocolVersion,
58
+ isUpdatePropsMessage,
59
+ matchesPreviewIdentity
32
60
  };
@@ -4,6 +4,7 @@ import { useEffect, useMemo, useState } from "preact/hooks";
4
4
  import {
5
5
  MSG_HEIGHT,
6
6
  MSG_READY,
7
+ PROTOCOL_VERSION,
7
8
  isRequestReadyMessage,
8
9
  isSetThemeMessage,
9
10
  isUpdatePropsMessage
@@ -18,13 +19,22 @@ function defaultsFromControls(controls) {
18
19
  for (const c of controls ?? []) out[c.prop] = c.defaultValue;
19
20
  return out;
20
21
  }
21
- function reportHeight() {
22
+ function reportHeight(identity) {
22
23
  const rect = document.body.getBoundingClientRect();
23
24
  const height = Math.ceil(rect.bottom + window.scrollY);
24
- window.parent?.postMessage({ type: MSG_HEIGHT, height }, "*");
25
+ postToParent({ type: MSG_HEIGHT, v: PROTOCOL_VERSION, height, ...identity });
26
+ }
27
+ function postToParent(message) {
28
+ const origin = window.location.origin;
29
+ if (origin === "null") return;
30
+ window.parent?.postMessage(message, origin);
31
+ }
32
+ function announceReady(identity) {
33
+ postToParent({ type: MSG_READY, v: PROTOCOL_VERSION, ...identity });
25
34
  }
26
35
  function PreviewApp({ registry }) {
27
- const [{ slug, variant }] = useState(readParams);
36
+ const [identity] = useState(readParams);
37
+ const { slug, variant } = identity;
28
38
  const [overrides, setOverrides] = useState({});
29
39
  const entry = useMemo(() => registry.getStoryBySlug(slug), [registry, slug]);
30
40
  const variantEntry = useMemo(
@@ -33,10 +43,11 @@ function PreviewApp({ registry }) {
33
43
  );
34
44
  useEffect(() => {
35
45
  function onMessage(e) {
46
+ if (e.origin !== window.location.origin) return;
36
47
  if (e.source !== window.parent) return;
37
48
  if (isRequestReadyMessage(e.data)) {
38
- window.parent?.postMessage({ type: MSG_READY }, "*");
39
- reportHeight();
49
+ announceReady(identity);
50
+ reportHeight(identity);
40
51
  return;
41
52
  }
42
53
  if (isUpdatePropsMessage(e.data)) {
@@ -49,14 +60,15 @@ function PreviewApp({ registry }) {
49
60
  }
50
61
  window.addEventListener("message", onMessage);
51
62
  document.documentElement.dataset.sgPreviewHydrated = "1";
52
- window.parent?.postMessage({ type: MSG_READY }, "*");
63
+ announceReady(identity);
53
64
  return () => window.removeEventListener("message", onMessage);
54
65
  }, []);
55
66
  useEffect(() => {
56
- reportHeight();
57
- const t1 = window.setTimeout(reportHeight, 100);
58
- const t2 = window.setTimeout(reportHeight, 500);
59
- const ro = new ResizeObserver(reportHeight);
67
+ const report = () => reportHeight(identity);
68
+ report();
69
+ const t1 = window.setTimeout(report, 100);
70
+ const t2 = window.setTimeout(report, 500);
71
+ const ro = new ResizeObserver(report);
60
72
  ro.observe(document.body);
61
73
  return () => {
62
74
  window.clearTimeout(t1);
@@ -17,6 +17,32 @@ export interface ThemeOption {
17
17
  }
18
18
  export declare const THEME_OPTIONS: ThemeOption[];
19
19
  export declare const DEFAULT_THEME_MODE: ThemeMode;
20
+ /**
21
+ * What a stage does about theme. `"none"` hands theme to the host (#883): the
22
+ * stage never posts `sg:setTheme`, so a preview document that picks its own
23
+ * theme (e.g. from a `previewParams` entry) is never overridden.
24
+ */
25
+ export type FrameThemeMode = ThemeMode | "none";
26
+ /** Query keys the stage owns; `previewParams` entries using them are ignored. */
27
+ export declare const RESERVED_PREVIEW_PARAMS: readonly string[];
28
+ /**
29
+ * The preview document URL for one variant: `slug` and `variant` first, then
30
+ * every `previewParams` entry URL-encoded in insertion order. A reserved key
31
+ * in `previewParams` is dropped rather than thrown on — the identity pair must
32
+ * stay authoritative, and throwing from an island would blank the whole page.
33
+ */
34
+ export declare function buildPreviewSrc(previewUrl: string, slug: string, exportName: string, previewParams?: Readonly<Record<string, string>>): string;
35
+ /**
36
+ * `allow-forms` is required by `packages/demo-ui/src/forms/` stories: #499 saw
37
+ * Chromium block submission without it (the submit listener did not fire and
38
+ * the frame did not navigate), while with it the listener fired and the frame
39
+ * navigated to the real action. The catalog form stories omit their enhancer
40
+ * islands, so `contact-form.stories.tsx` cannot show this: it renders
41
+ * `<ContactForm />` alone; `ContactFormEnhancer` lives at previewRoute
42
+ * `/preview/contact`, which the detail page links out to instead of rendering
43
+ * in a preview iframe.
44
+ */
45
+ export declare const DEFAULT_FRAME_SANDBOX: readonly string[];
20
46
  export interface VariantFrameProps {
21
47
  slug: string;
22
48
  /** Story export name (e.g. "Variants"). */
@@ -25,8 +51,11 @@ export interface VariantFrameProps {
25
51
  name: string;
26
52
  /** Declarative control descriptors (metadata only). */
27
53
  controls?: StoryControl[];
28
- /** Toolbar-owned theme mode. "follow" tracks the catalog's `data-theme`. */
29
- themeMode: ThemeMode;
54
+ /**
55
+ * Toolbar-owned theme mode. "follow" tracks the catalog's `data-theme`;
56
+ * "none" never posts `sg:setTheme` (host-controlled theme).
57
+ */
58
+ themeMode: FrameThemeMode;
30
59
  /** Toolbar-owned viewport preset. */
31
60
  viewportId: ViewportId;
32
61
  /**
@@ -34,6 +63,25 @@ export interface VariantFrameProps {
34
63
  * `routes.componentsPreview`). Defaults to the unprefixed PREVIEW_ROUTE_PATH.
35
64
  */
36
65
  previewUrl?: string;
66
+ /**
67
+ * Iframe `sandbox` tokens. Defaults to DEFAULT_FRAME_SANDBOX. Dropping
68
+ * `allow-same-origin` gives the frame an opaque origin, which the protocol's
69
+ * same-origin rule then rejects — only do that with a frame that does not
70
+ * need to talk back.
71
+ */
72
+ frameSandbox?: readonly string[];
73
+ /** Iframe `allow` (permissions policy) directives, joined with `; `. Absent by default. */
74
+ frameAllow?: readonly string[];
75
+ /**
76
+ * Extra query params appended, URL-encoded, after `slug`/`variant`. The
77
+ * reserved keys `slug` and `variant` are ignored.
78
+ */
79
+ previewParams?: Readonly<Record<string, string>>;
80
+ /**
81
+ * Fixed iframe height in px. When set, `sg:height` reports are ignored;
82
+ * when absent the frame auto-sizes from them.
83
+ */
84
+ fixedHeight?: number;
37
85
  }
38
86
  declare function VariantFrame(props: VariantFrameProps): JSX.Element;
39
87
  declare namespace VariantFrame {