storybooks and styled-components with new config system
See original GitHub issueThere is no documentation how to integrate storybook and styled components on the new config system
previously I had a .storybook/config.js:
import { configure } from "@storybook/react"
import { action } from "@storybook/addon-actions"
import { addDecorator } from '@storybook/react';
import React from 'react';
import GlobalStyle from '../src/components/GlobalStyle';
function withGlobalStyles(storyFn) {
return (
<React.Fragment>
<GlobalStyle />
{storyFn()}
</React.Fragment>
);
}
addDecorator(withGlobalStyles);
// automatically import all files ending in *.stories.js
configure(require.context("../src", true, /\.stories\.js$/), module)
// Gatsby's Link overrides:
// Gatsby defines a global called ___loader to prevent its method calls from creating console errors you override it here
global.___loader = {
enqueue: () => {},
hovering: () => {},
}
// Gatsby internal mocking to prevent unnecessary errors in storybook testing environment
global.__PATH_PREFIX__ = ""
// This is to utilized to override the window.___navigate method Gatsby defines and uses to report what path a Link would be taking us to if it wasn't inside a storybook
window.___navigate = pathname => {
action("NavigateTo:")(pathname)
}
How do this translates to a .storybook/main.js config?
Issue Analytics
- State:
- Created 4 years ago
- Comments:20 (2 by maintainers)
Top Results From Across the Web
Build UI components - Storybook Tutorials
Learn how to develop UIs with components and design systems. Our in-depth frontend guides are created by Storybook maintainers and peer-reviewed by the...
Read more >Setting up a Design System in Storybook with React, Styled ...
To use Tailwind together with Styled-Components, we will need to install ... On the root directory, add a new file called postcss.config.js ...
Read more >Default styles in Storybook with Styled Components
Recently I was building a Storybook for my new project. ... import { createGlobalStyle } from "styled-components"; import reboot from ...
Read more >Starting with Gatsby + Styled Components + Storybook
Step 1: Installation · Step 2. Create new component and stories · Step 3. How to set global styles in Storybook environment ·...
Read more >The Ultimate Guide to Storybook for React Applications - PART II
When you installed Storybook, a new file called main.js was added to your ... We'll build the component in React with Styled Components, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@cristianaortiga i’m going to detail the steps i took to triage this and make both frameworks work together, with the added bonus of using Styled Components. The reproduction used is a “mashed redux”(pardon the bad pun) version of the LearnStorybook tutorial and the Gatsby documentation on how to use Styled Components. Here we go.
Created a new Gatsby website based on the hello world starter.
Installed the necessary packages for Styled components, more specifically
gatsby-plugin-styled-components
,styled-components
babel-plugin-styled-components
.Added Storybook to the site, through running
npx -p @storybook/cli sb init
After the installation process finished i added some extra Storybook packages, more specifically
@storybook/addon-a11y
,@storybook/addon-viewport
and@storybook/addon-knobs
.Updated the configuration file(
.storybook/main.js
) in order for make Storybook look inside thecomponents
folder as oposed to the default config, which will introspect all of thesrc
folder. I did this for one key thing, Gatsby treats thepages
folder as a special folder. And if you write a story somewhere and run Storybook in development mode for instance, Storybook will fail and you’ll get accosted by some weird error messages, probably something like the following:With that my
.storybook/main.js
now looks like:main.js
file. Probably 90% of the time you wont’ need nothing more, but there are some cases like this one that will require some extra work. What this means is that everything that affects the way the component stories “look and feel” on a global scale, or if some addons are needed, then that configuration should be in a file calledpreview.js
, more specifically adding system wide decorators, arguments or use a global css, or in this particular case use Styled components. With that out of the way i created apreview.js
inside the.storybook
folder with the following:.storybook
folder calledwebpack.config.js
(using this will change Storybook to full control mode, meaning that you’re responsible for controlling what is built and configured) with the following content:With that the config part is over, onto the components and stories.
src\components
folder and inside it i added some components:GlobalStyle.js
that will be used by both Gatsby and Storybook with the following inside it:Task.js
with the following inside:Tasks.js
which will be basically a composite component ofTask
with the following:User.js
with the following:IndexScreen.js
with the following inside:This is a special component, for two reasons, one, it contains a GraphQL query and you’ll see shortly how you can handle it inside Storybook and two it will “act” as traditional Gatsby page, to avoid having to deal with error i mentioned above.
We have the components and Storybook configured, onto the stories.
I created a Storybook story for each component, so that you could see it all in effect.
User.stories.js
:Task.stories.js
contains the following:Tasks.stories.js
contains the following:IndexScreen.stories.js
After all of this, issuing
yarn storybook
(i use yarn, if you use npm, you’ll need to use npm run storybook) to start Storybook in development mode and waiting for the build to complete, i’m presented with this:Should you encounter this issue in your side, this is due to the fact that the
IndexScreen
component uses a GraphQL query and is expecting something to be present that is not, the data relevant to the component does not exist anywhere. The trick to bypass this is the following, runyarn develop
, (once again i’m using yarn, if you use npm, adjust accordingly). to generate a Gatsby development build and with that create the piece of data that is required and then run Storybook withyarn storybook
.After that, opening up
http://localhost:6006
in your browser you should be presented with:In order to make this reproduction complete i added the serve package to my Gatsby site to see how Storybook would behave if i issued a Storybook production build. With that i changed my scripts aswell to incorporate this change and now it looks like
Issued
yarn build-storybook
it ran to completion and thenyarn preview-storybook
and opening uphttp://localhost:5000
i’m presented with mocked production deployment version of Storybook.The whole code behind this comment is in this repo, i would sugest you go over it at your own pace and tinker with it, before deep diving into the pr.
A nice touch to include in the documentation update would be mentioning the fix to the error stated above. I think that could help out not only the Gatsby community but also the Storybook community.
I’ll be on the lookout for the pr and continue to help you with it. And i really hope this goes through and you get your much deserved swag.
Finally, sorry for the extremely long post, but i intended to provide you a full blown implementation that you could use as a baseline to when you update the documentation, also don’t forget to mention this issue when you submit the pr.
Update: I got it even simpler, zero config typescript, no preset, with Storyook 6 (on next branch)
main.ts: