npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

styled-modules

v0.2.4

Published

CSS Modules support in Next.js

Downloads

28

Readme

Styled Modules

Greenkeeper badge Build Status Coverage Status NPM Version

CSS Modules support in Next.js based on styled-jsx.

Installation

Styled Modules uses CSS Loader to enable CSS Modules, run the following to install it:

npm install css-loader --save-dev

Next, run the following to install Styled Modules:

npm install styled-modules

Configuration

In the root of your Next.js project directory, create next.config.js with the following:

// next.config.js
module.exports = (config, { dev }) => {
  config.module.rules.push(
    {
      test: /\.css$/,
      use: [
        'babel-loader',
        'styled-modules/loader',
        'css-loader?modules',
      ],
    },
  );
  return config;
};

Next, add styled-modules/babel to plugins in your .babelrc:

{
  "plugins": [
    ["styled-modules/babel", {
      "pattern": "\\.css$"
    }]
  ]
}

Usage

Now, you can start using CSS Modules in your Next.js project as follows:

import styles from './styles.css';

export default () => (
  <div>
    <h1 className={styles.title}>Styled Modules</h1>
    <ol>
      {[
        'Item 1',
        'Item 2',
        'Item 3'
      ].map((item, index) => (
        <li key={index}>{item}</li>
      ))}
    </ol>
    {!this.props.hidden && (
      <div>Hidden content</div>
    )}
  </div>
);

Notice that the styles only got applied on the client side. To make it works on the server side as well, create a custom document at ./pages/_document.js with the following code:

import Document from 'next/document';
import flush from 'styled-modules/server';

class MyDocument extends Document {
  static getInitialProps({ renderPage }) {
    return {
      ...renderPage(),
      styles: flush(),
    };
  }
}

export default MyDocument;

How It Works

  • Check whether styled-modules/style is imported explicitly. If so, then it won't transpile. This will allow you to manually wrap your component with styled-modules/style. Note that you should only wrap the root of your component.
  • Find any import and require statements that matches pattern option. If none is matched, then it won't transpile.
  • Find any jsx syntaxes. if its parent is not a jsx syntax, then wrap it with styled-modules/style.

The example above transpiles to the following:

import _StyledModules from 'styled-module/style';
import styles from './styles.css';

export default () => (
  <_StyledModules
    styles={[{
      __hash: styles.__hash,
      __css: styles.__css
    }]}
  >
    <div>
      <h1 className={styles.title}>Styled Modules</h1>
      <ol>
        {[
          'Item 1',
          'Item 2',
          'Item 3'
        ].map((item, index) => (
          <_StyledModules
            key={index}
            styles={[{
              __hash: styles.__hash,
              __css: styles.__css
            }]}
          >
            <li>{item}</li>
          </_StyledModules>
        ))}
      </ol>
      {!this.props.hidden && (
        <_StyledModules
          styles={[{
            __hash: styles.__hash,
            __css: styles.__css
          }]}
        >
          <div>Hidden content</div>
        </_StyledModules>
      )}
    </div>
  </_StyledModules>
);

As you can see, the styled-modules/style component is injected multiple times. This could be a problem when a wrapped component parent depends on or needs to manipulate the children, i.e. react-transition-group. To avoid that, you can explicitly import and wrap the component like the following:

import _StyledModules from 'styled-modules/style';
import styles from './styles.css';

export default () => (
  <_StyledModules styles={[styles]}>
    <div>
      <h1 className={styles.title}>Styled Modules</h1>
      <ol>
        {[
          'Item 1',
          'Item 2',
          'Item 3'
        ].map((item, index) => (
          <li key={index}>{item}</li>
        ))}
      </ol>
      {!this.props.hidden && (
        <div>Hidden content</div>
      )}
    </div>
  </_StyledModules>
);

License

MIT