Skip to content

Commit b8de481

Browse files
committed
Added ES7 decorators support.
1 parent 5f54600 commit b8de481

File tree

3 files changed

+79
-8
lines changed

3 files changed

+79
-8
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Refer to [`css-modulesify`](https://github.com/css-modules/css-modulesify).
136136
*/
137137
```
138138

139-
Then you need to decorate your component using `CSSModules`, e.g.
139+
You need to decorate your component using `react-css-modules`, e.g.
140140

141141
```js
142142
import React from 'react';
@@ -157,6 +157,26 @@ export default CSSModules(Car, styles);
157157

158158
Thats it!
159159

160+
As the name implies, `react-css-modules` is compatible with the [ES7 decorators](https://github.com/wycats/javascript-decorators) syntax:
161+
162+
```js
163+
import React from 'react';
164+
import styles from './car.css';
165+
import CSSModules from 'react-css-modules';
166+
167+
@CSSModules(styles)
168+
export default class extends React.Component {
169+
render () {
170+
return <div className='car'>
171+
<div className='front-door'>front-door</div>
172+
<div className='back-door'>back-door</div>
173+
</div>;
174+
}
175+
}
176+
```
177+
178+
[Awesome!](https://twitter.com/intent/retweet?tweet_id=636497036603428864)
179+
160180
### Options
161181

162182
Options are supplied as the third parameter to the `CSSModules` function.

dist/index.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ var _linkClass = require('./linkClass');
1818

1919
var _linkClass2 = _interopRequireDefault(_linkClass);
2020

21+
var functionConstructor = undefined,
22+
decoratorConstructor = undefined;
23+
2124
/**
22-
* @param {ReactClass} Component
25+
* When used as a function.
26+
*
27+
* @param {Function} Component
2328
* @param {Object} styles CSS modules class map.
2429
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
25-
* @return {ReactClass}
30+
* @return {Function}
2631
*/
27-
28-
exports['default'] = function (Component, styles) {
32+
functionConstructor = function (Component, styles) {
2933
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
3034

3135
return (function (_Component) {
@@ -60,4 +64,25 @@ exports['default'] = function (Component, styles) {
6064
})(Component);
6165
};
6266

67+
/**
68+
* When used as a ES7 decorator.
69+
*
70+
* @param {Object} styles CSS modules class map.
71+
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
72+
* @return {Function}
73+
*/
74+
decoratorConstructor = function (styles, options) {
75+
return function (Component) {
76+
return functionConstructor(Component, styles, options);
77+
};
78+
};
79+
80+
exports['default'] = function () {
81+
if (typeof arguments[0] === 'function') {
82+
return functionConstructor(arguments[0], arguments[1], arguments[2]);
83+
} else {
84+
return decoratorConstructor(arguments[0], arguments[1]);
85+
}
86+
};
87+
6388
module.exports = exports['default'];

src/index.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import linkClass from './linkClass';
22

3+
let functionConstructor,
4+
decoratorConstructor;
5+
36
/**
4-
* @param {ReactClass} Component
7+
* When used as a function.
8+
*
9+
* @param {Function} Component
510
* @param {Object} styles CSS modules class map.
611
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
7-
* @return {ReactClass}
12+
* @return {Function}
813
*/
9-
export default (Component, styles, options = {}) => {
14+
functionConstructor = (Component, styles, options = {}) => {
1015
return class extends Component {
1116
render () {
1217
if (options.allowMultiple !== false) {
@@ -23,5 +28,26 @@ export default (Component, styles, options = {}) => {
2328

2429
return linkClass(super.render(), styles, options);
2530
}
31+
};
32+
};
33+
34+
/**
35+
* When used as a ES7 decorator.
36+
*
37+
* @param {Object} styles CSS modules class map.
38+
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
39+
* @return {Function}
40+
*/
41+
decoratorConstructor = (styles, options) => {
42+
return (Component) => {
43+
return functionConstructor(Component, styles, options);
44+
};
45+
};
46+
47+
export default (...args) => {
48+
if (typeof args[0] === 'function') {
49+
return functionConstructor(args[0], args[1], args[2]);
50+
} else {
51+
return decoratorConstructor(args[0], args[1]);
2652
}
2753
};

0 commit comments

Comments
 (0)