-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use parameters for info addon #3697
Changes from 3 commits
a1b4296
8b174bc
848cf43
6217be8
29aaa62
0ccf131
1a81105
f1d318b
44ffa26
837cfbe
f1ae4ea
bb3c5bf
5738a8e
cd6cdd3
3cc1566
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,14 +99,94 @@ storiesOf('Component', module) | |
|
||
## Usage as decorator | ||
|
||
It is possible to add infos by default to all components by using a global or story decorator. The drawback is you won't be able to display a distinct info message per story. | ||
It is possible to add infos by default to all components by using a global or story decorator. | ||
|
||
It is important to declare this decorator as **the first decorator**, otherwise it won't work well. | ||
|
||
```js | ||
addDecorator((story, context) => withInfo('common info')(story)(context)); | ||
addDecorator(withInfo); | ||
``` | ||
|
||
Then, you can use the `info` parameter to pass certain options to your stories. | ||
This can be done per book of stories: | ||
|
||
```js | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import Component from './Component'; | ||
|
||
storiesOf('Component', module) | ||
.addParameters({ | ||
info: { | ||
// Your settings | ||
} | ||
}) | ||
.add('with some emoji', () => <Component/>); | ||
``` | ||
|
||
...or for each story individually: | ||
```js | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import Component from './Component'; | ||
|
||
storiesOf('Component', module) | ||
.add( | ||
'with some emoji', | ||
() => <Component emoji/>, | ||
{ info : { inline: false, header: false } } // Make your component render inline with the additional info | ||
) | ||
.add( | ||
'with no emoji', | ||
() => <Component/>, | ||
{ info: '☹️ no emojis' } // Add additional info text | ||
); | ||
``` | ||
|
||
...or even together: | ||
|
||
```js | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import Component from './Component'; | ||
|
||
storiesOf('Component', module) | ||
.addParameters({ | ||
info: { // Make a default for all stories in this book, | ||
inline: true, // where the components are inlined | ||
styles: { | ||
header: { | ||
h1: { | ||
color: 'red' // and the headers of the sections are red. | ||
} | ||
} | ||
}, | ||
} | ||
}) | ||
.add( | ||
'green version', | ||
() => <Component green/>, | ||
{ | ||
info: { | ||
styles: { | ||
headers: { | ||
h1: { | ||
color: 'green' // Still inlined but with green headers! | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
.add( | ||
'something else', | ||
() => <Component different/>, | ||
{ | ||
info: "This story has additional text added to the info!" // Still inlined and with red headers! | ||
} | ||
); | ||
``` | ||
|
||
|
||
## Global options | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's deprecate this and the usage as a HoC ("Basic usage" and "Usage with options") -- so let's remove it from the docs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tmeasday on a related note, do you think we should have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tmeasday Just so we're on the same line: you want to deprecate the Related to this, should I then rewrite all the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry guys, missed this!
Probably a good idea @shilman -- perhaps a small refactor of the stories after we get the last few PRs merged is in order.
Yeah that was what I was thinking, do you agree? Definitely get rid of the HoC; and
What I've been doing is leaving the existing ones as "tests" for the deprecated functionality (we can remove them when actually delete that functionality). Then making sure there are stories to cover all the new ways of doing it! You don't need to re-test that parameter inheritance works though (although maybe adding a the story in I hope I'm making sense here 😬 |
||
|
||
To configure default options for all usage of the info option, use `setDefaults` in `.storybook/config.js`: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import React from 'react'; | ||
import nestedObjectAssign from 'nested-object-assign'; | ||
import { makeDecorator } from '@storybook/addons'; | ||
import { logger } from '@storybook/client-logger'; | ||
import Story from './components/Story'; | ||
import PropTable from './components/PropTable'; | ||
|
@@ -82,10 +83,17 @@ function addInfo(storyFn, context, infoOptions) { | |
return <Story {...props}>{storyFn(context)}</Story>; | ||
} | ||
|
||
export const withInfo = textOrOptions => { | ||
const options = typeof textOrOptions === 'string' ? { text: textOrOptions } : textOrOptions; | ||
return storyFn => context => addInfo(storyFn, context, options); | ||
}; | ||
export const withInfo = makeDecorator({ | ||
name: 'withInfo', | ||
parameterName: 'info', | ||
wrapper: (getStory, context, { options, parameters }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to skip the addon if they don't set options or parameters? Either by passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise (or maybe anyway) we should document setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Def agree with the Example: https://github.com/storybooks/storybook/pull/3697/files#diff-c915ecbefba3a2734ebd93eb258431dbR353 This is basically an parameters- and options-less story, besides some minor options (header styling) and parameters (inline and manual text). The configuration don't add a lot, but the addon itself adds a lot of information stand-alone already. So I would say just only documenting the disable flag should be the only way There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, makes sense. Thanks for thinking about that. |
||
const storyOptions = parameters || options; | ||
const infoOptions = typeof storyOptions === 'string' ? { text: storyOptions } : storyOptions; | ||
const mergedOptions = | ||
typeof infoOptions === 'string' ? infoOptions : Object.assign(options, infoOptions); | ||
return addInfo(getStory, context, mergedOptions); | ||
}, | ||
}); | ||
|
||
export { Story }; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,13 +105,27 @@ export default class ClientApi { | |
|
||
const fileName = m ? m.id : null; | ||
|
||
// Add the fully decorated getStory function. | ||
this._storyStore.addStory(kind, storyName, this._decorateStory(getStory, decorators), { | ||
...this._globalParameters, | ||
...localParameters, | ||
...parameters, | ||
fileName, | ||
const allParam = { fileName }; | ||
|
||
[this._globalParameters, localParameters, parameters].forEach(params => { | ||
if (params) { | ||
Object.keys(params).forEach(key => { | ||
if (typeof params[key] === 'object') { | ||
allParam[key] = Object.assign({ ...allParam[key] }, params[key]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just |
||
} else { | ||
allParam[key] = params[key]; | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
// Add the fully decorated getStory function. | ||
this._storyStore.addStory( | ||
kind, | ||
storyName, | ||
this._decorateStory(getStory, decorators), | ||
allParam | ||
); | ||
return api; | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is worth indicating this should be done in
.storybook/config.js