Skip to content

Commit

Permalink
Core: Module format story decorators (#7490)
Browse files Browse the repository at this point in the history
Core: Module format story decorators
  • Loading branch information
shilman authored Jul 19, 2019
2 parents d3e9961 + f9b9a91 commit c8867c8
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 14 deletions.
7 changes: 6 additions & 1 deletion addons/docs/__snapshots__/mdx-compiler-plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ function MDXContent({ components, ...props }) {
mdxType=\\"Meta\\"
/>
<h1>{\`Decorated story\`}</h1>
<Story name=\\"one\\" mdxType=\\"Story\\">
<Story
name=\\"one\\"
decorators={[storyFn => <div className=\\"local\\">{storyFn()}</div>]}
mdxType=\\"Story\\"
>
<Button mdxType=\\"Button\\">One</Button>
</Story>
</MDXLayout>
Expand All @@ -92,6 +96,7 @@ MDXContent.isMDXComponent = true;
export const one = () => <Button>One</Button>;
one.story = {};
one.story.parameters = { mdxSource: \`<Button>One</Button>\` };
one.story.decorators = [storyFn => <div className=\\"local\\">{storyFn()}</div>];
const componentMeta = {
title: 'Button',
Expand Down
2 changes: 1 addition & 1 deletion addons/docs/fixtures/decorators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { Story, Meta } from '@storybook/addon-docs/blocks';

# Decorated story

<Story name="one">
<Story name="one" decorators={[storyFn => <div className="local">{storyFn()}</div>]}>
<Button>One</Button>
</Story>
7 changes: 6 additions & 1 deletion addons/docs/mdx-compiler-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ function genStoryExport(ast, counter) {
statements.push(`${storyKey}.story.parameters = { mdxSource: ${source} };`);
}

// console.log(statements);
let decorators = getAttr(ast.openingElement, 'decorators');
decorators = decorators && decorators.expression;
if (decorators) {
const { code: decos } = generate(decorators, {});
statements.push(`${storyKey}.story.decorators = ${decos};`);
}

return {
[storyKey]: statements.join('\n'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ export const useInfoAsStoryDecorator = () => <BaseButton label="Button" />;

useInfoAsStoryDecorator.story = {
name: 'Use Info as story decorator',
parameters: {
decorators: [withInfo('Info can take options via the global or local decorator as well.')],
},
decorators: [withInfo('Info can take options via the global or local decorator as well.')],
};

export const usingParamatersAcrossAllStories = () => <BaseButton label="Button" />;
Expand Down
14 changes: 13 additions & 1 deletion examples/official-storybook/stories/core/decorators.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ export default {

export const all = () => <p>Story</p>;
all.story = {
decorators: [
s => (
<>
<p>Local Decorator</p>
{s()}
</>
),
],
};

export const deprecated = () => <p>Story</p>;
deprecated.story = {
parameters: {
decorators: [
s => (
<>
<p>Local Decorator</p>
<p>Deprecated Local Decorator</p>
{s()}
</>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import { Meta, Story } from '@storybook/addon-docs/blocks';
title='Some.Button'
decorators={[withKnobs, storyFn => <div className='foo'>{storyFn}</div>]} />

<Story name='with decorator'><Button label='The Button' /></Story>
<Story name='with decorator' decorators={[withKnobs]}><Button label='The Button' /></Story>
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export const withDecorator = () => <Button label="The Button" />;

withDecorator.story = {
name: 'with decorator',
decorators: [withKnobs],
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export default {
};

export const story1 = () => <Button label="The Button" />;
story1.story = { name: 'with decorator' };
story1.story = {
name: 'with decorator',
decorators: [withKnobs],
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import { Meta, Story } from '@storybook/addon-docs/blocks';
title='Some.Button'
decorators={[withKnobs, storyFn => <div className='foo'>{storyFn}</div>]} />

<Story name='with decorator'><Button label='The Button' /></Story>
<Story name='with decorator' decorators={[withKnobs]}><Button label='The Button' /></Story>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import Button from './Button';

storiesOf('Some.Button', module)
.add('with story params and decorators', () => <Button label="The Button" />, {
bar: 1,
decorators: [withKnobs, storyFn => <div className="foo">{storyFn}</div>],
})
.add('with story decorators', () => <Button label="The Button" />, {
decorators: [withKnobs],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import Button from './Button';

export default {
title: 'Some.Button',
};

export const withStoryParamsAndDecorators = () => <Button label="The Button" />;

withStoryParamsAndDecorators.story = {
name: 'with story params and decorators',

parameters: {
bar: 1,
},

decorators: [withKnobs, storyFn => <div className="foo">{storyFn}</div>],
};

export const withStoryDecorators = () => <Button label="The Button" />;

withStoryDecorators.story = {
name: 'with story decorators',
decorators: [withKnobs],
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const testNames = [
'exports',
'collision',
'const',
'story-decorators',
];

testNames.forEach(testName => {
Expand Down
27 changes: 25 additions & 2 deletions lib/codemod/src/transforms/convert-storiesof-to-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ export default function transformer(file, api, options) {
const j = api.jscodeshift;
const root = j(file.source);

function extractDecorators(parameters) {
if (!parameters) {
return {};
}
let storyDecorators = parameters.properties.find(p => p.key.name === 'decorators');
if (!storyDecorators) {
return { storyParams: parameters };
}
storyDecorators = storyDecorators.value;
const storyParams = { ...parameters };
storyParams.properties = storyParams.properties.filter(p => p.key.name !== 'decorators');
if (storyParams.properties.length === 0) {
return { storyDecorators };
}
return { storyParams, storyDecorators };
}

function convertToModuleExports(path, originalExports, counter) {
const base = j(path);

Expand Down Expand Up @@ -144,8 +161,14 @@ export default function transformer(file, api, options) {
}

if (add.node.arguments.length > 2) {
const storyParams = add.node.arguments[2];
storyAnnotations.push(j.property('init', j.identifier('parameters'), storyParams));
const originalStoryParams = add.node.arguments[2];
const { storyParams, storyDecorators } = extractDecorators(originalStoryParams);
if (storyParams) {
storyAnnotations.push(j.property('init', j.identifier('parameters'), storyParams));
}
if (storyDecorators) {
storyAnnotations.push(j.property('init', j.identifier('decorators'), storyDecorators));
}
}

if (storyAnnotations.length > 0) {
Expand Down
9 changes: 7 additions & 2 deletions lib/core/src/client/preview/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,13 @@ export default function start(render, { decorateStory } = {}) {
Object.keys(exports).forEach(key => {
if (isExportStory(key, meta)) {
const storyFn = exports[key];
const { name, parameters } = storyFn.story || {};
kind.add(name || key, storyFn, parameters);
const { name, parameters, decorators } = storyFn.story || {};
if (parameters && parameters.decorators) {
deprecate(() => {},
`${kindName} => ${name || key}: story.parameters.decorators is deprecated; use story.decorators instead.`)();
}
const decoratorParams = decorators ? { decorators } : null;
kind.add(name || key, storyFn, { ...parameters, ...decoratorParams });
}
});

Expand Down

1 comment on commit c8867c8

@vercel
Copy link

@vercel vercel bot commented on c8867c8 Jul 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.