@histoire/plugin-react 0.0.0-stage → 1.0.0-beta.3
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/LICENSE +21 -0
- package/assets/histoire-react-text.svg +1 -0
- package/assets/histoire-react.svg +1 -0
- package/dist/client/host.d.ts +7 -0
- package/dist/client/host.js +72 -0
- package/dist/client/index.d.ts +4 -0
- package/dist/client/index.js +6 -0
- package/dist/client/mount.d.ts +21 -0
- package/dist/client/mount.js +17 -0
- package/dist/client/render.d.ts +39 -0
- package/dist/client/render.js +27 -0
- package/dist/client/state.d.ts +2 -0
- package/dist/client/state.js +28 -0
- package/dist/collect/index.d.ts +6 -0
- package/dist/collect/index.js +41 -0
- package/dist/components/Story.d.ts +3 -0
- package/dist/components/Story.js +52 -0
- package/dist/components/Variant.d.ts +3 -0
- package/dist/components/Variant.js +32 -0
- package/dist/components/context.d.ts +31 -0
- package/dist/components/context.js +11 -0
- package/dist/controls/index.d.ts +3 -0
- package/dist/controls/index.js +5 -0
- package/dist/controls/wrap-vue-control.d.ts +12 -0
- package/dist/controls/wrap-vue-control.js +63 -0
- package/dist/helpers.d.ts +24 -0
- package/dist/helpers.js +6 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/index.node.d.ts +4 -0
- package/dist/index.node.js +85 -0
- package/dist/types.d.ts +24 -0
- package/dist/util/initial-state.d.ts +7 -0
- package/dist/util/initial-state.js +60 -0
- package/dist/util/root.d.ts +12 -0
- package/dist/util/root.js +71 -0
- package/dist/util/setup.d.ts +3 -0
- package/dist/util/setup.js +20 -0
- package/dist/util/variant.d.ts +5 -0
- package/dist/util/variant.js +38 -0
- package/dist/util/vite.d.ts +2 -0
- package/dist/util/vite.js +8 -0
- package/package.json +94 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Guillaume Chau
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 64"><g fill="none" stroke="#0ea5e9" stroke-width="3"><ellipse cx="32" cy="32" rx="29" ry="11"/><ellipse cx="32" cy="32" rx="29" ry="11" transform="rotate(60 32 32)"/><ellipse cx="32" cy="32" rx="29" ry="11" transform="rotate(120 32 32)"/></g><circle cx="32" cy="32" r="5" fill="#0ea5e9"/><text x="76" y="44" font-family="sans-serif" font-size="36" font-weight="600" fill="#0ea5e9">Histoire</text></svg>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><g fill="none" stroke="#0ea5e9" stroke-width="3"><ellipse cx="32" cy="32" rx="29" ry="11"/><ellipse cx="32" cy="32" rx="29" ry="11" transform="rotate(60 32 32)"/><ellipse cx="32" cy="32" rx="29" ry="11" transform="rotate(120 32 32)"/></g><circle cx="32" cy="32" r="5" fill="#0ea5e9"/></svg>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Story, Variant } from '@histoire/shared';
|
|
2
|
+
/** Bind a Vue adapter to a React root, cancelling stale asynchronous mounts. */
|
|
3
|
+
export declare function createReactAdapter(props: {
|
|
4
|
+
story: Story;
|
|
5
|
+
variant?: Variant;
|
|
6
|
+
slotName?: string;
|
|
7
|
+
}, mode: 'mount' | 'render', emit: (event: 'ready') => void): import("vue").Ref<HTMLDivElement | undefined, HTMLDivElement | undefined>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { createReactHost } from "../util/root.js";
|
|
2
|
+
import { callSetupFunctions } from "../util/setup.js";
|
|
3
|
+
import { onBeforeUnmount, onMounted, ref, watch } from "@histoire/vendors/vue";
|
|
4
|
+
import { useHistoireGlobals } from "@histoire/shared";
|
|
5
|
+
// src/client/host.ts
|
|
6
|
+
/** Bind a Vue adapter to a React root, cancelling stale asynchronous mounts. */
|
|
7
|
+
function createReactAdapter(props, mode, emit) {
|
|
8
|
+
const el = ref();
|
|
9
|
+
let dispose;
|
|
10
|
+
/** Replace current root and publish readiness only for this captured mount. */
|
|
11
|
+
async function mount() {
|
|
12
|
+
dispose?.();
|
|
13
|
+
const story = props.story;
|
|
14
|
+
const variant = props.variant;
|
|
15
|
+
const target = document.createElement("div");
|
|
16
|
+
el.value.append(target);
|
|
17
|
+
const host = createReactHost(target);
|
|
18
|
+
let active = true;
|
|
19
|
+
dispose = () => {
|
|
20
|
+
active = false;
|
|
21
|
+
host.destroy();
|
|
22
|
+
target.remove();
|
|
23
|
+
};
|
|
24
|
+
const pending = [];
|
|
25
|
+
const wrappers = [];
|
|
26
|
+
const api = {
|
|
27
|
+
app: host.app,
|
|
28
|
+
story,
|
|
29
|
+
variant: variant ?? null,
|
|
30
|
+
globals: useHistoireGlobals(),
|
|
31
|
+
/** Setup chains share preview ownership, independent of configuration root. */
|
|
32
|
+
isActive: () => active,
|
|
33
|
+
/** Accept providers only while this root still owns its target. */
|
|
34
|
+
addWrapper: (wrapper) => {
|
|
35
|
+
if (active) wrappers.push(wrapper);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
try {
|
|
39
|
+
await callSetupFunctions(api, target);
|
|
40
|
+
if (!active) return;
|
|
41
|
+
host.render(story.file.component, {
|
|
42
|
+
mode,
|
|
43
|
+
story,
|
|
44
|
+
variant,
|
|
45
|
+
slotName: props.slotName ?? "default",
|
|
46
|
+
pending,
|
|
47
|
+
isActive: () => active
|
|
48
|
+
}, wrappers);
|
|
49
|
+
await Promise.all(pending);
|
|
50
|
+
if (active) emit("ready");
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (active) {
|
|
53
|
+
dispose?.();
|
|
54
|
+
dispose = void 0;
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
onMounted(mount);
|
|
60
|
+
watch(() => [
|
|
61
|
+
props.story,
|
|
62
|
+
props.story.file?.component,
|
|
63
|
+
props.variant,
|
|
64
|
+
props.slotName
|
|
65
|
+
], mount);
|
|
66
|
+
onBeforeUnmount(() => {
|
|
67
|
+
dispose?.();
|
|
68
|
+
dispose = void 0;
|
|
69
|
+
});
|
|
70
|
+
return el;
|
|
71
|
+
}
|
|
72
|
+
export { createReactAdapter };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import mount_default from "./mount.js";
|
|
2
|
+
import render_default from "./render.js";
|
|
3
|
+
// src/client/index.ts
|
|
4
|
+
/** Explicit source props provide source code until React code generation exists. */
|
|
5
|
+
function generateSourceCode() {}
|
|
6
|
+
export { mount_default as MountStory, render_default as RenderStory, generateSourceCode };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Story } from '@histoire/shared';
|
|
2
|
+
import type { PropType } from '@histoire/vendors/vue';
|
|
3
|
+
/** Hidden configuration adapter matching Histoire's MountStory contract. */
|
|
4
|
+
declare const _default: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
5
|
+
story: {
|
|
6
|
+
type: PropType<Story>;
|
|
7
|
+
required: true;
|
|
8
|
+
};
|
|
9
|
+
}>, {
|
|
10
|
+
el: import("vue").Ref<HTMLDivElement | undefined, HTMLDivElement | undefined>;
|
|
11
|
+
}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
12
|
+
ready: () => true;
|
|
13
|
+
}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
14
|
+
story: {
|
|
15
|
+
type: PropType<Story>;
|
|
16
|
+
required: true;
|
|
17
|
+
};
|
|
18
|
+
}>> & Readonly<{
|
|
19
|
+
onReady?: (() => any) | undefined;
|
|
20
|
+
}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
21
|
+
export default _default;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createReactAdapter } from "./host.js";
|
|
2
|
+
import { defineComponent, h } from "@histoire/vendors/vue";
|
|
3
|
+
// src/client/mount.ts
|
|
4
|
+
/** Hidden configuration adapter matching Histoire's MountStory contract. */
|
|
5
|
+
var mount_default = defineComponent({
|
|
6
|
+
name: "MountStory",
|
|
7
|
+
props: { story: {
|
|
8
|
+
type: Object,
|
|
9
|
+
required: true
|
|
10
|
+
} },
|
|
11
|
+
emits: { ready: () => true },
|
|
12
|
+
setup: (props, { emit }) => ({ el: createReactAdapter(props, "mount", emit) }),
|
|
13
|
+
render() {
|
|
14
|
+
return h("div", { ref: "el" });
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
export { mount_default as default };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Story, Variant } from '@histoire/shared';
|
|
2
|
+
import type { PropType } from '@histoire/vendors/vue';
|
|
3
|
+
/** Preview adapter rendering selected default or controls slot. */
|
|
4
|
+
declare const _default: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
|
|
5
|
+
story: {
|
|
6
|
+
type: PropType<Story>;
|
|
7
|
+
required: true;
|
|
8
|
+
};
|
|
9
|
+
variant: {
|
|
10
|
+
type: PropType<Variant>;
|
|
11
|
+
required: true;
|
|
12
|
+
};
|
|
13
|
+
slotName: {
|
|
14
|
+
type: StringConstructor;
|
|
15
|
+
default: string;
|
|
16
|
+
};
|
|
17
|
+
}>, {
|
|
18
|
+
el: import("vue").Ref<HTMLDivElement | undefined, HTMLDivElement | undefined>;
|
|
19
|
+
}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
|
|
20
|
+
ready: () => true;
|
|
21
|
+
}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
|
|
22
|
+
story: {
|
|
23
|
+
type: PropType<Story>;
|
|
24
|
+
required: true;
|
|
25
|
+
};
|
|
26
|
+
variant: {
|
|
27
|
+
type: PropType<Variant>;
|
|
28
|
+
required: true;
|
|
29
|
+
};
|
|
30
|
+
slotName: {
|
|
31
|
+
type: StringConstructor;
|
|
32
|
+
default: string;
|
|
33
|
+
};
|
|
34
|
+
}>> & Readonly<{
|
|
35
|
+
onReady?: (() => any) | undefined;
|
|
36
|
+
}>, {
|
|
37
|
+
slotName: string;
|
|
38
|
+
}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
39
|
+
export default _default;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createReactAdapter } from "./host.js";
|
|
2
|
+
import { defineComponent, h } from "@histoire/vendors/vue";
|
|
3
|
+
// src/client/render.ts
|
|
4
|
+
/** Preview adapter rendering selected default or controls slot. */
|
|
5
|
+
var render_default = defineComponent({
|
|
6
|
+
name: "RenderStory",
|
|
7
|
+
props: {
|
|
8
|
+
story: {
|
|
9
|
+
type: Object,
|
|
10
|
+
required: true
|
|
11
|
+
},
|
|
12
|
+
variant: {
|
|
13
|
+
type: Object,
|
|
14
|
+
required: true
|
|
15
|
+
},
|
|
16
|
+
slotName: {
|
|
17
|
+
type: String,
|
|
18
|
+
default: "default"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
emits: { ready: () => true },
|
|
22
|
+
setup: (props, { emit }) => ({ el: createReactAdapter(props, "render", emit) }),
|
|
23
|
+
render() {
|
|
24
|
+
return h("div", { ref: "el" });
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
export { render_default as default };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useMemo, useSyncExternalStore } from "react";
|
|
2
|
+
import { watch } from "@histoire/vendors/vue";
|
|
3
|
+
// src/client/state.ts
|
|
4
|
+
/** Subscribe React to deep Vue state mutations using a stable numeric snapshot. */
|
|
5
|
+
function useVariantState(state) {
|
|
6
|
+
const store = useMemo(() => {
|
|
7
|
+
let version = 0;
|
|
8
|
+
return {
|
|
9
|
+
/** Snapshot changes only when subscription starts or Vue reports a mutation. */
|
|
10
|
+
snapshot: () => version,
|
|
11
|
+
/** Vue watcher belongs to React's subscription lifetime. */
|
|
12
|
+
subscribe(listener) {
|
|
13
|
+
if (!state) return () => {};
|
|
14
|
+
return watch(state, () => {
|
|
15
|
+
version++;
|
|
16
|
+
listener();
|
|
17
|
+
}, {
|
|
18
|
+
deep: true,
|
|
19
|
+
flush: "sync",
|
|
20
|
+
immediate: true
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}, [state]);
|
|
25
|
+
useSyncExternalStore(store.subscribe, store.snapshot, store.snapshot);
|
|
26
|
+
return state ?? {};
|
|
27
|
+
}
|
|
28
|
+
export { useVariantState };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ServerRunPayload } from '@histoire/shared';
|
|
2
|
+
import type { ComponentType } from 'react';
|
|
3
|
+
/** Collect metadata without rendering preview content; always release the root. */
|
|
4
|
+
export declare function collectStory(component: ComponentType, payload: ServerRunPayload): Promise<void>;
|
|
5
|
+
/** Histoire collection worker entry. */
|
|
6
|
+
export declare function run(payload: ServerRunPayload): Promise<void>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createReactHost } from "../util/root.js";
|
|
2
|
+
import { callSetupFunctions } from "../util/setup.js";
|
|
3
|
+
// src/collect/index.ts
|
|
4
|
+
/** Collect metadata without rendering preview content; always release the root. */
|
|
5
|
+
async function collectStory(component, payload) {
|
|
6
|
+
const host = createReactHost(payload.el);
|
|
7
|
+
const wrappers = [];
|
|
8
|
+
let active = true;
|
|
9
|
+
try {
|
|
10
|
+
await callSetupFunctions({
|
|
11
|
+
app: host.app,
|
|
12
|
+
story: null,
|
|
13
|
+
variant: null,
|
|
14
|
+
/** Collection setup owns only this temporary root. */
|
|
15
|
+
isActive: () => active,
|
|
16
|
+
/** Registration must return void, never Array.push's numeric result. */
|
|
17
|
+
addWrapper: (wrapper) => {
|
|
18
|
+
wrappers.push(wrapper);
|
|
19
|
+
}
|
|
20
|
+
}, payload.el);
|
|
21
|
+
host.render(component, {
|
|
22
|
+
mode: "collect",
|
|
23
|
+
collection: payload,
|
|
24
|
+
pending: [],
|
|
25
|
+
isActive: () => active
|
|
26
|
+
}, wrappers);
|
|
27
|
+
} finally {
|
|
28
|
+
active = false;
|
|
29
|
+
host.destroy();
|
|
30
|
+
if (typeof setImmediate === "function") await new Promise((resolve) => setImmediate(resolve));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** Histoire collection worker entry. */
|
|
34
|
+
async function run(payload) {
|
|
35
|
+
const { default: component } = await import(
|
|
36
|
+
/* @vite-ignore */
|
|
37
|
+
payload.file.moduleId
|
|
38
|
+
);
|
|
39
|
+
await collectStory(component, payload);
|
|
40
|
+
}
|
|
41
|
+
export { collectStory, run };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { HstContext, useHstContext } from "./context.js";
|
|
2
|
+
import { Variant } from "./Variant.js";
|
|
3
|
+
import { Children, isValidElement } from "react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/components/Story.tsx
|
|
6
|
+
/** Find variant declarations inside fragments and element children without executing content. */
|
|
7
|
+
function findVariants(children) {
|
|
8
|
+
const result = [];
|
|
9
|
+
Children.forEach(children, (child) => {
|
|
10
|
+
if (!isValidElement(child)) return;
|
|
11
|
+
if (child.type === Variant) result.push(child);
|
|
12
|
+
else if (child.props.children) result.push(...findVariants(child.props.children));
|
|
13
|
+
});
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
/** Declare story metadata, configure variants, or display selected variant. */
|
|
17
|
+
function Story(props) {
|
|
18
|
+
const context = useHstContext();
|
|
19
|
+
const variants = typeof props.children === "function" ? [] : findVariants(props.children);
|
|
20
|
+
const implicit = variants.length === 0;
|
|
21
|
+
const declarations = implicit ? [/* @__PURE__ */ jsx(Variant, { ...props }, "_default")] : variants;
|
|
22
|
+
let collectedStory = context.collectedStory;
|
|
23
|
+
if (context.mode === "collect") {
|
|
24
|
+
const { file, storyData } = context.collection;
|
|
25
|
+
collectedStory = {
|
|
26
|
+
id: props.id ?? file.id,
|
|
27
|
+
title: props.title ?? file.fileName,
|
|
28
|
+
group: props.group,
|
|
29
|
+
layout: props.layout,
|
|
30
|
+
matrix: props.matrix,
|
|
31
|
+
icon: props.icon,
|
|
32
|
+
iconColor: props.iconColor,
|
|
33
|
+
docsOnly: props.docsOnly,
|
|
34
|
+
meta: props.meta,
|
|
35
|
+
variants: []
|
|
36
|
+
};
|
|
37
|
+
const index = storyData.findIndex((story) => story.id === collectedStory.id);
|
|
38
|
+
if (index === -1) storyData.push(collectedStory);
|
|
39
|
+
else storyData[index] = collectedStory;
|
|
40
|
+
}
|
|
41
|
+
return declarations.map((declaration, index) => /* @__PURE__ */ jsx(HstContext.Provider, {
|
|
42
|
+
value: {
|
|
43
|
+
...context,
|
|
44
|
+
collectedStory,
|
|
45
|
+
storyProps: props,
|
|
46
|
+
index,
|
|
47
|
+
implicit
|
|
48
|
+
},
|
|
49
|
+
children: declaration
|
|
50
|
+
}, declaration.key ?? index));
|
|
51
|
+
}
|
|
52
|
+
export { Story };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useHstContext } from "./context.js";
|
|
2
|
+
import { useVariantState } from "../client/state.js";
|
|
3
|
+
import { configureVariant } from "../util/variant.js";
|
|
4
|
+
import { useLayoutEffect } from "react";
|
|
5
|
+
// src/components/Variant.tsx
|
|
6
|
+
/** Declare one variant; render only its selected content or controls. */
|
|
7
|
+
function Variant(props) {
|
|
8
|
+
const context = useHstContext();
|
|
9
|
+
const variant = context.story?.variants[context.index ?? 0];
|
|
10
|
+
const state = useVariantState(variant?.state);
|
|
11
|
+
useLayoutEffect(() => {
|
|
12
|
+
if (context.mode === "collect") {
|
|
13
|
+
const story = context.collectedStory;
|
|
14
|
+
story.variants[context.index ?? 0] = {
|
|
15
|
+
id: context.implicit ? "_default" : props.id ?? `${story.id}-${context.index ?? 0}`,
|
|
16
|
+
title: context.implicit ? "default" : props.title ?? "untitled",
|
|
17
|
+
icon: props.icon,
|
|
18
|
+
iconColor: props.iconColor,
|
|
19
|
+
meta: context.implicit ? void 0 : props.meta
|
|
20
|
+
};
|
|
21
|
+
} else if (context.mode === "mount" && variant) context.pending.push(configureVariant(variant, props, context));
|
|
22
|
+
}, [
|
|
23
|
+
context,
|
|
24
|
+
variant,
|
|
25
|
+
props
|
|
26
|
+
]);
|
|
27
|
+
if (context.mode !== "render" || !variant || context.variant?.id !== variant.id) return null;
|
|
28
|
+
if (context.slotName === "controls") return (props.controls ?? context.storyProps?.controls)?.({ state }) ?? null;
|
|
29
|
+
if (context.slotName !== "default") return null;
|
|
30
|
+
return typeof props.children === "function" ? props.children({ state }) : props.children;
|
|
31
|
+
}
|
|
32
|
+
export { Variant };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ServerRunPayload, ServerStory, Story, Variant } from '@histoire/shared';
|
|
2
|
+
import type { StoryProps } from '../types.js';
|
|
3
|
+
/** Per-root mode and ownership, never shared between concurrent previews. */
|
|
4
|
+
export interface HstContextValue {
|
|
5
|
+
/** Adapter phase. */
|
|
6
|
+
mode: 'collect' | 'mount' | 'render';
|
|
7
|
+
/** Metadata collection payload. */
|
|
8
|
+
collection?: ServerRunPayload;
|
|
9
|
+
/** Story being configured or rendered. */
|
|
10
|
+
story?: Story;
|
|
11
|
+
/** Story being collected. */
|
|
12
|
+
collectedStory?: ServerStory;
|
|
13
|
+
/** Currently selected preview variant. */
|
|
14
|
+
variant?: Variant;
|
|
15
|
+
/** Slot requested by Histoire. */
|
|
16
|
+
slotName?: string;
|
|
17
|
+
/** Parent declaration supplying inherited defaults. */
|
|
18
|
+
storyProps?: StoryProps<any>;
|
|
19
|
+
/** Stable ordinal assigned by Story's declaration traversal. */
|
|
20
|
+
index?: number;
|
|
21
|
+
/** Marks implicit default declarations. */
|
|
22
|
+
implicit?: boolean;
|
|
23
|
+
/** Await asynchronous state initialization before emitting ready. */
|
|
24
|
+
pending: Promise<unknown>[];
|
|
25
|
+
/** Check captured root ownership after asynchronous setup. */
|
|
26
|
+
isActive: () => boolean;
|
|
27
|
+
}
|
|
28
|
+
/** Internal framework context. */
|
|
29
|
+
export declare const HstContext: import("react").Context<HstContextValue | null>;
|
|
30
|
+
/** Read adapter context and reject stories used outside Histoire. */
|
|
31
|
+
export declare function useHstContext(): HstContextValue;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
// src/components/context.tsx
|
|
3
|
+
/** Internal framework context. */
|
|
4
|
+
var HstContext = createContext(null);
|
|
5
|
+
/** Read adapter context and reject stories used outside Histoire. */
|
|
6
|
+
function useHstContext() {
|
|
7
|
+
const context = useContext(HstContext);
|
|
8
|
+
if (!context) throw new Error("React Story and Variant must render inside Histoire.");
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
11
|
+
export { HstContext, useHstContext };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** React wrappers for Histoire's built-in controls, including native Vue listeners. */
|
|
2
|
+
export declare const HstButton: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstButtonGroup: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstCheckbox: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstSwitch: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstCheckboxList: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstText: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstNumber: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstSlider: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstTextarea: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstSelect: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstRadio: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstJson: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstColorShades: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstTokenList: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstTokenGrid: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstCopyIcon: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, HstColorSelect: (props: import("./wrap-vue-control.js").ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
|
3
|
+
export type { ReactControlProps } from './wrap-vue-control.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { wrapVueControl } from "./wrap-vue-control.js";
|
|
2
|
+
import { components } from "@histoire/controls";
|
|
3
|
+
/** React wrappers for Histoire's built-in controls, including native Vue listeners. */
|
|
4
|
+
var { HstButton, HstButtonGroup, HstCheckbox, HstSwitch, HstCheckboxList, HstText, HstNumber, HstSlider, HstTextarea, HstSelect, HstRadio, HstJson, HstColorShades, HstTokenList, HstTokenGrid, HstCopyIcon, HstColorSelect } = Object.fromEntries(Object.entries(components).map(([name, component]) => [name, wrapVueControl(component)]));
|
|
5
|
+
export { HstButton, HstButtonGroup, HstCheckbox, HstCheckboxList, HstColorSelect, HstColorShades, HstCopyIcon, HstJson, HstNumber, HstRadio, HstSelect, HstSlider, HstSwitch, HstText, HstTextarea, HstTokenGrid, HstTokenList };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Component } from '@histoire/vendors/vue';
|
|
2
|
+
/** Common control props; additional props and Vue listeners pass through. */
|
|
3
|
+
export interface ReactControlProps {
|
|
4
|
+
/** Current value passed to Vue's modelValue. */
|
|
5
|
+
value?: any;
|
|
6
|
+
/** Receive Vue update:modelValue events. */
|
|
7
|
+
onChange?: (value: any) => void;
|
|
8
|
+
/** Control-specific props and event handlers. */
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
/** Mount one Vue control inside a React-owned host and mirror changing props. */
|
|
12
|
+
export declare function wrapVueControl(control: Component): (props: ReactControlProps) => import("react").DetailedReactHTMLElement<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { Children, Fragment, createElement, isValidElement, useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
import { createApp, defineComponent, h, onBeforeUnmount, onMounted, ref, shallowReactive, shallowRef } from "@histoire/vendors/vue";
|
|
3
|
+
import { configureControlsApp } from "@histoire/controls";
|
|
4
|
+
import { createPortal } from "react-dom";
|
|
5
|
+
// src/controls/wrap-vue-control.ts
|
|
6
|
+
/** Ignore empty React nodes, including fragments, so Vue can render slot fallbacks. */
|
|
7
|
+
function hasSlotContent(children) {
|
|
8
|
+
return Children.toArray(children).some((child) => {
|
|
9
|
+
if (isValidElement(child) && child.type === Fragment) return hasSlotContent(child.props.children);
|
|
10
|
+
return child !== "";
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
/** Mount one Vue control inside a React-owned host and mirror changing props. */
|
|
14
|
+
function wrapVueControl(control) {
|
|
15
|
+
/** React facade for a Vue control, sharing bundled Vue with Histoire. */
|
|
16
|
+
function WrappedControl(props) {
|
|
17
|
+
const { children } = props;
|
|
18
|
+
const target = useRef(null);
|
|
19
|
+
const [slotTargets, setSlotTargets] = useState([]);
|
|
20
|
+
const state = useRef(shallowReactive({}));
|
|
21
|
+
const slotsEnabled = useRef(shallowRef(false));
|
|
22
|
+
const nextSlotId = useRef(0);
|
|
23
|
+
useLayoutEffect(() => {
|
|
24
|
+
const { value, onChange, children, ...rest } = props;
|
|
25
|
+
for (const key of Object.keys(state.current)) delete state.current[key];
|
|
26
|
+
Object.assign(state.current, rest, {
|
|
27
|
+
"modelValue": value,
|
|
28
|
+
"onUpdate:modelValue": onChange
|
|
29
|
+
});
|
|
30
|
+
slotsEnabled.current.value = hasSlotContent(children);
|
|
31
|
+
});
|
|
32
|
+
useLayoutEffect(() => {
|
|
33
|
+
let active = true;
|
|
34
|
+
setSlotTargets([]);
|
|
35
|
+
const SlotHost = defineComponent({
|
|
36
|
+
/** Register each host without sharing a single ref across repeated slots. */
|
|
37
|
+
setup() {
|
|
38
|
+
const element = ref();
|
|
39
|
+
const id = nextSlotId.current++;
|
|
40
|
+
onMounted(() => {
|
|
41
|
+
if (active) setSlotTargets((targets) => [...targets, {
|
|
42
|
+
id,
|
|
43
|
+
element: element.value
|
|
44
|
+
}]);
|
|
45
|
+
});
|
|
46
|
+
onBeforeUnmount(() => {
|
|
47
|
+
if (active) setSlotTargets((targets) => targets.filter((target) => target.id !== id));
|
|
48
|
+
});
|
|
49
|
+
return () => h("span", { ref: element });
|
|
50
|
+
} });
|
|
51
|
+
const app = createApp({ render: () => h(control, state.current, slotsEnabled.current.value ? { default: () => h(SlotHost) } : void 0) });
|
|
52
|
+
configureControlsApp(app);
|
|
53
|
+
app.mount(target.current);
|
|
54
|
+
return () => {
|
|
55
|
+
active = false;
|
|
56
|
+
app.unmount();
|
|
57
|
+
};
|
|
58
|
+
}, []);
|
|
59
|
+
return createElement("div", { ref: target }, slotTargets.map(({ id, element }) => createPortal(children, element, String(id))));
|
|
60
|
+
}
|
|
61
|
+
return WrappedControl;
|
|
62
|
+
}
|
|
63
|
+
export { wrapVueControl };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Story, Variant } from '@histoire/shared';
|
|
2
|
+
import type { ComponentType, PropsWithChildren } from 'react';
|
|
3
|
+
import type { Root } from 'react-dom/client';
|
|
4
|
+
/** Provider component installed around a story tree. */
|
|
5
|
+
export type ReactStoryWrapper = ComponentType<PropsWithChildren>;
|
|
6
|
+
/** Setup payload shared by collection, hidden configuration, and preview roots. */
|
|
7
|
+
export interface ReactStorySetupApi {
|
|
8
|
+
/** React root owned by this mount. */
|
|
9
|
+
app: Root;
|
|
10
|
+
/** Story metadata, absent during collection. */
|
|
11
|
+
story: Story | null;
|
|
12
|
+
/** Selected variant, absent during collection and configuration. */
|
|
13
|
+
variant: Variant | null;
|
|
14
|
+
/** Current host-owned globals for isolated story document. */
|
|
15
|
+
globals?: import('@histoire/shared').HistoireGlobals;
|
|
16
|
+
/** Add a provider; first registered wrapper is outermost. */
|
|
17
|
+
addWrapper: (component: ReactStoryWrapper) => void;
|
|
18
|
+
/** Whether this captured root still owns its mount; absent in manual payloads. */
|
|
19
|
+
isActive?: () => boolean;
|
|
20
|
+
}
|
|
21
|
+
/** Setup may register providers or return one provider directly. */
|
|
22
|
+
export type ReactStorySetupHandler = (api: ReactStorySetupApi) => void | ReactStoryWrapper | Promise<void | ReactStoryWrapper>;
|
|
23
|
+
/** Preserve inferred handler types for setup files. */
|
|
24
|
+
export declare function defineSetupReact(handler: ReactStorySetupHandler): ReactStorySetupHandler;
|
package/dist/helpers.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Variant } from "./components/Variant.js";
|
|
2
|
+
import { Story } from "./components/Story.js";
|
|
3
|
+
import { HstButton, HstButtonGroup, HstCheckbox, HstCheckboxList, HstColorSelect, HstColorShades, HstCopyIcon, HstJson, HstNumber, HstRadio, HstSelect, HstSlider, HstSwitch, HstText, HstTextarea, HstTokenGrid, HstTokenList } from "./controls/index.js";
|
|
4
|
+
import { defineSetupReact } from "./helpers.js";
|
|
5
|
+
export { HstButton, HstButtonGroup, HstCheckbox, HstCheckboxList, HstColorSelect, HstColorShades, HstCopyIcon, HstJson, HstNumber, HstRadio, HstSelect, HstSlider, HstSwitch, HstText, HstTextarea, HstTokenGrid, HstTokenList, Story, Variant, defineSetupReact };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Variant } from "./components/Variant.js";
|
|
2
|
+
import { Story } from "./components/Story.js";
|
|
3
|
+
import { HstButton, HstButtonGroup, HstCheckbox, HstCheckboxList, HstColorSelect, HstColorShades, HstCopyIcon, HstJson, HstNumber, HstRadio, HstSelect, HstSlider, HstSwitch, HstText, HstTextarea, HstTokenGrid, HstTokenList } from "./controls/index.js";
|
|
4
|
+
import { defineSetupReact } from "./helpers.js";
|
|
5
|
+
import "./index.js";
|
|
6
|
+
import { usesLegacyVite } from "./util/vite.js";
|
|
7
|
+
import { defaultColors } from "histoire";
|
|
8
|
+
// src/index.node.ts
|
|
9
|
+
/** Stable name for the project's React configuration diagnostic. */
|
|
10
|
+
var JSX_CHECK_PLUGIN_NAME = "histoire:check-react-jsx";
|
|
11
|
+
/** Official React transformer names; JSX alone also describes Vue-only plugins. */
|
|
12
|
+
var REACT_JSX_PLUGIN_NAMES = /* @__PURE__ */ new Set([
|
|
13
|
+
"vite:react-babel",
|
|
14
|
+
"vite:react-swc",
|
|
15
|
+
"vite:react-oxc"
|
|
16
|
+
]);
|
|
17
|
+
/** Register React story discovery and framework adapters. JSX plugin remains project-owned. */
|
|
18
|
+
function HstReact() {
|
|
19
|
+
return {
|
|
20
|
+
name: "@histoire/plugin-react",
|
|
21
|
+
metadata: {
|
|
22
|
+
displayName: "React",
|
|
23
|
+
icon: "logo-react",
|
|
24
|
+
description: "React story support",
|
|
25
|
+
docsUrl: "https://histoire.dev/guide/plugins/official.html",
|
|
26
|
+
repoUrl: "https://github.com/histoire-dev/histoire/tree/main/packages/histoire-plugin-react"
|
|
27
|
+
},
|
|
28
|
+
/** Story patterns replace arrays during config merging, so extend incoming defaults explicitly. */
|
|
29
|
+
defaultConfig: (config) => ({
|
|
30
|
+
storyMatch: [.../* @__PURE__ */ new Set([
|
|
31
|
+
...config.storyMatch,
|
|
32
|
+
"**/*.story.tsx",
|
|
33
|
+
"**/*.story.jsx"
|
|
34
|
+
])],
|
|
35
|
+
supportMatch: [{
|
|
36
|
+
id: "react",
|
|
37
|
+
patterns: ["**/*.story.tsx", "**/*.story.jsx"],
|
|
38
|
+
pluginIds: ["react"]
|
|
39
|
+
}],
|
|
40
|
+
viteNodeTransformMode: { web: [/\.[jt]sx$/] },
|
|
41
|
+
vite: {
|
|
42
|
+
...usesLegacyVite() ? { esbuild: {
|
|
43
|
+
jsx: "automatic",
|
|
44
|
+
jsxImportSource: "react"
|
|
45
|
+
} } : { oxc: { jsx: {
|
|
46
|
+
runtime: "automatic",
|
|
47
|
+
importSource: "react"
|
|
48
|
+
} } },
|
|
49
|
+
resolve: { dedupe: [
|
|
50
|
+
"react",
|
|
51
|
+
"react-dom",
|
|
52
|
+
"react/jsx-runtime",
|
|
53
|
+
"react/jsx-dev-runtime"
|
|
54
|
+
] },
|
|
55
|
+
plugins: [{
|
|
56
|
+
name: JSX_CHECK_PLUGIN_NAME,
|
|
57
|
+
/** Vitest's separate browser server also needs the CommonJS client entry. */
|
|
58
|
+
config: () => ({ optimizeDeps: { include: ["react-dom/client"] } }),
|
|
59
|
+
/** Inspect final plugins, including those loaded from project vite.config. */
|
|
60
|
+
configResolved(config) {
|
|
61
|
+
if (usesLegacyVite()) {
|
|
62
|
+
for (const plugin of config.plugins) if (plugin.name === "vite:react:refresh-wrapper") plugin.applyToEnvironment = (environment) => environment.config.consumer === "client";
|
|
63
|
+
}
|
|
64
|
+
if (!config.plugins.some((plugin) => REACT_JSX_PLUGIN_NAMES.has(plugin.name))) config.logger.warn("[Histoire React] Add @vitejs/plugin-react to your Vite config to transform JSX stories.");
|
|
65
|
+
}
|
|
66
|
+
}]
|
|
67
|
+
},
|
|
68
|
+
theme: {
|
|
69
|
+
colors: { primary: defaultColors.sky },
|
|
70
|
+
logo: {
|
|
71
|
+
square: "@histoire/plugin-react/assets/histoire-react.svg",
|
|
72
|
+
light: "@histoire/plugin-react/assets/histoire-react-text.svg",
|
|
73
|
+
dark: "@histoire/plugin-react/assets/histoire-react-text.svg"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}),
|
|
77
|
+
supportPlugin: {
|
|
78
|
+
id: "react",
|
|
79
|
+
moduleName: "@histoire/plugin-react",
|
|
80
|
+
setupFn: "setupReact",
|
|
81
|
+
importStoryComponent: (file, index) => `import Comp${index} from ${JSON.stringify(file.moduleId)}`
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export { HstButton, HstButtonGroup, HstCheckbox, HstCheckboxList, HstColorSelect, HstColorShades, HstCopyIcon, HstJson, HstNumber, HstRadio, HstReact, HstSelect, HstSlider, HstSwitch, HstText, HstTextarea, HstTokenGrid, HstTokenList, Story, Variant, defineSetupReact };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { StoryProps as SharedStoryProps, VariantProps as SharedVariantProps } from '@histoire/shared';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
import type { ReactStorySetupHandler } from './helpers.js';
|
|
4
|
+
/** State exposed to both preview content and controls. */
|
|
5
|
+
export interface StorySlotProps<S = Record<string, any>> {
|
|
6
|
+
/** Vue reactive variant state; mutations update React subscribers. */
|
|
7
|
+
state: S;
|
|
8
|
+
}
|
|
9
|
+
/** Static content or a render callback receiving shared state. */
|
|
10
|
+
export type StoryContent<S = Record<string, any>> = ReactNode | ((props: StorySlotProps<S>) => ReactNode);
|
|
11
|
+
/** React variant declaration with optional initial state and controls. */
|
|
12
|
+
export interface VariantProps<S = Record<string, any>> extends Omit<SharedVariantProps, 'setupApp'> {
|
|
13
|
+
/** Initialize this variant's state during hidden configuration. */
|
|
14
|
+
initState?: () => S | Promise<S>;
|
|
15
|
+
/** Setup callback for this variant's preview and controls roots. */
|
|
16
|
+
setupApp?: ReactStorySetupHandler;
|
|
17
|
+
/** Preview content. */
|
|
18
|
+
children?: StoryContent<S>;
|
|
19
|
+
/** Controls render callback, overriding story-level controls. */
|
|
20
|
+
controls?: (props: StorySlotProps<S>) => ReactNode;
|
|
21
|
+
}
|
|
22
|
+
/** React story declaration; inherited props supply defaults to variants. */
|
|
23
|
+
export interface StoryProps<S = Record<string, any>> extends Omit<SharedStoryProps, 'setupApp'>, Omit<VariantProps<S>, 'meta'> {
|
|
24
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detach initializer data while retaining callbacks and opaque instance owners.
|
|
3
|
+
* Plain objects, arrays, dates, regular expressions, maps, sets, and binary data
|
|
4
|
+
* are copied. Other instances retain their identity and runtime behavior.
|
|
5
|
+
* Root back-references point to the existing variant state owner, not a spare copy.
|
|
6
|
+
*/
|
|
7
|
+
export declare function cloneInitialState<S extends Record<string, any>>(state: S, owner: Record<string, any>): S;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { toRaw } from "@histoire/vendors/vue";
|
|
2
|
+
// src/util/initial-state.ts
|
|
3
|
+
/**
|
|
4
|
+
* Detach initializer data while retaining callbacks and opaque instance owners.
|
|
5
|
+
* Plain objects, arrays, dates, regular expressions, maps, sets, and binary data
|
|
6
|
+
* are copied. Other instances retain their identity and runtime behavior.
|
|
7
|
+
* Root back-references point to the existing variant state owner, not a spare copy.
|
|
8
|
+
*/
|
|
9
|
+
function cloneInitialState(state, owner) {
|
|
10
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
11
|
+
/** Copy enumerable fields without invoking prototype setters on new objects. */
|
|
12
|
+
function copyFields(source, target) {
|
|
13
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
14
|
+
if (!Object.getOwnPropertyDescriptor(source, key)?.enumerable) continue;
|
|
15
|
+
Object.defineProperty(target, key, {
|
|
16
|
+
value: copy(source[key]),
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** Unwrap each Vue proxy and copy each mutable owner once, preserving aliases. */
|
|
24
|
+
function copy(value) {
|
|
25
|
+
if (value === null || typeof value !== "object") return value;
|
|
26
|
+
const raw = toRaw(value);
|
|
27
|
+
if (seen.has(raw)) return seen.get(raw);
|
|
28
|
+
let result;
|
|
29
|
+
if (Array.isArray(raw)) {
|
|
30
|
+
result = [];
|
|
31
|
+
result.length = raw.length;
|
|
32
|
+
} else if (raw instanceof Date) result = new Date(raw.getTime());
|
|
33
|
+
else if (raw instanceof RegExp) {
|
|
34
|
+
result = new RegExp(raw.source, raw.flags);
|
|
35
|
+
result.lastIndex = raw.lastIndex;
|
|
36
|
+
} else if (raw instanceof Map) result = /* @__PURE__ */ new Map();
|
|
37
|
+
else if (raw instanceof Set) result = /* @__PURE__ */ new Set();
|
|
38
|
+
else if (raw instanceof ArrayBuffer) result = raw.slice(0);
|
|
39
|
+
else if (ArrayBuffer.isView(raw)) {
|
|
40
|
+
const buffer = copy(raw.buffer);
|
|
41
|
+
result = raw instanceof DataView ? new DataView(buffer, raw.byteOffset, raw.byteLength) : new raw.constructor(buffer, raw.byteOffset, raw.length);
|
|
42
|
+
} else {
|
|
43
|
+
const prototype = Object.getPrototypeOf(raw);
|
|
44
|
+
if (prototype !== Object.prototype && prototype !== null) return raw;
|
|
45
|
+
result = Object.create(prototype);
|
|
46
|
+
}
|
|
47
|
+
seen.set(raw, result);
|
|
48
|
+
if (raw instanceof Map) for (const [key, value] of raw) result.set(copy(key), copy(value));
|
|
49
|
+
else if (raw instanceof Set) for (const value of raw) result.add(copy(value));
|
|
50
|
+
else if (!ArrayBuffer.isView(raw)) copyFields(raw, result);
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
if (state === null || typeof state !== "object") return state;
|
|
54
|
+
const raw = toRaw(state);
|
|
55
|
+
const result = {};
|
|
56
|
+
seen.set(raw, owner);
|
|
57
|
+
copyFields(raw, result);
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
export { cloneInitialState };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ComponentType } from 'react';
|
|
2
|
+
import type { HstContextValue } from '../components/context.js';
|
|
3
|
+
import type { ReactStoryWrapper } from '../helpers.js';
|
|
4
|
+
/** Own one React root and make initial render errors visible to Histoire callers. */
|
|
5
|
+
export declare function createReactHost(target: HTMLElement): {
|
|
6
|
+
/** Root exposed to story setup callbacks. */
|
|
7
|
+
app: import("react-dom/client").Root;
|
|
8
|
+
/** Flush synchronous mount while attributing story test registrations. */
|
|
9
|
+
render(component: ComponentType, context: HstContextValue, wrappers: ReactStoryWrapper[]): void;
|
|
10
|
+
/** Report cleanup failures without interrupting the adapter's remaining teardown. */
|
|
11
|
+
destroy(): void;
|
|
12
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { HstContext } from "../components/context.js";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
import { flushSync } from "react-dom";
|
|
5
|
+
import { withStoryExecution } from "@histoire/shared";
|
|
6
|
+
import { createRoot } from "react-dom/client";
|
|
7
|
+
// src/util/root.tsx
|
|
8
|
+
/** Own one React root and make initial render errors visible to Histoire callers. */
|
|
9
|
+
function createReactHost(target) {
|
|
10
|
+
let rendering = false;
|
|
11
|
+
let failed = false;
|
|
12
|
+
let failure;
|
|
13
|
+
const errorWindow = target.ownerDocument.defaultView?.window;
|
|
14
|
+
/** Match React's global reporting while preserving the target document's realm. */
|
|
15
|
+
function reportFailure(error) {
|
|
16
|
+
if (typeof errorWindow?.reportError === "function") {
|
|
17
|
+
errorWindow.reportError(error);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (errorWindow) {
|
|
21
|
+
const event = new errorWindow.ErrorEvent("error", {
|
|
22
|
+
bubbles: true,
|
|
23
|
+
cancelable: true,
|
|
24
|
+
message: typeof error === "object" && error !== null && "message" in error ? String(error.message) : String(error),
|
|
25
|
+
error
|
|
26
|
+
});
|
|
27
|
+
if (!errorWindow.dispatchEvent(event)) return;
|
|
28
|
+
}
|
|
29
|
+
console.error(error);
|
|
30
|
+
}
|
|
31
|
+
const app = createRoot(target, {
|
|
32
|
+
/** Initial React 19 errors belong to the mount caller; later errors remain observable. */
|
|
33
|
+
onUncaughtError(error) {
|
|
34
|
+
if (rendering) {
|
|
35
|
+
if (!failed) {
|
|
36
|
+
failed = true;
|
|
37
|
+
failure = error;
|
|
38
|
+
}
|
|
39
|
+
} else reportFailure(error);
|
|
40
|
+
} });
|
|
41
|
+
return {
|
|
42
|
+
/** Root exposed to story setup callbacks. */
|
|
43
|
+
app,
|
|
44
|
+
/** Flush synchronous mount while attributing story test registrations. */
|
|
45
|
+
render(component, context, wrappers) {
|
|
46
|
+
let tree = createElement(component);
|
|
47
|
+
for (const wrapper of [...wrappers].reverse()) tree = createElement(wrapper, {}, tree);
|
|
48
|
+
failed = false;
|
|
49
|
+
failure = void 0;
|
|
50
|
+
rendering = true;
|
|
51
|
+
try {
|
|
52
|
+
withStoryExecution(() => flushSync(() => app.render(/* @__PURE__ */ jsx(HstContext.Provider, {
|
|
53
|
+
value: context,
|
|
54
|
+
children: tree
|
|
55
|
+
}))), target);
|
|
56
|
+
} finally {
|
|
57
|
+
rendering = false;
|
|
58
|
+
}
|
|
59
|
+
if (failed) throw failure;
|
|
60
|
+
},
|
|
61
|
+
/** Report cleanup failures without interrupting the adapter's remaining teardown. */
|
|
62
|
+
destroy() {
|
|
63
|
+
try {
|
|
64
|
+
flushSync(() => app.unmount());
|
|
65
|
+
} catch (error) {
|
|
66
|
+
reportFailure(error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export { createReactHost };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { withStoryExecution } from "@histoire/shared";
|
|
2
|
+
import * as generatedSetup from "virtual:$histoire-generated-global-setup";
|
|
3
|
+
import * as setup from "virtual:$histoire-setup";
|
|
4
|
+
// src/util/setup.ts
|
|
5
|
+
/** Invoke generated, user, and variant setup in order under captured mount ownership. */
|
|
6
|
+
async function callSetupFunctions(api, target) {
|
|
7
|
+
const isActive = api.isActive ?? (() => true);
|
|
8
|
+
const handlers = [
|
|
9
|
+
generatedSetup.setupReact,
|
|
10
|
+
setup.setupReact,
|
|
11
|
+
api.variant?.setupApp
|
|
12
|
+
];
|
|
13
|
+
for (const handler of handlers) {
|
|
14
|
+
if (!isActive()) return;
|
|
15
|
+
if (typeof handler !== "function") continue;
|
|
16
|
+
const wrapper = await withStoryExecution(() => handler(api), target);
|
|
17
|
+
if (wrapper && isActive()) api.addWrapper(wrapper);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export { callSetupFunctions };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Variant } from '@histoire/shared';
|
|
2
|
+
import type { HstContextValue } from '../components/context.js';
|
|
3
|
+
import type { VariantProps } from '../types.js';
|
|
4
|
+
/** Apply inherited configuration and async initial state to one runtime variant. */
|
|
5
|
+
export declare function configureVariant(variant: Variant, props: VariantProps<any>, context: HstContextValue): Promise<void>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { cloneInitialState } from "./initial-state.js";
|
|
2
|
+
// src/util/variant.ts
|
|
3
|
+
/** Apply inherited configuration and async initial state to one runtime variant. */
|
|
4
|
+
async function configureVariant(variant, props, context) {
|
|
5
|
+
const inherited = context.storyProps ?? {};
|
|
6
|
+
const initState = props.initState ?? inherited.initState;
|
|
7
|
+
if (initState) {
|
|
8
|
+
const state = await initState();
|
|
9
|
+
if (!context.isActive()) return;
|
|
10
|
+
Object.assign(variant.state, cloneInitialState(state, variant.state));
|
|
11
|
+
}
|
|
12
|
+
if (!context.isActive()) return;
|
|
13
|
+
const controls = props.controls ?? inherited.controls;
|
|
14
|
+
const handlers = [...new Set([inherited.setupApp, props.setupApp].filter(Boolean))];
|
|
15
|
+
Object.assign(variant, {
|
|
16
|
+
source: props.source ?? inherited.source,
|
|
17
|
+
responsiveDisabled: props.responsiveDisabled ?? inherited.responsiveDisabled ?? false,
|
|
18
|
+
autoPropsDisabled: props.autoPropsDisabled ?? inherited.autoPropsDisabled ?? false,
|
|
19
|
+
slots: () => ({
|
|
20
|
+
default: true,
|
|
21
|
+
controls: !!controls,
|
|
22
|
+
source: null
|
|
23
|
+
}),
|
|
24
|
+
/** Run story setup before variant setup, retaining returned providers. */
|
|
25
|
+
setupApp: handlers.length ? async (api) => {
|
|
26
|
+
for (const handler of handlers) {
|
|
27
|
+
if (api.isActive?.() === false) return;
|
|
28
|
+
const wrapper = await handler(api);
|
|
29
|
+
if (api.isActive?.() === false) return;
|
|
30
|
+
if (wrapper) api.addWrapper(wrapper);
|
|
31
|
+
}
|
|
32
|
+
} : void 0,
|
|
33
|
+
configReady: true
|
|
34
|
+
});
|
|
35
|
+
context.story.meta ??= {};
|
|
36
|
+
Object.assign(context.story.meta, { hasVariantChildComponents: !context.implicit });
|
|
37
|
+
}
|
|
38
|
+
export { configureVariant };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
// src/util/vite.ts
|
|
3
|
+
/** Resolve the Vite instance used by Histoire, rather than the JSX plugin's peer. */
|
|
4
|
+
function usesLegacyVite() {
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
return createRequire(require.resolve("histoire"))("vite/package.json").version.startsWith("7.");
|
|
7
|
+
}
|
|
8
|
+
export { usesLegacyVite };
|
package/package.json
CHANGED
|
@@ -1,6 +1,97 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@histoire/plugin-react",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0-beta.3",
|
|
5
|
+
"description": "Histoire plugin for React 18/19 stories",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"url": "https://github.com/Akryum/histoire.git",
|
|
9
|
+
"type": "git",
|
|
10
|
+
"directory": "packages/histoire-plugin-react"
|
|
11
|
+
},
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"histoire-plugin"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.node.d.ts",
|
|
21
|
+
"browser": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.node.js"
|
|
23
|
+
},
|
|
24
|
+
"./client": "./dist/client/index.js",
|
|
25
|
+
"./collect": "./dist/collect/index.js",
|
|
26
|
+
"./*": "./*"
|
|
27
|
+
},
|
|
28
|
+
"main": "./dist/index.node.js",
|
|
29
|
+
"module": "./dist/index.node.js",
|
|
30
|
+
"types": "./dist/index.node.d.ts",
|
|
31
|
+
"files": [
|
|
32
|
+
"assets",
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
37
|
+
"react-dom": "^18.0.0 || ^19.0.0",
|
|
38
|
+
"histoire": "^1.0.0-beta.3"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"pathe": "^1.1.2",
|
|
42
|
+
"@histoire/controls": "^1.0.0-beta.3",
|
|
43
|
+
"@histoire/vendors": "^1.0.0-beta.3",
|
|
44
|
+
"@histoire/shared": "^1.0.0-beta.3"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^22.10.1",
|
|
48
|
+
"@types/react": "^19.0.0",
|
|
49
|
+
"@types/react-dom": "^19.0.0",
|
|
50
|
+
"@typescript/native": "npm:typescript@7.0.2",
|
|
51
|
+
"@vitejs/plugin-vue-jsx": "^5.1.3",
|
|
52
|
+
"jsdom": "^27.4.0",
|
|
53
|
+
"react": "^19.0.0",
|
|
54
|
+
"react-dom": "^19.0.0",
|
|
55
|
+
"typescript": "npm:@typescript/typescript6@6.0.2",
|
|
56
|
+
"vite": "^8.3.5",
|
|
57
|
+
"vitest": "5.1.1",
|
|
58
|
+
"histoire": "1.0.0-beta.3"
|
|
59
|
+
},
|
|
60
|
+
"histoire": {
|
|
61
|
+
"marketplace": {
|
|
62
|
+
"displayName": "React",
|
|
63
|
+
"summary": "Collect and render React 18 and 19 stories.",
|
|
64
|
+
"category": "frameworks",
|
|
65
|
+
"frameworks": [
|
|
66
|
+
"react"
|
|
67
|
+
],
|
|
68
|
+
"monogram": "Re",
|
|
69
|
+
"tint": "sky",
|
|
70
|
+
"adds": [],
|
|
71
|
+
"access": [
|
|
72
|
+
"story-frames"
|
|
73
|
+
],
|
|
74
|
+
"config": {
|
|
75
|
+
"import": "HstReact",
|
|
76
|
+
"options": []
|
|
77
|
+
},
|
|
78
|
+
"recommend": [
|
|
79
|
+
{
|
|
80
|
+
"dependency": "react",
|
|
81
|
+
"reason": "React detected"
|
|
82
|
+
}
|
|
83
|
+
],
|
|
84
|
+
"requires": {
|
|
85
|
+
"histoire": "^1.0.0-beta.1",
|
|
86
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
87
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
"scripts": {
|
|
92
|
+
"build": "rimraf dist && vite build && tsc --emitDeclarationOnly",
|
|
93
|
+
"watch": "vite build --watch",
|
|
94
|
+
"test": "vitest run",
|
|
95
|
+
"test:dev": "vitest"
|
|
96
|
+
}
|
|
6
97
|
}
|
package/README.md
DELETED