-
-
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
How to use context? #76
Comments
Hi @davidpelaez we are doing this by setting context on each component. Not ideal, but in the context of a component library which we are building - it makes sense, having each component be self containing so they can be used in isolation.
|
@davidpelaez, They way you do it the correct way to do it. But, I think we can introduce a new API to make these things a bit easier. See: import { storiesOf } from '@kadira/storybook';
import Context from '../context';
import MyComp from '../my_comp';
storiesOf('MyComp').
.addDecorator(function(getStory) {
return (<Context>getStory()</Context>)
})
.add('default view', () => (
<MyComp>Something here</MyComp>
)) This is just a nice syntax but it will wrap each story with the context. But, I think that's the way it should be anyway. |
The same is needed for material-ui. Currently I wrap every story with a |
@mcbain exactly I came to this because of material-ui. Thanks for pointing David Peláez Tamayo On Fri, Apr 15, 2016 at 1:47 AM, mcbain [email protected] wrote:
|
@mnmtanish could you work on the If some else could do it before @mnmtanish that's great too :) |
Could you not use something like https://github.com/mattzeunert/react-with-context? |
I'm tring to use storybook with Material-ui. And the Dropdown menu in material-ui not response for clicking.
here is content in context.js:
|
Storybook can't read |
I had the same issue with SelectField, the answer is in the material-ui readme.md You need to load the
import injectTapEventPlugin from 'react-tap-event-plugin';
// Needed for onTouchTap
// http://stackoverflow.com/a/34015469/988941
injectTapEventPlugin(); |
Thanks for both of your answers! It's working now. |
Is it possible get formsy-react to work with storybook / meteor-mantra ? All fields just disappears in storybook. |
@VirtueMe Where did you wind up putting the |
I added it to the |
Was trying to use Material UI with storybook today and came up with this solution:
import React from 'react';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import defaultTheme from '../../../common/app/defaultTheme';
export default function materialize(Component, props) {
return (
<MuiThemeProvider muiTheme={defaultTheme}>
<Component {...props} />
</MuiThemeProvider>
);
}
import React from 'react';
import Header from '../Header';
import materialize from './materialize';
import { storiesOf, action } from '@kadira/storybook';
storiesOf('Header', module)
.add('default button view', () => {
const props = {
label: "Button"
}
return materialize(Header, props);
})
.add('primary button view', () => {
const props = {
label: "Button",
primary: true
}
return materialize(Header, props);
}); |
@michaltakac No need for the import React from 'react';
import { action, storiesOf } from '@kadira/storybook';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import StarRating from '../common-ui/components/StarRating';
storiesOf('common.StarRating', module)
.addDecorator(story => (
<MuiThemeProvider muiTheme={getMuiTheme()}>
{story()}
</MuiThemeProvider>
))
.add('summary mode', _ => (
renderStarRating()
))
.add('details mode', _ => (
renderStarRating({ mode: 'details' })
))
.add('rate mode', _ => (
renderStarRating({ mode: 'rate' })
));
function renderStarRating(props) {
const ratings = [
{ name: 'Bob G', rating: 5 },
{ name: 'Ali G', rating: 4 },
{ name: 'Frankenstein', rating: 1 },
{ name: 'Santa Claus', rating: 3 },
{ name: 'Another Person', rating: 4 },
{ name: 'Some Gal', rating: 5 },
{ name: 'Some Critical Dude', rating: 1 },
];
return <div style={rootStyle}>
<StarRating ratings={ratings} { ...props } />
</div>
}
const rootStyle = {
alignItems: 'center',
display: 'flex',
justifyContent: 'center',
marginTop: 200,
}; |
Thank you for example code, helped! |
Shouldn't this be closed now that addDecorator exists? |
@vladfr I think yes. |
Hi! Thank you for finding such an elegant solution to this annoying problem! However, can the MUI support be generalized in a way that I can add the decorator only once, in the global config, instead of each individual story? For reference, my
|
@Domiii You can add decorator both locally and globally:
|
@usulpro Does this work for you with the latest version? When visiting the storybook frontend, I get:
|
@Domiii your issue is your webpack config can't understand png files. |
I did an horrible: const story = (...args) => ({
add(name, fn) {
storiesOf(...args).add(name, () => <Wrapper>{fn()}</Wrapper>);
return this;
},
}); same usage than before: configure(() => {
story('StatusChip', module)
.add('with status', () => <StatusChip status="Rejected" />)
.add('with progress', () => <StatusChip progress={72} />);
}) It would be great to allow to configure |
@caub this should do effectively the same:
See https://storybook.js.org/basics/writing-stories/#using-decorators |
I'm using "useContext(user)" in my component and use that variable to populate the component. How would I be able to get that data into the storybook? I can't useContext without a functional component, so I don't know how to access that data in the storybook? I looked everywhere to see an example, but couldn't find it. |
Yeah you definitely need one:
Or, maybe even better, |
Currently many UI libraries like material-ui depend on the context for theming. I wasn't able to figure out a way to setup this. Maybe I should have a global container and use it to wrap all my stories? It doesn't feel 100% clean, but I wanted to check if this if the correct approach.
Thanks for react-storybook, this is amazing! We've wanted this for so long in our team, it's currently changing the way we work with new projects. :)
The text was updated successfully, but these errors were encountered: