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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pandolajs/babel-plugin-react-css-modules

v1.1.1

Published

CSS Modules for react

Downloads

6

Readme

@pandolajs/babel-plugin-react-css-modules

CSS Modules for react application. Find more details here

Motivaion

The purpose of the plugin is to improve the experience of development with CSS Modules in React Application.

It's superfluous when use CSS Modules to add class name to an element, we have to write code like this className={styles.foo} and we can't use Emmet to finish our code smoothly.

There are some solutions like HOC react-css-modules or babel plugin babel-plugin-react-css-modules, they are excellent and solve the most problems that bother you when developing. You can choose them when @pandolajs/babel-plugin-react-css-modules is not meet your situation.

What difference

We add local CSS class to element via className prop as usual, and add global CSS class via styleName prop only.

Features

  • You can import a style file (css, less, sass, scss) anonymously or named. Like below:
  // import anonymously
  import './index.less'

  // import named
  import styles from './index.less'
  • You can add local class name via prop className with a string or an object with conditions. Like below:
  // string
  <div className="foo bar" />

  // object
  <div className={{
    foot: this.isFoot,
    disabled: !this.useable
  }}>
  • You can add global class name vis prop styleName with a string or an object with conditions. Like below:
  // string
  <div styleName="foo bar" />

  // object
  <div styleName={{
    foot: this.isFoot,
    disabled: !this.useable
  }}>
  • You use className and styleName props at the same time in any format mentioned above in one element. Like below:
  <div classNam="local-foo" styleName="global-bar" />

Usage

Configurataion

  • Step1: Install the plugin.
  npm i -D @pandolajs/babel-plugin-react-css-modules
  • Step2: Install the @pandolajs/classnames as a dependency of you project.
  npm i -S @pandolajs/classnames

@pandolajs/classnames is a small javascript library without any dependencies, it is used as a runtime dependency when plugin tranform your code. You can provide your own implamentation of course like @pandolajs/classnames API.

  • Step3: config plugin in your babel.config.js or .babelrc.

babel.config.js

  {
    plugins: [
      ['@pandolajs/react-css-modules', options]
    ]
  }

options?: {classname?: {}, handleTemplate?: boolean}

You can specify a option to custom plugin behavior.

  • classnames?: {name: string, source: string, default: boolean}

    • classnames.name: string specify the name of classnames method

    • classnames.source: string specify the module of classnames, default '@pandolajs/classname', you can also specify a relative or absolute path base your project

    • classnames.default: boolean speify import as default or destruct from module, default value is true

  • handleTemplate?: boolean indicate the plugin wether to handle className={btn-${color}} case. Default is false.

You should be careful to set this option to true when in an old project, becasue there maybe some code like className={{styles.btn} ${styles.btnSuccess}} already, it will cause unexpected layout of your app after tranformation by the plugin. You can find the deep reason of here.

configuration

  {
    plugins: [
      ['@pandolajs/react-css-modules', {
        classnames: {
          name: 'cls',
          source: '@scope/your-classname',
          default: false
        }
      }]
    ]
  }

input.js

  <div className="class1 class2" />

output.js

  import { cls } from "@scope/your-classname";
  <div className={cls(_CSSM_, {
    "class1": true,
    "class2": true
  })} />

How it works

You can find more transform example in test directory.