Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

15 Commits

Repository files navigation

Build StatusCoveragenpm

react-themeable

Utility for making React components easily themeable.

This project is still experimental, so feedback from component authors would be greatly appreciated!

Why?

The React community is highly fragmented when it comes to styling. How do we write components that can happily co-exist with all of these competing approaches?

With react-themeable, you can support custom themes provided by CSS Modules, Radium, Aphrodite, React Style, JSS, global CSS or inline styles as easily as this:

<MyComponenttheme={theme}/>

Install

npm install --save react-themeable

Usage

react-themeable exposes just a single function.

This function is designed to accept a theme prop inside of your render method. This then returns a small helper function that accepts a key and a series of style names.

consttheme=themeable(this.props.theme);
...
<div{...theme(key, ...styleNames)}/>

Note: A unique key for each themed element is required for Radium to work correctly.

This helper function detects whether a theme is class or style based, and creates the appropriate attributes for you.

For example:

importReact,{Component}from'react';importthemeablefrom'react-themeable';classMyComponentextendsComponent{render(){consttheme=themeable(this.props.theme);return(<div{...theme(1,'root')}><div{...theme(2,'foo','bar')}>Foo Bar</div><div{...theme(3,'baz')}>Baz</div></div>);}}

A theme can now be passed to your component like so:

CSS Modules

.foo { color: red; }
.foo:hover { color: green; }
.bar { color: blue; }
importthemefrom'./MyComponentTheme.css';
...
<MyComponenttheme={theme}/>

Radium

importRadiumfrom'radium';consttheme={foo: {color: 'red',':hover': {color: 'green'}},bar: {color: 'blue'}};constThemedMyComponent=Radium(MyComponent);
...
<ThemedMyComponenttheme={theme}/>

Aphrodite

import{StyleSheet,css}from'aphrodite';consttheme=StyleSheet.create({foo: {color: 'red',':hover': {color: 'green'}},bar: {color: 'blue'}});
...
<MyComponenttheme={[theme,css]}/>

React Style

importStyleSheetfrom'react-style';consttheme=StyleSheet.create({foo: {color: 'red'},bar: {color: 'blue'}});
...
<MyComponenttheme={theme}/>

JSS

importjssfrom'jss';constsheet=jss.createStyleSheet({foo: {color: 'red'},bar: {color: 'blue'}}).attach();
...
<MyComponenttheme={sheet.classes}/>

Global CSS

.MyComponent__foo { color: red; }
.MyComponent__foo:hover { color: green; }
.MyComponent__bar { color: blue; }
consttheme={foo: 'MyComponent__foo',bar: 'MyComponent__bar'};
...
<MyComponenttheme={theme}/>

Inline styles

consttheme={foo: {color: 'red'},bar: {color: 'blue'}};
...
<MyComponenttheme={theme}/>

License

MIT

About

Utility for making React components easily themeable

Resources

Stars

545 stars

Watchers

9 watching

Forks

Releases

Packages

Used by

Contributors

Languages