Skip to content

Commit 45f2352

Browse files
author
Rick
committed
Merge branch 'master' of git://github.com/moret/react-inline-css into moret-master
2 parents 3e68de1 + cd70834 commit 45f2352

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,55 @@ You get namespaced CSS that works on sub-components (comparable to HTML5 `<style
7373

7474
For a cascaded effect, see the `index.html` demo.
7575

76+
## Options
77+
78+
### Component Name
79+
80+
You can override the `&` as the default selector to the current component. This is useful if you want to require the css from an external file and make any precompilations steps with it. Here's an ES6 example with [SASS loader for Webpack](https://www.npmjs.com/package/sass-loader):
81+
82+
**component.js**
83+
```javascript
84+
import React from 'react';
85+
import InlineCss from 'react-inline-css';
86+
let css = require('!raw!sass!./component.scss');
87+
88+
class Component extends React.Component {
89+
render() {
90+
return (
91+
<InlineCss componentName='base' stylesheet={css}>
92+
<div className='facebook'>Mao is no longer red!</div>
93+
<div className='google'>Mao is no longer red!</div>
94+
<div className='twitter'>Mao is no longer red!</div>
95+
</InlineCss>
96+
);
97+
}
98+
};
99+
100+
export default Transmit.createContainer(Component);
101+
```
102+
103+
**component.css**
104+
```scss
105+
base {
106+
color: red;
107+
108+
.facebook {
109+
color: blue;
110+
}
111+
.google {
112+
color: blue;
113+
}
114+
.twitter {
115+
color: green;
116+
}
117+
}
118+
```
119+
120+
**result**
121+
122+
![screenshot](https://i.imgur.com/e3ErqTz.png?1)
123+
124+
76125
## Installation
77126

78127
npm install --save react-inline-css

src/react-inline-css.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,30 @@ var refCounter = 0;
1111
var InlineCss = React.createClass({
1212
displayName: "InlineCss",
1313
propTypes: {
14+
componentName: React.PropTypes.string,
1415
stylesheet: React.PropTypes.string.isRequired,
1516
namespace: React.PropTypes.string,
1617
wrapper: React.PropTypes.string
1718
},
18-
_transformSheet: function (stylesheet, namespace) {
19+
_transformSheet: function (stylesheet, namespace, componentName) {
1920
return stylesheet.
2021
// Prettier output.
2122
replace(/}\s*/ig, '\n}\n').
2223
// Regular rules are namespaced.
2324
replace(
2425
/(^|}|;|,)\s*([&a-z0-9\-_\.:#\(\),>*\s]+)\s*(\{)/ig,
25-
function (matched) { return matched.replace(/&/g, "#" + namespace); }
26+
function (matched) {
27+
return matched.replace(componentName, "#" + namespace);
28+
}
2629
);
2730
},
2831
render: function () {
32+
var componentName = this.props.componentName || "&";
2933
var Wrapper = this.props.wrapper || "div";
3034
var namespace = this.props.namespace || "InlineCss-" + refCounter++;
31-
var transformedSheet = this._transformSheet(this.props.stylesheet, namespace);
35+
var transformedSheet = this._transformSheet(
36+
this.props.stylesheet, namespace, componentName
37+
);
3238

3339
return React.createElement(
3440
Wrapper,

0 commit comments

Comments
 (0)