UNPKG

6.02 kBMarkdownView Raw
1[![Travis Status][trav_img]][trav_site]
2[![AppVeyor Status][appveyor_img]][appveyor_site]
3[![Coverage Status][cov_img]][cov_site]
4[![NPM Package][npm_img]][npm_site]
5[![Dependency Status][david_img]][david_site]
6![gzipped size][size_img]
7
8# Radium
9
10```
11npm install radium
12```
13
14Radium is a set of tools to manage inline styles on React elements. It gives you powerful styling capabilities without CSS.
15
16_Inspired by_ <a href="https://speakerdeck.com/vjeux/react-css-in-js">React: CSS in JS</a>
17by <a href="https://twitter.com/Vjeux">vjeux</a>.
18
19## Overview
20
21Eliminating CSS in favor of inline styles that are computed on the fly is a powerful approach, providing a number of benefits over traditional CSS:
22
23- Scoped styles without selectors
24- Avoids specificity conflicts
25- Source order independence
26- Dead code elimination
27- Highly expressive
28
29Despite that, there are some common CSS features and techniques that inline styles don't easily accommodate: media queries, browser states (:hover, :focus, :active) and modifiers (no more .btn-primary!). Radium offers a standard interface and abstractions for dealing with these problems.
30
31When we say expressive, we mean it: math, concatenation, regex, conditionals, functions–JavaScript is at your disposal. Modern web applications demand that the display changes when data changes, and Radium is here to help.
32
33For a short technical explanation, see [How does Radium work?](#how-does-radium-work).
34
35Convinced about CSS in JS with React, but not Radium? Check out our comprehensive [comparison of 14+ alternatives][docs_comparison].
36
37## Features
38
39* Conceptually simple extension of normal inline styles
40* Browser state styles to support `:hover`, `:focus`, and `:active`
41* Media queries
42* Automatic vendor prefixing
43* Keyframes animation helper
44* ES6 class and `createClass` support
45
46## Docs
47
48- [Overview][docs_guides]
49- [API Docs][docs_api]
50- [Frequently Asked Questions (FAQ)][docs_faq]
51
52## Usage
53
54Start by adding the `@Radium` decorator to your component class. Alternatively, wrap `Radium()` around your component, like `module.exports = Radium(Component)`, or `Component = Radium(Component)`, which works with classes, `createClass`, and stateless components (functions that take props and return a ReactElement). Then, write a style object as you normally would with inline styles, and add in styles for interactive states and media queries. Pass the style object to your component via `style={...}` and let Radium do the rest!
55
56```jsx
57<Button kind="primary">Radium Button</Button>
58```
59
60```jsx
61var Radium = require('radium');
62var React = require('react');
63var color = require('color');
64
65@Radium
66class Button extends React.Component {
67 static propTypes = {
68 kind: PropTypes.oneOf(['primary', 'warning']).isRequired
69 };
70
71 render() {
72 // Radium extends the style attribute to accept an array. It will merge
73 // the styles in order. We use this feature here to apply the primary
74 // or warning styles depending on the value of the `kind` prop. Since its
75 // all just JavaScript, you can use whatever logic you want to decide which
76 // styles are applied (props, state, context, etc).
77 return (
78 <button
79 style={[
80 styles.base,
81 styles[this.props.kind]
82 ]}>
83 {this.props.children}
84 </button>
85 );
86 }
87}
88
89// You can create your style objects dynamically or share them for
90// every instance of the component.
91var styles = {
92 base: {
93 color: '#fff',
94
95 // Adding interactive state couldn't be easier! Add a special key to your
96 // style object (:hover, :focus, :active, or @media) with the additional rules.
97 ':hover': {
98 background: color('#0074d9').lighten(0.2).hexString()
99 }
100 },
101
102 primary: {
103 background: '#0074D9'
104 },
105
106 warning: {
107 background: '#FF4136'
108 }
109};
110```
111
112## Examples
113
114To see the universal examples:
115
116```
117npm install
118npm run universal
119```
120
121To see local client-side only examples in action, do this:
122
123```
124npm install
125npm run examples
126```
127
128## How does Radium work?
129
130Following is a short technical explanation of Radium's inner workings:
131
132- Wrap the `render` function
133- Recurse into the result of the original `render`
134- For each element:
135 - Add handlers to props if interactive styles are specified, e.g. `onMouseEnter` for `:hover`, wrapping existing handlers if necessary
136 - If any of the handlers are triggered, e.g. by hovering, Radium calls `setState` to update a Radium-specific field on the components state object
137 - On re-render, resolve any interactive styles that apply, e.g. `:hover`, by looking up the element's key or ref in the Radium-specific state
138
139## More with Radium
140
141You can find a list of other tools, components, and frameworks to help you build with Radium on our [wiki](https://github.com/FormidableLabs/radium/wiki). Contributions welcome!
142
143## Contributing
144
145Please see [CONTRIBUTING](https://github.com/FormidableLabs/radium/blob/master/CONTRIBUTING.md)
146
147[trav_img]: https://api.travis-ci.org/FormidableLabs/radium.svg
148[trav_site]: https://travis-ci.org/FormidableLabs/radium
149[cov_img]: https://img.shields.io/coveralls/FormidableLabs/radium.svg
150[cov_site]: https://coveralls.io/r/FormidableLabs/radium
151[npm_img]: https://img.shields.io/npm/v/radium.svg
152[npm_site]: https://www.npmjs.org/package/radium
153[david_img]: https://img.shields.io/david/FormidableLabs/radium.svg
154[david_site]: https://david-dm.org/FormidableLabs/radium
155[size_img]: https://badges.herokuapp.com/size/npm/radium/dist/radium.min.js?gzip=true&label=gzipped
156[docs_comparison]: https://github.com/FormidableLabs/radium/tree/master/docs/comparison
157[docs_guides]: https://github.com/FormidableLabs/radium/tree/master/docs/guides
158[docs_api]: https://github.com/FormidableLabs/radium/tree/master/docs/api
159[docs_faq]: https://github.com/FormidableLabs/radium/tree/master/docs/faq
160[appveyor_img]: https://ci.appveyor.com/api/projects/status/github/formidablelabs/radium?branch=master&svg=true
161[appveyor_site]: https://ci.appveyor.com/project/ryan-roemer/radium