Simple inline style manager for the organized and customizable css styles in React. If you know material-ui it's similar to the ThemeManager but more general.
Here is an example about managing a set of styles with react-theme and Radium.
It isn't handle pseudo selectors, prefixing, media queries, or convert to CSS but works well with other libraries who does.
Basic usage
A theme has a list of style sources. Every source is a function which returns an object:
var theme = theme theme // {color: red}
JS Bin
Mixins
The returned style object can have a old React style mixins with the name of other sources.
themetheme theme // {color: red, fontFamily: 'Roboto'}
JS Bin
Doing logic in style source
The first argument of the style source is the theme so you can .getStyle()
other styles in it.
themetheme theme // {color: 'navajowhite', backgroudColor: ?}
JS Bin
You can manage (and later customize) your other configs and variables (like colors, spacing, transitions, etc.) it the same way as the other styles!
Using modifiers
If you used that, it's similar to the old Radium modifiers:
theme var modifier = error: true kind: 'dotted'theme // {color: 'red', borderStyle: 'dotted'}
JS Bin
You can add some optional part to your style as objects and activate them with the values of the modifier object. Nested parts will be also resolved:
theme var modifier = primary: true hover: truetheme // {color: 'teal'}
JS Bin
Modifiers is passed as the second argument to the style source so you you can use it to get other styles with the same modifier:
themevar style = theme
JS Bin
Extending source
theme.setSource(name, source)
simply replaces the old source if the theme has one with the same name. If you want to keep the original source and extend with an other one you can use theme.extendSource(name, source)
:
themetheme var modifier = bordered: truetheme// {color: 'lime', borderStyle: 'groove', resize: 'both'}
JS Bin
Theme calls both source function and merges them.
Button example
Maybe the best way to provide the theme is to have a default theme that the user can clone, customize and put the custom theme into the context so the component can easily check it there is a custom style to use instead of the default.
static contextTypes = theme: ReactPropTypesobject { return thiscontexttheme || defaultTheme } { var label mod style = thisprops; var s = this return <button style=s>label</button> }
JS Bin - Bootstrap buttons example
API
theme.getStyle(sourceName, modifier, additionalStyleObejct)
- sourceName see above
- modifier see above
- additionalStyleObejct: This object will be merged with the resolved style object. It's usefull to merge the built in styles with the user dfined props.style.
theme.setSource(sourceName, sourceFunction)
theme.extendSource(sourceName, sourceFunction)
theme.clone()
Returns a new Theme instance whit the same style sources.