Skip to content
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

CSS modules #270

Closed
michaltakac opened this issue Jun 22, 2016 · 20 comments
Closed

CSS modules #270

michaltakac opened this issue Jun 22, 2016 · 20 comments

Comments

@michaltakac
Copy link

Hi, I'm trying to use SASS styles like this in my React components:

import React, { Component } from 'react';
import Drawer from 'material-ui/Drawer';
import { Link } from 'react-router' ;
import styles from './sidebar.scss' ;

class Sidebar extends Component {
  constructor(props) {
    super(props);
    this.state = { open: true };
  }

  render() {
    return (
      <div>
        <Drawer className={styles.drawer} open={this.state.open}>
          <div className={styles.header}>
            <Link className={styles.profilePic} to="#">Item</Link>
          </div>
          <Link className={styles.item} to="#">Profile</Link>
          <Link className={styles.item} to="#">Dashboard</Link>
          <Link className={styles.item} to="#">Charts</Link>
          <Link className={styles.item} to="#">Calendar</Link>
        </Drawer>
      </div>
    );
  }
}

export default Sidebar;

Styles are working in my app normally, all good. But when I open them in storybook, styles won't show up. Is this problem with my webpack setup for storybook? Here is how it looks like:

const path = require('path');
const autoprefixer = require('autoprefixer');

module.exports = {
  module: {
    loaders: [
      {
        loader: 'url-loader?limit=10000',
        test: /\.(gif|jpg|png|svg)$/,
        include: path.resolve(__dirname, '../')
      }, {
        loader: 'url-loader?limit=1',
        test: /favicon\.ico$/,
        include: path.resolve(__dirname, '../')
      }, {
        loader: 'url-loader?limit=100000',
        test: /\.(ttf|eot|woff(2)?)(\?[a-z0-9]+)?$/,
        include: path.resolve(__dirname, '../')
      },
      {
        loader: 'style-loader!css-loader',
        test: /\.css$/,
        include: path.resolve(__dirname, '../'),
        exclude: path.resolve(__dirname, '../node_modules')
      },
      {
        loader: 'style-loader!css-loader!sass-loader',
        test: /\.scss$/,
        include: path.resolve(__dirname, '../'),
        exclude: path.resolve(__dirname, '../node_modules')
      },
    ]
  },
  sassLoader: {
    includePaths: path.resolve(__dirname, '../src/browser')
  },
};

If I import styles with import './sidebar.scss' ; and then write class names explicitly into classNames, then styles are used correctly in storybook (so SASS files are loaded without problem, loaders in webpack works).

What should I do if I want to use styles like I shown in my Sidebar component on top of this comment?

@nickick
Copy link

nickick commented Jun 28, 2016

We're having this problem too, but with PostCSS. Any help here is appreciated.

@arunoda
Copy link
Member

arunoda commented Jun 28, 2016

Is it possible to send me a sample repo, I can work with this.
So, I can find out the problem with PostCSS o SASS.

@michaltakac
Copy link
Author

seems like when I use this loader setup in my app's webpack config:

      {
        test: /\.css?$/,
        loaders: [ 'style-loader', 'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]' ],
        include: path.resolve(__dirname, '../')
      },
      {
        test: /\.scss$/,
        loader: 'style!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!sass-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
        include: path.resolve(__dirname, '../')
      }

and the same in storybooks webpack:

     {
        test: /\.css?$/,
        loaders: [ 'style', 'css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]' ],
        include: __dirname
      },
      {
        test: /\.scss$/,
        loader: 'style!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!sass-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
        include: __dirname
      }

Then CSS modules works from both plain .css files and also from .scss files.
Installed packages sass-loader, css-loader. Example repo:
https://github.com/michaltakac/react-storybook-demo/tree/sass-css-modules, branch sass-css-modules.

Or check commit: michaltakac/react-storybook-demo@aa13f1d

Maybe I guess this can be closed then? @nickick @arunoda

@arunoda
Copy link
Member

arunoda commented Jun 29, 2016

Thansk @michaltakac for the sample.
I'll close this issue.

@garrettmaring
Copy link

This solution seems to not work anymore. I'm having trouble getting CSS modules (specifically, React CSS Modules) to work within Storybook.

Steps:

  1. create-react-app
  2. Get Storybooks setup
  3. Load component that uses React CSS Modules

@blaze-su
Copy link

+1

@MarcoWorms
Copy link

MarcoWorms commented Nov 29, 2016

I am having the same issue here. I can't make it work with css modules.

@arunoda

@blaze-su
Copy link

Look here. Work Fine

#541

@MarcoWorms
Copy link

MarcoWorms commented Nov 29, 2016

I got it to work, thanks. Just for future reference, if you want to just add css modules to your react storybook project:

  1. add a webpack.config.js on your .storybook directory

  2. put this inside:

module.exports = {
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: 'style!css?modules'
      }
    ]
  }
}
  1. be happy with sane css in storybook :)

@karuzela
Copy link

karuzela commented Mar 21, 2017

Guys (particularly @arunoda), do you have any clue what I could be doing wrong here? Styles from CSS Modules don't show up in the storybook despite the solution you've provided above. I've officially run out of ideas.

const path = require('path');

module.exports = {
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: 'style!css?modules'
      },
      {
        test: /\.scss$/,
        loader: 'style!css!sass',
        exclude: /node_modules/,
        include: path.resolve(__dirname, '../../')
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        loaders: [
          'file?hash=sha512&digest=hex&name=[hash].[ext]',
          'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'
        ]
      },
    ]
  },
}

@eduardoinnorway
Copy link

Not working at all

@daiyanze
Copy link

Since webpack released version 2.0, I think it might have affected how css modules settings work.

@MarcoWorms
Copy link

MarcoWorms commented Jul 26, 2017

For webpack 2 I'm using this in .storybook/webpack.config.js and it's working fine:

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
            options: {
              modules: true,
            },
          },
        ],
      },
    ],
  },
}

@daiyanze
Copy link

@MacroWorms It works like a charm. Thank you for updating the solution.

@hkmarques
Copy link

@MarcoWorms works as of Nov 10th, thanks for sharing!

@wiesson
Copy link

wiesson commented Jun 15, 2018

The upcoming version of https://github.com/facebook/create-react-app (v2) will have support for css-modules. How could storybook detect that?

@hellobrian
Copy link

@MarcoWorms works for me as of Aug. 2nd. Thanks!

@onisaint
Copy link

the above is invalid as of Feb. 2019.
Just found storybooks has a dependency on react-scripts.
just install react-scripts and everything should work...
Happy coding!

@jnhooper
Copy link

I had similar issues and this config ended up working for me.

module.exports = (BaseConfig, env, defaultConfig) => {
  const rules = [];
  // Do this because sass-loader does not like it when there are
  // multiple declarations of it in rules.. and we want css modules
  // turned on
  defaultConfig.module.rules.forEach(rule => {
    const testCssString = "myscss.scss";
    const regex = RegExp(rule.test);
    if(!regex.test(testCssString)){
      rules.push(rule);
    }
  });
  defaultConfig.module.rules = rules;

  defaultConfig.module.rules = [
    {
      test: /\.scss$/,
      include: srcDir,
      loaders: [
        "style-loader",
        {
          loader: "css-loader",
          options: {
            importLoaders: "1",
            localIdentName: "[name]__[local]___[hash:base64:5]",
            modules: true,
          }
        },
        "sass-loader"
      ]
  },
      {
        test: /\.svg$/,
        issuer: {
          test: /.jsx?$/
        },
        use: ['@svgr/webpack']
      },
  ...rules
  ]

@Vazerthon
Copy link

the above is invalid as of Feb. 2019.
Just found storybooks has a dependency on react-scripts.
just install react-scripts and everything should work...
Happy coding!

Thanks @madhudskumar! Installing react-scripts worked for me in a Gatsby project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests