react-cosmos-renderer 6.0.0-beta.8 → 6.0.0-canary.01bd836.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 (30) hide show
  1. package/dist/client.d.ts +1 -0
  2. package/dist/client.js +1 -0
  3. package/dist/fixture/FixtureCapture/FixtureCapture.d.ts +1 -1
  4. package/dist/fixture/FixtureCapture/FixtureCapture.js +1 -3
  5. package/dist/fixture/FixtureCapture/classState/index.js +2 -2
  6. package/dist/fixture/FixtureCapture/classState/{useFixtureState.d.ts → useFixtureClassState.d.ts} +1 -1
  7. package/dist/fixture/FixtureCapture/classState/{useFixtureState.js → useFixtureClassState.js} +31 -47
  8. package/dist/fixture/FixtureCapture/classState/useReadClassState.js +10 -15
  9. package/dist/fixture/FixtureCapture/props/index.js +21 -37
  10. package/dist/fixture/FixtureCapture/props/useFixtureProps.d.ts +2 -2
  11. package/dist/fixture/FixtureCapture/props/useFixtureProps.js +6 -6
  12. package/dist/fixture/Viewport.d.ts +1 -1
  13. package/dist/fixture/Viewport.js +5 -9
  14. package/dist/fixture/useFixtureState.d.ts +2 -0
  15. package/dist/fixture/useFixtureState.js +12 -0
  16. package/dist/fixture/useSelect/useCreateFixtureState.js +12 -16
  17. package/dist/fixture/useSelect/useCurrentValue.js +5 -7
  18. package/dist/fixture/useSelect/useSetValue.js +9 -13
  19. package/dist/fixture/useValue/useCreateFixtureState.js +11 -14
  20. package/dist/fixture/useValue/useCurrentValue.js +6 -7
  21. package/dist/fixture/useValue/useSetValue.js +10 -13
  22. package/dist/fixtureLoaders/ClientFixtureLoader.d.ts +2 -2
  23. package/dist/fixtureLoaders/FixtureLoaderConnect.d.ts +2 -2
  24. package/dist/fixtureLoaders/RendererSync.d.ts +1 -1
  25. package/dist/fixtureLoaders/RendererSync.js +1 -1
  26. package/dist/fixtureLoaders/ServerFixtureLoader.d.ts +1 -1
  27. package/dist/fixtureLoaders/defaultRenderMessage.d.ts +1 -2
  28. package/dist/fixtureLoaders/defaultRenderMessage.js +1 -2
  29. package/dist/fixtureModule/FixtureModule.d.ts +3 -2
  30. package/package.json +3 -3
package/dist/client.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './fixture/FixtureCapture/FixtureCapture.js';
2
2
  export * from './fixture/FixtureContext.js';
3
3
  export * from './fixture/Viewport.js';
4
+ export * from './fixture/useFixtureState.js';
4
5
  export * from './fixture/useSelect/index.js';
5
6
  export * from './fixture/useValue/index.js';
6
7
  export * from './fixtureLoaders/ClientFixtureLoader.js';
package/dist/client.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './fixture/FixtureCapture/FixtureCapture.js';
2
2
  export * from './fixture/FixtureContext.js';
3
3
  export * from './fixture/Viewport.js';
4
+ export * from './fixture/useFixtureState.js';
4
5
  export * from './fixture/useSelect/index.js';
5
6
  export * from './fixture/useValue/index.js';
6
7
  export * from './fixtureLoaders/ClientFixtureLoader.js';
@@ -4,5 +4,5 @@ type Props = {
4
4
  children: React.ReactNode;
5
5
  decoratorId: FixtureDecoratorId;
6
6
  };
7
- export declare function FixtureCapture({ children, decoratorId }: Props): React.JSX.Element;
7
+ export declare function FixtureCapture({ children, decoratorId }: Props): React.ReactNode;
8
8
  export {};
@@ -6,12 +6,10 @@
6
6
  // 1. Props of client side components used in server fixtures.
7
7
  // 2. Props of native elements rendered by server components (eg. div, p, etc.)
8
8
  'use client';
9
- import React from 'react';
10
9
  import { useClassStateCapture } from './classState/index.js';
11
10
  import { usePropsCapture } from './props/index.js';
12
11
  export function FixtureCapture({ children, decoratorId }) {
13
12
  let fixture = usePropsCapture(children, decoratorId);
14
13
  fixture = useClassStateCapture(fixture, decoratorId);
15
- // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18051
16
- return React.createElement(React.Fragment, null, fixture);
14
+ return fixture;
17
15
  }
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { useFixtureState } from './useFixtureState.js';
2
+ import { useFixtureClassState } from './useFixtureClassState.js';
3
3
  import { useReadClassState } from './useReadClassState.js';
4
4
  export function useClassStateCapture(fixture, decoratorId) {
5
5
  const elRefs = React.useRef({});
@@ -9,5 +9,5 @@ export function useClassStateCapture(fixture, decoratorId) {
9
9
  };
10
10
  }, []);
11
11
  useReadClassState(fixture, decoratorId, elRefs);
12
- return useFixtureState(fixture, decoratorId, elRefs);
12
+ return useFixtureClassState(fixture, decoratorId, elRefs);
13
13
  }
@@ -1,4 +1,4 @@
1
1
  import { MutableRefObject, ReactNode } from 'react';
2
2
  import { FixtureDecoratorId } from 'react-cosmos-core';
3
3
  import { ElRefs } from './shared.js';
4
- export declare function useFixtureState(fixture: ReactNode, decoratorId: FixtureDecoratorId, elRefs: MutableRefObject<ElRefs>): ReactNode;
4
+ export declare function useFixtureClassState(fixture: ReactNode, decoratorId: FixtureDecoratorId, elRefs: MutableRefObject<ElRefs>): ReactNode;
@@ -1,16 +1,16 @@
1
1
  import { isEqual } from 'lodash-es';
2
- import { useContext, useEffect, useRef, } from 'react';
3
- import { createFixtureStateClassState, createValues, extendWithValues, findFixtureStateClassState, getComponentName, getFixtureStateClassState, removeFixtureStateClassState, } from 'react-cosmos-core';
4
- import { FixtureContext } from '../../FixtureContext.js';
2
+ import { useEffect, useRef, } from 'react';
3
+ import { createClassStateFixtureStateItem, createValues, extendWithValues, filterClassStateFixtureState, findClassStateFixtureStateItem, getComponentName, removeClassStateFixtureStateItem, } from 'react-cosmos-core';
4
+ import { useFixtureState } from '../../useFixtureState.js';
5
5
  import { findRelevantElementPaths } from '../shared/findRelevantElementPaths.js';
6
6
  import { decorateFixtureRefs } from './decorateFixtureRefs/index.js';
7
7
  import { replaceState, } from './shared.js';
8
- export function useFixtureState(fixture, decoratorId, elRefs) {
8
+ export function useFixtureClassState(fixture, decoratorId, elRefs) {
9
9
  const elPaths = findRelevantElementPaths(fixture);
10
- const { fixtureState, setFixtureState } = useContext(FixtureContext);
11
- const lastFixtureState = useFixtureStateRef(fixtureState);
10
+ const [classStateFs, setClassStateFs] = useFixtureState('classState');
11
+ const lastFsRef = useFixtureStateRef(classStateFs);
12
12
  // Keep a copy of the previous fixture state to observe changes
13
- const prevFixtureState = useRef(fixtureState);
13
+ const prevFsRef = useRef(classStateFs);
14
14
  // Remember initial state of child components to use as a default when
15
15
  // resetting fixture state
16
16
  const initialStates = useRef({});
@@ -29,14 +29,11 @@ export function useFixtureState(fixture, decoratorId, elRefs) {
29
29
  // Remove fixture state for removed child elements (likely via HMR)
30
30
  // FIXME: Also invalidate fixture state at this element path if the
31
31
  // component type of the corresponding element changed
32
- const fsProps = getFixtureStateClassState(fixtureState, decoratorId);
33
- fsProps.forEach(({ elementId }) => {
32
+ const decoratorFs = filterClassStateFixtureState(classStateFs, decoratorId);
33
+ decoratorFs.forEach(({ elementId }) => {
34
34
  const { elPath } = elementId;
35
35
  if (elPaths.indexOf(elementId.elPath) === -1) {
36
- setFixtureState(prevFs => ({
37
- ...prevFs,
38
- classState: removeFixtureStateClassState(fixtureState, elementId),
39
- }));
36
+ setClassStateFs(prevFs => removeClassStateFixtureStateItem(prevFs, elementId));
40
37
  if (elRefs.current[elPath]) {
41
38
  delete elRefs.current[elPath];
42
39
  delete initialStates.current[elPath];
@@ -49,22 +46,19 @@ export function useFixtureState(fixture, decoratorId, elRefs) {
49
46
  // Component fixture state can be provided before the fixture mounts (eg.
50
47
  // a previous snapshot of a fixture state or the current fixture state
51
48
  // from another renderer)
52
- const fsClassState = findFixtureStateClassState(fixtureState, elementId);
53
- if (!fsClassState) {
49
+ const fsItem = findClassStateFixtureStateItem(classStateFs, elementId);
50
+ if (!fsItem) {
54
51
  if (initialStates.current[elPath]) {
55
52
  const { state } = initialStates.current[elPath];
56
53
  const elRef = elRefs.current[elPath];
57
54
  if (!isEqual(elRef.state, state)) {
58
55
  replaceState(elRef, state);
59
56
  }
60
- setFixtureState(prevFs => ({
61
- ...prevFs,
62
- classState: createFixtureStateClassState({
63
- fixtureState: prevFs,
64
- elementId,
65
- values: createValues(state),
66
- componentName: getComponentName(elRef.constructor),
67
- }),
57
+ setClassStateFs(prevFs => createClassStateFixtureStateItem({
58
+ classStateFs: prevFs,
59
+ elementId,
60
+ values: createValues(state),
61
+ componentName: getComponentName(elRef.constructor),
68
62
  }));
69
63
  }
70
64
  }
@@ -85,23 +79,16 @@ export function useFixtureState(fixture, decoratorId, elRefs) {
85
79
  // 2. The fixture state changed
86
80
  // Here we're interested in the second scenario. In the first scenario
87
81
  // we want to let the component state override the fixture state.
88
- const prevFsClassState = findFixtureStateClassState(prevFixtureState.current, elementId);
89
- if (prevFsClassState && !isEqual(prevFsClassState, fsClassState)) {
90
- return replaceState(elRef, extendWithValues(elRef.state, fsClassState.values));
82
+ const prevFsItem = findClassStateFixtureStateItem(prevFsRef.current, elementId);
83
+ if (prevFsItem && !isEqual(prevFsItem, fsItem)) {
84
+ return replaceState(elRef, extendWithValues(elRef.state, fsItem.values));
91
85
  }
92
86
  }
93
87
  });
94
- }, [
95
- decoratorId,
96
- elPaths,
97
- elRefs,
98
- fixtureState,
99
- fixtureState.classState,
100
- setFixtureState,
101
- ]);
88
+ }, [classStateFs, decoratorId, elPaths, elRefs, setClassStateFs]);
102
89
  // Update prev fixture state ref *after* running effects that reference it
103
90
  useEffect(() => {
104
- prevFixtureState.current = fixtureState;
91
+ prevFsRef.current = classStateFs;
105
92
  });
106
93
  return decorateFixtureRefs(fixture, handleRef, cachedRefHandlers.current);
107
94
  function handleRef(elPath, elRef) {
@@ -117,16 +104,13 @@ export function useFixtureState(fixture, decoratorId, elRefs) {
117
104
  elRefs.current[elPath] = elRef;
118
105
  setInitialState(initialStates.current, elPath, elRef);
119
106
  const elementId = { decoratorId, elPath };
120
- const fsClassState = findFixtureStateClassState(lastFixtureState.current, elementId);
107
+ const fsClassState = findClassStateFixtureStateItem(lastFsRef.current, elementId);
121
108
  if (!fsClassState) {
122
- setFixtureState(prevFs => ({
123
- ...prevFs,
124
- classState: createFixtureStateClassState({
125
- fixtureState: prevFs,
126
- elementId,
127
- values: createValues(state),
128
- componentName: getComponentName(elRef.constructor),
129
- }),
109
+ setClassStateFs(prevFs => createClassStateFixtureStateItem({
110
+ classStateFs: prevFs,
111
+ elementId,
112
+ values: createValues(state),
113
+ componentName: getComponentName(elRef.constructor),
130
114
  }));
131
115
  }
132
116
  else {
@@ -135,10 +119,10 @@ export function useFixtureState(fixture, decoratorId, elRefs) {
135
119
  }
136
120
  }
137
121
  // Make latest fixture state accessible in ref callback
138
- function useFixtureStateRef(fixtureState) {
139
- const ref = useRef(fixtureState);
122
+ function useFixtureStateRef(classStateFs) {
123
+ const ref = useRef(classStateFs);
140
124
  useEffect(() => {
141
- ref.current = fixtureState;
125
+ ref.current = classStateFs;
142
126
  });
143
127
  return ref;
144
128
  }
@@ -1,14 +1,14 @@
1
1
  import { isEqual } from 'lodash-es';
2
- import { useContext, useEffect, useRef, } from 'react';
3
- import { createValues, extendWithValues, findFixtureStateClassState, updateFixtureStateClassState, } from 'react-cosmos-core';
4
- import { FixtureContext } from '../../FixtureContext.js';
2
+ import { useEffect, useRef } from 'react';
3
+ import { createValues, extendWithValues, findClassStateFixtureStateItem, updateClassStateFixtureStateItem, } from 'react-cosmos-core';
4
+ import { useFixtureState } from '../../useFixtureState.js';
5
5
  import { findRelevantElementPaths } from '../shared/findRelevantElementPaths.js';
6
6
  // How often to check the state of the loaded component and update the fixture
7
7
  // state if it changed
8
8
  const REFRESH_INTERVAL = 200;
9
9
  export function useReadClassState(fixture, decoratorId, elRefs) {
10
10
  const elPaths = findRelevantElementPaths(fixture);
11
- const { fixtureState, setFixtureState } = useContext(FixtureContext);
11
+ const [classStateFs, setClassStateFs] = useFixtureState('classState');
12
12
  const timeoutId = useRef(null);
13
13
  useEffect(() => {
14
14
  // The check should run even if no element paths are found at mount, because
@@ -33,18 +33,13 @@ export function useReadClassState(fixture, decoratorId, elRefs) {
33
33
  }
34
34
  const { state } = elRefs.current[elPath];
35
35
  const elementId = { decoratorId, elPath };
36
- const fsClassState = findFixtureStateClassState(fixtureState, elementId);
37
- if (fsClassState &&
38
- state &&
39
- !doesFixtureStateMatchClassState(fsClassState, state)) {
36
+ const fsItem = findClassStateFixtureStateItem(classStateFs, elementId);
37
+ if (fsItem && state && !doesFixtureStateMatchClassState(fsItem, state)) {
40
38
  fixtureStateChangeScheduled = true;
41
- setFixtureState(prevFs => ({
42
- ...prevFs,
43
- classState: updateFixtureStateClassState({
44
- fixtureState: prevFs,
45
- elementId,
46
- values: createValues(state),
47
- }),
39
+ setClassStateFs(prevFs => updateClassStateFixtureStateItem({
40
+ classStateFs: prevFs,
41
+ elementId,
42
+ values: createValues(state),
48
43
  }));
49
44
  }
50
45
  });
@@ -1,30 +1,27 @@
1
- import { useContext, useEffect, useRef } from 'react';
2
- import { areNodesEqual, createFixtureStateProps, createValues, findFixtureStateProps, getComponentName, getFixtureStateProps, removeFixtureStateProps, updateFixtureStateProps, } from 'react-cosmos-core';
3
- import { FixtureContext } from '../../FixtureContext.js';
1
+ import { useEffect, useRef } from 'react';
2
+ import { areNodesEqual, createPropsFixtureStateItem, createValues, filterPropsFixtureState, findPropsFixtureStateItem, getComponentName, removePropsFixtureStateItem, updatePropsFixtureStateItem, } from 'react-cosmos-core';
3
+ import { useFixtureState } from '../../useFixtureState.js';
4
4
  import { findRelevantElementPaths } from '../shared/findRelevantElementPaths.js';
5
5
  import { getElementAtPath, getExpectedElementAtPath, } from '../shared/nodeTree/index.js';
6
6
  import { useFixtureProps } from './useFixtureProps.js';
7
7
  export function usePropsCapture(fixture, decoratorId) {
8
- const { fixtureState, setFixtureState } = useContext(FixtureContext);
8
+ const [propsFs, setPropsFs] = useFixtureState('props');
9
9
  const prevFixtureRef = useRef(fixture);
10
10
  const elPaths = findRelevantElementPaths(fixture);
11
11
  useEffect(() => {
12
12
  // Create empty fixture state
13
- if (!fixtureState.props && elPaths.length === 0) {
13
+ if (!propsFs && elPaths.length === 0) {
14
14
  // Make sure not to override any (currently pending) fixture state props
15
- setFixtureState(prevFs => ({ ...prevFs, props: prevFs.props || [] }));
15
+ setPropsFs(prevFs => prevFs ?? []);
16
16
  return;
17
17
  }
18
18
  // Remove fixture state for removed child elements (likely via HMR)
19
19
  // FIXME: Also invalidate fixture state at this element path if the
20
20
  // component type of the corresponding element changed
21
- const fsProps = getFixtureStateProps(fixtureState, decoratorId);
22
- fsProps.forEach(({ elementId }) => {
21
+ const decoratorFs = filterPropsFixtureState(propsFs, decoratorId);
22
+ decoratorFs.forEach(({ elementId }) => {
23
23
  if (elPaths.indexOf(elementId.elPath) === -1) {
24
- setFixtureState(prevFs => ({
25
- ...prevFs,
26
- props: removeFixtureStateProps(fixtureState, elementId),
27
- }));
24
+ setPropsFs(prevFs => removePropsFixtureStateItem(prevFs, elementId));
28
25
  }
29
26
  });
30
27
  elPaths.forEach(elPath => {
@@ -33,42 +30,29 @@ export function usePropsCapture(fixture, decoratorId) {
33
30
  // Component fixture state can be provided before the fixture mounts (eg.
34
31
  // a previous snapshot of a fixture state or the current fixture state
35
32
  // from another renderer)
36
- if (!findFixtureStateProps(fixtureState, elementId)) {
33
+ if (!findPropsFixtureStateItem(propsFs, elementId)) {
37
34
  const componentName = getComponentName(childEl.type);
38
- setFixtureState(prevFs => ({
39
- ...prevFs,
40
- props: createFixtureStateProps({
41
- fixtureState: prevFs,
42
- elementId,
43
- values: createValues(childEl.props),
44
- componentName,
45
- }),
35
+ setPropsFs(prevFs => createPropsFixtureStateItem({
36
+ propsFs: prevFs,
37
+ elementId,
38
+ values: createValues(childEl.props),
39
+ componentName,
46
40
  }));
47
41
  }
48
42
  else {
49
43
  const prevChildEl = getElementAtPath(prevFixtureRef.current, elPath);
50
44
  if (!areNodesEqual(prevChildEl, childEl, false)) {
51
- setFixtureState(prevFs => ({
52
- ...prevFs,
53
- props: updateFixtureStateProps({
54
- fixtureState,
55
- elementId,
56
- values: createValues(childEl.props),
57
- }),
45
+ setPropsFs(prevFs => updatePropsFixtureStateItem({
46
+ propsFs: prevFs,
47
+ elementId,
48
+ values: createValues(childEl.props),
58
49
  }));
59
50
  }
60
51
  }
61
52
  });
62
- }, [
63
- fixture,
64
- decoratorId,
65
- elPaths,
66
- fixtureState,
67
- fixtureState.props,
68
- setFixtureState,
69
- ]);
53
+ }, [decoratorId, elPaths, fixture, propsFs, setPropsFs]);
70
54
  useEffect(() => {
71
55
  prevFixtureRef.current = fixture;
72
56
  });
73
- return useFixtureProps(fixture, fixtureState, decoratorId);
57
+ return useFixtureProps(fixture, propsFs, decoratorId);
74
58
  }
@@ -1,3 +1,3 @@
1
1
  import React from 'react';
2
- import { FixtureDecoratorId, FixtureState } from 'react-cosmos-core';
3
- export declare function useFixtureProps(fixture: React.ReactNode, fixtureState: FixtureState, decoratorId: FixtureDecoratorId): React.ReactNode;
2
+ import { FixtureDecoratorId, PropsFixtureState } from 'react-cosmos-core';
3
+ export declare function useFixtureProps(fixture: React.ReactNode, propsFs: PropsFixtureState | undefined, decoratorId: FixtureDecoratorId): React.ReactNode;
@@ -1,9 +1,9 @@
1
1
  import { isEqual, mapValues } from 'lodash-es';
2
2
  import React from 'react';
3
- import { DEFAULT_RENDER_KEY, extendWithValues, findFixtureStateProps, getComponentName, } from 'react-cosmos-core';
3
+ import { DEFAULT_RENDER_KEY, extendWithValues, findPropsFixtureStateItem, getComponentName, } from 'react-cosmos-core';
4
4
  import { findRelevantElementPaths } from '../shared/findRelevantElementPaths.js';
5
5
  import { getChildrenPath, setElementAtPath } from '../shared/nodeTree/index.js';
6
- export function useFixtureProps(fixture, fixtureState, decoratorId) {
6
+ export function useFixtureProps(fixture, propsFs, decoratorId) {
7
7
  const propCache = React.useMemo(() => ({}),
8
8
  // React.useMemo is used as a cache invalidated by decoratorId
9
9
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -11,9 +11,9 @@ export function useFixtureProps(fixture, fixtureState, decoratorId) {
11
11
  const elPaths = findRelevantElementPaths(fixture);
12
12
  return elPaths.reduce((extendedFixture, elPath) => {
13
13
  const elementId = { decoratorId, elPath };
14
- const fsProps = findFixtureStateProps(fixtureState, elementId);
14
+ const fsItem = findPropsFixtureStateItem(propsFs, elementId);
15
15
  return setElementAtPath(extendedFixture, elPath, element => {
16
- if (!fsProps || componentTypeChanged(fsProps.componentName)) {
16
+ if (!fsItem || componentTypeChanged(fsItem.componentName)) {
17
17
  return {
18
18
  ...element,
19
19
  key: getElRenderKey(elPath, DEFAULT_RENDER_KEY),
@@ -23,7 +23,7 @@ export function useFixtureProps(fixture, fixtureState, decoratorId) {
23
23
  // stored in fixture state
24
24
  // See https://github.com/react-cosmos/react-cosmos/pull/920 for context
25
25
  const originalProps = element.props;
26
- const extendedProps = extendWithValues(originalProps, fsProps.values);
26
+ const extendedProps = extendWithValues(originalProps, fsItem.values);
27
27
  // Preserve identity between renders for indentical non-primitive props
28
28
  const cachedProps = mapValues(extendedProps, (value, propName) => {
29
29
  const key = getPropCacheKey(elPath, propName);
@@ -49,7 +49,7 @@ export function useFixtureProps(fixture, fixtureState, decoratorId) {
49
49
  props: hasChildElPaths(elPaths, elPath)
50
50
  ? { ...cachedProps, children: originalProps.children }
51
51
  : cachedProps,
52
- key: getElRenderKey(elPath, fsProps.renderKey),
52
+ key: getElRenderKey(elPath, fsItem.renderKey),
53
53
  };
54
54
  function componentTypeChanged(componentName) {
55
55
  return componentName !== getComponentName(element.type);
@@ -4,7 +4,7 @@ type Props = {
4
4
  width: number;
5
5
  height: number;
6
6
  };
7
- export declare function Viewport({ children, width, height }: Props): React.JSX.Element;
7
+ export declare function Viewport({ children, width, height }: Props): React.ReactNode;
8
8
  export declare namespace Viewport {
9
9
  var cosmosCapture: boolean;
10
10
  }
@@ -1,15 +1,11 @@
1
1
  'use client';
2
2
  import React from 'react';
3
- import { FixtureContext } from './FixtureContext.js';
3
+ import { useFixtureState } from './useFixtureState.js';
4
4
  export function Viewport({ children, width, height }) {
5
- const { setFixtureState } = React.useContext(FixtureContext);
5
+ const [, setViewport] = useFixtureState('viewport');
6
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);
7
+ setViewport({ width, height });
8
+ }, [setViewport, width, height]);
9
+ return children;
14
10
  }
15
11
  Viewport.cosmosCapture = false;
@@ -0,0 +1,2 @@
1
+ import { FixtureStateChange } from 'react-cosmos-core';
2
+ export declare function useFixtureState<T>(name: string): readonly [T | undefined, (change: FixtureStateChange<T>) => void];
@@ -0,0 +1,12 @@
1
+ import { useCallback, useContext } from 'react';
2
+ import { fixtureStateByName, updateFixtureState, } from 'react-cosmos-core';
3
+ import { FixtureContext } from './FixtureContext.js';
4
+ export function useFixtureState(name) {
5
+ const { fixtureState, setFixtureState } = useContext(FixtureContext);
6
+ return [
7
+ fixtureStateByName(fixtureState, name),
8
+ useCallback((change) => {
9
+ setFixtureState(prevFs => updateFixtureState(prevFs, name, change));
10
+ }, [name, setFixtureState]),
11
+ ];
12
+ }
@@ -1,30 +1,26 @@
1
- import React, { useEffect } from 'react';
2
- import { findFixtureStateControl } from 'react-cosmos-core';
3
- import { FixtureContext } from '../FixtureContext.js';
1
+ import { useEffect } from 'react';
2
+ import { useFixtureState } from '../useFixtureState.js';
4
3
  import { getDefaultSelectValue } from './shared.js';
5
4
  export function useCreateFixtureState(selectName, args) {
6
- const { setFixtureState } = React.useContext(FixtureContext);
5
+ const [, setFixtureState] = useFixtureState('controls');
7
6
  const defaultValue = getDefaultSelectValue(args);
8
7
  useEffect(() => {
9
8
  // The fixture state for this select is (re)created in two situations:
10
9
  // 1. Initially: No corresponding fixture state select is found
11
10
  // 2: Default value change: Current value is reset to new default value
12
11
  setFixtureState(prevFs => {
13
- const fsControl = findFixtureStateControl(prevFs, selectName);
14
- if (fsControl &&
15
- fsControl.type === 'select' &&
16
- fsControl.defaultValue === defaultValue)
12
+ const controlFs = prevFs && prevFs[selectName];
13
+ if (controlFs &&
14
+ controlFs.type === 'select' &&
15
+ controlFs.defaultValue === defaultValue)
17
16
  return prevFs;
18
17
  return {
19
18
  ...prevFs,
20
- controls: {
21
- ...prevFs.controls,
22
- [selectName]: {
23
- type: 'select',
24
- options: args.options,
25
- defaultValue,
26
- currentValue: defaultValue,
27
- },
19
+ [selectName]: {
20
+ type: 'select',
21
+ options: args.options,
22
+ defaultValue,
23
+ currentValue: defaultValue,
28
24
  },
29
25
  };
30
26
  });
@@ -1,11 +1,9 @@
1
- import { useContext } from 'react';
2
- import { findFixtureStateControl } from 'react-cosmos-core';
3
- import { FixtureContext } from '../FixtureContext.js';
1
+ import { useFixtureState } from '../useFixtureState.js';
4
2
  import { getDefaultSelectValue } from './shared.js';
5
3
  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
4
+ const [fixtureState] = useFixtureState('controls');
5
+ const controlFs = fixtureState && fixtureState[selectName];
6
+ return controlFs && controlFs.type === 'select'
7
+ ? controlFs.currentValue
10
8
  : getDefaultSelectValue(args);
11
9
  }
@@ -1,23 +1,19 @@
1
- import { useCallback, useContext } from 'react';
2
- import { findFixtureStateControl } from 'react-cosmos-core';
3
- import { FixtureContext } from '../FixtureContext.js';
1
+ import { useCallback } from 'react';
2
+ import { useFixtureState } from '../useFixtureState.js';
4
3
  export function useSetValue(selectName) {
5
- const { setFixtureState } = useContext(FixtureContext);
4
+ const [, setFixtureState] = useFixtureState('controls');
6
5
  return useCallback(value => {
7
6
  setFixtureState(prevFs => {
8
- const fsControl = findFixtureStateControl(prevFs, selectName);
9
- if (!fsControl || fsControl.type !== 'select') {
7
+ const controlFs = prevFs && prevFs[selectName];
8
+ if (!controlFs || controlFs.type !== 'select') {
10
9
  console.warn(`Invalid fixture state for select: ${selectName}`);
11
- return prevFs;
10
+ return prevFs ?? {};
12
11
  }
13
12
  return {
14
13
  ...prevFs,
15
- controls: {
16
- ...prevFs.controls,
17
- [selectName]: {
18
- ...fsControl,
19
- currentValue: value,
20
- },
14
+ [selectName]: {
15
+ ...controlFs,
16
+ currentValue: value,
21
17
  },
22
18
  };
23
19
  });
@@ -1,28 +1,25 @@
1
1
  import { isEqual } from 'lodash-es';
2
2
  import React from 'react';
3
- import { createValue, extendWithValue, findFixtureStateControl, } from 'react-cosmos-core';
4
- import { FixtureContext } from '../FixtureContext.js';
3
+ import { createValue, extendWithValue, } from 'react-cosmos-core';
4
+ import { useFixtureState } from '../useFixtureState.js';
5
5
  export function useCreateFixtureState(inputName, defaultValue) {
6
- const { setFixtureState } = React.useContext(FixtureContext);
6
+ const [, setFixtureState] = useFixtureState('controls');
7
7
  React.useEffect(() => {
8
8
  // The fixture state for this value is (re)created in two situations:
9
9
  // 1. Initially: No corresponding fixture state value is found
10
10
  // 2: Default value change: Current value is reset to new default value
11
11
  setFixtureState(prevFs => {
12
- const fsControl = findFixtureStateControl(prevFs, inputName);
13
- if (fsControl &&
14
- fsControl.type === 'standard' &&
15
- fsValueExtendsBaseValue(fsControl.defaultValue, defaultValue))
12
+ const controlFs = prevFs && prevFs[inputName];
13
+ if (controlFs &&
14
+ controlFs.type === 'standard' &&
15
+ fsValueExtendsBaseValue(controlFs.defaultValue, defaultValue))
16
16
  return prevFs;
17
17
  return {
18
18
  ...prevFs,
19
- controls: {
20
- ...prevFs.controls,
21
- [inputName]: {
22
- type: 'standard',
23
- defaultValue: createValue(defaultValue),
24
- currentValue: createValue(defaultValue),
25
- },
19
+ [inputName]: {
20
+ type: 'standard',
21
+ defaultValue: createValue(defaultValue),
22
+ currentValue: createValue(defaultValue),
26
23
  },
27
24
  };
28
25
  });
@@ -1,12 +1,11 @@
1
- import React from 'react';
2
- import { extendWithValue, findFixtureStateControl, } from 'react-cosmos-core';
3
- import { FixtureContext } from '../FixtureContext.js';
1
+ import { extendWithValue, } from 'react-cosmos-core';
2
+ import { useFixtureState } from '../useFixtureState.js';
4
3
  export function useCurrentValue(inputName, defaultValue) {
5
- const { fixtureState } = React.useContext(FixtureContext);
6
- const fsControl = findFixtureStateControl(fixtureState, inputName);
7
- return fsControl && fsControl.type === 'standard'
4
+ const [fixtureState] = useFixtureState('controls');
5
+ const controlFs = fixtureState && fixtureState[inputName];
6
+ return controlFs && controlFs.type === 'standard'
8
7
  ? // Types of fixture state values cannot be guaranteed at read time, which
9
8
  // means that tampering with the fixture state can cause runtime errors
10
- extendWithValue(defaultValue, fsControl.currentValue)
9
+ extendWithValue(defaultValue, controlFs.currentValue)
11
10
  : defaultValue;
12
11
  }
@@ -1,8 +1,8 @@
1
1
  import React from 'react';
2
- import { createValue, extendWithValue, findFixtureStateControl, } from 'react-cosmos-core';
3
- import { FixtureContext } from '../FixtureContext.js';
2
+ import { createValue, extendWithValue, } from 'react-cosmos-core';
3
+ import { useFixtureState } from '../useFixtureState.js';
4
4
  export function useSetValue(inputName, defaultValue) {
5
- const { setFixtureState } = React.useContext(FixtureContext);
5
+ const [, setFixtureState] = useFixtureState('controls');
6
6
  return React.useCallback(stateChange => {
7
7
  setFixtureState(prevFs => {
8
8
  const currentValue = typeof stateChange === 'function'
@@ -14,21 +14,18 @@ export function useSetValue(inputName, defaultValue) {
14
14
  : stateChange;
15
15
  return {
16
16
  ...prevFs,
17
- controls: {
18
- ...prevFs.controls,
19
- [inputName]: {
20
- type: 'standard',
21
- defaultValue: createValue(defaultValue),
22
- currentValue: createValue(currentValue),
23
- },
17
+ [inputName]: {
18
+ type: 'standard',
19
+ defaultValue: createValue(defaultValue),
20
+ currentValue: createValue(currentValue),
24
21
  },
25
22
  };
26
23
  });
27
24
  }, [setFixtureState, defaultValue, inputName]);
28
25
  }
29
26
  function getCurrentValueFromFixtureState(fixtureState, inputName, defaultValue) {
30
- const fsControl = findFixtureStateControl(fixtureState, inputName);
31
- return fsControl && fsControl.type === 'standard'
32
- ? extendWithValue(defaultValue, fsControl.currentValue)
27
+ const controlFs = fixtureState && fixtureState[inputName];
28
+ return controlFs && controlFs.type === 'standard'
29
+ ? extendWithValue(defaultValue, controlFs.currentValue)
33
30
  : defaultValue;
34
31
  }
@@ -1,9 +1,9 @@
1
- import React, { ReactElement } from 'react';
1
+ import React from 'react';
2
2
  import { ReactDecorator, UserModuleWrappers } from 'react-cosmos-core';
3
3
  type Props = {
4
4
  moduleWrappers: UserModuleWrappers;
5
5
  globalDecorators?: ReactDecorator[];
6
- renderMessage?: (msg: string) => ReactElement;
6
+ renderMessage?: (msg: string) => React.ReactNode;
7
7
  };
8
8
  export declare function ClientFixtureLoader({ moduleWrappers, globalDecorators, renderMessage, }: Props): React.JSX.Element;
9
9
  export {};
@@ -4,8 +4,8 @@ import { SelectedFixture } from './SelectedFixture.js';
4
4
  type Props = {
5
5
  moduleWrappers: UserModuleWrappers;
6
6
  selectedFixture: SelectedFixture | null;
7
- renderMessage: (msg: string) => React.ReactElement;
8
- renderFixture: (selected: SelectedFixture) => React.ReactElement;
7
+ renderMessage: (msg: string) => React.ReactNode;
8
+ renderFixture: (selected: SelectedFixture) => React.ReactNode;
9
9
  };
10
10
  export declare function FixtureLoaderConnect({ moduleWrappers, selectedFixture, renderMessage, renderFixture, }: Props): React.JSX.Element;
11
11
  export {};
@@ -4,5 +4,5 @@ type Props = {
4
4
  children: React.ReactNode;
5
5
  fixtures: FixtureList;
6
6
  };
7
- export declare function RendererSync({ children, fixtures }: Props): React.JSX.Element;
7
+ export declare function RendererSync({ children, fixtures }: Props): React.ReactNode;
8
8
  export {};
@@ -61,5 +61,5 @@ export function RendererSync({ children, fixtures }) {
61
61
  unselectFixture();
62
62
  }
63
63
  }), [locked, rendererConnect, rendererId, selectFixture, unselectFixture]);
64
- return React.createElement(React.Fragment, null, children);
64
+ return children;
65
65
  }
@@ -4,7 +4,7 @@ import { SelectedFixture } from './SelectedFixture.js';
4
4
  type Props = {
5
5
  moduleWrappers: UserModuleWrappers;
6
6
  globalDecorators?: ReactDecorator[];
7
- renderMessage?: (msg: string) => React.ReactElement;
7
+ renderMessage?: (msg: string) => React.ReactNode;
8
8
  selectedFixture: SelectedFixture | null;
9
9
  };
10
10
  export declare function ServerFixtureLoader({ moduleWrappers, globalDecorators, renderMessage, selectedFixture, }: Props): React.JSX.Element;
@@ -1,2 +1 @@
1
- import React from 'react';
2
- export declare function defaultRenderMessage(msg: string): React.JSX.Element;
1
+ export declare function defaultRenderMessage(msg: string): string;
@@ -1,4 +1,3 @@
1
- import React from 'react';
2
1
  export function defaultRenderMessage(msg) {
3
- return React.createElement(React.Fragment, null, msg);
2
+ return msg;
4
3
  }
@@ -1,3 +1,4 @@
1
+ /// <reference types="react/experimental" />
1
2
  import React from 'react';
2
3
  import { FixtureId, FixtureState, ReactDecorator, ReactDecoratorModule, ReactFixtureModule } from 'react-cosmos-core';
3
4
  type Props = {
@@ -8,7 +9,7 @@ type Props = {
8
9
  initialFixtureState?: FixtureState;
9
10
  renderKey: number;
10
11
  lazy: boolean;
11
- renderMessage: (msg: string) => React.ReactElement;
12
+ renderMessage: (msg: string) => React.ReactNode;
12
13
  };
13
- export declare function FixtureModule({ fixtureModule, decoratorModules, globalDecorators, fixtureId, initialFixtureState, renderKey, lazy, renderMessage, }: Props): React.JSX.Element;
14
+ export declare function FixtureModule({ fixtureModule, decoratorModules, globalDecorators, fixtureId, initialFixtureState, renderKey, lazy, renderMessage, }: Props): string | number | boolean | Iterable<React.ReactNode> | React.PromiseLikeOfReactNode | React.JSX.Element | null | undefined;
14
15
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-cosmos-renderer",
3
- "version": "6.0.0-beta.8",
3
+ "version": "6.0.0-canary.01bd836.0+01bd836",
4
4
  "description": "React Cosmos Renderer",
5
5
  "repository": "https://github.com/react-cosmos/react-cosmos/tree/main/packages/react-cosmos-renderer",
6
6
  "license": "MIT",
@@ -12,8 +12,8 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "lodash-es": "4.17.21",
15
- "react-cosmos-core": "6.0.0-beta.8",
15
+ "react-cosmos-core": "6.0.0-canary.01bd836.0+01bd836",
16
16
  "react-is": "18.2.0"
17
17
  },
18
- "gitHead": "2f47b29e24c6a68c4a62b7862d67909905648f7e"
18
+ "gitHead": "01bd836748e8156b0d563013e6119375c463404f"
19
19
  }