question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storybook in React app isn't loading emotion styles.

See original GitHub issue

Describe the bug Storybook in React app isn’t loading emotion styles.

To Reproduce Steps to reproduce the behavior:

  1. adding storybook to existing react app which uses emotion.
  2. set up webpack.config.js in ./storybook folder with code snippet below. Set up tsconfig.json with subsequent code snippet.
  3. npm run storybook

Expected behavior I expect that storybook will load my stories with styles. It loads the stories in the storybook but they don’t have styles associated.

Code snippets

for webpack.config.js in ./storybook

const path = require('path');

const SRC_PATH = path.join(__dirname, '../src');

module.exports = async ({ config, mode }) => {
    config.module.rules.push({
        test: /\.(ts|tsx)$/,
        include: [SRC_PATH],
        use: [
            {
                loader: require.resolve('awesome-typescript-loader'),
                options: {
                    configFileName: './.storybook/tsconfig.json',
                }
            },
            { loader: require.resolve('react-docgen-typescript-loader') }
        ]
    });
    config.resolve.extensions.push('.ts', '.tsx');
    return config;
};

for tsconfig.json in .storybook/

{
  "compilerOptions": {
    "baseUrl": "./",
    "allowSyntheticDefaultImports": true,
    "module": "es2015",
    "target": "es5",
    "lib": ["es6", "dom"],
    "sourceMap": true,
    "allowJs": false,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "../",
    "outDir": "dist",
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "declaration": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules",
    "build",
    "dist",
    "scripts",
    "acceptance-tests",
    "webpack",
    "jest",
    "src/setupTests.ts",
    "**/*/*.test.ts",
    "examples"
  ],
  "awesomeTypescriptLoaderOptions": {
    "useBabel": true,
    "babelOptions": {
      "babelrc": false, /* Important line */
      "presets": ["@babel/preset-env", "@emotion/babel-preset-css-prop"],
      "plugins": ["emotion"]
    },
    "babelCore": "@babel/core" // needed for Babel v7
  }
}

System:

  • OS: Windows
  • Device: na
  • Browser: Chrome
  • Framework: React
  • Addons: na
  • Version: latest

Additional Context: When I inspect the markup I can see that styles with data-emotion attributes are in the file and a class is specified (Also I see my css selectors and css styles in these style tags), but the dom doesn’t associate to those styles with any class as if something fails in setting up the emotion connection between classes for styles and the markup…

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
duncanleungcommented, Jan 7, 2020

In my project I was able to get Storybook to recognize css-prop (without using the jsx pragma) by adding this config to /.storybook/webpack.config.js.

Line 17 - Line 19:

  config.module.rules[0].use[0].options.presets = [
    require.resolve('@babel/preset-react'),
    require.resolve('@babel/preset-env'),
    // Emotion preset must run BEFORE reacts preset to properly convert css-prop.
    // Babel preset-ordering runs reversed (from last to first). Emotion has to be after React preset.
    require.resolve('@emotion/babel-preset-css-prop'),
  ];

Also, I am including the @emotion/babel-preset-css-prop for .ts and .tsx files

Line 41 - Line 43:

  config.module.rules.push({
    test: /\.(ts|tsx)$/,
    loader: require.resolve('babel-loader'),
    options: {
      presets: [
        ['react-app', { flow: false, typescript: true }],
        // Emotion preset must run BEFORE reacts preset to properly convert css-prop.
        // Babel preset-ordering runs reversed (from last to first). Emotion has to be after React preset.
        require.resolve('@emotion/babel-preset-css-prop'),
      ],
      // other plugins here...
    },
  });

I created a template to share my project config that uses Gatsby + TypeScript + Emotion + Storybook + React Intl + SVGR + Jest: https://github.com/duncanleung/gatsby-typescript-emotion-storybook

Here’s a link to my webpack.config.js that enables @emotion/babel-preset-css-prop:

0reactions
VinSpeecommented, Sep 27, 2019

Sorry for the late reply! WRT my use in the Docs page, the /** @jsx */ pragma worked fine

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Storybook not showing styles
Every component in React is self contained - your DialogModal component won't get styled because in Storybook it is not being rendered within ......
Read more >
Theming
Reuse the theme variables above for a native Storybook developer experience. The theming engine relies on emotion, a CSS-in-JS library. // YourTheme.js ......
Read more >
Use Emotion CSS Prop in Storybook - Duncan Leung
I was running into an issue where my Storybook webpack.config.js was not correctly parsing and evaluating Emotion styles passed via the css prop ......
Read more >
[Solved]-Storybook not showing styles-Reactjs
You'll need to import any styles you use in App.js globally in storybook, by importing them in .storybook/preview.js (create the file if it...
Read more >
Simple Storybook React Setup with Dark Mode Integration
We are going to use Emotion to style our components and also to manage the dark mode theme switching. You can use any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found