From 0186ff8da8022ec674930035c975f202cb6c1d13 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 19 Jan 2023 15:18:18 +0100 Subject: [PATCH 01/25] add new props and stories --- code/ui/blocks/src/blocks/Canvas.stories.tsx | 58 ++++---------- code/ui/blocks/src/blocks/Canvas.tsx | 79 ++++++++++++++++--- .../internal/InternalCanvas.stories.tsx | 35 +++++++- 3 files changed, 118 insertions(+), 54 deletions(-) diff --git a/code/ui/blocks/src/blocks/Canvas.stories.tsx b/code/ui/blocks/src/blocks/Canvas.stories.tsx index 329690713f78..0194b952f39f 100644 --- a/code/ui/blocks/src/blocks/Canvas.stories.tsx +++ b/code/ui/blocks/src/blocks/Canvas.stories.tsx @@ -1,6 +1,6 @@ import React from 'react'; import type { Meta, StoryObj } from '@storybook/react'; -import { Canvas, SourceState } from './Canvas'; +import { Canvas } from './Canvas'; import { Story as StoryComponent } from './Story'; import * as ButtonStories from '../examples/Button.stories'; @@ -9,58 +9,26 @@ const meta: Meta = { parameters: { relativeCsfPaths: ['../examples/Button.stories'], }, - render: (args) => { - return ( - - - - ); - }, }; export default meta; type Story = StoryObj; -export const BasicStory: Story = {}; - -export const WithSourceOpen: Story = { - args: { - withSource: SourceState.OPEN, - }, -}; -export const WithSourceClosed: Story = { - args: { - withSource: SourceState.CLOSED, - }, -}; - -export const WithMdxSource: Story = { - name: 'With MDX Source', +export const Default: Story = { args: { - withSource: SourceState.OPEN, - mdxSource: `', // spaces should be removed by the prettier formatter + format: 'html', + }, + }, +}; + const ClassNameStoryDescription = () => (

This story sets the className prop on the Canvas to{' '} diff --git a/code/ui/blocks/src/blocks/Canvas.tsx b/code/ui/blocks/src/blocks/Canvas.tsx index 068156e49e65..2e9a92ebda99 100644 --- a/code/ui/blocks/src/blocks/Canvas.tsx +++ b/code/ui/blocks/src/blocks/Canvas.tsx @@ -7,6 +7,7 @@ import type { DocsContextProps } from './DocsContext'; import { DocsContext } from './DocsContext'; import type { SourceContextProps } from './SourceContainer'; import { SourceContext } from './SourceContainer'; +import type { SourceProps } from './Source'; import { useSourceProps, SourceState as DeprecatedSourceState, SourceState } from './Source'; import { useStories } from './useStory'; import { getStoryId, Story } from './Story'; @@ -37,7 +38,7 @@ type DeprecatedCanvasProps = Omit< type CanvasProps = Pick & { of: ModuleExport; sourceState?: 'hidden' | 'shown' | 'none'; - source?: any; // TODO: get from Source component (and or block) when that is ready + source?: Omit; story?: any; // TODO: get from Story component (and or block) when that is ready }; @@ -100,11 +101,16 @@ export const Canvas: FC = (props) => { ); const { isLoading, previewProps } = useDeprecatedPreviewProps(props, docsContext, sourceContext); + if (!of && !children) { + throw new Error('No story passed to the Canvas block. Did you forget to pass the `of` prop?'); + } + if (of) { // TODO: loading? return ( = (props) => { ); } - if (!of && !children) { - throw new Error('No story passed to the Canvas block. Did you forget to pass the `of` prop?'); - } if (isLoading) return ; diff --git a/code/ui/blocks/src/blocks/Source.tsx b/code/ui/blocks/src/blocks/Source.tsx index a990dd7c87f0..03e0653ce3ec 100644 --- a/code/ui/blocks/src/blocks/Source.tsx +++ b/code/ui/blocks/src/blocks/Source.tsx @@ -33,7 +33,7 @@ type SourceParameters = SourceCodeProps & { originalSource?: string; }; -type SourceProps = Omit & { +export type SourceProps = Omit & { /** * Pass the export defining a story to render its source * @@ -112,14 +112,13 @@ export const useSourceProps = ( // The check didn't actually change the type. let stories: PreparedStory[] = storiesFromIds as PreparedStory[]; if (props.of) { - const resolved = docsContext.resolveModuleExport(props.of); + const resolved = docsContext.resolveOf(props.of); if (resolved.type !== 'story') throw new Error(`Invalid value passed to the 'of' prop, it should be a story export.`); stories = [resolved.story]; } else if (stories.length === 0) { stories = [docsContext.storyById()]; } - const sourceParameters = (stories[0].parameters.docs?.source || {}) as SourceParameters; let { code } = props; // We will fall back to `sourceParameters.code`, but per story below let format = props.format ?? sourceParameters.format; @@ -140,7 +139,6 @@ export const useSourceProps = ( } const state = getSourceState(stories as PreparedStory[]); - return code ? { code, From d6ad83dcb08896fbb12a760969ddb2e979c83937 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 19 Jan 2023 16:32:59 +0100 Subject: [PATCH 03/25] fix sourceState --- code/ui/blocks/src/blocks/Canvas.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/code/ui/blocks/src/blocks/Canvas.tsx b/code/ui/blocks/src/blocks/Canvas.tsx index 2e9a92ebda99..0c719c7d212c 100644 --- a/code/ui/blocks/src/blocks/Canvas.tsx +++ b/code/ui/blocks/src/blocks/Canvas.tsx @@ -14,12 +14,6 @@ import { getStoryId, Story } from './Story'; export { DeprecatedSourceState as SourceState }; -const sourceStateMap: Record = { - shown: SourceState.OPEN, - hidden: SourceState.CLOSED, - none: SourceState.NONE, -}; - type DeprecatedCanvasProps = Omit< PurePreviewProps, 'isExpanded' | 'isLoading' | 'withToolbar' | 'additionalActions' | 'className' @@ -94,7 +88,6 @@ export const Canvas: FC = (props) => { { ...source, of, - state: sourceStateMap[sourceState], }, docsContext, sourceContext From 52d9c5f80197f17caad991e8f79fe416eec6b25a Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 19 Jan 2023 20:57:20 +0100 Subject: [PATCH 04/25] support story props --- code/ui/blocks/src/blocks/Canvas.stories.tsx | 16 +++++++++++++++- code/ui/blocks/src/blocks/Canvas.tsx | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/code/ui/blocks/src/blocks/Canvas.stories.tsx b/code/ui/blocks/src/blocks/Canvas.stories.tsx index 21286a463562..7d4468b66a92 100644 --- a/code/ui/blocks/src/blocks/Canvas.stories.tsx +++ b/code/ui/blocks/src/blocks/Canvas.stories.tsx @@ -20,7 +20,7 @@ export default meta; type Story = StoryObj; -export const Default: Story = { +export const DefaultAttached: Story = { args: { of: ButtonStories.Primary, }, @@ -91,6 +91,20 @@ export const WithSourceProps: Story = { }, }; +export const WithInlineStoryProps: Story = { + args: { + of: ButtonStories.Primary, + story: { inline: false, height: '200px' }, + }, +}; + +export const WithAutoplayingStory: Story = { + args: { + of: ButtonStories.Clicking, + story: { autoplay: true }, + }, +}; + const ClassNameStoryDescription = () => (

This story sets the className prop on the Canvas to{' '} diff --git a/code/ui/blocks/src/blocks/Canvas.tsx b/code/ui/blocks/src/blocks/Canvas.tsx index 0c719c7d212c..690d64b823b1 100644 --- a/code/ui/blocks/src/blocks/Canvas.tsx +++ b/code/ui/blocks/src/blocks/Canvas.tsx @@ -10,6 +10,7 @@ import { SourceContext } from './SourceContainer'; import type { SourceProps } from './Source'; import { useSourceProps, SourceState as DeprecatedSourceState, SourceState } from './Source'; import { useStories } from './useStory'; +import type { StoryProps } from './Story'; import { getStoryId, Story } from './Story'; export { DeprecatedSourceState as SourceState }; @@ -33,7 +34,7 @@ type CanvasProps = Pick; - story?: any; // TODO: get from Story component (and or block) when that is ready + story?: Pick; }; const useDeprecatedPreviewProps = ( From f7eb5ded10dfb9a7c71285ef07fafa44ed656ad8 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Thu, 19 Jan 2023 21:17:07 +0100 Subject: [PATCH 05/25] support layout prop --- code/ui/blocks/src/blocks/Canvas.stories.tsx | 30 ++++++++++++++++---- code/ui/blocks/src/blocks/Canvas.tsx | 20 ++++++++++++- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/code/ui/blocks/src/blocks/Canvas.stories.tsx b/code/ui/blocks/src/blocks/Canvas.stories.tsx index 7d4468b66a92..33f63e31f3d2 100644 --- a/code/ui/blocks/src/blocks/Canvas.stories.tsx +++ b/code/ui/blocks/src/blocks/Canvas.stories.tsx @@ -32,7 +32,7 @@ export const WithToolbar: Story = { withToolbar: true, }, }; -export const WithAdditionalActions: Story = { +export const AdditionalActions: Story = { args: { of: ButtonStories.Primary, additionalActions: [ @@ -79,10 +79,30 @@ export const SourceStateNone: Story = { }, }; -export const WithSourceProps: Story = { +export const LayoutFullscreen: Story = { + args: { + of: ButtonStories.Primary, + layout: 'fullscreen', + }, +}; + +export const LayoutCentered: Story = { + args: { + of: ButtonStories.Primary, + layout: 'centered', + }, +}; + +export const LayoutPadded: Story = { + args: { + of: ButtonStories.Primary, + layout: 'padded', + }, +}; + +export const SourceProps: Story = { args: { of: ButtonStories.Primary, - sourceState: 'shown', source: { language: 'html', code: '', // spaces should be removed by the prettier formatter @@ -91,14 +111,14 @@ export const WithSourceProps: Story = { }, }; -export const WithInlineStoryProps: Story = { +export const InlineStoryProps: Story = { args: { of: ButtonStories.Primary, story: { inline: false, height: '200px' }, }, }; -export const WithAutoplayingStory: Story = { +export const AutoplayingStory: Story = { args: { of: ButtonStories.Clicking, story: { autoplay: true }, diff --git a/code/ui/blocks/src/blocks/Canvas.tsx b/code/ui/blocks/src/blocks/Canvas.tsx index 690d64b823b1..50a76c1f1ecc 100644 --- a/code/ui/blocks/src/blocks/Canvas.tsx +++ b/code/ui/blocks/src/blocks/Canvas.tsx @@ -35,6 +35,7 @@ type CanvasProps = Pick; story?: Pick; + layout?: 'padded' | 'centered' | 'fullscreen'; }; const useDeprecatedPreviewProps = ( @@ -80,6 +81,7 @@ export const Canvas: FC = (props) => { of, story: storyProps, sourceState = 'hidden', + layout: layoutProp, source, withToolbar, additionalActions, @@ -101,6 +103,10 @@ export const Canvas: FC = (props) => { if (of) { // TODO: loading? + + const layout = + layoutProp || of.parameters?.layout || of.parameters?.docs?.canvas?.layout || 'padded'; + return ( = (props) => { additionalActions={additionalActions} className={className} > - + ); } From 77f0c6a25f451377e9c7deec640314539e20b095 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Fri, 20 Jan 2023 12:25:53 +0100 Subject: [PATCH 06/25] disable sourcecontext --- code/ui/blocks/src/blocks/Canvas.stories.tsx | 19 +++++++++++++------ code/ui/blocks/src/blocks/Canvas.tsx | 2 +- .../internal/InternalCanvas.stories.tsx | 11 +++++++++-- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/code/ui/blocks/src/blocks/Canvas.stories.tsx b/code/ui/blocks/src/blocks/Canvas.stories.tsx index 33f63e31f3d2..2c3f2a8d1d56 100644 --- a/code/ui/blocks/src/blocks/Canvas.stories.tsx +++ b/code/ui/blocks/src/blocks/Canvas.stories.tsx @@ -8,13 +8,13 @@ const meta: Meta = { component: Canvas, parameters: { relativeCsfPaths: ['../examples/Button.stories'], - snippets: { - 'storybook-blocks-example-button--primary': { - code: `const emitted = 'source';`, - }, - }, + // snippets: { + // 'storybook-blocks-example-button--primary': { + // code: `const emitted = 'source';`, + // }, + // }, }, - decorators: SourceStoriesMeta.decorators, + // decorators: SourceStoriesMeta.decorators, }; export default meta; @@ -26,6 +26,13 @@ export const DefaultAttached: Story = { }, }; +export const DefaultUnattached: Story = { + args: { + of: ButtonStories.Primary, + }, + parameters: { attached: false }, +}; + export const WithToolbar: Story = { args: { of: ButtonStories.Primary, diff --git a/code/ui/blocks/src/blocks/Canvas.tsx b/code/ui/blocks/src/blocks/Canvas.tsx index 50a76c1f1ecc..6f3195cbd3b2 100644 --- a/code/ui/blocks/src/blocks/Canvas.tsx +++ b/code/ui/blocks/src/blocks/Canvas.tsx @@ -39,7 +39,7 @@ type CanvasProps = Pick, sourceContext: SourceContextProps ) => { diff --git a/code/ui/blocks/src/blocks/internal/InternalCanvas.stories.tsx b/code/ui/blocks/src/blocks/internal/InternalCanvas.stories.tsx index fc800c2f3dcc..c62b6c6eab35 100644 --- a/code/ui/blocks/src/blocks/internal/InternalCanvas.stories.tsx +++ b/code/ui/blocks/src/blocks/internal/InternalCanvas.stories.tsx @@ -7,6 +7,7 @@ import { expect } from '@storybook/jest'; import { Canvas, SourceState } from '../Canvas'; import { Story as StoryComponent } from '../Story'; import * as ButtonStories from '../../examples/Button.stories'; +import SourceStoriesMeta from '../Source.stories'; const meta: Meta = { title: 'Blocks/Internal/Canvas', @@ -14,7 +15,13 @@ const meta: Meta = { parameters: { theme: 'light', relativeCsfPaths: ['../examples/Button.stories'], + // snippets: { + // 'storybook-blocks-example-button--primary': { + // code: `const emitted = 'source';`, }, + // }, + // }, + // decorators: SourceStoriesMeta.decorators, render: (args) => { return ( @@ -40,8 +47,8 @@ const expectAmountOfStoriesInSource = await userEvent.click(showCodeButton); // Assert - check that the correct amount of stories' source is shown - const booleanControlNodes = await canvas.findAllByText(`label`); - await expect(booleanControlNodes).toHaveLength(amount); + const buttonNodes = await canvas.findAllByText(`label`); + await expect(buttonNodes).toHaveLength(amount); }; export const BasicStoryChild: Story = {}; From 5eb25d0358bb9a99f7b383abb98f0cace9e14347 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Fri, 20 Jan 2023 15:02:42 +0100 Subject: [PATCH 07/25] add stories for layout parameters --- code/ui/blocks/src/blocks/Canvas.stories.tsx | 78 ++++++++++++++----- code/ui/blocks/src/blocks/Canvas.tsx | 2 +- .../src/examples/CanvasParameters.stories.tsx | 31 ++++++++ 3 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 code/ui/blocks/src/examples/CanvasParameters.stories.tsx diff --git a/code/ui/blocks/src/blocks/Canvas.stories.tsx b/code/ui/blocks/src/blocks/Canvas.stories.tsx index 2c3f2a8d1d56..6b85f88cb4e7 100644 --- a/code/ui/blocks/src/blocks/Canvas.stories.tsx +++ b/code/ui/blocks/src/blocks/Canvas.stories.tsx @@ -1,20 +1,14 @@ import React from 'react'; import type { Meta, StoryObj } from '@storybook/react'; import { Canvas } from './Canvas'; -import SourceStoriesMeta from './Source.stories'; import * as ButtonStories from '../examples/Button.stories'; +import * as ParameterStories from '../examples/CanvasParameters.stories'; const meta: Meta = { component: Canvas, parameters: { - relativeCsfPaths: ['../examples/Button.stories'], - // snippets: { - // 'storybook-blocks-example-button--primary': { - // code: `const emitted = 'source';`, - // }, - // }, + relativeCsfPaths: ['../examples/Button.stories', '../examples/CanvasParameters.stories'], }, - // decorators: SourceStoriesMeta.decorators, }; export default meta; @@ -33,13 +27,13 @@ export const DefaultUnattached: Story = { parameters: { attached: false }, }; -export const WithToolbar: Story = { +export const PropWithToolbar: Story = { args: { of: ButtonStories.Primary, withToolbar: true, }, }; -export const AdditionalActions: Story = { +export const PropAdditionalActions: Story = { args: { of: ButtonStories.Primary, additionalActions: [ @@ -65,49 +59,91 @@ export const AdditionalActions: Story = { }, }; -export const SourceStateShown: Story = { +export const PropSourceStateShown: Story = { args: { of: ButtonStories.Primary, sourceState: 'shown', }, }; -export const SourceStateHidden: Story = { +export const PropSourceStateHidden: Story = { args: { of: ButtonStories.Primary, sourceState: 'hidden', }, }; -export const SourceStateNone: Story = { +export const PropSourceStateNone: Story = { args: { of: ButtonStories.Primary, sourceState: 'none', }, }; -export const LayoutFullscreen: Story = { +export const PropLayoutFullscreen: Story = { args: { of: ButtonStories.Primary, layout: 'fullscreen', }, }; -export const LayoutCentered: Story = { +export const PropLayoutCentered: Story = { args: { of: ButtonStories.Primary, layout: 'centered', }, }; -export const LayoutPadded: Story = { +export const PropLayoutPadded: Story = { args: { of: ButtonStories.Primary, layout: 'padded', }, }; -export const SourceProps: Story = { +export const ParameterDocsCanvasLayoutFullscreen: Story = { + name: 'parameters.docs.canvas.layout = fullscreen', + args: { + of: ParameterStories.DocsCanvasLayoutFullscreen, + }, +}; + +export const ParameterDocsCanvasLayoutCentered: Story = { + name: 'parameters.docs.canvas.layout = centered', + args: { + of: ParameterStories.DocsCanvasLayoutCentered, + }, +}; + +export const ParameterDocsCanvasLayoutPadded: Story = { + name: 'parameters.docs.canvas.layout = padded', + args: { + of: ParameterStories.DocsCanvasLayoutPadded, + }, +}; + +export const ParameterLayoutFullscreen: Story = { + name: 'parameters.layout = fullscreen', + args: { + of: ParameterStories.LayoutFullscreen, + }, +}; + +export const ParameterLayoutCentered: Story = { + name: 'parameters.layout = centered', + args: { + of: ParameterStories.LayoutCentered, + }, +}; + +export const ParameterLayoutPadded: Story = { + name: 'parameters.layout = padded', + args: { + of: ParameterStories.LayoutPadded, + }, +}; + +export const PropSource: Story = { args: { of: ButtonStories.Primary, source: { @@ -118,14 +154,14 @@ export const SourceProps: Story = { }, }; -export const InlineStoryProps: Story = { +export const PropInlineStory: Story = { args: { of: ButtonStories.Primary, story: { inline: false, height: '200px' }, }, }; -export const AutoplayingStory: Story = { +export const PropAutoplayingStory: Story = { args: { of: ButtonStories.Clicking, story: { autoplay: true }, @@ -142,8 +178,8 @@ const ClassNameStoryDescription = () => ( /** * This is a comment on classname */ -export const ClassName: Story = { - name: 'ClassName', +export const PropClassName: Story = { + name: 'Prop ClassName', args: { of: ButtonStories.Primary, className: 'my-custom-classname', diff --git a/code/ui/blocks/src/blocks/Canvas.tsx b/code/ui/blocks/src/blocks/Canvas.tsx index 6f3195cbd3b2..5972fa64ad27 100644 --- a/code/ui/blocks/src/blocks/Canvas.tsx +++ b/code/ui/blocks/src/blocks/Canvas.tsx @@ -55,7 +55,7 @@ const useDeprecatedPreviewProps = ( const stories = useStories(storyIds, docsContext); const isLoading = stories.some((s) => !s); const sourceProps = useSourceProps( - mdxSource ? { code: decodeURI(mdxSource) } : { ids: storyIds }, + mdxSource ? { code: decodeURI(mdxSource), of: props.of } : { ids: storyIds, of: props.of }, docsContext, sourceContext ); diff --git a/code/ui/blocks/src/examples/CanvasParameters.stories.tsx b/code/ui/blocks/src/examples/CanvasParameters.stories.tsx new file mode 100644 index 000000000000..9d52e90c1a90 --- /dev/null +++ b/code/ui/blocks/src/examples/CanvasParameters.stories.tsx @@ -0,0 +1,31 @@ +import type { Meta, StoryObj } from '@storybook/react'; + +import { EmptyExample } from './EmptyExample'; + +const meta = { + title: 'example/CanvasParameters', + component: EmptyExample, +} satisfies Meta; +export default meta; + +type Story = StoryObj; + +export const LayoutFullscreen: Story = { + parameters: { layout: 'fullscreen' }, +}; +export const LayoutPadded: Story = { + parameters: { layout: 'padded' }, +}; +export const LayoutCentered: Story = { + parameters: { layout: 'centered' }, +}; + +export const DocsCanvasLayoutFullscreen: Story = { + parameters: { docs: { canvas: { layout: 'fullscreen' } } }, +}; +export const DocsCanvasLayoutPadded: Story = { + parameters: { docs: { canvas: { layout: 'padded' } } }, +}; +export const DocsCanvasLayoutCentered: Story = { + parameters: { docs: { canvas: { layout: 'centered' } } }, +}; From a5f14bf500e2840eccc4612dc0ec169968695281 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Fri, 20 Jan 2023 15:33:03 +0100 Subject: [PATCH 08/25] support parameters --- code/ui/blocks/src/blocks/Canvas.stories.tsx | 73 ++++++++++++++++++- code/ui/blocks/src/blocks/Canvas.tsx | 33 +++------ .../src/examples/CanvasParameters.stories.tsx | 50 +++++++++++++ 3 files changed, 130 insertions(+), 26 deletions(-) diff --git a/code/ui/blocks/src/blocks/Canvas.stories.tsx b/code/ui/blocks/src/blocks/Canvas.stories.tsx index 6b85f88cb4e7..7bd2890a2db7 100644 --- a/code/ui/blocks/src/blocks/Canvas.stories.tsx +++ b/code/ui/blocks/src/blocks/Canvas.stories.tsx @@ -3,11 +3,16 @@ import type { Meta, StoryObj } from '@storybook/react'; import { Canvas } from './Canvas'; import * as ButtonStories from '../examples/Button.stories'; import * as ParameterStories from '../examples/CanvasParameters.stories'; +import * as SourceParameterStories from '../examples/SourceParameters.stories'; const meta: Meta = { component: Canvas, parameters: { - relativeCsfPaths: ['../examples/Button.stories', '../examples/CanvasParameters.stories'], + relativeCsfPaths: [ + '../examples/Button.stories', + '../examples/CanvasParameters.stories', + '../examples/SourceParameters.stories', + ], }, }; export default meta; @@ -33,6 +38,12 @@ export const PropWithToolbar: Story = { withToolbar: true, }, }; +export const ParameterWithToolbar: Story = { + name: 'parameters.docs.canvas.withToolbar = true', + args: { + of: ParameterStories.WithToolbar, + }, +}; export const PropAdditionalActions: Story = { args: { of: ButtonStories.Primary, @@ -58,6 +69,12 @@ export const PropAdditionalActions: Story = { ], }, }; +export const ParameterAdditionalActions: Story = { + name: 'parameters.docs.canvas.additionalActions = [ ... ]', + args: { + of: ParameterStories.AdditionalActions, + }, +}; export const PropSourceStateShown: Story = { args: { @@ -79,6 +96,26 @@ export const PropSourceStateNone: Story = { sourceState: 'none', }, }; +export const ParametersSourceStateShown: Story = { + name: 'parameters.docs.canvas.sourceState = shown', + args: { + of: ParameterStories.SourceStateShown, + }, +}; + +export const ParametersSourceStateHidden: Story = { + name: 'parameters.docs.canvas.sourceState = hidden', + args: { + of: ParameterStories.SourceStateHidden, + }, +}; + +export const ParametersSourceStateNone: Story = { + name: 'parameters.docs.canvas.sourceState = none', + args: { + of: ParameterStories.SourceStateNone, + }, +}; export const PropLayoutFullscreen: Story = { args: { @@ -154,6 +191,12 @@ export const PropSource: Story = { }, }; +export const ParameterSource: Story = { + args: { + of: SourceParameterStories.CodeLanguage, + }, +}; + export const PropInlineStory: Story = { args: { of: ButtonStories.Primary, @@ -168,6 +211,11 @@ export const PropAutoplayingStory: Story = { }, }; +export const ParameterStory: Story = { + args: { + of: ParameterStories.StoryParameters, + }, +}; const ClassNameStoryDescription = () => (

This story sets the className prop on the Canvas to{' '} @@ -175,9 +223,6 @@ const ClassNameStoryDescription = () => ( this, it also adds a style tag that sets another background color for that class:

); -/** - * This is a comment on classname - */ export const PropClassName: Story = { name: 'Prop ClassName', args: { @@ -198,3 +243,23 @@ export const PropClassName: Story = { ), }; + +export const ParameterClassName: Story = { + name: 'Parameter ClassName', + args: { + of: ParameterStories.ClassName, + }, + render: (args) => ( + <> + + + + + ), +}; diff --git a/code/ui/blocks/src/blocks/Canvas.tsx b/code/ui/blocks/src/blocks/Canvas.tsx index 5972fa64ad27..f57ffffdcca7 100644 --- a/code/ui/blocks/src/blocks/Canvas.tsx +++ b/code/ui/blocks/src/blocks/Canvas.tsx @@ -1,3 +1,4 @@ +/* eslint-disable react/destructuring-assignment */ import React, { Children, useContext } from 'react'; import type { FC, ReactElement, ReactNode } from 'react'; import type { ModuleExport, Renderer } from '@storybook/types'; @@ -76,25 +77,8 @@ const useDeprecatedPreviewProps = ( export const Canvas: FC = (props) => { const docsContext = useContext(DocsContext); const sourceContext = useContext(SourceContext); - const { - children, - of, - story: storyProps, - sourceState = 'hidden', - layout: layoutProp, - source, - withToolbar, - additionalActions, - className, - } = props; - const sourceProps = useSourceProps( - { - ...source, - of, - }, - docsContext, - sourceContext - ); + const { children, of, source } = props; + const sourceProps = useSourceProps({ ...source, of }, docsContext, sourceContext); const { isLoading, previewProps } = useDeprecatedPreviewProps(props, docsContext, sourceContext); if (!of && !children) { @@ -105,7 +89,12 @@ export const Canvas: FC = (props) => { // TODO: loading? const layout = - layoutProp || of.parameters?.layout || of.parameters?.docs?.canvas?.layout || 'padded'; + props.layout ?? of.parameters?.layout ?? of.parameters?.docs?.canvas?.layout ?? 'padded'; + const withToolbar = props.withToolbar ?? of.parameters?.docs?.canvas?.withToolbar ?? false; + const additionalActions = + props.additionalActions ?? of.parameters?.docs?.canvas?.additionalActions; + const sourceState = props.sourceState ?? of.parameters?.docs?.canvas?.sourceState ?? 'hidden'; + const className = props.className ?? of.parameters?.docs?.canvas?.className; return ( = (props) => { > ; +export const WithToolbar: Story = { + parameters: { docs: { canvas: { withToolbar: true } } }, +}; +export const AdditionalActions: Story = { + parameters: { + docs: { + canvas: { + additionalActions: [ + { + title: 'Open in GitHub', + onClick: () => { + window.open( + 'https://github.com/storybookjs/storybook/blob/next/code/ui/blocks/src/examples/Button.stories.tsx', + '_blank' + ); + }, + }, + { + title: 'Go to documentation', + onClick: () => { + window.open( + 'https://storybook.js.org/docs/react/essentials/controls#annotation', + '_blank' + ); + }, + }, + ], + }, + }, + }, +}; + +export const SourceStateShown: Story = { + parameters: { docs: { canvas: { sourceState: 'shown' } } }, +}; +export const SourceStateHidden: Story = { + parameters: { docs: { canvas: { sourceState: 'hidden' } } }, +}; +export const SourceStateNone: Story = { + parameters: { docs: { canvas: { sourceState: 'none' } } }, +}; + export const LayoutFullscreen: Story = { parameters: { layout: 'fullscreen' }, }; @@ -29,3 +71,11 @@ export const DocsCanvasLayoutPadded: Story = { export const DocsCanvasLayoutCentered: Story = { parameters: { docs: { canvas: { layout: 'centered' } } }, }; + +export const ClassName: Story = { + parameters: { docs: { canvas: { className: 'my-custom-classname' } } }, +}; + +export const StoryParameters: Story = { + parameters: { docs: { story: { inline: false, height: '200px' } } }, +}; From 6aa9acd90a1b6cfddb92697321476d7d97629090 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Fri, 20 Jan 2023 22:33:45 +0100 Subject: [PATCH 09/25] add meta prop, clean up stories --- code/ui/blocks/src/blocks/Canvas.stories.tsx | 192 ++++++++++--------- code/ui/blocks/src/blocks/Canvas.tsx | 4 +- 2 files changed, 109 insertions(+), 87 deletions(-) diff --git a/code/ui/blocks/src/blocks/Canvas.stories.tsx b/code/ui/blocks/src/blocks/Canvas.stories.tsx index 7bd2890a2db7..5e0834b2f9f3 100644 --- a/code/ui/blocks/src/blocks/Canvas.stories.tsx +++ b/code/ui/blocks/src/blocks/Canvas.stories.tsx @@ -1,6 +1,8 @@ import React from 'react'; import type { Meta, StoryObj } from '@storybook/react'; +import dedent from 'ts-dedent'; import { Canvas } from './Canvas'; +import SourceStoriesMeta from './Source.stories'; import * as ButtonStories from '../examples/Button.stories'; import * as ParameterStories from '../examples/CanvasParameters.stories'; import * as SourceParameterStories from '../examples/SourceParameters.stories'; @@ -13,7 +15,18 @@ const meta: Meta = { '../examples/CanvasParameters.stories', '../examples/SourceParameters.stories', ], + snippets: { + 'storybook-blocks-example-button--primary': { + code: dedent` +