|
1 | 1 | /* eslint-disable react/destructuring-assignment */
|
2 |
| -import React, { Children, useContext } from 'react'; |
3 |
| -import type { FC, ReactElement, ReactNode } from 'react'; |
4 |
| -import type { ModuleExport, ModuleExports, PreparedStory, Renderer } from '@storybook/types'; |
5 |
| -import { deprecate } from '@storybook/client-logger'; |
6 |
| -import dedent from 'ts-dedent'; |
| 2 | +import React, { useContext } from 'react'; |
| 3 | +import type { FC } from 'react'; |
| 4 | +import type { ModuleExport, ModuleExports } from '@storybook/types'; |
7 | 5 | import type { Layout, PreviewProps as PurePreviewProps } from '../components';
|
8 |
| -import { Preview as PurePreview, PreviewSkeleton } from '../components'; |
9 |
| -import type { DocsContextProps } from './DocsContext'; |
| 6 | +import { Preview as PurePreview } from '../components'; |
10 | 7 | import { DocsContext } from './DocsContext';
|
11 |
| -import type { SourceContextProps } from './SourceContainer'; |
12 | 8 | import { SourceContext } from './SourceContainer';
|
13 | 9 | import type { SourceProps } from './Source';
|
14 |
| -import { useSourceProps, SourceState as DeprecatedSourceState, SourceState } from './Source'; |
15 |
| -import { useStories } from './useStory'; |
| 10 | +import { useSourceProps } from './Source'; |
16 | 11 | import type { StoryProps } from './Story';
|
17 |
| -import { getStoryId, Story } from './Story'; |
| 12 | +import { Story } from './Story'; |
18 | 13 | import { useOf } from './useOf';
|
19 | 14 |
|
20 |
| -export { DeprecatedSourceState as SourceState }; |
21 |
| - |
22 |
| -type DeprecatedCanvasProps = { |
23 |
| - /** |
24 |
| - * @deprecated multiple stories are not supported |
25 |
| - */ |
26 |
| - isColumn?: boolean; |
27 |
| - /** |
28 |
| - * @deprecated multiple stories are not supported |
29 |
| - */ |
30 |
| - columns?: number; |
31 |
| - /** |
32 |
| - * @deprecated use `sourceState` instead |
33 |
| - */ |
34 |
| - withSource?: DeprecatedSourceState; |
35 |
| - /** |
36 |
| - * @deprecated use `source.code` instead |
37 |
| - */ |
38 |
| - mdxSource?: string; |
39 |
| - /** |
40 |
| - * @deprecated reference stories with the `of` prop instead |
41 |
| - */ |
42 |
| - children?: ReactNode; |
43 |
| -}; |
44 |
| - |
45 | 15 | type CanvasProps = Pick<PurePreviewProps, 'withToolbar' | 'additionalActions' | 'className'> & {
|
46 | 16 | /**
|
47 | 17 | * Pass the export defining a story to render that story
|
@@ -92,129 +62,16 @@ type CanvasProps = Pick<PurePreviewProps, 'withToolbar' | 'additionalActions' |
|
92 | 62 | story?: Pick<StoryProps, 'inline' | 'height' | 'autoplay' | '__forceInitialArgs' | '__primary'>;
|
93 | 63 | };
|
94 | 64 |
|
95 |
| -const useDeprecatedPreviewProps = ( |
96 |
| - { |
97 |
| - withSource, |
98 |
| - mdxSource, |
99 |
| - children, |
100 |
| - layout: layoutProp, |
101 |
| - ...props |
102 |
| - }: DeprecatedCanvasProps & { of?: ModuleExport; layout?: Layout }, |
103 |
| - docsContext: DocsContextProps<Renderer>, |
104 |
| - sourceContext: SourceContextProps |
105 |
| -) => { |
106 |
| - /* |
107 |
| - get all story IDs by traversing through the children, |
108 |
| - filter out any non-story children (e.g. text nodes) |
109 |
| - and then get the id from each story depending on available props |
110 |
| - */ |
111 |
| - const storyIds = (Children.toArray(children) as ReactElement[]) |
112 |
| - .filter((c) => c.props && (c.props.id || c.props.name || c.props.of)) |
113 |
| - .map((c) => getStoryId(c.props, docsContext)); |
114 |
| - |
115 |
| - const stories = useStories(storyIds, docsContext); |
116 |
| - const isLoading = stories.some((s) => !s); |
117 |
| - const sourceProps = useSourceProps( |
118 |
| - { |
119 |
| - ...(mdxSource ? { code: decodeURI(mdxSource) } : { ids: storyIds }), |
120 |
| - ...(props.of && { of: props.of }), |
121 |
| - }, |
122 |
| - docsContext, |
123 |
| - sourceContext |
124 |
| - ); |
125 |
| - if (withSource === SourceState.NONE) { |
126 |
| - return { isLoading, previewProps: props }; |
127 |
| - } |
128 |
| - |
129 |
| - // if the user has specified a layout prop, use that... |
130 |
| - let layout = layoutProp; |
131 |
| - // ...otherwise, try to infer it from the story parameters |
132 |
| - stories.forEach((story) => { |
133 |
| - if (layout || !story) { |
134 |
| - return; |
135 |
| - } |
136 |
| - layout = story?.parameters.layout ?? story.parameters.docs?.canvas?.layout; |
137 |
| - }); |
138 |
| - |
139 |
| - return { |
140 |
| - isLoading, |
141 |
| - previewProps: { |
142 |
| - ...props, // pass through columns etc. |
143 |
| - layout: layout ?? 'padded', |
144 |
| - withSource: sourceProps, |
145 |
| - isExpanded: (withSource || sourceProps.state) === SourceState.OPEN, |
146 |
| - }, |
147 |
| - }; |
148 |
| -}; |
149 |
| - |
150 |
| -export const Canvas: FC<CanvasProps & DeprecatedCanvasProps> = (props) => { |
| 65 | +export const Canvas: FC<CanvasProps> = (props) => { |
151 | 66 | const docsContext = useContext(DocsContext);
|
152 | 67 | const sourceContext = useContext(SourceContext);
|
153 |
| - const { children, of, source } = props; |
| 68 | + const { of, source } = props; |
154 | 69 | if ('of' in props && of === undefined) {
|
155 | 70 | throw new Error('Unexpected `of={undefined}`, did you mistype a CSF file reference?');
|
156 | 71 | }
|
157 | 72 |
|
158 |
| - const { isLoading, previewProps } = useDeprecatedPreviewProps(props, docsContext, sourceContext); |
159 |
| - |
160 |
| - let story: PreparedStory; |
161 |
| - let sourceProps; |
162 |
| - /** |
163 |
| - * useOf and useSourceProps will throw if they can't find the story, in the scenario where |
164 |
| - * the doc is unattached (no primary story) and 'of' is undefined. |
165 |
| - * That scenario is valid in the deprecated API, where children is used as story refs rather than 'of'. |
166 |
| - * So if children is passed we allow the error to be swallowed and we'll use them instead. |
167 |
| - * We use two separate try blocks and throw the error afterwards to not break the rules of hooks. |
168 |
| - */ |
169 |
| - let hookError; |
170 |
| - try { |
171 |
| - ({ story } = useOf(of || 'story', ['story'])); |
172 |
| - } catch (error) { |
173 |
| - if (!children) { |
174 |
| - hookError = error; |
175 |
| - } |
176 |
| - } |
177 |
| - try { |
178 |
| - sourceProps = useSourceProps({ ...source, ...(of && { of }) }, docsContext, sourceContext); |
179 |
| - } catch (error) { |
180 |
| - if (!children) { |
181 |
| - hookError = error; |
182 |
| - } |
183 |
| - } |
184 |
| - if (hookError) { |
185 |
| - // eslint-disable-next-line @typescript-eslint/no-throw-literal |
186 |
| - throw hookError; |
187 |
| - } |
188 |
| - |
189 |
| - if (props.withSource) { |
190 |
| - deprecate(dedent`Setting source state with \`withSource\` is deprecated, please use \`sourceState\` with 'hidden', 'shown' or 'none' instead. |
191 |
| - |
192 |
| - Please refer to the migration guide: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#canvas-block |
193 |
| - `); |
194 |
| - } |
195 |
| - if (props.mdxSource) { |
196 |
| - deprecate(dedent`Setting source code with \`mdxSource\` is deprecated, please use source={{code: '...'}} instead. |
197 |
| - |
198 |
| - Please refer to the migration guide: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#canvas-block |
199 |
| - `); |
200 |
| - } |
201 |
| - if (props.isColumn !== undefined || props.columns !== undefined) { |
202 |
| - deprecate(dedent`\`isColumn\` and \`columns\` props are deprecated as the Canvas block now only supports showing a single story. |
203 |
| - |
204 |
| - Please refer to the migration guide: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#canvas-block |
205 |
| - `); |
206 |
| - } |
207 |
| - if (children) { |
208 |
| - deprecate(dedent`Passing children to Canvas is deprecated, please use the \`of\` prop instead to reference a story. |
209 |
| - |
210 |
| - Please refer to the migration guide: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#canvas-block |
211 |
| - `); |
212 |
| - return isLoading ? ( |
213 |
| - <PreviewSkeleton /> |
214 |
| - ) : ( |
215 |
| - <PurePreview {...previewProps}>{children}</PurePreview> |
216 |
| - ); |
217 |
| - } |
| 73 | + const { story } = useOf(of || 'story', ['story']); |
| 74 | + const sourceProps = useSourceProps({ ...source, ...(of && { of }) }, docsContext, sourceContext); |
218 | 75 |
|
219 | 76 | const layout =
|
220 | 77 | props.layout ?? story.parameters.layout ?? story.parameters.docs?.canvas?.layout ?? 'padded';
|
|
0 commit comments