react-cosmos-renderer 6.0.0-canary.660d495.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/LICENSE +21 -0
  2. package/client.d.ts +1 -0
  3. package/client.js +1 -0
  4. package/dist/client.d.ts +7 -0
  5. package/dist/client.js +7 -0
  6. package/dist/fixture/FixtureCapture/FixtureCapture.d.ts +8 -0
  7. package/dist/fixture/FixtureCapture/FixtureCapture.js +17 -0
  8. package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/index.d.ts +5 -0
  9. package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/index.js +46 -0
  10. package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.d.ts +4 -0
  11. package/dist/fixture/FixtureCapture/classState/decorateFixtureRefs/isRefSupported.js +19 -0
  12. package/dist/fixture/FixtureCapture/classState/index.d.ts +3 -0
  13. package/dist/fixture/FixtureCapture/classState/index.js +13 -0
  14. package/dist/fixture/FixtureCapture/classState/shared.d.ts +18 -0
  15. package/dist/fixture/FixtureCapture/classState/shared.js +14 -0
  16. package/dist/fixture/FixtureCapture/classState/useFixtureState.d.ts +4 -0
  17. package/dist/fixture/FixtureCapture/classState/useFixtureState.js +153 -0
  18. package/dist/fixture/FixtureCapture/classState/useReadClassState.d.ts +4 -0
  19. package/dist/fixture/FixtureCapture/classState/useReadClassState.js +58 -0
  20. package/dist/fixture/FixtureCapture/props/index.d.ts +3 -0
  21. package/dist/fixture/FixtureCapture/props/index.js +74 -0
  22. package/dist/fixture/FixtureCapture/props/useFixtureProps.d.ts +3 -0
  23. package/dist/fixture/FixtureCapture/props/useFixtureProps.js +68 -0
  24. package/dist/fixture/FixtureCapture/shared/findRelevantElementPaths.d.ts +2 -0
  25. package/dist/fixture/FixtureCapture/shared/findRelevantElementPaths.js +23 -0
  26. package/dist/fixture/FixtureCapture/shared/nodeTree/findElementPaths.d.ts +2 -0
  27. package/dist/fixture/FixtureCapture/shared/nodeTree/findElementPaths.js +22 -0
  28. package/dist/fixture/FixtureCapture/shared/nodeTree/getElementAtPath.d.ts +3 -0
  29. package/dist/fixture/FixtureCapture/shared/nodeTree/getElementAtPath.js +26 -0
  30. package/dist/fixture/FixtureCapture/shared/nodeTree/index.d.ts +11 -0
  31. package/dist/fixture/FixtureCapture/shared/nodeTree/index.js +11 -0
  32. package/dist/fixture/FixtureCapture/shared/nodeTree/setElementAtPath.d.ts +2 -0
  33. package/dist/fixture/FixtureCapture/shared/nodeTree/setElementAtPath.js +35 -0
  34. package/dist/fixture/FixtureCapture/shared/nodeTree/shared.d.ts +2 -0
  35. package/dist/fixture/FixtureCapture/shared/nodeTree/shared.js +6 -0
  36. package/dist/fixture/FixtureContext.d.ts +7 -0
  37. package/dist/fixture/FixtureContext.js +5 -0
  38. package/dist/fixture/Viewport.d.ts +11 -0
  39. package/dist/fixture/Viewport.js +15 -0
  40. package/dist/fixture/useSelect/index.d.ts +2 -0
  41. package/dist/fixture/useSelect/index.js +11 -0
  42. package/dist/fixture/useSelect/shared.d.ts +6 -0
  43. package/dist/fixture/useSelect/shared.js +3 -0
  44. package/dist/fixture/useSelect/useCreateFixtureState.d.ts +2 -0
  45. package/dist/fixture/useSelect/useCreateFixtureState.js +32 -0
  46. package/dist/fixture/useSelect/useCurrentValue.d.ts +2 -0
  47. package/dist/fixture/useSelect/useCurrentValue.js +11 -0
  48. package/dist/fixture/useSelect/useSetValue.d.ts +2 -0
  49. package/dist/fixture/useSelect/useSetValue.js +25 -0
  50. package/dist/fixture/useValue/index.d.ts +7 -0
  51. package/dist/fixture/useValue/index.js +9 -0
  52. package/dist/fixture/useValue/shared.d.ts +3 -0
  53. package/dist/fixture/useValue/shared.js +1 -0
  54. package/dist/fixture/useValue/useCreateFixtureState.d.ts +2 -0
  55. package/dist/fixture/useValue/useCreateFixtureState.js +33 -0
  56. package/dist/fixture/useValue/useCurrentValue.d.ts +2 -0
  57. package/dist/fixture/useValue/useCurrentValue.js +12 -0
  58. package/dist/fixture/useValue/useSetValue.d.ts +3 -0
  59. package/dist/fixture/useValue/useSetValue.js +34 -0
  60. package/dist/fixtureLoaders/ClientFixtureLoader.d.ts +9 -0
  61. package/dist/fixtureLoaders/ClientFixtureLoader.js +18 -0
  62. package/dist/fixtureLoaders/FixtureLoaderConnect.d.ts +11 -0
  63. package/dist/fixtureLoaders/FixtureLoaderConnect.js +19 -0
  64. package/dist/fixtureLoaders/RendererSync.d.ts +8 -0
  65. package/dist/fixtureLoaders/RendererSync.js +52 -0
  66. package/dist/fixtureLoaders/ServerFixtureChangeListener.d.ts +6 -0
  67. package/dist/fixtureLoaders/ServerFixtureChangeListener.js +20 -0
  68. package/dist/fixtureLoaders/ServerFixtureLoader.d.ts +10 -0
  69. package/dist/fixtureLoaders/ServerFixtureLoader.js +21 -0
  70. package/dist/fixtureLoaders/defaultRenderMessage.d.ts +2 -0
  71. package/dist/fixtureLoaders/defaultRenderMessage.js +4 -0
  72. package/dist/fixtureLoaders/useFixtureSelection.d.ts +7 -0
  73. package/dist/fixtureLoaders/useFixtureSelection.js +31 -0
  74. package/dist/fixtureModule/DecoratedFixture.d.ts +9 -0
  75. package/dist/fixtureModule/DecoratedFixture.js +13 -0
  76. package/dist/fixtureModule/FixtureModule.d.ts +14 -0
  77. package/dist/fixtureModule/FixtureModule.js +13 -0
  78. package/dist/fixtureModule/FixtureProvider.d.ts +11 -0
  79. package/dist/fixtureModule/FixtureProvider.js +74 -0
  80. package/dist/fixtureModule/createFixtureNode.d.ts +3 -0
  81. package/dist/fixtureModule/createFixtureNode.js +19 -0
  82. package/dist/fixtureModule/decorateFixture.d.ts +3 -0
  83. package/dist/fixtureModule/decorateFixture.js +4 -0
  84. package/dist/index.d.ts +6 -0
  85. package/dist/index.js +6 -0
  86. package/dist/moduleLoaders/AsyncModuleLoader.d.ts +9 -0
  87. package/dist/moduleLoaders/AsyncModuleLoader.js +14 -0
  88. package/dist/moduleLoaders/LazyModuleLoader.d.ts +10 -0
  89. package/dist/moduleLoaders/LazyModuleLoader.js +25 -0
  90. package/dist/moduleLoaders/StaticModuleLoader.d.ts +10 -0
  91. package/dist/moduleLoaders/StaticModuleLoader.js +8 -0
  92. package/dist/moduleLoaders/importLazyFixtureModules.d.ts +5 -0
  93. package/dist/moduleLoaders/importLazyFixtureModules.js +6 -0
  94. package/dist/rendererConnect/RendererContext.d.ts +10 -0
  95. package/dist/rendererConnect/RendererContext.js +11 -0
  96. package/dist/rendererConnect/createNoopRendererConnect.d.ts +2 -0
  97. package/dist/rendererConnect/createNoopRendererConnect.js +8 -0
  98. package/dist/rendererConnect/createPostMessageConnect.d.ts +2 -0
  99. package/dist/rendererConnect/createPostMessageConnect.js +22 -0
  100. package/dist/rendererConnect/createWebSocketsConnect.d.ts +2 -0
  101. package/dist/rendererConnect/createWebSocketsConnect.js +32 -0
  102. package/index.d.ts +1 -0
  103. package/index.js +1 -0
  104. 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,2 @@
1
+ import { ReactElement, ReactNode } from 'react';
2
+ export declare function setElementAtPath(node: ReactNode, elPath: string, updater: (el: ReactElement) => ReactElement): ReactNode;
@@ -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,2 @@
1
+ export declare function getChildrenPath(elPath: string): string;
2
+ export declare function isRootPath(elPath: string): boolean;
@@ -0,0 +1,6 @@
1
+ export function getChildrenPath(elPath) {
2
+ return isRootPath(elPath) ? 'props.children' : `${elPath}.props.children`;
3
+ }
4
+ export function isRootPath(elPath) {
5
+ return elPath === '';
6
+ }
@@ -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,5 @@
1
+ import React from 'react';
2
+ export const FixtureContext = React.createContext({
3
+ fixtureState: {},
4
+ setFixtureState: () => { },
5
+ });
@@ -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,2 @@
1
+ import { SetSelectValue, UseSelectArgs } from './shared.js';
2
+ export declare function useSelect<Option extends string>(selectName: string, args: UseSelectArgs<Option>): [Option, SetSelectValue<Option>];
@@ -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,3 @@
1
+ export function getDefaultSelectValue({ options, defaultValue, }) {
2
+ return typeof defaultValue === 'string' ? defaultValue : options[0];
3
+ }
@@ -0,0 +1,2 @@
1
+ import { UseSelectArgs } from './shared.js';
2
+ export declare function useCreateFixtureState<Option extends string>(selectName: string, args: UseSelectArgs<Option>): void;
@@ -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,2 @@
1
+ import { UseSelectArgs } from './shared.js';
2
+ export declare function useCurrentValue<Option extends string>(selectName: string, args: UseSelectArgs<Option>): Option;
@@ -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,2 @@
1
+ import { SetSelectValue } from './shared.js';
2
+ export declare function useSetValue<Option extends string>(selectName: string): SetSelectValue<Option>;
@@ -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,3 @@
1
+ import React from 'react';
2
+ import { FixtureStateData } from 'react-cosmos-core';
3
+ export type SetValue<T extends FixtureStateData> = React.Dispatch<React.SetStateAction<T>>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { FixtureStateData } from 'react-cosmos-core';
2
+ export declare function useCreateFixtureState(inputName: string, defaultValue: FixtureStateData): void;
@@ -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,2 @@
1
+ import { FixtureStateData } from 'react-cosmos-core';
2
+ export declare function useCurrentValue<T extends FixtureStateData>(inputName: string, defaultValue: T): T;
@@ -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,3 @@
1
+ import { FixtureStateData } from 'react-cosmos-core';
2
+ import { SetValue } from './shared.js';
3
+ export declare function useSetValue<T extends FixtureStateData>(inputName: string, defaultValue: T): SetValue<T>;
@@ -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,8 @@
1
+ import React from 'react';
2
+ import { FixtureList } from 'react-cosmos-core';
3
+ type Props = {
4
+ children: React.ReactNode;
5
+ fixtures: FixtureList;
6
+ };
7
+ export declare function RendererSync({ children, fixtures }: Props): JSX.Element;
8
+ export {};
@@ -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 } = 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,
26
+ },
27
+ });
28
+ }, [fixtures, 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,6 @@
1
+ import React from 'react';
2
+ type Props = {
3
+ children: React.ReactNode;
4
+ };
5
+ export declare function ServerFixtureChangeListener({ children }: Props): JSX.Element;
6
+ export {};
@@ -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,2 @@
1
+ /// <reference types="react" />
2
+ export declare function defaultRenderMessage(msg: string): JSX.Element;
@@ -0,0 +1,4 @@
1
+ import React from 'react';
2
+ export function defaultRenderMessage(msg) {
3
+ return React.createElement(React.Fragment, null, msg);
4
+ }
@@ -0,0 +1,7 @@
1
+ import { FixtureId, FixtureState } from 'react-cosmos-core';
2
+ export type FixtureSelection = {
3
+ fixtureId: FixtureId;
4
+ initialFixtureState: FixtureState;
5
+ renderKey: number;
6
+ };
7
+ export declare function useFixtureSelection(): FixtureSelection | null;
@@ -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
+ }