@takazudo/zudo-sg 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.
- package/CHANGELOG.md +35 -0
- package/LICENSE +21 -0
- package/README.md +99 -0
- package/bin/zudo-sg.js +6 -0
- package/dist/catalog/component-thumb.d.ts +78 -0
- package/dist/catalog/component-thumb.js +110 -0
- package/dist/catalog/index.d.ts +4 -0
- package/dist/catalog/index.js +40 -0
- package/dist/catalog/tile-size.d.ts +28 -0
- package/dist/catalog/tile-size.js +42 -0
- package/dist/chrome/_styleguide-layout.d.ts +59 -0
- package/dist/chrome/_styleguide-layout.js +69 -0
- package/dist/chrome/index.d.ts +4 -0
- package/dist/chrome/index.js +39 -0
- package/dist/chrome/panel-contract.d.ts +15 -0
- package/dist/chrome/panel-contract.js +65 -0
- package/dist/chrome/panel-scripts-source.d.ts +2 -0
- package/dist/chrome/panel-scripts-source.js +121 -0
- package/dist/chrome/panel-scripts.d.ts +5 -0
- package/dist/chrome/panel-scripts.js +12 -0
- package/dist/cli/bin.d.ts +1 -0
- package/dist/cli/bin.js +74 -0
- package/dist/cli/config.d.ts +54 -0
- package/dist/cli/config.js +14 -0
- package/dist/cli/registry/build-registry-source.d.ts +27 -0
- package/dist/cli/registry/build-registry-source.js +77 -0
- package/dist/cli/registry/discover-stories.d.ts +36 -0
- package/dist/cli/registry/discover-stories.js +61 -0
- package/dist/cli/registry/gen-registry.d.ts +23 -0
- package/dist/cli/registry/gen-registry.js +74 -0
- package/dist/cli/scaffold/component-scaffold.d.ts +64 -0
- package/dist/cli/scaffold/component-scaffold.js +233 -0
- package/dist/cli/scaffold/new-component.d.ts +17 -0
- package/dist/cli/scaffold/new-component.js +119 -0
- package/dist/cli/token-manifest/css-var-parser.d.ts +9 -0
- package/dist/cli/token-manifest/css-var-parser.js +23 -0
- package/dist/cli/token-manifest/gen-token-manifest.d.ts +16 -0
- package/dist/cli/token-manifest/gen-token-manifest.js +49 -0
- package/dist/cli/token-manifest/ui-token-manifest.d.ts +78 -0
- package/dist/cli/token-manifest/ui-token-manifest.js +340 -0
- package/dist/code-panel/code-panel.d.ts +18 -0
- package/dist/code-panel/code-panel.js +76 -0
- package/dist/code-panel/copy-button.d.ts +15 -0
- package/dist/code-panel/copy-button.js +92 -0
- package/dist/code-panel/css-injection.d.ts +6 -0
- package/dist/code-panel/css-injection.js +37 -0
- package/dist/code-panel/editor-setup.d.ts +10 -0
- package/dist/code-panel/editor-setup.js +33 -0
- package/dist/code-panel/index.d.ts +6 -0
- package/dist/code-panel/index.js +10 -0
- package/dist/code-panel/source-editor.d.ts +8 -0
- package/dist/code-panel/source-editor.js +39 -0
- package/dist/config/index.d.ts +73 -0
- package/dist/config/index.js +68 -0
- package/dist/host-paths.d.ts +19 -0
- package/dist/host-paths.js +41 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +18 -0
- package/dist/islands.d.ts +2 -0
- package/dist/islands.js +6 -0
- package/dist/plugins/preview-css.d.ts +53 -0
- package/dist/plugins/preview-css.js +183 -0
- package/dist/plugins/routes.d.ts +84 -0
- package/dist/plugins/routes.js +237 -0
- package/dist/plugins/zdtp-apply-proxy.d.ts +58 -0
- package/dist/plugins/zdtp-apply-proxy.js +171 -0
- package/dist/preview/detail-workbench.d.ts +22 -0
- package/dist/preview/detail-workbench.js +179 -0
- package/dist/preview/index.d.ts +8 -0
- package/dist/preview/index.js +21 -0
- package/dist/preview/messages.d.ts +34 -0
- package/dist/preview/messages.js +32 -0
- package/dist/preview/preview-app.d.ts +11 -0
- package/dist/preview/preview-app.js +87 -0
- package/dist/preview/route.d.ts +1 -0
- package/dist/preview/route.js +5 -0
- package/dist/preview/variant-frame.d.ts +42 -0
- package/dist/preview/variant-frame.js +289 -0
- package/dist/preview-css/compile.d.ts +40 -0
- package/dist/preview-css/compile.js +42 -0
- package/dist/preview-css/index.d.ts +2 -0
- package/dist/preview-css/index.js +10 -0
- package/dist/registry/component-docs.d.ts +38 -0
- package/dist/registry/component-docs.js +34 -0
- package/dist/registry/index.d.ts +6 -0
- package/dist/registry/index.js +28 -0
- package/dist/registry/nav-nodes.d.ts +22 -0
- package/dist/registry/nav-nodes.js +36 -0
- package/dist/registry/registry.d.ts +68 -0
- package/dist/registry/registry.js +91 -0
- package/dist/safelist.css +2 -0
- package/dist/search/catalog-filter.d.ts +12 -0
- package/dist/search/catalog-filter.js +175 -0
- package/dist/search/index.d.ts +2 -0
- package/dist/search/index.js +4 -0
- package/dist/sg-context.d.ts +24 -0
- package/dist/sg-context.js +6 -0
- package/dist/sg-routes.d.ts +12 -0
- package/dist/sg-routes.js +17 -0
- package/dist/stories/index.d.ts +2 -0
- package/dist/stories/index.js +4 -0
- package/dist/stories/types.d.ts +164 -0
- package/dist/stories/types.js +6 -0
- package/dist/token-dashboard/create-token-dashboards.d.ts +24 -0
- package/dist/token-dashboard/create-token-dashboards.js +57 -0
- package/dist/token-dashboard/dashboard-inventory.d.ts +11 -0
- package/dist/token-dashboard/dashboard-inventory.js +13 -0
- package/dist/token-dashboard/index.d.ts +5 -0
- package/dist/token-dashboard/index.js +20 -0
- package/dist/token-dashboard/ui-token-tabs.d.ts +53 -0
- package/dist/token-dashboard/ui-token-tabs.js +159 -0
- package/dist/token-tweak/bootstrap-preview-token-panel.d.ts +12 -0
- package/dist/token-tweak/bootstrap-preview-token-panel.js +18 -0
- package/dist/token-tweak/iframe-css-vars-bridge.d.ts +35 -0
- package/dist/token-tweak/iframe-css-vars-bridge.js +73 -0
- package/dist/token-tweak/index.d.ts +6 -0
- package/dist/token-tweak/index.js +36 -0
- package/dist/token-tweak/preview-iframe-registry.d.ts +16 -0
- package/dist/token-tweak/preview-iframe-registry.js +48 -0
- package/dist/token-tweak/preview-token-panel-bootstrap.d.ts +6 -0
- package/dist/token-tweak/preview-token-panel-bootstrap.js +17 -0
- package/dist/token-tweak/preview-token-panel-config.d.ts +42 -0
- package/dist/token-tweak/preview-token-panel-config.js +37 -0
- package/dist/token-tweak/preview-tokens-button.d.ts +5 -0
- package/dist/token-tweak/preview-tokens-button.js +17 -0
- package/dist/token-tweak/token-panel-native-bootstrap.d.ts +17 -0
- package/dist/token-tweak/token-panel-native-bootstrap.js +49 -0
- package/package.json +172 -0
- package/routes-src/_chrome.tsx +83 -0
- package/routes-src/_context.ts +15 -0
- package/routes-src/_preview-app.tsx +32 -0
- package/routes-src/_registry.ts +12 -0
- package/routes-src/components-index.tsx +95 -0
- package/routes-src/components-preview.tsx +55 -0
- package/routes-src/components-slug.tsx +145 -0
- package/routes-src/tokens.tsx +76 -0
- package/styles.css +612 -0
- package/virtual-modules.d.ts +25 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { default as default2 } from "./preview-app.js";
|
|
2
|
+
import { default as default3 } from "./detail-workbench.js";
|
|
3
|
+
import {
|
|
4
|
+
default as default4,
|
|
5
|
+
DEFAULT_THEME_MODE,
|
|
6
|
+
DEFAULT_VIEWPORT_ID,
|
|
7
|
+
THEME_OPTIONS,
|
|
8
|
+
VIEWPORTS
|
|
9
|
+
} from "./variant-frame.js";
|
|
10
|
+
export * from "./messages.js";
|
|
11
|
+
import { PREVIEW_ROUTE_PATH } from "./route.js";
|
|
12
|
+
export {
|
|
13
|
+
DEFAULT_THEME_MODE,
|
|
14
|
+
DEFAULT_VIEWPORT_ID,
|
|
15
|
+
default3 as DetailWorkbench,
|
|
16
|
+
PREVIEW_ROUTE_PATH,
|
|
17
|
+
default2 as PreviewApp,
|
|
18
|
+
THEME_OPTIONS,
|
|
19
|
+
VIEWPORTS,
|
|
20
|
+
default4 as VariantFrame
|
|
21
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export declare const MSG_UPDATE_PROPS: "sg:updateProps";
|
|
2
|
+
export declare const MSG_SET_THEME: "sg:setTheme";
|
|
3
|
+
export declare const MSG_REQUEST_READY: "sg:requestReady";
|
|
4
|
+
export declare const MSG_READY: "sg:ready";
|
|
5
|
+
export declare const MSG_HEIGHT: "sg:height";
|
|
6
|
+
export type PreviewTheme = "light" | "dark";
|
|
7
|
+
export interface UpdatePropsMessage {
|
|
8
|
+
type: typeof MSG_UPDATE_PROPS;
|
|
9
|
+
/** Prop name → value, merged over the variant's static props on re-render. */
|
|
10
|
+
props: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
export interface RequestReadyMessage {
|
|
13
|
+
type: typeof MSG_REQUEST_READY;
|
|
14
|
+
}
|
|
15
|
+
export interface HeightMessage {
|
|
16
|
+
type: typeof MSG_HEIGHT;
|
|
17
|
+
height: number;
|
|
18
|
+
}
|
|
19
|
+
export interface SetThemeMessage {
|
|
20
|
+
type: typeof MSG_SET_THEME;
|
|
21
|
+
/** A resolved theme. The frame must never receive the unresolved `auto`. */
|
|
22
|
+
theme: PreviewTheme;
|
|
23
|
+
}
|
|
24
|
+
export interface ReadyMessage {
|
|
25
|
+
type: typeof MSG_READY;
|
|
26
|
+
}
|
|
27
|
+
export type ParentToPreviewMessage = RequestReadyMessage | UpdatePropsMessage | SetThemeMessage;
|
|
28
|
+
export type PreviewToParentMessage = ReadyMessage | HeightMessage;
|
|
29
|
+
export type PreviewMessage = ParentToPreviewMessage | PreviewToParentMessage;
|
|
30
|
+
export declare function isRequestReadyMessage(value: unknown): value is RequestReadyMessage;
|
|
31
|
+
export declare function isUpdatePropsMessage(value: unknown): value is UpdatePropsMessage;
|
|
32
|
+
export declare function isHeightMessage(value: unknown): value is HeightMessage;
|
|
33
|
+
export declare function isSetThemeMessage(value: unknown): value is SetThemeMessage;
|
|
34
|
+
export declare function isReadyMessage(value: unknown): value is ReadyMessage;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const MSG_UPDATE_PROPS = "sg:updateProps";
|
|
2
|
+
const MSG_SET_THEME = "sg:setTheme";
|
|
3
|
+
const MSG_REQUEST_READY = "sg:requestReady";
|
|
4
|
+
const MSG_READY = "sg:ready";
|
|
5
|
+
const MSG_HEIGHT = "sg:height";
|
|
6
|
+
function isRequestReadyMessage(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && value.type === MSG_REQUEST_READY;
|
|
8
|
+
}
|
|
9
|
+
function isUpdatePropsMessage(value) {
|
|
10
|
+
return typeof value === "object" && value !== null && value.type === MSG_UPDATE_PROPS && typeof value.props === "object";
|
|
11
|
+
}
|
|
12
|
+
function isHeightMessage(value) {
|
|
13
|
+
return typeof value === "object" && value !== null && value.type === MSG_HEIGHT && typeof value.height === "number";
|
|
14
|
+
}
|
|
15
|
+
function isSetThemeMessage(value) {
|
|
16
|
+
return typeof value === "object" && value !== null && value.type === MSG_SET_THEME && (value.theme === "light" || value.theme === "dark");
|
|
17
|
+
}
|
|
18
|
+
function isReadyMessage(value) {
|
|
19
|
+
return typeof value === "object" && value !== null && value.type === MSG_READY;
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
MSG_HEIGHT,
|
|
23
|
+
MSG_READY,
|
|
24
|
+
MSG_REQUEST_READY,
|
|
25
|
+
MSG_SET_THEME,
|
|
26
|
+
MSG_UPDATE_PROPS,
|
|
27
|
+
isHeightMessage,
|
|
28
|
+
isReadyMessage,
|
|
29
|
+
isRequestReadyMessage,
|
|
30
|
+
isSetThemeMessage,
|
|
31
|
+
isUpdatePropsMessage
|
|
32
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { JSX } from "preact";
|
|
2
|
+
import type { Registry } from "../registry/index.js";
|
|
3
|
+
export interface PreviewAppProps {
|
|
4
|
+
/** The host's story registry (`createRegistry(...)` result). In-bundle only — never island props. */
|
|
5
|
+
registry: Pick<Registry, "getStoryBySlug">;
|
|
6
|
+
}
|
|
7
|
+
declare function PreviewApp({ registry }: PreviewAppProps): JSX.Element;
|
|
8
|
+
declare namespace PreviewApp {
|
|
9
|
+
var displayName: string;
|
|
10
|
+
}
|
|
11
|
+
export default PreviewApp;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from "preact/jsx-runtime";
|
|
3
|
+
import { useEffect, useMemo, useState } from "preact/hooks";
|
|
4
|
+
import {
|
|
5
|
+
MSG_HEIGHT,
|
|
6
|
+
MSG_READY,
|
|
7
|
+
isRequestReadyMessage,
|
|
8
|
+
isSetThemeMessage,
|
|
9
|
+
isUpdatePropsMessage
|
|
10
|
+
} from "./messages.js";
|
|
11
|
+
function readParams() {
|
|
12
|
+
if (typeof location === "undefined") return { slug: "", variant: "" };
|
|
13
|
+
const p = new URLSearchParams(location.search);
|
|
14
|
+
return { slug: p.get("slug") ?? "", variant: p.get("variant") ?? "" };
|
|
15
|
+
}
|
|
16
|
+
function defaultsFromControls(controls) {
|
|
17
|
+
const out = {};
|
|
18
|
+
for (const c of controls ?? []) out[c.prop] = c.defaultValue;
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
function reportHeight() {
|
|
22
|
+
const rect = document.body.getBoundingClientRect();
|
|
23
|
+
const height = Math.ceil(rect.bottom + window.scrollY);
|
|
24
|
+
window.parent?.postMessage({ type: MSG_HEIGHT, height }, "*");
|
|
25
|
+
}
|
|
26
|
+
function PreviewApp({ registry }) {
|
|
27
|
+
const [{ slug, variant }] = useState(readParams);
|
|
28
|
+
const [overrides, setOverrides] = useState({});
|
|
29
|
+
const entry = useMemo(() => registry.getStoryBySlug(slug), [registry, slug]);
|
|
30
|
+
const variantEntry = useMemo(
|
|
31
|
+
() => entry?.variants.find((v) => v.exportName === variant),
|
|
32
|
+
[entry, variant]
|
|
33
|
+
);
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
function onMessage(e) {
|
|
36
|
+
if (e.source !== window.parent) return;
|
|
37
|
+
if (isRequestReadyMessage(e.data)) {
|
|
38
|
+
window.parent?.postMessage({ type: MSG_READY }, "*");
|
|
39
|
+
reportHeight();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (isUpdatePropsMessage(e.data)) {
|
|
43
|
+
setOverrides((prev) => ({ ...prev, ...e.data.props }));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (isSetThemeMessage(e.data)) {
|
|
47
|
+
document.documentElement.dataset.theme = e.data.theme;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
window.addEventListener("message", onMessage);
|
|
51
|
+
document.documentElement.dataset.sgPreviewHydrated = "1";
|
|
52
|
+
window.parent?.postMessage({ type: MSG_READY }, "*");
|
|
53
|
+
return () => window.removeEventListener("message", onMessage);
|
|
54
|
+
}, []);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
reportHeight();
|
|
57
|
+
const t1 = window.setTimeout(reportHeight, 100);
|
|
58
|
+
const t2 = window.setTimeout(reportHeight, 500);
|
|
59
|
+
const ro = new ResizeObserver(reportHeight);
|
|
60
|
+
ro.observe(document.body);
|
|
61
|
+
return () => {
|
|
62
|
+
window.clearTimeout(t1);
|
|
63
|
+
window.clearTimeout(t2);
|
|
64
|
+
ro.disconnect();
|
|
65
|
+
};
|
|
66
|
+
}, []);
|
|
67
|
+
if (!entry || !variantEntry) {
|
|
68
|
+
return /* @__PURE__ */ jsxs("div", { class: "p-hsp-md text-muted text-small", children: [
|
|
69
|
+
"Unknown preview: slug=",
|
|
70
|
+
/* @__PURE__ */ jsx("code", { children: slug }),
|
|
71
|
+
" variant=",
|
|
72
|
+
/* @__PURE__ */ jsx("code", { children: variant })
|
|
73
|
+
] });
|
|
74
|
+
}
|
|
75
|
+
const variantArgs = {};
|
|
76
|
+
const merged = {
|
|
77
|
+
...defaultsFromControls(variantEntry.story.controls),
|
|
78
|
+
...variantArgs,
|
|
79
|
+
...overrides
|
|
80
|
+
};
|
|
81
|
+
return /* @__PURE__ */ jsx("div", { class: "p-hsp-md", "data-sg-variant-root": true, children: variantEntry.story.render(merged) });
|
|
82
|
+
}
|
|
83
|
+
PreviewApp.displayName = "PreviewApp";
|
|
84
|
+
var preview_app_default = PreviewApp;
|
|
85
|
+
export {
|
|
86
|
+
preview_app_default as default
|
|
87
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const PREVIEW_ROUTE_PATH: string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { JSX } from "preact";
|
|
2
|
+
import type { StoryControl } from "../stories/index.js";
|
|
3
|
+
import { type PreviewTheme } from "./messages.js";
|
|
4
|
+
export type ViewportId = "mobile" | "tablet" | "desktop" | "full";
|
|
5
|
+
export interface Viewport {
|
|
6
|
+
id: ViewportId;
|
|
7
|
+
label: string;
|
|
8
|
+
width: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const VIEWPORTS: Viewport[];
|
|
11
|
+
/** Fluid width — the fixed presets are opt-in. */
|
|
12
|
+
export declare const DEFAULT_VIEWPORT_ID: ViewportId;
|
|
13
|
+
export type ThemeMode = "follow" | PreviewTheme;
|
|
14
|
+
export interface ThemeOption {
|
|
15
|
+
id: ThemeMode;
|
|
16
|
+
label: string;
|
|
17
|
+
}
|
|
18
|
+
export declare const THEME_OPTIONS: ThemeOption[];
|
|
19
|
+
export declare const DEFAULT_THEME_MODE: ThemeMode;
|
|
20
|
+
export interface VariantFrameProps {
|
|
21
|
+
slug: string;
|
|
22
|
+
/** Story export name (e.g. "Variants"). */
|
|
23
|
+
exportName: string;
|
|
24
|
+
/** Human label shown above the preview. */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Declarative control descriptors (metadata only). */
|
|
27
|
+
controls?: StoryControl[];
|
|
28
|
+
/** Toolbar-owned theme mode. "follow" tracks the catalog's `data-theme`. */
|
|
29
|
+
themeMode: ThemeMode;
|
|
30
|
+
/** Toolbar-owned viewport preset. */
|
|
31
|
+
viewportId: ViewportId;
|
|
32
|
+
/**
|
|
33
|
+
* Base-prefixed URL of the preview route (the host's `withBase` applied to
|
|
34
|
+
* `routes.componentsPreview`). Defaults to the unprefixed PREVIEW_ROUTE_PATH.
|
|
35
|
+
*/
|
|
36
|
+
previewUrl?: string;
|
|
37
|
+
}
|
|
38
|
+
declare function VariantFrame(props: VariantFrameProps): JSX.Element;
|
|
39
|
+
declare namespace VariantFrame {
|
|
40
|
+
var displayName: string;
|
|
41
|
+
}
|
|
42
|
+
export default VariantFrame;
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from "preact/jsx-runtime";
|
|
3
|
+
import { useEffect, useMemo, useRef, useState } from "preact/hooks";
|
|
4
|
+
import { onAfterNavigate } from "@takazudo/zudo-doc/transitions";
|
|
5
|
+
import {
|
|
6
|
+
MSG_REQUEST_READY,
|
|
7
|
+
MSG_SET_THEME,
|
|
8
|
+
MSG_UPDATE_PROPS,
|
|
9
|
+
isHeightMessage,
|
|
10
|
+
isReadyMessage
|
|
11
|
+
} from "./messages.js";
|
|
12
|
+
import { PREVIEW_ROUTE_PATH } from "./route.js";
|
|
13
|
+
import {
|
|
14
|
+
registerPreviewIframe,
|
|
15
|
+
unregisterPreviewIframe
|
|
16
|
+
} from "../token-tweak/preview-iframe-registry.js";
|
|
17
|
+
const VIEWPORTS = [
|
|
18
|
+
{ id: "mobile", label: "Mobile", width: "320px" },
|
|
19
|
+
{ id: "tablet", label: "Tablet", width: "768px" },
|
|
20
|
+
{ id: "desktop", label: "Desktop", width: "1280px" },
|
|
21
|
+
{ id: "full", label: "Full", width: "100%" }
|
|
22
|
+
];
|
|
23
|
+
const DEFAULT_VIEWPORT_ID = "full";
|
|
24
|
+
const THEME_OPTIONS = [
|
|
25
|
+
{ id: "follow", label: "Follow catalog" },
|
|
26
|
+
{ id: "light", label: "Light" },
|
|
27
|
+
{ id: "dark", label: "Dark" }
|
|
28
|
+
];
|
|
29
|
+
const DEFAULT_THEME_MODE = "follow";
|
|
30
|
+
function viewportWidth(id) {
|
|
31
|
+
const preset = VIEWPORTS.find((v) => v.id === id) ?? VIEWPORTS.at(-1);
|
|
32
|
+
return preset?.width ?? "100%";
|
|
33
|
+
}
|
|
34
|
+
function readCatalogTheme() {
|
|
35
|
+
const theme = document.documentElement.getAttribute("data-theme");
|
|
36
|
+
return theme === "light" || theme === "dark" ? theme : null;
|
|
37
|
+
}
|
|
38
|
+
function VariantFrame(props) {
|
|
39
|
+
const {
|
|
40
|
+
slug,
|
|
41
|
+
exportName,
|
|
42
|
+
name,
|
|
43
|
+
controls,
|
|
44
|
+
themeMode,
|
|
45
|
+
viewportId,
|
|
46
|
+
previewUrl = PREVIEW_ROUTE_PATH
|
|
47
|
+
} = props;
|
|
48
|
+
const iframeRef = useRef(null);
|
|
49
|
+
const readyRef = useRef(false);
|
|
50
|
+
const themeModeRef = useRef(themeMode);
|
|
51
|
+
themeModeRef.current = themeMode;
|
|
52
|
+
const [height, setHeight] = useState(180);
|
|
53
|
+
const src = useMemo(
|
|
54
|
+
() => `${previewUrl}?slug=${encodeURIComponent(slug)}&variant=${encodeURIComponent(exportName)}`,
|
|
55
|
+
[previewUrl, slug, exportName]
|
|
56
|
+
);
|
|
57
|
+
function sendTheme(theme) {
|
|
58
|
+
iframeRef.current?.contentWindow?.postMessage(
|
|
59
|
+
{ type: MSG_SET_THEME, theme },
|
|
60
|
+
"*"
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
function syncTheme() {
|
|
64
|
+
if (!readyRef.current) return;
|
|
65
|
+
const mode = themeModeRef.current;
|
|
66
|
+
const theme = mode === "follow" ? readCatalogTheme() : mode;
|
|
67
|
+
if (theme) sendTheme(theme);
|
|
68
|
+
}
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
function onMessage(e) {
|
|
71
|
+
if (e.source !== iframeRef.current?.contentWindow) return;
|
|
72
|
+
if (isReadyMessage(e.data)) {
|
|
73
|
+
readyRef.current = true;
|
|
74
|
+
syncTheme();
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (isHeightMessage(e.data)) {
|
|
78
|
+
setHeight(Math.max(80, Math.ceil(e.data.height)));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
window.addEventListener("message", onMessage);
|
|
82
|
+
iframeRef.current?.contentWindow?.postMessage(
|
|
83
|
+
{ type: MSG_REQUEST_READY },
|
|
84
|
+
"*"
|
|
85
|
+
);
|
|
86
|
+
return () => window.removeEventListener("message", onMessage);
|
|
87
|
+
}, []);
|
|
88
|
+
useEffect(() => {
|
|
89
|
+
syncTheme();
|
|
90
|
+
}, [themeMode]);
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
const observer = new MutationObserver((records) => {
|
|
93
|
+
if (records.some((record) => record.attributeName === "data-theme") && themeModeRef.current === "follow") {
|
|
94
|
+
syncTheme();
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
observer.observe(document.documentElement, {
|
|
98
|
+
attributes: true,
|
|
99
|
+
attributeFilter: ["data-theme"]
|
|
100
|
+
});
|
|
101
|
+
const unsubscribeAfterNavigate = onAfterNavigate(() => {
|
|
102
|
+
if (themeModeRef.current === "follow") syncTheme();
|
|
103
|
+
});
|
|
104
|
+
return () => {
|
|
105
|
+
observer.disconnect();
|
|
106
|
+
unsubscribeAfterNavigate();
|
|
107
|
+
};
|
|
108
|
+
}, []);
|
|
109
|
+
useEffect(() => {
|
|
110
|
+
const iframe = iframeRef.current;
|
|
111
|
+
if (!iframe) return;
|
|
112
|
+
registerPreviewIframe(iframe);
|
|
113
|
+
return () => unregisterPreviewIframe(iframe);
|
|
114
|
+
}, []);
|
|
115
|
+
function sendProps(props2) {
|
|
116
|
+
iframeRef.current?.contentWindow?.postMessage(
|
|
117
|
+
{ type: MSG_UPDATE_PROPS, props: props2 },
|
|
118
|
+
"*"
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
return /* @__PURE__ */ jsxs("section", { class: "border border-border rounded-md overflow-hidden bg-surface", children: [
|
|
122
|
+
/* @__PURE__ */ jsx("div", { class: "px-hsp-md py-vsp-2xs border-b border-border bg-surface-2", children: /* @__PURE__ */ jsx("span", { class: "text-caption leading-normal font-medium text-fg", children: name }) }),
|
|
123
|
+
/* @__PURE__ */ jsx(
|
|
124
|
+
"div",
|
|
125
|
+
{
|
|
126
|
+
role: "region",
|
|
127
|
+
"aria-label": "Preview viewport canvas",
|
|
128
|
+
tabIndex: 0,
|
|
129
|
+
class: "flex overflow-x-auto bg-bg p-hsp-md focus-visible:outline focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-focus",
|
|
130
|
+
children: /* @__PURE__ */ jsx("div", { class: "mx-auto shrink-0", style: { width: viewportWidth(viewportId) }, children: /* @__PURE__ */ jsx(
|
|
131
|
+
"iframe",
|
|
132
|
+
{
|
|
133
|
+
ref: iframeRef,
|
|
134
|
+
src,
|
|
135
|
+
title: `${slug} \u2014 ${name}`,
|
|
136
|
+
loading: "lazy",
|
|
137
|
+
sandbox: "allow-same-origin allow-scripts allow-forms",
|
|
138
|
+
style: {
|
|
139
|
+
width: "100%",
|
|
140
|
+
height: `${height}px`,
|
|
141
|
+
// An iframe's layout viewport is its content box. Keep the
|
|
142
|
+
// border at zero so a 1280px preset reaches the 1280px
|
|
143
|
+
// breakpoint; a visible border belongs on the wrapper.
|
|
144
|
+
border: "0",
|
|
145
|
+
display: "block"
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
) })
|
|
149
|
+
}
|
|
150
|
+
),
|
|
151
|
+
controls && controls.length > 0 && /* @__PURE__ */ jsx("div", { class: "border-t border-border px-hsp-md py-vsp-xs", children: /* @__PURE__ */ jsx(ControlsPanel, { controls, onChange: sendProps }) })
|
|
152
|
+
] });
|
|
153
|
+
}
|
|
154
|
+
function seedState(controls) {
|
|
155
|
+
const out = {};
|
|
156
|
+
for (const c of controls) out[c.prop] = c.defaultValue;
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
function coerceNumber(raw, control) {
|
|
160
|
+
const n = Number(raw);
|
|
161
|
+
if (Number.isNaN(n)) return control.defaultValue;
|
|
162
|
+
let v = n;
|
|
163
|
+
if (control.min !== void 0) v = Math.max(control.min, v);
|
|
164
|
+
if (control.max !== void 0) v = Math.min(control.max, v);
|
|
165
|
+
return v;
|
|
166
|
+
}
|
|
167
|
+
function ControlsPanel({
|
|
168
|
+
controls,
|
|
169
|
+
onChange
|
|
170
|
+
}) {
|
|
171
|
+
const [values, setValues] = useState(
|
|
172
|
+
() => seedState(controls)
|
|
173
|
+
);
|
|
174
|
+
const [open, setOpen] = useState(true);
|
|
175
|
+
function apply(next) {
|
|
176
|
+
setValues(next);
|
|
177
|
+
onChange(next);
|
|
178
|
+
}
|
|
179
|
+
function set(prop, value) {
|
|
180
|
+
apply({ ...values, [prop]: value });
|
|
181
|
+
}
|
|
182
|
+
function reset() {
|
|
183
|
+
apply(seedState(controls));
|
|
184
|
+
}
|
|
185
|
+
return /* @__PURE__ */ jsxs("div", { class: "flex flex-col gap-vsp-2xs", children: [
|
|
186
|
+
/* @__PURE__ */ jsxs("div", { class: "flex items-center justify-between gap-hsp-md", children: [
|
|
187
|
+
/* @__PURE__ */ jsxs(
|
|
188
|
+
"button",
|
|
189
|
+
{
|
|
190
|
+
type: "button",
|
|
191
|
+
onClick: () => setOpen((o) => !o),
|
|
192
|
+
"aria-expanded": open,
|
|
193
|
+
class: "flex items-center gap-hsp-2xs text-caption leading-normal uppercase tracking-wide text-muted hover:text-fg cursor-pointer",
|
|
194
|
+
children: [
|
|
195
|
+
/* @__PURE__ */ jsx("span", { "aria-hidden": "true", children: open ? "\u25BC" : "\u25B6" }),
|
|
196
|
+
"Controls"
|
|
197
|
+
]
|
|
198
|
+
}
|
|
199
|
+
),
|
|
200
|
+
open && /* @__PURE__ */ jsx(
|
|
201
|
+
"button",
|
|
202
|
+
{
|
|
203
|
+
type: "button",
|
|
204
|
+
onClick: reset,
|
|
205
|
+
class: "text-caption leading-normal rounded-sm border border-border px-hsp-xs py-vsp-3xs text-muted hover:text-fg transition-colors cursor-pointer",
|
|
206
|
+
children: "Reset"
|
|
207
|
+
}
|
|
208
|
+
)
|
|
209
|
+
] }),
|
|
210
|
+
open && /* @__PURE__ */ jsx("div", { class: "flex flex-wrap gap-hsp-md", children: controls.map((control) => /* @__PURE__ */ jsxs("label", { class: "flex items-center gap-hsp-2xs text-small text-fg", children: [
|
|
211
|
+
/* @__PURE__ */ jsx("span", { class: "text-muted", children: control.label }),
|
|
212
|
+
control.type === "select" && /* @__PURE__ */ jsx(
|
|
213
|
+
"select",
|
|
214
|
+
{
|
|
215
|
+
class: "border border-border rounded-sm bg-surface px-hsp-2xs py-vsp-3xs text-small",
|
|
216
|
+
value: values[control.prop],
|
|
217
|
+
onChange: (e) => set(control.prop, e.target.value),
|
|
218
|
+
children: control.options.map((opt) => /* @__PURE__ */ jsx("option", { value: opt, children: opt }))
|
|
219
|
+
}
|
|
220
|
+
),
|
|
221
|
+
control.type === "boolean" && /* @__PURE__ */ jsx(
|
|
222
|
+
"input",
|
|
223
|
+
{
|
|
224
|
+
type: "checkbox",
|
|
225
|
+
checked: values[control.prop],
|
|
226
|
+
onChange: (e) => set(control.prop, e.target.checked)
|
|
227
|
+
}
|
|
228
|
+
),
|
|
229
|
+
control.type === "text" && /* @__PURE__ */ jsx(
|
|
230
|
+
"input",
|
|
231
|
+
{
|
|
232
|
+
type: "text",
|
|
233
|
+
value: values[control.prop],
|
|
234
|
+
class: "border border-border rounded-sm bg-surface px-hsp-2xs py-vsp-3xs text-small",
|
|
235
|
+
onInput: (e) => set(control.prop, e.target.value)
|
|
236
|
+
}
|
|
237
|
+
),
|
|
238
|
+
control.type === "number" && (control.ui === "input" ? /* @__PURE__ */ jsx(
|
|
239
|
+
"input",
|
|
240
|
+
{
|
|
241
|
+
type: "number",
|
|
242
|
+
value: values[control.prop],
|
|
243
|
+
min: control.min,
|
|
244
|
+
max: control.max,
|
|
245
|
+
step: control.step,
|
|
246
|
+
class: "w-[5rem] border border-border rounded-sm bg-surface px-hsp-2xs py-vsp-3xs text-small",
|
|
247
|
+
onInput: (e) => set(
|
|
248
|
+
control.prop,
|
|
249
|
+
coerceNumber(e.target.value, control)
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
) : /* @__PURE__ */ jsxs("span", { class: "flex items-center gap-hsp-2xs", children: [
|
|
253
|
+
/* @__PURE__ */ jsx(
|
|
254
|
+
"input",
|
|
255
|
+
{
|
|
256
|
+
type: "range",
|
|
257
|
+
value: values[control.prop],
|
|
258
|
+
min: control.min,
|
|
259
|
+
max: control.max,
|
|
260
|
+
step: control.step,
|
|
261
|
+
onInput: (e) => set(
|
|
262
|
+
control.prop,
|
|
263
|
+
coerceNumber(e.target.value, control)
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
),
|
|
267
|
+
/* @__PURE__ */ jsx("span", { class: "text-muted tabular-nums w-[2.5rem] text-right", children: String(values[control.prop]) })
|
|
268
|
+
] })),
|
|
269
|
+
control.type === "color" && /* @__PURE__ */ jsx(
|
|
270
|
+
"input",
|
|
271
|
+
{
|
|
272
|
+
type: "color",
|
|
273
|
+
value: values[control.prop],
|
|
274
|
+
class: "h-[1.5rem] w-[2.5rem] border border-border rounded-sm bg-surface",
|
|
275
|
+
onInput: (e) => set(control.prop, e.target.value)
|
|
276
|
+
}
|
|
277
|
+
)
|
|
278
|
+
] })) })
|
|
279
|
+
] });
|
|
280
|
+
}
|
|
281
|
+
VariantFrame.displayName = "VariantFrame";
|
|
282
|
+
var variant_frame_default = VariantFrame;
|
|
283
|
+
export {
|
|
284
|
+
DEFAULT_THEME_MODE,
|
|
285
|
+
DEFAULT_VIEWPORT_ID,
|
|
286
|
+
THEME_OPTIONS,
|
|
287
|
+
VIEWPORTS,
|
|
288
|
+
variant_frame_default as default
|
|
289
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Attribute carried by the preview document's `<html>`. */
|
|
2
|
+
export declare const PREVIEW_DOC_ATTRIBUTE = "data-sg-preview-doc";
|
|
3
|
+
export interface CompilePreviewCssOptions {
|
|
4
|
+
/** Minify the output (default `true`). */
|
|
5
|
+
minify?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface CompilePreviewCssResult {
|
|
8
|
+
/** The compiled, rescoped, optimized stylesheet. */
|
|
9
|
+
css: string;
|
|
10
|
+
/**
|
|
11
|
+
* Absolute forward-slash paths of every CSS/JS file the compile read: the
|
|
12
|
+
* entry first, then each `onDependency` file (imports, plugins, configs).
|
|
13
|
+
* Deduplicated. A change to any of these requires a recompile.
|
|
14
|
+
*/
|
|
15
|
+
dependencies: string[];
|
|
16
|
+
/**
|
|
17
|
+
* Absolute forward-slash paths of the content files the candidate scan
|
|
18
|
+
* read (the `@source` globs). Editing one can add/remove utilities, so a
|
|
19
|
+
* dev watcher should treat them as recompile triggers as well.
|
|
20
|
+
*/
|
|
21
|
+
sourceFiles: string[];
|
|
22
|
+
/** Number of utility candidates the scan produced. */
|
|
23
|
+
candidateCount: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* zfb injects the host's global stylesheet into every route AFTER the preview
|
|
27
|
+
* `<link>` with no per-route opt-out, and both worlds define colliding
|
|
28
|
+
* `--color-*` tokens. Rescoping the preview world's token roots to
|
|
29
|
+
* `:root[data-sg-preview-doc]` (specificity 0,1,1 vs 0,1,0) keeps the preview
|
|
30
|
+
* tokens winning regardless of link order. Must run on the unminified
|
|
31
|
+
* `build()` output — `optimize` collapses the whitespace these patterns match.
|
|
32
|
+
*/
|
|
33
|
+
export declare function scopeTokensToPreviewDoc(css: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Compile a host preview stylesheet entry.
|
|
36
|
+
*
|
|
37
|
+
* @param entryAbsPath absolute path to the entry CSS file (resolve the host's
|
|
38
|
+
* project-root-relative `previewStyles` option before calling).
|
|
39
|
+
*/
|
|
40
|
+
export declare function compilePreviewCss(entryAbsPath: string, options?: CompilePreviewCssOptions): Promise<CompilePreviewCssResult>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, resolve } from "node:path";
|
|
3
|
+
import { compile, optimize } from "@tailwindcss/node";
|
|
4
|
+
import { Scanner } from "@tailwindcss/oxide";
|
|
5
|
+
import { toForwardSlash } from "../host-paths.js";
|
|
6
|
+
const PREVIEW_DOC_ATTRIBUTE = "data-sg-preview-doc";
|
|
7
|
+
function scopeTokensToPreviewDoc(css) {
|
|
8
|
+
const scoped = `:root[${PREVIEW_DOC_ATTRIBUTE}]`;
|
|
9
|
+
return css.replaceAll(":root, :host {", `${scoped}, :host {`).replaceAll(":root {", `${scoped} {`);
|
|
10
|
+
}
|
|
11
|
+
async function compilePreviewCss(entryAbsPath, options = {}) {
|
|
12
|
+
const { minify = true } = options;
|
|
13
|
+
const entry = resolve(entryAbsPath);
|
|
14
|
+
const input = await readFile(entry, "utf8");
|
|
15
|
+
const dependencies = /* @__PURE__ */ new Set([toForwardSlash(entry)]);
|
|
16
|
+
const compiler = await compile(input, {
|
|
17
|
+
base: dirname(entry),
|
|
18
|
+
from: entry,
|
|
19
|
+
onDependency: (path) => {
|
|
20
|
+
dependencies.add(toForwardSlash(resolve(path)));
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
const sources = compiler.sources.map((source) => ({ ...source }));
|
|
24
|
+
if (compiler.root && compiler.root !== "none") {
|
|
25
|
+
sources.push({ base: compiler.root.base, pattern: compiler.root.pattern, negated: false });
|
|
26
|
+
}
|
|
27
|
+
const scanner = new Scanner({ sources });
|
|
28
|
+
const candidates = scanner.scan();
|
|
29
|
+
const built = scopeTokensToPreviewDoc(compiler.build(candidates));
|
|
30
|
+
const css = optimize(built, { file: toForwardSlash(entry), minify }).code;
|
|
31
|
+
return {
|
|
32
|
+
css,
|
|
33
|
+
dependencies: [...dependencies],
|
|
34
|
+
sourceFiles: scanner.files.map((file) => toForwardSlash(file)),
|
|
35
|
+
candidateCount: candidates.length
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
PREVIEW_DOC_ATTRIBUTE,
|
|
40
|
+
compilePreviewCss,
|
|
41
|
+
scopeTokensToPreviewDoc
|
|
42
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Collection name of `componentsRoots[0]`; later roots append their index. */
|
|
2
|
+
export declare const COMPONENT_DOCS_COLLECTION = "componentDocs";
|
|
3
|
+
/** One components root's registry key prefix and its docs collection — `sgContext.componentDocs[i]`. */
|
|
4
|
+
export interface ComponentDocsRoot {
|
|
5
|
+
/** `storyModules` key prefix without `./` or a trailing slash, e.g. `"ui/src"`. */
|
|
6
|
+
keyPrefix: string;
|
|
7
|
+
/** Content collection holding this root's MDX docs, e.g. `"componentDocs"`. */
|
|
8
|
+
collection: string;
|
|
9
|
+
}
|
|
10
|
+
/** A resolved doc lookup: `getEntry(collection, slug)`. */
|
|
11
|
+
export interface ComponentDocRef {
|
|
12
|
+
collection: string;
|
|
13
|
+
slug: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* `packages/demo-ui/src` → `demo-ui/src` — the `storyModules` map key prefix for a
|
|
17
|
+
* components root. Strips a leading `./`, a leading `packages/` segment (the
|
|
18
|
+
* repo's workspace-package convention) and trailing slashes; a root outside
|
|
19
|
+
* `packages/` keys off its project-root-relative dir verbatim.
|
|
20
|
+
*/
|
|
21
|
+
export declare function deriveMapKeyPrefix(dir: string): string;
|
|
22
|
+
/** Collection name for `componentsRoots[index]`. */
|
|
23
|
+
export declare function componentDocsCollectionName(index: number): string;
|
|
24
|
+
/** The `sgContext.componentDocs` list for a `componentsRoots` array (same order as the collections). */
|
|
25
|
+
export declare function componentDocsRoots(roots: ReadonlyArray<{
|
|
26
|
+
dir: string;
|
|
27
|
+
}>): ComponentDocsRoot[];
|
|
28
|
+
/**
|
|
29
|
+
* Resolve the doc collection + slug for a story registry entry's `path`, from
|
|
30
|
+
* the components root its key belongs to (longest matching prefix wins, so a
|
|
31
|
+
* nested root is never shadowed by its parent). Returns `null` when no root
|
|
32
|
+
* matches or the path is not the story key shape.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* resolveComponentDoc("./ui/src/button/button.stories.tsx", [{ keyPrefix: "ui/src", collection: "componentDocs" }])
|
|
36
|
+
* // { collection: "componentDocs", slug: "button/button" }
|
|
37
|
+
*/
|
|
38
|
+
export declare function resolveComponentDoc(storyPath: string, roots: ReadonlyArray<ComponentDocsRoot>): ComponentDocRef | null;
|