Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow choosing component name on css instead of the default "&" #5

Merged
merged 1 commit into from
Jul 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,55 @@ You get namespaced CSS that works on sub-components (comparable to HTML5 `<style

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

## Options

### Component Name

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):

**component.js**
```javascript
import React from 'react';
import InlineCss from 'react-inline-css';
let css = require('!raw!sass!./component.scss');

class Component extends React.Component {
render() {
return (
<InlineCss componentName='base' stylesheet={css}>
<div className='facebook'>Mao is no longer red!</div>
<div className='google'>Mao is no longer red!</div>
<div className='twitter'>Mao is no longer red!</div>
</InlineCss>
);
}
};

export default Transmit.createContainer(Component);
```

**component.css**
```scss
base {
color: red;

.facebook {
color: blue;
}
.google {
color: blue;
}
.twitter {
color: green;
}
}
```

**result**

![screenshot](https://i.imgur.com/e3ErqTz.png?1)


## Installation

npm install --save react-inline-css
Expand Down
12 changes: 9 additions & 3 deletions src/react-inline-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,30 @@ var refCounter = 0;
var InlineCss = React.createClass({
displayName: "InlineCss",
propTypes: {
componentName: React.PropTypes.string,
stylesheet: React.PropTypes.string.isRequired,
namespace: React.PropTypes.string,
wrapper: React.PropTypes.string
},
_transformSheet: function (stylesheet, namespace) {
_transformSheet: function (stylesheet, namespace, componentName) {
return stylesheet.
// Prettier output.
replace(/}\s*/ig, '\n}\n').
// Regular rules are namespaced.
replace(
/(^|}|;|,)\s*([&a-z0-9\-_\.:#\(\),>*\s]+)\s*(\{)/ig,
function (matched) { return matched.replace(/&/g, "#" + namespace); }
function (matched) {
return matched.replace(componentName, "#" + namespace);
}
);
},
render: function () {
var componentName = this.props.componentName || "&";
var Wrapper = this.props.wrapper || "div";
var namespace = this.props.namespace || "InlineCss-" + refCounter++;
var transformedSheet = this._transformSheet(this.props.stylesheet, namespace);
var transformedSheet = this._transformSheet(
this.props.stylesheet, namespace, componentName
);

return React.createElement(
Wrapper,
Expand Down