Skip to content

Commit 94d66ee

Browse files
committed
Added "styles" property.
1 parent 764cc6e commit 94d66ee

File tree

3 files changed

+102
-12
lines changed

3 files changed

+102
-12
lines changed

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ React CSS Modules implement automatic mapping of CSS modules. Every CSS class is
1616
- [Module Bundler](#module-bundler)
1717
- [webpack](#webpack)
1818
- [Browserify](#browserify)
19+
- [Extending CSS Styles](#extending-css-styles)
1920
- [Decorator](#decorator)
2021
- [Options](#options)
2122
- [`allowMultiple`](#allowmultiple)
@@ -153,6 +154,70 @@ Refer to [webpack-demo](https://github.com/css-modules/webpack-demo) or [react-c
153154

154155
Refer to [`css-modulesify`](https://github.com/css-modules/css-modulesify).
155156

157+
### Extending CSS Styles
158+
159+
Use `styles` property to overwrite the default component styles.
160+
161+
Explanation using `Table` component:
162+
163+
```js
164+
import React from 'react';
165+
import CSSModules from 'react-css-modules';
166+
import styles from './table.css';
167+
168+
class Table extends React.Component {
169+
render () {
170+
return <div styleName='table'>
171+
<div styleName='row'>
172+
<div styleName='cell'>A0</div>
173+
<div styleName='cell'>B0</div>
174+
</div>
175+
</div>;
176+
}
177+
}
178+
179+
export default CSSModules(Table, styles);
180+
```
181+
182+
In this example, `CSSModules` is used to decorate `Table` component using `./table.css` CSS Modules. When `Table` component is rendered, it will use the properties of the `styles` object to construct `className` values.
183+
184+
Using `styles` property you can overwrite the default component `styles` object, e.g.
185+
186+
```js
187+
import customStyles from './table-custom-styles.css';
188+
189+
<Table styles={customStyles} />;
190+
```
191+
192+
[Interoperable CSS](https://github.com/css-modules/icss) can [extend other ICSS](https://github.com/css-modules/css-modules#dependencies). Use this feature to extend default styles, e.g.
193+
194+
```css
195+
/* table-custom-styles.css */
196+
.table {
197+
composes: table from './table.css';
198+
}
199+
200+
.row {
201+
composes: row from './table.css';
202+
}
203+
204+
/* .cell {
205+
composes: cell from './table.css';
206+
} */
207+
208+
.table {
209+
width: 400px;
210+
}
211+
212+
.cell {
213+
float: left; width: 154px; background: #eee; padding: 10px; margin: 10px 0 10px 10px;
214+
}
215+
```
216+
217+
In this example, `table-custom-styles.css` selectively extends `table.css` (the default styles of `Table` component).
218+
219+
Refer to the [`UsingStylesProperty` example](https://github.com/gajus/react-css-modules-examples/tree/master/src/UsingStylesProperty) for an example of a working implementation.
220+
156221
### Decorator
157222

158223
```js

dist/index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
var _lodashLangIsObject2 = require('lodash/lang/isObject');
4+
5+
var _lodashLangIsObject3 = _interopRequireDefault(_lodashLangIsObject2);
6+
37
Object.defineProperty(exports, '__esModule', {
48
value: true
59
});
@@ -8,8 +12,6 @@ var _createClass = (function () { function defineProperties(target, props) { for
812

913
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
1014

11-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
12-
1315
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
1416

1517
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
@@ -18,18 +20,20 @@ var _linkClass = require('./linkClass');
1820

1921
var _linkClass2 = _interopRequireDefault(_linkClass);
2022

23+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
24+
2125
var decoratorConstructor = undefined,
2226
functionConstructor = undefined;
2327

2428
/**
2529
* When used as a function.
2630
*
2731
* @param {Function} Component
28-
* @param {Object} styles CSS Modules class map.
32+
* @param {Object} defaultStyles CSS Modules class map.
2933
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
3034
* @return {Function}
3135
*/
32-
functionConstructor = function (Component, styles, options) {
36+
functionConstructor = function (Component, defaultStyles, options) {
3337
return (function (_Component) {
3438
_inherits(_class, _Component);
3539

@@ -42,6 +46,16 @@ functionConstructor = function (Component, styles, options) {
4246
_createClass(_class, [{
4347
key: 'render',
4448
value: function render() {
49+
var styles = undefined;
50+
51+
if (this.props.styles) {
52+
styles = this.props.styles;
53+
} else if ((0, _lodashLangIsObject3['default'])(defaultStyles)) {
54+
styles = defaultStyles;
55+
} else {
56+
styles = {};
57+
}
58+
4559
return (0, _linkClass2['default'])(_get(Object.getPrototypeOf(_class.prototype), 'render', this).call(this), styles, options);
4660
}
4761
}]);
@@ -53,13 +67,13 @@ functionConstructor = function (Component, styles, options) {
5367
/**
5468
* When used as a ES7 decorator.
5569
*
56-
* @param {Object} styles CSS Modules class map.
70+
* @param {Object} defaultStyles CSS Modules class map.
5771
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
5872
* @return {Function}
5973
*/
60-
decoratorConstructor = function (styles, options) {
74+
decoratorConstructor = function (defaultStyles, options) {
6175
return function (Component) {
62-
return functionConstructor(Component, styles, options);
76+
return functionConstructor(Component, defaultStyles, options);
6377
};
6478
};
6579

src/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import linkClass from './linkClass';
2+
import _ from 'lodash';
23

34
let decoratorConstructor,
45
functionConstructor;
@@ -7,13 +8,23 @@ let decoratorConstructor,
78
* When used as a function.
89
*
910
* @param {Function} Component
10-
* @param {Object} styles CSS Modules class map.
11+
* @param {Object} defaultStyles CSS Modules class map.
1112
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
1213
* @return {Function}
1314
*/
14-
functionConstructor = (Component, styles, options) => {
15+
functionConstructor = (Component, defaultStyles, options) => {
1516
return class extends Component {
1617
render () {
18+
let styles;
19+
20+
if (this.props.styles) {
21+
styles = this.props.styles;
22+
} else if (_.isObject(defaultStyles)) {
23+
styles = defaultStyles;
24+
} else {
25+
styles = {};
26+
}
27+
1728
return linkClass(super.render(), styles, options);
1829
}
1930
};
@@ -22,13 +33,13 @@ functionConstructor = (Component, styles, options) => {
2233
/**
2334
* When used as a ES7 decorator.
2435
*
25-
* @param {Object} styles CSS Modules class map.
36+
* @param {Object} defaultStyles CSS Modules class map.
2637
* @param {Object} options {@link https://github.com/gajus/react-css-modules#options}
2738
* @return {Function}
2839
*/
29-
decoratorConstructor = (styles, options) => {
40+
decoratorConstructor = (defaultStyles, options) => {
3041
return (Component) => {
31-
return functionConstructor(Component, styles, options);
42+
return functionConstructor(Component, defaultStyles, options);
3243
};
3344
};
3445

0 commit comments

Comments
 (0)