react-cosmos-renderer 6.0.0-canary.3afe6dd.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/LICENSE +21 -0
- package/client.d.ts +1 -0
- package/client.js +1 -0
- package/dist/client.d.ts +8 -0
- package/dist/client.js +8 -0
- package/dist/fixture/FixtureCapture/FixtureCapture.d.ts +8 -0
- package/dist/fixture/FixtureCapture/FixtureCapture.js +17 -0
- package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/index.d.ts +5 -0
- package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/index.js +46 -0
- package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.d.ts +4 -0
- package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.js +19 -0
- package/dist/fixture/FixtureCapture/classState/index.d.ts +3 -0
- package/dist/fixture/FixtureCapture/classState/index.js +13 -0
- package/dist/fixture/FixtureCapture/classState/shared.d.ts +18 -0
- package/dist/fixture/FixtureCapture/classState/shared.js +14 -0
- package/dist/fixture/FixtureCapture/classState/useFixtureState.d.ts +4 -0
- package/dist/fixture/FixtureCapture/classState/useFixtureState.js +153 -0
- package/dist/fixture/FixtureCapture/classState/useReadClassState.d.ts +4 -0
- package/dist/fixture/FixtureCapture/classState/useReadClassState.js +58 -0
- package/dist/fixture/FixtureCapture/props/index.d.ts +3 -0
- package/dist/fixture/FixtureCapture/props/index.js +74 -0
- package/dist/fixture/FixtureCapture/props/useFixtureProps.d.ts +3 -0
- package/dist/fixture/FixtureCapture/props/useFixtureProps.js +68 -0
- package/dist/fixture/FixtureCapture/shared/findRelevantElementPaths.d.ts +2 -0
- package/dist/fixture/FixtureCapture/shared/findRelevantElementPaths.js +23 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/findElementPaths.d.ts +2 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/findElementPaths.js +22 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/getElementAtPath.d.ts +3 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/getElementAtPath.js +26 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/index.d.ts +11 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/index.js +11 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/setElementAtPath.d.ts +2 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/setElementAtPath.js +35 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/shared.d.ts +2 -0
- package/dist/fixture/FixtureCapture/shared/nodeTree/shared.js +6 -0
- package/dist/fixture/FixtureContext.d.ts +7 -0
- package/dist/fixture/FixtureContext.js +5 -0
- package/dist/fixture/Viewport.d.ts +11 -0
- package/dist/fixture/Viewport.js +15 -0
- package/dist/fixture/useSelect/index.d.ts +2 -0
- package/dist/fixture/useSelect/index.js +11 -0
- package/dist/fixture/useSelect/shared.d.ts +6 -0
- package/dist/fixture/useSelect/shared.js +3 -0
- package/dist/fixture/useSelect/useCreateFixtureState.d.ts +2 -0
- package/dist/fixture/useSelect/useCreateFixtureState.js +32 -0
- package/dist/fixture/useSelect/useCurrentValue.d.ts +2 -0
- package/dist/fixture/useSelect/useCurrentValue.js +11 -0
- package/dist/fixture/useSelect/useSetValue.d.ts +2 -0
- package/dist/fixture/useSelect/useSetValue.js +25 -0
- package/dist/fixture/useValue/index.d.ts +7 -0
- package/dist/fixture/useValue/index.js +9 -0
- package/dist/fixture/useValue/shared.d.ts +3 -0
- package/dist/fixture/useValue/shared.js +1 -0
- package/dist/fixture/useValue/useCreateFixtureState.d.ts +2 -0
- package/dist/fixture/useValue/useCreateFixtureState.js +33 -0
- package/dist/fixture/useValue/useCurrentValue.d.ts +2 -0
- package/dist/fixture/useValue/useCurrentValue.js +12 -0
- package/dist/fixture/useValue/useSetValue.d.ts +3 -0
- package/dist/fixture/useValue/useSetValue.js +34 -0
- package/dist/fixtureLoaders/ClientFixtureLoader.d.ts +9 -0
- package/dist/fixtureLoaders/ClientFixtureLoader.js +18 -0
- package/dist/fixtureLoaders/FixtureLoaderConnect.d.ts +11 -0
- package/dist/fixtureLoaders/FixtureLoaderConnect.js +19 -0
- package/dist/fixtureLoaders/RendererSync.d.ts +8 -0
- package/dist/fixtureLoaders/RendererSync.js +52 -0
- package/dist/fixtureLoaders/ServerFixtureChangeListener.d.ts +6 -0
- package/dist/fixtureLoaders/ServerFixtureChangeListener.js +20 -0
- package/dist/fixtureLoaders/ServerFixtureLoader.d.ts +10 -0
- package/dist/fixtureLoaders/ServerFixtureLoader.js +21 -0
- package/dist/fixtureLoaders/defaultRenderMessage.d.ts +2 -0
- package/dist/fixtureLoaders/defaultRenderMessage.js +4 -0
- package/dist/fixtureLoaders/useFixtureSelection.d.ts +7 -0
- package/dist/fixtureLoaders/useFixtureSelection.js +31 -0
- package/dist/fixtureModule/DecoratedFixture.d.ts +9 -0
- package/dist/fixtureModule/DecoratedFixture.js +13 -0
- package/dist/fixtureModule/FixtureModule.d.ts +14 -0
- package/dist/fixtureModule/FixtureModule.js +13 -0
- package/dist/fixtureModule/FixtureProvider.d.ts +11 -0
- package/dist/fixtureModule/FixtureProvider.js +64 -0
- package/dist/fixtureModule/createFixtureNode.d.ts +3 -0
- package/dist/fixtureModule/createFixtureNode.js +19 -0
- package/dist/fixtureModule/decorateFixture.d.ts +3 -0
- package/dist/fixtureModule/decorateFixture.js +4 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/moduleLoaders/AsyncModuleLoader.d.ts +9 -0
- package/dist/moduleLoaders/AsyncModuleLoader.js +14 -0
- package/dist/moduleLoaders/LazyModuleLoader.d.ts +10 -0
- package/dist/moduleLoaders/LazyModuleLoader.js +25 -0
- package/dist/moduleLoaders/StaticModuleLoader.d.ts +10 -0
- package/dist/moduleLoaders/StaticModuleLoader.js +8 -0
- package/dist/moduleLoaders/importLazyFixtureModules.d.ts +5 -0
- package/dist/moduleLoaders/importLazyFixtureModules.js +6 -0
- package/dist/rendererConnect/RendererContext.d.ts +12 -0
- package/dist/rendererConnect/RendererContext.js +13 -0
- package/dist/rendererConnect/RendererProvider.d.ts +11 -0
- package/dist/rendererConnect/RendererProvider.js +25 -0
- package/dist/rendererConnect/createNoopRendererConnect.d.ts +2 -0
- package/dist/rendererConnect/createNoopRendererConnect.js +8 -0
- package/dist/rendererConnect/createPostMessageConnect.d.ts +2 -0
- package/dist/rendererConnect/createPostMessageConnect.js +22 -0
- package/dist/rendererConnect/createWebSocketsConnect.d.ts +2 -0
- package/dist/rendererConnect/createWebSocketsConnect.js +32 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +19 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ReactDecorator, ReactDecoratorModule, ReactFixture } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
fixture: ReactFixture;
|
|
5
|
+
userDecoratorModules: ReactDecoratorModule[];
|
|
6
|
+
globalDecorators?: ReactDecorator[];
|
|
7
|
+
};
|
|
8
|
+
export declare function DecoratedFixture({ fixture, userDecoratorModules, globalDecorators, }: Props): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React, { useMemo } from 'react';
|
|
2
|
+
import { FixtureCapture } from '../fixture/FixtureCapture/FixtureCapture.js';
|
|
3
|
+
import { createFixtureNode } from './createFixtureNode.js';
|
|
4
|
+
import { decorateFixture } from './decorateFixture.js';
|
|
5
|
+
export function DecoratedFixture({ fixture, userDecoratorModules, globalDecorators = [], }) {
|
|
6
|
+
return useMemo(() => {
|
|
7
|
+
const decorators = [
|
|
8
|
+
...globalDecorators,
|
|
9
|
+
...userDecoratorModules.map(m => m.default),
|
|
10
|
+
];
|
|
11
|
+
return decorateFixture(React.createElement(FixtureCapture, { decoratorId: "root" }, createFixtureNode(fixture)), decorators);
|
|
12
|
+
}, [fixture, globalDecorators, userDecoratorModules]);
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { FixtureId, FixtureState, ReactDecorator, ReactDecoratorModule, ReactFixtureModule } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
fixtureModule: ReactFixtureModule;
|
|
5
|
+
decoratorModules: ReactDecoratorModule[];
|
|
6
|
+
globalDecorators?: ReactDecorator[];
|
|
7
|
+
fixtureId: FixtureId;
|
|
8
|
+
initialFixtureState?: FixtureState;
|
|
9
|
+
renderKey: number;
|
|
10
|
+
lazy: boolean;
|
|
11
|
+
renderMessage: (msg: string) => React.ReactElement;
|
|
12
|
+
};
|
|
13
|
+
export declare function FixtureModule({ fixtureModule, decoratorModules, globalDecorators, fixtureId, initialFixtureState, renderKey, lazy, renderMessage, }: Props): JSX.Element;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getFixtureFromExport, getFixtureItemFromExport, } from 'react-cosmos-core';
|
|
3
|
+
import { DecoratedFixture } from './DecoratedFixture.js';
|
|
4
|
+
import { FixtureProvider } from './FixtureProvider.js';
|
|
5
|
+
export function FixtureModule({ fixtureModule, decoratorModules, globalDecorators, fixtureId, initialFixtureState, renderKey, lazy, renderMessage, }) {
|
|
6
|
+
const fixtureItem = React.useMemo(() => getFixtureItemFromExport(fixtureModule.default), [fixtureModule.default]);
|
|
7
|
+
const fixture = getFixtureFromExport(fixtureModule.default, fixtureId.name);
|
|
8
|
+
if (typeof fixture === 'undefined') {
|
|
9
|
+
return renderMessage(`Invalid fixture name: ${fixtureId.name}`);
|
|
10
|
+
}
|
|
11
|
+
return (React.createElement(FixtureProvider, { key: renderKey, fixtureId: fixtureId, initialFixtureState: initialFixtureState, fixtureItem: fixtureItem, lazy: lazy },
|
|
12
|
+
React.createElement(DecoratedFixture, { fixture: fixture, userDecoratorModules: decoratorModules, globalDecorators: globalDecorators })));
|
|
13
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { FixtureId, FixtureListItem, FixtureState } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
fixtureId: FixtureId;
|
|
6
|
+
initialFixtureState?: FixtureState;
|
|
7
|
+
fixtureItem: FixtureListItem;
|
|
8
|
+
lazy: boolean;
|
|
9
|
+
};
|
|
10
|
+
export declare function FixtureProvider(props: Props): JSX.Element;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { isEqual } from 'lodash-es';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { FixtureContext } from '../fixture/FixtureContext.js';
|
|
5
|
+
import { RendererContext } from '../rendererConnect/RendererContext.js';
|
|
6
|
+
export function FixtureProvider(props) {
|
|
7
|
+
const [state, setState] = React.useState({
|
|
8
|
+
fixtureState: props.initialFixtureState || {},
|
|
9
|
+
syncedFixtureState: {},
|
|
10
|
+
});
|
|
11
|
+
const { rendererId, rendererConnect, setLazyItems } = React.useContext(RendererContext);
|
|
12
|
+
React.useEffect(() => {
|
|
13
|
+
// Only multi fixtures have extra info that isn't already available in the
|
|
14
|
+
// fixture list provided to the Cosmos UI (fixture names, which in lazy mode
|
|
15
|
+
// are revealed after importing a fixture module).
|
|
16
|
+
if (props.lazy) {
|
|
17
|
+
setLazyItems(props.fixtureItem.type === 'multi'
|
|
18
|
+
? { [props.fixtureId.path]: props.fixtureItem }
|
|
19
|
+
: noLazyItem);
|
|
20
|
+
}
|
|
21
|
+
}, [props.fixtureId.path, props.fixtureItem, props.lazy, setLazyItems]);
|
|
22
|
+
React.useEffect(() => {
|
|
23
|
+
if (!isEqual(state.fixtureState, state.syncedFixtureState)) {
|
|
24
|
+
rendererConnect.postMessage({
|
|
25
|
+
type: 'fixtureStateChange',
|
|
26
|
+
payload: {
|
|
27
|
+
rendererId,
|
|
28
|
+
fixtureId: props.fixtureId,
|
|
29
|
+
fixtureState: state.fixtureState,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
setState(prevState => ({
|
|
33
|
+
...prevState,
|
|
34
|
+
syncedFixtureState: state.fixtureState,
|
|
35
|
+
}));
|
|
36
|
+
}
|
|
37
|
+
}, [
|
|
38
|
+
props.fixtureId,
|
|
39
|
+
rendererConnect,
|
|
40
|
+
rendererId,
|
|
41
|
+
state.fixtureState,
|
|
42
|
+
state.syncedFixtureState,
|
|
43
|
+
]);
|
|
44
|
+
React.useEffect(() => rendererConnect.onMessage(msg => {
|
|
45
|
+
if (msg.type === 'setFixtureState' &&
|
|
46
|
+
msg.payload.rendererId === rendererId) {
|
|
47
|
+
const { fixtureId, fixtureState } = msg.payload;
|
|
48
|
+
setState(prevState =>
|
|
49
|
+
// Ensure fixture state applies to currently selected fixture
|
|
50
|
+
isEqual(fixtureId, props.fixtureId)
|
|
51
|
+
? { ...prevState, fixtureState, syncedFixtureState: fixtureState }
|
|
52
|
+
: prevState);
|
|
53
|
+
}
|
|
54
|
+
}), [props.fixtureId, rendererConnect, rendererId]);
|
|
55
|
+
const setFixtureState = React.useCallback(stateUpdate => {
|
|
56
|
+
setState(prevState => ({
|
|
57
|
+
...prevState,
|
|
58
|
+
fixtureState: stateUpdate(prevState.fixtureState),
|
|
59
|
+
}));
|
|
60
|
+
}, [setState]);
|
|
61
|
+
const contextValue = React.useMemo(() => ({ fixtureState: state.fixtureState, setFixtureState }), [setFixtureState, state.fixtureState]);
|
|
62
|
+
return (React.createElement(FixtureContext.Provider, { value: contextValue }, props.children));
|
|
63
|
+
}
|
|
64
|
+
const noLazyItem = {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export function createFixtureNode(fixture) {
|
|
3
|
+
// Warning: In a React Server Components setup this function is called on the
|
|
4
|
+
// server. When a fixture module uses the 'use client' directive, the fixture
|
|
5
|
+
// arg received here will be a function wrapper regardless of the fixture
|
|
6
|
+
// module contents. In this scenario, the fixture module will be automatically
|
|
7
|
+
// rendered on the client side, where React expects a component default export.
|
|
8
|
+
// This results in the following limitation: In a React Server Components
|
|
9
|
+
// setup, Client fixtures have to export a single function component. They
|
|
10
|
+
// can't be multi fixtures and they can't export React elements directly.
|
|
11
|
+
return isFunctionFixture(fixture) ? (React.createElement(FixtureElement, { Component: fixture })) : (fixture);
|
|
12
|
+
}
|
|
13
|
+
function isFunctionFixture(fixture) {
|
|
14
|
+
return typeof fixture === 'function';
|
|
15
|
+
}
|
|
16
|
+
function FixtureElement({ Component }) {
|
|
17
|
+
return React.createElement(Component, null);
|
|
18
|
+
}
|
|
19
|
+
FixtureElement.cosmosCapture = false;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './fixtureLoaders/ServerFixtureLoader.js';
|
|
2
|
+
export * from './fixtureModule/createFixtureNode.js';
|
|
3
|
+
export * from './fixtureModule/decorateFixture.js';
|
|
4
|
+
export * from './rendererConnect/createNoopRendererConnect.js';
|
|
5
|
+
export * from './rendererConnect/createPostMessageConnect.js';
|
|
6
|
+
export * from './rendererConnect/createWebSocketsConnect.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './fixtureLoaders/ServerFixtureLoader.js';
|
|
2
|
+
export * from './fixtureModule/createFixtureNode.js';
|
|
3
|
+
export * from './fixtureModule/decorateFixture.js';
|
|
4
|
+
export * from './rendererConnect/createNoopRendererConnect.js';
|
|
5
|
+
export * from './rendererConnect/createPostMessageConnect.js';
|
|
6
|
+
export * from './rendererConnect/createWebSocketsConnect.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { FixtureModules, UserModuleWrappers } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
moduleWrappers: UserModuleWrappers;
|
|
5
|
+
fixturePath: string;
|
|
6
|
+
renderModules: (modules: FixtureModules) => React.ReactElement;
|
|
7
|
+
};
|
|
8
|
+
export declare function AsyncModuleLoader({ moduleWrappers, fixturePath, renderModules, }: Props): Promise<React.ReactElement<any, string | React.JSXElementConstructor<any>>>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getSortedDecoratorsForFixturePath, } from 'react-cosmos-core';
|
|
2
|
+
import { importLazyFixtureModules } from './importLazyFixtureModules.js';
|
|
3
|
+
export async function AsyncModuleLoader({ moduleWrappers, fixturePath, renderModules, }) {
|
|
4
|
+
return renderModules(await getModules(moduleWrappers, fixturePath));
|
|
5
|
+
}
|
|
6
|
+
async function getModules(moduleWrappers, fixturePath) {
|
|
7
|
+
if (moduleWrappers.lazy) {
|
|
8
|
+
return await importLazyFixtureModules(moduleWrappers.fixtures[fixturePath], getSortedDecoratorsForFixturePath(fixturePath, moduleWrappers.decorators));
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
fixtureModule: moduleWrappers.fixtures[fixturePath].module,
|
|
12
|
+
decoratorModules: getSortedDecoratorsForFixturePath(fixturePath, moduleWrappers.decorators).map(d => d.module),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ByPath, FixtureModules, LazyReactDecoratorWrapper, LazyReactFixtureWrapper } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
fixtureWrapper: LazyReactFixtureWrapper;
|
|
5
|
+
decorators: ByPath<LazyReactDecoratorWrapper>;
|
|
6
|
+
fixturePath: string;
|
|
7
|
+
renderModules: (modules: FixtureModules) => React.ReactElement;
|
|
8
|
+
};
|
|
9
|
+
export declare function LazyModuleLoader({ fixtureWrapper, decorators, fixturePath, renderModules, }: Props): React.ReactElement<any, string | React.JSXElementConstructor<any>> | null;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getSortedDecoratorsForFixturePath, } from 'react-cosmos-core';
|
|
3
|
+
import { importLazyFixtureModules } from './importLazyFixtureModules.js';
|
|
4
|
+
export function LazyModuleLoader({ fixtureWrapper, decorators, fixturePath, renderModules, }) {
|
|
5
|
+
const modules = useLazyFixtureModules(fixturePath, fixtureWrapper, decorators);
|
|
6
|
+
return modules && renderModules(modules);
|
|
7
|
+
}
|
|
8
|
+
function useLazyFixtureModules(fixturePath, fixtureWrapper, decoratorWrappers) {
|
|
9
|
+
const [state, setState] = React.useState(null);
|
|
10
|
+
React.useEffect(() => {
|
|
11
|
+
let canceled = false;
|
|
12
|
+
(async () => {
|
|
13
|
+
const modules = await importLazyFixtureModules(fixtureWrapper, getSortedDecoratorsForFixturePath(fixturePath, decoratorWrappers));
|
|
14
|
+
if (!canceled) {
|
|
15
|
+
setState({ fixturePath, modules });
|
|
16
|
+
}
|
|
17
|
+
})();
|
|
18
|
+
return () => {
|
|
19
|
+
canceled = true;
|
|
20
|
+
};
|
|
21
|
+
}, [decoratorWrappers, fixturePath, fixtureWrapper]);
|
|
22
|
+
// Stop returning modules once fixturePath changed to prevent rendering
|
|
23
|
+
// the previous fixture until the new fixture modules are loaded
|
|
24
|
+
return state && state.fixturePath === fixturePath ? state.modules : null;
|
|
25
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ByPath, FixtureModules, ReactDecoratorWrapper, ReactFixtureWrapper } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
fixtureWrapper: ReactFixtureWrapper;
|
|
5
|
+
decorators: ByPath<ReactDecoratorWrapper>;
|
|
6
|
+
fixturePath: string;
|
|
7
|
+
renderModules: (modules: FixtureModules) => React.ReactElement;
|
|
8
|
+
};
|
|
9
|
+
export declare function StaticModuleLoader({ fixtureWrapper, decorators, fixturePath, renderModules, }: Props): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getSortedDecoratorsForFixturePath, } from 'react-cosmos-core';
|
|
3
|
+
export function StaticModuleLoader({ fixtureWrapper, decorators, fixturePath, renderModules, }) {
|
|
4
|
+
return renderModules(React.useMemo(() => ({
|
|
5
|
+
fixtureModule: fixtureWrapper.module,
|
|
6
|
+
decoratorModules: getSortedDecoratorsForFixturePath(fixturePath, decorators).map(d => d.module),
|
|
7
|
+
}), [decorators, fixturePath, fixtureWrapper.module]));
|
|
8
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { LazyReactDecoratorWrapper, LazyReactFixtureWrapper } from 'react-cosmos-core';
|
|
2
|
+
export declare function importLazyFixtureModules(fixtureWrapper: LazyReactFixtureWrapper, decoratorWrappers: LazyReactDecoratorWrapper[]): Promise<{
|
|
3
|
+
fixtureModule: import("react-cosmos-core").ReactFixtureModule;
|
|
4
|
+
decoratorModules: import("react-cosmos-core").ReactDecoratorModule[];
|
|
5
|
+
}>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { FixtureList, RendererConnect, RendererSearchParams } from 'react-cosmos-core';
|
|
3
|
+
export type RendererContextValue = {
|
|
4
|
+
rendererId: string;
|
|
5
|
+
rendererConnect: RendererConnect;
|
|
6
|
+
searchParams: RendererSearchParams;
|
|
7
|
+
setSearchParams(nextParams: RendererSearchParams): void;
|
|
8
|
+
reloadRenderer(): void;
|
|
9
|
+
lazyItems: FixtureList;
|
|
10
|
+
setLazyItems: React.Dispatch<React.SetStateAction<FixtureList>>;
|
|
11
|
+
};
|
|
12
|
+
export declare const RendererContext: React.Context<RendererContextValue>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export const RendererContext = React.createContext({
|
|
3
|
+
rendererId: 'default-renderer-id',
|
|
4
|
+
rendererConnect: {
|
|
5
|
+
postMessage: () => { },
|
|
6
|
+
onMessage: () => () => { },
|
|
7
|
+
},
|
|
8
|
+
searchParams: {},
|
|
9
|
+
setSearchParams: () => { },
|
|
10
|
+
reloadRenderer: () => { },
|
|
11
|
+
lazyItems: {},
|
|
12
|
+
setLazyItems: () => { },
|
|
13
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { RendererConnect, RendererSearchParams } from 'react-cosmos-core';
|
|
3
|
+
export type Props = {
|
|
4
|
+
children: React.ReactNode;
|
|
5
|
+
rendererId: string;
|
|
6
|
+
rendererConnect: RendererConnect;
|
|
7
|
+
searchParams: RendererSearchParams;
|
|
8
|
+
setSearchParams(nextParams: RendererSearchParams): void;
|
|
9
|
+
reloadRenderer(): void;
|
|
10
|
+
};
|
|
11
|
+
export declare function RendererProvider(props: Props): JSX.Element;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { RendererContext } from './RendererContext.js';
|
|
4
|
+
export function RendererProvider(props) {
|
|
5
|
+
const [lazyItems, setLazyItems] = React.useState({});
|
|
6
|
+
const value = React.useMemo(() => {
|
|
7
|
+
return {
|
|
8
|
+
rendererId: props.rendererId,
|
|
9
|
+
rendererConnect: props.rendererConnect,
|
|
10
|
+
searchParams: props.searchParams,
|
|
11
|
+
setSearchParams: props.setSearchParams,
|
|
12
|
+
reloadRenderer: props.reloadRenderer,
|
|
13
|
+
lazyItems,
|
|
14
|
+
setLazyItems,
|
|
15
|
+
};
|
|
16
|
+
}, [
|
|
17
|
+
lazyItems,
|
|
18
|
+
props.reloadRenderer,
|
|
19
|
+
props.rendererConnect,
|
|
20
|
+
props.rendererId,
|
|
21
|
+
props.searchParams,
|
|
22
|
+
props.setSearchParams,
|
|
23
|
+
]);
|
|
24
|
+
return (React.createElement(RendererContext.Provider, { value: value }, props.children));
|
|
25
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { registerPlaygroundShortcuts, } from 'react-cosmos-core';
|
|
2
|
+
export function createPostMessageConnect() {
|
|
3
|
+
function postMessage(msg) {
|
|
4
|
+
parent.postMessage(msg, '*');
|
|
5
|
+
}
|
|
6
|
+
return {
|
|
7
|
+
postMessage,
|
|
8
|
+
onMessage(onMessage) {
|
|
9
|
+
function handleMessage(msg) {
|
|
10
|
+
onMessage(msg.data);
|
|
11
|
+
}
|
|
12
|
+
window.addEventListener('message', handleMessage, false);
|
|
13
|
+
const removeShortcuts = registerPlaygroundShortcuts(command => {
|
|
14
|
+
postMessage({ type: 'playgroundCommand', payload: { command } });
|
|
15
|
+
});
|
|
16
|
+
return () => {
|
|
17
|
+
window.removeEventListener('message', handleMessage);
|
|
18
|
+
removeShortcuts();
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { rendererSocketMessage, } from 'react-cosmos-core';
|
|
2
|
+
export function createWebSocketsConnect(url) {
|
|
3
|
+
let pendingMessages = [];
|
|
4
|
+
const socket = new WebSocket(url);
|
|
5
|
+
socket.addEventListener('open', () => {
|
|
6
|
+
if (pendingMessages.length > 0) {
|
|
7
|
+
pendingMessages.forEach(msg => socket.send(JSON.stringify(msg)));
|
|
8
|
+
pendingMessages = [];
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
postMessage(rendererResponse) {
|
|
13
|
+
const socketMessage = rendererSocketMessage(rendererResponse);
|
|
14
|
+
if (socket && socket.readyState === WebSocket.OPEN) {
|
|
15
|
+
socket.send(JSON.stringify(socketMessage));
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
pendingMessages.push(socketMessage);
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
onMessage(onMessage) {
|
|
22
|
+
function handleMessage(msg) {
|
|
23
|
+
const socketMessage = JSON.parse(msg.data);
|
|
24
|
+
if (socketMessage.channel === 'renderer') {
|
|
25
|
+
onMessage(socketMessage.message);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
socket.addEventListener('message', handleMessage);
|
|
29
|
+
return () => socket.removeEventListener('message', handleMessage);
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/index.js';
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/index.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-cosmos-renderer",
|
|
3
|
+
"version": "6.0.0-canary.3afe6dd.0+3afe6dd",
|
|
4
|
+
"description": "React Cosmos Renderer",
|
|
5
|
+
"repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos-renderer",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./index.js",
|
|
11
|
+
"./client": "./client.js"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"lodash-es": "^4.17.21",
|
|
15
|
+
"react-cosmos-core": "6.0.0-canary.3afe6dd.0+3afe6dd",
|
|
16
|
+
"react-is": "^18.2.0"
|
|
17
|
+
},
|
|
18
|
+
"gitHead": "3afe6ddef9490e34f6b8151100800473f3ea2c29"
|
|
19
|
+
}
|