An open API service indexing awesome lists of open source software.

https://github.com/larsnystrom/module-style

Declarative way to specify module-css with React.
https://github.com/larsnystrom/module-style

css css-modules react

Last synced: 10 months ago
JSON representation

Declarative way to specify module-css with React.

Awesome Lists containing this project

README

          

# ModuleStyle

Declarative way to specify module-css with React, using [css-loader](https://github.com/webpack/css-loader) and [react-side-effect](https://github.com/gaearon/react-side-effect).

Can be used both on the client and the server.

## Install

npm install --save module-style

## Usage

The `ModuleStyle` component is supposed to be used in tandem with [`css-loader`](https://github.com/webpack/css-loader), preferrably with [`css-modules`](https://github.com/webpack/css-loader#css-modules).

### Example

Install dependencies

npm install --save module-style
npm install --save-dev css-loader

`MyModule.js`: React component

import React from 'react';
import ModuleStyle from 'module-style;
import style, { locals as s } from './MyModule.css';

const MyModule = () =>



My module page


...




export default MyModule;

`MyModule.css`: Stylesheet

.root {
background-color: blue;
}

.container {
background-color: red;
}

`webpack.config.js`: Webpack configuration

{
module: {
loaders: [
{
test: /\.css/,
loaders: [
'css-loader?modules',
],
},
],
},
}

### Server-side

On the server you can get the style with the `ModuleStyle.rewind()` function.

import ReactDOM from 'react-dom';
import ModuleStyle from 'module-style';
import MyModule from './MyModule';
// ...

app.get('*', (req, res) => {
const content = ReactDOM.renderToString();
const style = ModuleStyle.rewind();

res.send(`



${style}


${content}


`);
});

## License

[ISC License](LICENSE.txt).