Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

radium

> 编程语言
Open source

A toolchain for React component styling.

7.3K stars0 likes2 views
WebsiteGitHub

About

A toolchain for React component styling.

Maintenance Status Travis Status AppVeyor Status Coverage Status NPM Package Dependency Status gzipped size

Radium

bash
yarn add radium
# or
npm install --save radium

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 JS by 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
  • API Docs
  • Frequently Asked Questions (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!

javascript
<Button kind="primary">Radium Button</Button>
…

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:

javascript
import Radium from 'radium';
import Radium, {Style} from 'radium';

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

javascript
const Radium = require('radium');
const {Style} = require('radium');

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

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

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:

javascript
resolve: {
  alias: {
    radium: require.resolve('radium/index');
  }
}

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:

bash
npm install
npm run universal

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

bash
npm install
npm run examples

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

Issues· 83 open

View all issuesOpen on GitHub
  • #1117

    Peer Dependency for React 17.

    Updated Mar 2, 2022
  • #1056

    ERESOLVE error with REACT 17.0.1

    Updated Feb 21, 2022
  • #1055

    Formik props and Radium interactive style unique key

    Updated Feb 3, 2021
  • #586

    Media queries throwing error: Unsupported style property

    Updated Jan 21, 2021
  • #907

    Link and NavLink react-router-dom components :hover not working

    bugverifiedUpdated Jan 21, 2021
  • #1049

    is it deprecated now?

    choredependenciesUpdated Sep 25, 2020
  • #1050

    Upgrade to webpack@4

    choredependenciesUpdated Sep 25, 2020
  • #1042

    TypeError: Cannot read property 'styleKeeperContext' of undefined

    Updated Jul 31, 2020
  • #912

    I have a problem about radium.keyframes

    high priorityunverifiedUpdated Jan 29, 2020
  • #754

    Issue with Keyframes and Style Component

    enhancementUpdated Jan 29, 2020

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 2, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言