|
3 | 3 | [](https://travis-ci.org/gajus/react-css-modules)
|
4 | 4 | [](https://www.npmjs.org/package/react-css-modules)
|
5 | 5 |
|
| 6 | +## What's the Problem? |
| 7 | + |
| 8 | +[CSS modules](https://github.com/css-modules/css-modules) are awesome. If you are not familiar with CSS modules, it is a concept of using a module bundler such as webpack to load CSS scoped to a particular document. CSS modules loader will generate a unique name for a each CSS class at the time of loading the CSS. Refer to [webpack-demo](https://css-modules.github.io/webpack-demo/) for a full example. |
| 9 | + |
| 10 | +In the context of React, this looks like this: |
| 11 | + |
| 12 | +```js |
| 13 | +import React from 'react'; |
| 14 | +import styles from './car.css'; |
| 15 | + |
| 16 | +export default class Car extends React.Component { |
| 17 | + render () { |
| 18 | + return <div className={styles.car}> |
| 19 | + <div className={styles.frontDoor}></div> |
| 20 | + <div className={styles.backDoor}></div> |
| 21 | + </div>; |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +Awesome! |
| 27 | + |
| 28 | +However, there are a several disadvantages of this approach: |
| 29 | + |
| 30 | +* You have to use `camelCael` CSS class names. |
| 31 | +* You have to use `styles` object whenever assigning a class. |
| 32 | + |
| 33 | +React CSS Modules enables seamless CSS modules for React, e.g. |
| 34 | + |
| 35 | +```js |
| 36 | +import React from 'react'; |
| 37 | +import styles from './car.css'; |
| 38 | +import CSSModules from 'react-css-modules'; |
| 39 | + |
| 40 | +class Car extends React.Component { |
| 41 | + render () { |
| 42 | + return <div className='car'> |
| 43 | + <div className='front-door'></div> |
| 44 | + <div className='back-door'></div> |
| 45 | + </div>; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +export default CSSModules(Car, styles); |
| 50 | +``` |
| 51 | + |
6 | 52 | ## Usage
|
7 | 53 |
|
8 | 54 | ```js
|
| 55 | +/** |
| 56 | + * @typedef CSSModules~Options |
| 57 | + * @property {Boolean} allowMultiple Determines whether `className` can have multiple class names. Throws an error when the constrained is not met. Default: true. |
| 58 | + * @property {Boolean} keepOriginal Determines whether the original `className` value is kept in addition to the appended CSS modules styles CSS class name. Default: true. |
| 59 | + * @property {Boolean} errorNotFound Determines whether an error is raised if `className` defines a CSS class(es) that is not present in the CSS modules styles. Default: false. |
| 60 | + */ |
| 61 | + |
9 | 62 | /**
|
10 | 63 | * @param {ReactClass} Component
|
11 | 64 | * @param {Object} styles CSS modules class map.
|
12 |
| - * @param {Object} options {@link https://github.com/gajus/react-css-modules#options} |
| 65 | + * @param {CSSModules~Options} options |
13 | 66 | * @return {ReactClass}
|
14 | 67 | */
|
15 | 68 | ```
|
@@ -82,11 +135,3 @@ However, using React CSS Modules, you can map as many CSS classes to the element
|
82 | 135 | ```
|
83 | 136 |
|
84 | 137 | This will work as you'd expect.
|
85 |
| - |
86 |
| -## Options |
87 |
| - |
88 |
| -|Name|Type|Description|Default| |
89 |
| -|---|---|---|---| |
90 |
| -|`allowMultiple`|`Boolean`| Determines whether `className` can have multiple class names. Throws an error when the constrained is not met. |`true`| |
91 |
| -|`keepOriginal`|`Boolean`| Determines whether the original `className` value is kept in addition to the appended CSS modules styles CSS class name. |`true`| |
92 |
| -|`errorNotFound`|`Boolean`| Determines whether an error is raised if `className` defines a CSS class(es) that is not present in the CSS modules styles. |`true`| |
|
0 commit comments