UNPKG

5.14 kBMarkdownView Raw
1# React CSS components
2
3[![Join the chat at https://gitter.im/andreypopp/react-css-components](https://img.shields.io/badge/gitter-join%20chat-green.svg)](https://gitter.im/andreypopp/react-css-components)
4[![Travis build status](https://img.shields.io/travis/andreypopp/react-css-components/master.svg)](https://travis-ci.org/andreypopp/react-css-components)
5[![npm](https://img.shields.io/npm/v/react-css-components.svg)](https://www.npmjs.com/package/react-css-components)
6
7<!-- START doctoc generated TOC please keep comment here to allow auto update -->
8<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
9**Table of Contents**
10
11- [Motivation](#motivation)
12- [Installation & Usage](#installation-&-usage)
13- [Base components](#base-components)
14 - [DOM components](#dom-components)
15 - [Composite components](#composite-components)
16- [Variants](#variants)
17 - [Named variants](#named-variants)
18 - [JavaScript expressions](#javascript-expressions)
19- [Customizing CSS loading](#customizing-css-loading)
20 - [CSS extraction](#css-extraction)
21 - [Using with SASS/SCSS/LESS/Stylus/...](#using-with-sassscsslessstylus)
22 - [Using with PostCSS (including autoprefixer)](#using-with-postcss-including-autoprefixer)
23
24<!-- END doctoc generated TOC please keep comment here to allow auto update -->
25
26## Motivation
27
28Define React presentational components with CSS (or even SASS or Less if you
29like).
30
31The implementation is based on [CSS modules][]. In fact React CSS Components is
32just a thin API on top of CSS modules.
33
34**NOTE:** The current implementation is based on Webpack but everything is ready
35to be ported onto other build systems (generic API is here just not yet
36documented). Raise an issue or better submit a PR if you have some ideas.
37
38## Installation & Usage
39
40Install from npm:
41
42 % npm install react-css-components style-loader css-loader
43
44Configure in `webpack.config.js`:
45
46```js
47module.exports = {
48 ...
49 module: {
50 loaders: [
51 {
52 test: /\.react.css$/,
53 loader: 'react-css-components',
54 }
55 ]
56 }
57 ...
58}
59```
60Now you can author React components in `Styles.react.css`:
61```css
62Label {
63 color: red;
64}
65
66Label:hover {
67 color: white;
68}
69```
70
71And consume them like regular React components:
72```js
73import {Label} from './styles.react.css'
74
75<Label /> // => <div class="<autogenerated classname>">...</div>
76```
77
78## Base components
79
80### DOM components
81
82By default React CSS Components produces styled `<div />` components. You can
83change that by defining `base:` property:
84
85```css
86FancyButton {
87 base: button;
88 color: red;
89}
90```
91
92Now `<FancyButton />` renders into `<button />`:
93
94```js
95import {FancyButton} from './styles.react.css'
96
97<FancyButton /> // => <button class="<autogenerated classname>">...</button>
98```
99
100### Composite components
101
102In fact any React components which accepts `className` props can be used as a
103base. That's means that React CSS Components can be used as theming tool for any
104UI library.
105
106Example:
107
108```css
109DangerButton {
110 base: react-ui-library/components/Button;
111 color: red;
112}
113```
114
115## Variants
116
117Variants is a mechanism which allows to define styling variants for a component.
118
119### Named variants
120
121You can define additional styling variants for your components:
122
123```css
124Label {
125 color: red;
126}
127
128Label:emphasis {
129 font-weight: bold;
130}
131```
132
133They are compiled as CSS classes which then can be controlled from JS via
134`variant` prop:
135
136```js
137<Label variant={{emphasis: true}} /> // sets both classes with `color` and `font-weight`
138```
139### JavaScript expressions
140
141You can define variants which are conditionally applied if JS expression against
142props evaluates to a truthy value.
143
144Example:
145
146```css
147Label {
148 color: red;
149}
150
151Label:prop(mode == "emphasis") {
152 font-weight: bold;
153}
154```
155
156Note that any free variable reference a member of `props`, thus in JS `mode`
157becomes `props.mode` in the example above.
158
159They are compiled as CSS classes as well. JS expressions within `prop(..)` are
160used to determine if corresponding CSS classes should be applied to DOM:
161
162```js
163<Label mode="emphasis" /> // sets both classes with `color` and `font-weight`
164```
165
166## Customizing CSS loading
167
168By default React CSS components loads CSS using `style-loader!css-loader` loader
169chain. That could be configured differently using `loadCSS` loader parameter.
170
171This could be used to enable features such as *CSS extraction*, processing
172stylesheets with *PostCSS/Autoprefixer* or even authoring stylesheets with
173*SASS* or *LESS*.
174
175### CSS extraction
176
177See the [complete example](./examples/css-extraction/webpack.config.js) which
178configures
179[`extract-text-webpack-plugin`](https://github.com/webpack/extract-text-webpack-plugin)
180to extract stylesheets to a separate chunk.
181
182### Using with SASS/SCSS/LESS/Stylus/...
183
184See the [complete example](./examples/sass/webpack.config.js) which
185uses SASS/SCSS to create React components.
186
187### Using with PostCSS (including autoprefixer)
188
189See the [complete example](./examples/postcss//webpack.config.js) which
190configures PostCSS with Autoprefixer to automatically add vendor prefixes to
191stylesheets.
192
193[CSS modules]: https://github.com/css-modules/css-modules