React Classname Module allows you to write your classnames for CSS Modules as easy as without CSS Modules. It's build with classnames, so you have a simple and powerful way to write your classnames.
Notice: This is currently a 0.x version. What this actually means is that it works in our project but we're too busy to write tests to be sure that it'll work in your project, too.
npm install --save react-classname-module
See this example. Yuck, it sucks!
importReactfrom"react";importstylesfrom"styles.css";classMyComponentextendsReact.Component{render(){return(<divclassName={styles.head}><h1className={styles["headline--h1"]}>Lorem ipsum</h1><divclassName={styles.content}>
...
</div></div>);}}exportdefaultMyComponent;With React Classname Module you can simply write this nice little piece of code:
importReactfrom"react";importcmfrom"react-classname-module";importstylesfrom"styles.css";classMyComponentextendsReact.Component{render(){return(<divclassName="head"><h1className="headline--h1">Lorem ipsum</h1><divclassName="content">
...
</div></div>);}}exportdefaultcm(MyComponent,styles);And that's it.
- Supports local and global classnames
- Works with stateless Components
- Works with multiple classes:
className="btn btn-highlight" - Works with classnames
- identical local and global classnames
- merging
classNameon a React Component:
e.g.<Foo className="bar" />will override the Components className with the CSS Module"bar"
Instead of writing a string into classname, you can pass a typical classnames object into it.
letclassnames={foo: true,bar: true,"foo-bar": false}<divclassName={classnames}></div>- Fixed missing
defaultPropsandpropTypesin stateless Components
- Removed silly dead code
- Support for stateless components
- initial version
