storybookjs / storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation
https://storybook.js.org
MIT License
84.67k stars 9.32k forks source link

Storybook in React app isn't loading emotion styles. #7325

Closed johnsocf closed 1 year ago

johnsocf commented 5 years ago

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:

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..

shilman commented 5 years ago

what version of emotion are you using?

slorber commented 5 years ago

Myself using emotion 10, the css prop won't work. There are similar issues in Emotion repo btw.

stale[bot] commented 5 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

justinanastos commented 5 years ago

I'm also seeing the css prop not working in any stories in Storybook v5.2-rc0. I'm using @emotion/core v10.0.15.

Update:

This is using the jsx pragma method (https://emotion.sh/docs/css-prop#jsx-pragma). Perhaps the jsx renderer is being overridden by Storybook?

image

shilman commented 5 years ago

Related: https://github.com/storybookjs/storybook/issues/7698

@justinanastos Are you using CSF? MDX? storiesOf stories?

Thanks!

justinanastos commented 5 years ago

@shilman it's a project using max and storiesOf, same one we went over before. I'm only using the css prop on storiesOf stories.

shilman commented 5 years ago

@vinspee Didn't you manage to get this working with a workaround?

justinanastos commented 5 years ago

I'd love to hear about a workaround! I tried the babel plugin with no luck, too.

stale[bot] commented 5 years ago

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

VinSpee commented 5 years ago

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

duncanleung commented 4 years ago

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:

ndelangen commented 1 year ago

This is fixed in storybook 7.0 beta.