Radium

A toolchain for React component styling.

README

[![Maintenance Status][maintenance-image]](#maintenance-status) [![Travis Status][trav_img]][trav_site] [![AppVeyor Status][appveyor_img]][appveyor_site] [![Coverage Status][cov_img]][cov_site] [![NPM Package][npm_img]][npm_site] [![Dependency Status][david_img]][david_site]
![gzipped size][size_img]

Radium


  1. ```sh
  2. yarn add radium
  3. # or
  4. npm install --save radium
  5. ```

Radium is a set of tools to manage inline styles on React elements. It gives you powerful styling capabilities without CSS.

_Inspired by_ React: CSS in JSby vjeux.

Maintenance Status


Archived: This project is no longer maintained by Formidable. We are no longer responding to issues or pull requests unless they relate to security concerns. We encourage interested developers to fork this project and make it their own!

Overview


Eliminating CSS in favor of inline styles that are computed on the fly is a powerful approach, providing a number of benefits over traditional CSS:

- Scoped styles without selectors
- Avoids specificity conflicts
- Source order independence
- Dead code elimination
- Highly expressive

Despite 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.

When 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.

For a short technical explanation, see How does Radium work?.

Features


- Conceptually simple extension of normal inline styles
- Browser state styles to support :hover, :focus, and :active
- Media queries
- Automatic vendor prefixing
- Keyframes animation helper
- ES6 class and createClass support

Docs


- [Overview][docs_guides]
- [API Docs][docs_api]
- [Frequently Asked Questions (FAQ)][docs_faq]

Usage


Start by wrapping your component class with Radium(), like export default 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!

  1. ``` js
  2. <Button kind="primary">Radium Button</Button>
  3. ```

  1. ``` js
  2. import Radium from 'radium';
  3. import React from 'react';
  4. import color from 'color';

  5. class Button extends React.Component {
  6.   static propTypes = {
  7.     kind: PropTypes.oneOf(['primary', 'warning']).isRequired
  8.   };

  9.   render() {
  10.     // Radium extends the style attribute to accept an array. It will merge
  11.     // the styles in order. We use this feature here to apply the primary
  12.     // or warning styles depending on the value of the `kind` prop. Since its
  13.     // all just JavaScript, you can use whatever logic you want to decide which
  14.     // styles are applied (props, state, context, etc).
  15.     return (
  16.       <button style={[styles.base, styles[this.props.kind]]}>
  17.         {this.props.children}
  18.       </button>
  19.     );
  20.   }
  21. }

  22. Button = Radium(Button);

  23. // You can create your style objects dynamically or share them for
  24. // every instance of the component.
  25. var styles = {
  26.   base: {
  27.     color: '#fff',

  28.     // Adding interactive state couldn't be easier! Add a special key to your
  29.     // style object (:hover, :focus, :active, or @media) with the additional rules.
  30.     ':hover': {
  31.       background: color('#0074d9')
  32.         .lighten(0.2)
  33.         .hexString()
  34.     }
  35.   },

  36.   primary: {
  37.     background: '#0074D9'
  38.   },

  39.   warning: {
  40.     background: '#FF4136'
  41.   }
  42. };
  43. ```

Importing Radium


As of v0.22.x, Radium is built as an ECMAScript Modules-first project. We now have a package.json:module entry pointing to our library files with import|export statements instead of CommonJS requires. We still support CommonJS requires with a special package.json:main entry pointing to root index.js to smooth over this transition. The basic takeaways are:

If you are using ESM with webpack or @std/esm with Node.js, imports like the following work fine without any gotchas:

  1. ``` js
  2. import Radium from 'radium';
  3. import Radium, {Style} from 'radium';
  4. ```

If you are using CommonJS with Node.js or webpack@1 requires work like normal:

  1. ``` js
  2. const Radium = require('radium');
  3. const {Style} = require('radium');
  4. ```

If you are using CommonJS with webpack@2+, however, you must instead add .default to the root Radium object import:

  1. ``` js
  2. const Radium = require('radium').default; // CHANGED: Must add `.default`
  3. const {Style} = require('radium'); // Works as per normal
  4. ```

If you cannot change the require statements directly (say Radium is included from a different library your project depends on) you can manually tweak the Radium import in your project's webpack configuration with the following:

  1. ``` js
  2. resolve: {
  3.   alias: {
  4.     radium: require.resolve('radium/index');
  5.   }
  6. }
  7. ```

which will allow const Radium = require('radium'); to still work. The configuration effectively forces webpack to point to code from package.json:main (which points to /index.js) instead of what is in package.json:module.

_Note:_ Radium uses Reflect which is not supported in IE11. You will need to bring in a polyfill like CoreJs in order to support <IE11.

Examples


To see the universal examples:

  1. ```
  2. npm install
  3. npm run universal
  4. ```

To see local client-side only examples in action, do this:

  1. ```
  2. npm install
  3. npm run examples
  4. ```

How does Radium work?


Following is a short technical explanation of Radium's inner workings:

- Wrap the render function
- Recurse into the result of the original render
- For each element:
  - Add handlers to props if interactive styles are specified, e.g. onMouseEnter for :hover, wrapping existing handlers if necessary
  - 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
  - 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

More with Radium


You can find a list of other tools, components, and frameworks to help you build with Radium on our wiki. Contributions welcome!

Contributing


Please see CONTRIBUTING

[trav_img]: https://api.travis-ci.com/FormidableLabs/radium.svg
[trav_site]: https://travis-ci.com/FormidableLabs/radium
[cov_img]: https://img.shields.io/coveralls/FormidableLabs/radium.svg
[cov_site]: https://coveralls.io/r/FormidableLabs/radium
[npm_img]: https://img.shields.io/npm/v/radium.svg
[npm_site]: https://www.npmjs.org/package/radium
[david_img]: https://img.shields.io/david/FormidableLabs/radium.svg
[david_site]: https://david-dm.org/FormidableLabs/radium
[size_img]: https://badges.herokuapp.com/size/npm/radium/dist/radium.min.js?gzip=true&label=gzipped
[docs_guides]: https://github.com/FormidableLabs/radium/tree/master/docs/guides
[docs_api]: https://github.com/FormidableLabs/radium/tree/master/docs/api
[docs_faq]: https://github.com/FormidableLabs/radium/tree/master/docs/faq
[appveyor_img]: https://ci.appveyor.com/api/projects/status/github/formidablelabs/radium?branch=master&svg=true
[appveyor_site]: https://ci.appveyor.com/project/ryan-roemer/radium
[maintenance-image]: https://img.shields.io/badge/maintenance-archived-red.svg