react-cosmos-renderer 6.0.0-canary.04683cb.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,26 @@
|
|
|
1
|
+
import { get } from 'lodash-es';
|
|
2
|
+
import { isReactElement } from 'react-cosmos-core';
|
|
3
|
+
import { isRootPath } from './shared.js';
|
|
4
|
+
// Why be silent about trying to fetch a node that isn't an element?
|
|
5
|
+
// Because users of this utility only care about elements. Whether the child
|
|
6
|
+
// node was removed or replaced by a different type of node (eg. string,
|
|
7
|
+
// array of elements, etc.) is irrelevant.
|
|
8
|
+
// NICETOHAVE: Assert child path validity
|
|
9
|
+
export function getElementAtPath(node, elPath) {
|
|
10
|
+
if (!isReactElement(node) && !Array.isArray(node)) {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
const rootNode = node;
|
|
14
|
+
const childNode = isRootPath(elPath) ? rootNode : get(rootNode, elPath);
|
|
15
|
+
if (!isReactElement(childNode)) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return childNode;
|
|
19
|
+
}
|
|
20
|
+
export function getExpectedElementAtPath(node, elPath) {
|
|
21
|
+
const el = getElementAtPath(node, elPath);
|
|
22
|
+
if (!el) {
|
|
23
|
+
throw new Error(`Element not found at path: ${elPath}`);
|
|
24
|
+
}
|
|
25
|
+
return el;
|
|
26
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility for extending React Elements from a tree of React Nodes.
|
|
3
|
+
*
|
|
4
|
+
* The root Node is the `children` prop of a parent Element. Besides the Node
|
|
5
|
+
* type, children can also be a function.
|
|
6
|
+
* https://reactjs.org/docs/jsx-in-depth.html#functions-as-children
|
|
7
|
+
*/
|
|
8
|
+
export { findElementPaths } from './findElementPaths.js';
|
|
9
|
+
export { getElementAtPath, getExpectedElementAtPath, } from './getElementAtPath.js';
|
|
10
|
+
export { setElementAtPath } from './setElementAtPath.js';
|
|
11
|
+
export { getChildrenPath } from './shared.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility for extending React Elements from a tree of React Nodes.
|
|
3
|
+
*
|
|
4
|
+
* The root Node is the `children` prop of a parent Element. Besides the Node
|
|
5
|
+
* type, children can also be a function.
|
|
6
|
+
* https://reactjs.org/docs/jsx-in-depth.html#functions-as-children
|
|
7
|
+
*/
|
|
8
|
+
export { findElementPaths } from './findElementPaths.js';
|
|
9
|
+
export { getElementAtPath, getExpectedElementAtPath, } from './getElementAtPath.js';
|
|
10
|
+
export { setElementAtPath } from './setElementAtPath.js';
|
|
11
|
+
export { getChildrenPath } from './shared.js';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { set } from 'lodash-es';
|
|
2
|
+
import { isReactElement } from 'react-cosmos-core';
|
|
3
|
+
import { getExpectedElementAtPath } from './getElementAtPath.js';
|
|
4
|
+
import { isRootPath } from './shared.js';
|
|
5
|
+
export function setElementAtPath(node, elPath, updater) {
|
|
6
|
+
const childEl = getExpectedElementAtPath(node, elPath);
|
|
7
|
+
const newEl = updater(childEl);
|
|
8
|
+
if (isRootPath(elPath)) {
|
|
9
|
+
return newEl;
|
|
10
|
+
}
|
|
11
|
+
// If the root is a non-Array non-Element Node we should be at the root path
|
|
12
|
+
// and returned already
|
|
13
|
+
const clonedRoot = cloneNode(node);
|
|
14
|
+
// _.set also accepts arrays
|
|
15
|
+
// https://github.com/lodash/lodash/blob/6018350ac10d5ce6a5b7db625140b82aeab804df/isObject.js#L15-L16
|
|
16
|
+
return set(clonedRoot, elPath, newEl);
|
|
17
|
+
}
|
|
18
|
+
function cloneNode(value) {
|
|
19
|
+
return Array.isArray(value)
|
|
20
|
+
? value.map(n => cloneNodeItem(n))
|
|
21
|
+
: cloneNodeItem(value);
|
|
22
|
+
}
|
|
23
|
+
function cloneNodeItem(value) {
|
|
24
|
+
return isReactElement(value) ? cloneReactElement(value) : value;
|
|
25
|
+
}
|
|
26
|
+
function cloneReactElement(value) {
|
|
27
|
+
const { children, ...otherProps } = value.props;
|
|
28
|
+
return {
|
|
29
|
+
...value,
|
|
30
|
+
props: {
|
|
31
|
+
...otherProps,
|
|
32
|
+
children: cloneNode(children),
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { FixtureState, SetFixtureState } from 'react-cosmos-core';
|
|
3
|
+
export type FixtureContextValue = {
|
|
4
|
+
fixtureState: FixtureState;
|
|
5
|
+
setFixtureState: SetFixtureState;
|
|
6
|
+
};
|
|
7
|
+
export declare const FixtureContext: React.Context<FixtureContextValue>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type Props = {
|
|
3
|
+
children: React.ReactNode;
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
};
|
|
7
|
+
export declare function Viewport({ children, width, height }: Props): JSX.Element;
|
|
8
|
+
export declare namespace Viewport {
|
|
9
|
+
var cosmosCapture: boolean;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { FixtureContext } from './FixtureContext.js';
|
|
4
|
+
export function Viewport({ children, width, height }) {
|
|
5
|
+
const { setFixtureState } = React.useContext(FixtureContext);
|
|
6
|
+
React.useEffect(() => {
|
|
7
|
+
setFixtureState(fixtureState => ({
|
|
8
|
+
...fixtureState,
|
|
9
|
+
viewport: { width, height },
|
|
10
|
+
}));
|
|
11
|
+
}, [setFixtureState, width, height]);
|
|
12
|
+
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18051
|
|
13
|
+
return React.createElement(React.Fragment, null, children);
|
|
14
|
+
}
|
|
15
|
+
Viewport.cosmosCapture = false;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useCreateFixtureState } from './useCreateFixtureState.js';
|
|
2
|
+
import { useCurrentValue } from './useCurrentValue.js';
|
|
3
|
+
import { useSetValue } from './useSetValue.js';
|
|
4
|
+
export function useSelect(selectName, args) {
|
|
5
|
+
if (!args || !args.options || !args.options.length)
|
|
6
|
+
throw new Error('No options provided to useSelect');
|
|
7
|
+
useCreateFixtureState(selectName, args);
|
|
8
|
+
const currentValue = useCurrentValue(selectName, args);
|
|
9
|
+
const setValue = useSetValue(selectName);
|
|
10
|
+
return [currentValue, setValue];
|
|
11
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type UseSelectArgs<Option extends string> = {
|
|
2
|
+
options: Option[];
|
|
3
|
+
defaultValue?: Option;
|
|
4
|
+
};
|
|
5
|
+
export type SetSelectValue<Option extends string> = (value: Option) => void;
|
|
6
|
+
export declare function getDefaultSelectValue<Option extends string>({ options, defaultValue, }: UseSelectArgs<Option>): Option;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React, { useEffect } from 'react';
|
|
2
|
+
import { findFixtureStateControl } from 'react-cosmos-core';
|
|
3
|
+
import { FixtureContext } from '../FixtureContext.js';
|
|
4
|
+
import { getDefaultSelectValue } from './shared.js';
|
|
5
|
+
export function useCreateFixtureState(selectName, args) {
|
|
6
|
+
const { setFixtureState } = React.useContext(FixtureContext);
|
|
7
|
+
const defaultValue = getDefaultSelectValue(args);
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
// The fixture state for this select is (re)created in two situations:
|
|
10
|
+
// 1. Initially: No corresponding fixture state select is found
|
|
11
|
+
// 2: Default value change: Current value is reset to new default value
|
|
12
|
+
setFixtureState(prevFs => {
|
|
13
|
+
const fsControl = findFixtureStateControl(prevFs, selectName);
|
|
14
|
+
if (fsControl &&
|
|
15
|
+
fsControl.type === 'select' &&
|
|
16
|
+
fsControl.defaultValue === defaultValue)
|
|
17
|
+
return prevFs;
|
|
18
|
+
return {
|
|
19
|
+
...prevFs,
|
|
20
|
+
controls: {
|
|
21
|
+
...prevFs.controls,
|
|
22
|
+
[selectName]: {
|
|
23
|
+
type: 'select',
|
|
24
|
+
options: args.options,
|
|
25
|
+
defaultValue,
|
|
26
|
+
currentValue: defaultValue,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}, [args.options, defaultValue, selectName, setFixtureState]);
|
|
32
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { findFixtureStateControl } from 'react-cosmos-core';
|
|
3
|
+
import { FixtureContext } from '../FixtureContext.js';
|
|
4
|
+
import { getDefaultSelectValue } from './shared.js';
|
|
5
|
+
export function useCurrentValue(selectName, args) {
|
|
6
|
+
const { fixtureState } = useContext(FixtureContext);
|
|
7
|
+
const fsControl = findFixtureStateControl(fixtureState, selectName);
|
|
8
|
+
return fsControl && fsControl.type === 'select'
|
|
9
|
+
? fsControl.currentValue
|
|
10
|
+
: getDefaultSelectValue(args);
|
|
11
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useCallback, useContext } from 'react';
|
|
2
|
+
import { findFixtureStateControl } from 'react-cosmos-core';
|
|
3
|
+
import { FixtureContext } from '../FixtureContext.js';
|
|
4
|
+
export function useSetValue(selectName) {
|
|
5
|
+
const { setFixtureState } = useContext(FixtureContext);
|
|
6
|
+
return useCallback(value => {
|
|
7
|
+
setFixtureState(prevFs => {
|
|
8
|
+
const fsControl = findFixtureStateControl(prevFs, selectName);
|
|
9
|
+
if (!fsControl || fsControl.type !== 'select') {
|
|
10
|
+
console.warn(`Invalid fixture state for select: ${selectName}`);
|
|
11
|
+
return prevFs;
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
...prevFs,
|
|
15
|
+
controls: {
|
|
16
|
+
...prevFs.controls,
|
|
17
|
+
[selectName]: {
|
|
18
|
+
...fsControl,
|
|
19
|
+
currentValue: value,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
}, [selectName, setFixtureState]);
|
|
25
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FixtureStateData } from 'react-cosmos-core';
|
|
2
|
+
import { SetValue } from './shared.js';
|
|
3
|
+
type Opts<T extends FixtureStateData> = {
|
|
4
|
+
defaultValue: T;
|
|
5
|
+
};
|
|
6
|
+
export declare function useValue<T extends FixtureStateData>(inputName: string, { defaultValue }: Opts<T>): [T, SetValue<T>];
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useCreateFixtureState } from './useCreateFixtureState.js';
|
|
2
|
+
import { useCurrentValue } from './useCurrentValue.js';
|
|
3
|
+
import { useSetValue } from './useSetValue.js';
|
|
4
|
+
export function useValue(inputName, { defaultValue }) {
|
|
5
|
+
useCreateFixtureState(inputName, defaultValue);
|
|
6
|
+
const currentValue = useCurrentValue(inputName, defaultValue);
|
|
7
|
+
const setValue = useSetValue(inputName, defaultValue);
|
|
8
|
+
return [currentValue, setValue];
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { isEqual } from 'lodash-es';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { createValue, extendWithValue, findFixtureStateControl, } from 'react-cosmos-core';
|
|
4
|
+
import { FixtureContext } from '../FixtureContext.js';
|
|
5
|
+
export function useCreateFixtureState(inputName, defaultValue) {
|
|
6
|
+
const { setFixtureState } = React.useContext(FixtureContext);
|
|
7
|
+
React.useEffect(() => {
|
|
8
|
+
// The fixture state for this value is (re)created in two situations:
|
|
9
|
+
// 1. Initially: No corresponding fixture state value is found
|
|
10
|
+
// 2: Default value change: Current value is reset to new default value
|
|
11
|
+
setFixtureState(prevFs => {
|
|
12
|
+
const fsControl = findFixtureStateControl(prevFs, inputName);
|
|
13
|
+
if (fsControl &&
|
|
14
|
+
fsControl.type === 'standard' &&
|
|
15
|
+
fsValueExtendsBaseValue(fsControl.defaultValue, defaultValue))
|
|
16
|
+
return prevFs;
|
|
17
|
+
return {
|
|
18
|
+
...prevFs,
|
|
19
|
+
controls: {
|
|
20
|
+
...prevFs.controls,
|
|
21
|
+
[inputName]: {
|
|
22
|
+
type: 'standard',
|
|
23
|
+
defaultValue: createValue(defaultValue),
|
|
24
|
+
currentValue: createValue(defaultValue),
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
}, [setFixtureState, inputName, defaultValue]);
|
|
30
|
+
}
|
|
31
|
+
function fsValueExtendsBaseValue(fsValue, baseValue) {
|
|
32
|
+
return isEqual(baseValue, extendWithValue(baseValue, fsValue));
|
|
33
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { extendWithValue, findFixtureStateControl, } from 'react-cosmos-core';
|
|
3
|
+
import { FixtureContext } from '../FixtureContext.js';
|
|
4
|
+
export function useCurrentValue(inputName, defaultValue) {
|
|
5
|
+
const { fixtureState } = React.useContext(FixtureContext);
|
|
6
|
+
const fsControl = findFixtureStateControl(fixtureState, inputName);
|
|
7
|
+
return fsControl && fsControl.type === 'standard'
|
|
8
|
+
? // Types of fixture state values cannot be guaranteed at read time, which
|
|
9
|
+
// means that tampering with the fixture state can cause runtime errors
|
|
10
|
+
extendWithValue(defaultValue, fsControl.currentValue)
|
|
11
|
+
: defaultValue;
|
|
12
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createValue, extendWithValue, findFixtureStateControl, } from 'react-cosmos-core';
|
|
3
|
+
import { FixtureContext } from '../FixtureContext.js';
|
|
4
|
+
export function useSetValue(inputName, defaultValue) {
|
|
5
|
+
const { setFixtureState } = React.useContext(FixtureContext);
|
|
6
|
+
return React.useCallback(stateChange => {
|
|
7
|
+
setFixtureState(prevFs => {
|
|
8
|
+
const currentValue = typeof stateChange === 'function'
|
|
9
|
+
? stateChange(
|
|
10
|
+
// Types of fixture state values cannot be guaranteed at read
|
|
11
|
+
// time, which means that tampering with the fixture state can
|
|
12
|
+
// cause runtime errors
|
|
13
|
+
getCurrentValueFromFixtureState(prevFs, inputName, defaultValue))
|
|
14
|
+
: stateChange;
|
|
15
|
+
return {
|
|
16
|
+
...prevFs,
|
|
17
|
+
controls: {
|
|
18
|
+
...prevFs.controls,
|
|
19
|
+
[inputName]: {
|
|
20
|
+
type: 'standard',
|
|
21
|
+
defaultValue: createValue(defaultValue),
|
|
22
|
+
currentValue: createValue(currentValue),
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
}, [setFixtureState, defaultValue, inputName]);
|
|
28
|
+
}
|
|
29
|
+
function getCurrentValueFromFixtureState(fixtureState, inputName, defaultValue) {
|
|
30
|
+
const fsControl = findFixtureStateControl(fixtureState, inputName);
|
|
31
|
+
return fsControl && fsControl.type === 'standard'
|
|
32
|
+
? extendWithValue(defaultValue, fsControl.currentValue)
|
|
33
|
+
: defaultValue;
|
|
34
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ReactElement } from 'react';
|
|
2
|
+
import { ReactDecorator, UserModuleWrappers } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
moduleWrappers: UserModuleWrappers;
|
|
5
|
+
globalDecorators?: ReactDecorator[];
|
|
6
|
+
renderMessage?: (msg: string) => ReactElement;
|
|
7
|
+
};
|
|
8
|
+
export declare function ClientFixtureLoader({ moduleWrappers, globalDecorators, renderMessage, }: Props): JSX.Element;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { FixtureModule } from '../fixtureModule/FixtureModule.js';
|
|
4
|
+
import { LazyModuleLoader } from '../moduleLoaders/LazyModuleLoader.js';
|
|
5
|
+
import { StaticModuleLoader } from '../moduleLoaders/StaticModuleLoader.js';
|
|
6
|
+
import { FixtureLoaderConnect } from './FixtureLoaderConnect.js';
|
|
7
|
+
import { defaultRenderMessage } from './defaultRenderMessage.js';
|
|
8
|
+
import { useFixtureSelection } from './useFixtureSelection.js';
|
|
9
|
+
export function ClientFixtureLoader({ moduleWrappers, globalDecorators, renderMessage = defaultRenderMessage, }) {
|
|
10
|
+
const fixtureSelection = useFixtureSelection();
|
|
11
|
+
return (React.createElement(FixtureLoaderConnect, { moduleWrappers: moduleWrappers, fixtureSelection: fixtureSelection, renderMessage: renderMessage, renderFixture: selection => {
|
|
12
|
+
function renderModules(modules) {
|
|
13
|
+
return (React.createElement(FixtureModule, { ...modules, ...selection, globalDecorators: globalDecorators, lazy: moduleWrappers.lazy, renderMessage: renderMessage }));
|
|
14
|
+
}
|
|
15
|
+
const { fixtureId } = selection;
|
|
16
|
+
return moduleWrappers.lazy ? (React.createElement(LazyModuleLoader, { fixtureWrapper: moduleWrappers.fixtures[fixtureId.path], decorators: moduleWrappers.decorators, fixturePath: fixtureId.path, renderModules: renderModules })) : (React.createElement(StaticModuleLoader, { fixtureWrapper: moduleWrappers.fixtures[fixtureId.path], decorators: moduleWrappers.decorators, fixturePath: fixtureId.path, renderModules: renderModules }));
|
|
17
|
+
} }));
|
|
18
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { UserModuleWrappers } from 'react-cosmos-core';
|
|
3
|
+
import { FixtureSelection } from './useFixtureSelection.js';
|
|
4
|
+
type Props = {
|
|
5
|
+
moduleWrappers: UserModuleWrappers;
|
|
6
|
+
fixtureSelection: FixtureSelection | null;
|
|
7
|
+
renderMessage: (msg: string) => React.ReactElement;
|
|
8
|
+
renderFixture: (selection: FixtureSelection) => React.ReactElement;
|
|
9
|
+
};
|
|
10
|
+
export declare function FixtureLoaderConnect({ moduleWrappers, fixtureSelection, renderMessage, renderFixture, }: Props): JSX.Element;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { getFixtureListFromWrappers, isInsideWindowIframe, } from 'react-cosmos-core';
|
|
3
|
+
import { RendererSync } from './RendererSync.js';
|
|
4
|
+
export function FixtureLoaderConnect({ moduleWrappers, fixtureSelection, renderMessage, renderFixture, }) {
|
|
5
|
+
const fixtures = React.useMemo(() => getFixtureListFromWrappers(moduleWrappers), [moduleWrappers]);
|
|
6
|
+
function renderInner() {
|
|
7
|
+
if (!fixtureSelection) {
|
|
8
|
+
return isInsideWindowIframe()
|
|
9
|
+
? null
|
|
10
|
+
: renderMessage('No fixture selected.');
|
|
11
|
+
}
|
|
12
|
+
const { fixtureId } = fixtureSelection;
|
|
13
|
+
if (!fixtures[fixtureId.path]) {
|
|
14
|
+
return renderMessage(`Fixture path not found: ${fixtureId.path}`);
|
|
15
|
+
}
|
|
16
|
+
return renderFixture(fixtureSelection);
|
|
17
|
+
}
|
|
18
|
+
return React.createElement(RendererSync, { fixtures: fixtures }, renderInner());
|
|
19
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { RendererContext } from '../rendererConnect/RendererContext.js';
|
|
4
|
+
export function RendererSync({ children, fixtures }) {
|
|
5
|
+
const { searchParams, rendererId, rendererConnect, reloadRenderer, lazyItems, } = React.useContext(RendererContext);
|
|
6
|
+
const { fixtureId: selectedFixtureId } = searchParams;
|
|
7
|
+
const readyRef = React.useRef(false);
|
|
8
|
+
React.useEffect(() => {
|
|
9
|
+
if (!readyRef.current) {
|
|
10
|
+
rendererConnect.postMessage({
|
|
11
|
+
type: 'rendererReady',
|
|
12
|
+
payload: {
|
|
13
|
+
rendererId,
|
|
14
|
+
selectedFixtureId,
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
readyRef.current = true;
|
|
18
|
+
}
|
|
19
|
+
}, [rendererConnect, rendererId, selectedFixtureId]);
|
|
20
|
+
React.useEffect(() => {
|
|
21
|
+
rendererConnect.postMessage({
|
|
22
|
+
type: 'fixtureListUpdate',
|
|
23
|
+
payload: {
|
|
24
|
+
rendererId,
|
|
25
|
+
fixtures: { ...fixtures, ...lazyItems },
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}, [fixtures, lazyItems, rendererConnect, rendererId]);
|
|
29
|
+
React.useEffect(() => rendererConnect.onMessage(msg => {
|
|
30
|
+
if (msg.type === 'pingRenderers') {
|
|
31
|
+
rendererConnect.postMessage({
|
|
32
|
+
type: 'rendererReady',
|
|
33
|
+
payload: {
|
|
34
|
+
rendererId,
|
|
35
|
+
selectedFixtureId,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
rendererConnect.postMessage({
|
|
39
|
+
type: 'fixtureListUpdate',
|
|
40
|
+
payload: {
|
|
41
|
+
rendererId,
|
|
42
|
+
fixtures,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
else if (msg.type === 'reloadRenderer' &&
|
|
47
|
+
msg.payload.rendererId === rendererId) {
|
|
48
|
+
reloadRenderer();
|
|
49
|
+
}
|
|
50
|
+
}), [fixtures, reloadRenderer, rendererConnect, rendererId, selectedFixtureId]);
|
|
51
|
+
return React.createElement(React.Fragment, null, children);
|
|
52
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { RendererContext } from '../rendererConnect/RendererContext.js';
|
|
4
|
+
export function ServerFixtureChangeListener({ children }) {
|
|
5
|
+
const { rendererId, rendererConnect, searchParams, setSearchParams } = React.useContext(RendererContext);
|
|
6
|
+
React.useEffect(() => rendererConnect.onMessage(msg => {
|
|
7
|
+
if (!searchParams.locked &&
|
|
8
|
+
msg.type === 'selectFixture' &&
|
|
9
|
+
msg.payload.rendererId === rendererId) {
|
|
10
|
+
const { fixtureId } = msg.payload;
|
|
11
|
+
setSearchParams({ fixtureId });
|
|
12
|
+
}
|
|
13
|
+
else if (!searchParams.locked &&
|
|
14
|
+
msg.type === 'unselectFixture' &&
|
|
15
|
+
msg.payload.rendererId === rendererId) {
|
|
16
|
+
setSearchParams({});
|
|
17
|
+
}
|
|
18
|
+
}), [rendererConnect, rendererId, searchParams.locked, setSearchParams]);
|
|
19
|
+
return React.createElement(React.Fragment, null, children);
|
|
20
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ReactDecorator, StringRendererSearchParams, UserModuleWrappers } from 'react-cosmos-core';
|
|
3
|
+
type Props = {
|
|
4
|
+
searchParams: StringRendererSearchParams;
|
|
5
|
+
moduleWrappers: UserModuleWrappers;
|
|
6
|
+
globalDecorators?: ReactDecorator[];
|
|
7
|
+
renderMessage?: (msg: string) => React.ReactElement;
|
|
8
|
+
};
|
|
9
|
+
export declare function ServerFixtureLoader({ searchParams, moduleWrappers, globalDecorators, renderMessage, }: Props): JSX.Element;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React, { Suspense } from 'react';
|
|
2
|
+
import { decodeRendererSearchParams, } from 'react-cosmos-core';
|
|
3
|
+
import { FixtureModule } from '../fixtureModule/FixtureModule.js';
|
|
4
|
+
import { AsyncModuleLoader } from '../moduleLoaders/AsyncModuleLoader.js';
|
|
5
|
+
import { FixtureLoaderConnect } from './FixtureLoaderConnect.js';
|
|
6
|
+
import { ServerFixtureChangeListener } from './ServerFixtureChangeListener.js';
|
|
7
|
+
import { defaultRenderMessage } from './defaultRenderMessage.js';
|
|
8
|
+
export function ServerFixtureLoader({ searchParams, moduleWrappers, globalDecorators, renderMessage = defaultRenderMessage, }) {
|
|
9
|
+
const { fixtureId = null } = decodeRendererSearchParams(searchParams);
|
|
10
|
+
const fixtureSelection = fixtureId && {
|
|
11
|
+
fixtureId,
|
|
12
|
+
initialFixtureState: {},
|
|
13
|
+
renderKey: 0,
|
|
14
|
+
};
|
|
15
|
+
return (React.createElement(ServerFixtureChangeListener, null,
|
|
16
|
+
React.createElement(FixtureLoaderConnect, { moduleWrappers: moduleWrappers, fixtureSelection: fixtureSelection, renderMessage: renderMessage, renderFixture: selection => (
|
|
17
|
+
// The suspense boundary allows the rendererReady response to be sent
|
|
18
|
+
// before loading the fixture modules.
|
|
19
|
+
React.createElement(Suspense, null,
|
|
20
|
+
React.createElement(AsyncModuleLoader, { moduleWrappers: moduleWrappers, fixturePath: selection.fixtureId.path, renderModules: modules => (React.createElement(FixtureModule, { ...modules, ...selection, globalDecorators: globalDecorators, lazy: moduleWrappers.lazy, renderMessage: renderMessage })) }))) })));
|
|
21
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { RendererContext } from '../rendererConnect/RendererContext.js';
|
|
3
|
+
export function useFixtureSelection() {
|
|
4
|
+
const { rendererId, rendererConnect, searchParams } = React.useContext(RendererContext);
|
|
5
|
+
const { fixtureId: selectedFixtureId = null } = searchParams;
|
|
6
|
+
const [selection, setSelection] = React.useState(() => selectedFixtureId && {
|
|
7
|
+
fixtureId: selectedFixtureId,
|
|
8
|
+
initialFixtureState: {},
|
|
9
|
+
renderKey: 0,
|
|
10
|
+
});
|
|
11
|
+
React.useEffect(() => rendererConnect.onMessage(msg => {
|
|
12
|
+
if (!searchParams.locked &&
|
|
13
|
+
msg.type === 'selectFixture' &&
|
|
14
|
+
msg.payload.rendererId === rendererId) {
|
|
15
|
+
const { fixtureId, fixtureState } = msg.payload;
|
|
16
|
+
setSelection(prevState => {
|
|
17
|
+
return {
|
|
18
|
+
fixtureId,
|
|
19
|
+
initialFixtureState: fixtureState,
|
|
20
|
+
renderKey: prevState ? prevState.renderKey + 1 : 0,
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
else if (!searchParams.locked &&
|
|
25
|
+
msg.type === 'unselectFixture' &&
|
|
26
|
+
msg.payload.rendererId === rendererId) {
|
|
27
|
+
setSelection(null);
|
|
28
|
+
}
|
|
29
|
+
}), [rendererConnect, rendererId, searchParams.locked]);
|
|
30
|
+
return selection;
|
|
31
|
+
}
|