Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
react-theme
Advanced tools
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:
import ReactTheme from 'react-theme'
var theme = new ReactTheme()
theme.setSource('label', () => ({
color: 'red'
}))
theme.getStyle('label') // {color: red}
#####JS Bin
###Mixins The returned style object can have a old React style mixins with the name of other sources.
theme.setSource('label', () => ({
mixins: ['font'],
color: 'red'
}))
theme.setSource('font', () => ({
color: 'white',
fontFamily: 'Roboto'
}))
theme.getStyle('label') // {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.
theme.setSource('palette', () => ({
textColor: 'navajowhite'
}))
theme.setSource('label', (theme) => {
var { textColor } = theme.getStyle('palette')
return {
color: textColor,
backgroudColor: complement(textColor)
}
})
theme.getStyle('label') // {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.setSource('label', () => ({
color: 'white',
//merge in if the modifier.error === true
error: {
color: 'red'
},
kind: {
//merge in if the modifier.kind === 'dotted'
dotted: {borderStyle: 'dotted'},
dashed: {borderStyle: 'dashed'}
}
}))
var modifier = {error: true, kind: 'dotted'}
theme.getStyle('label', modifier) // {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.setSource('label', () => ({
color: 'white',
primary: {
color: 'blue'
},
hover: {
color: 'navy',
primary: {
color: 'teal'
}
}
}))
var modifier = {primary: true, hover: true}
theme.getStyle('label', modifier) // {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:
theme.setSource('label', (theme, modifier) => {
var { lineHeight } = theme.getStyle('config', modifier)
return {
//mixins are automatically resolved with the given modifier
mixins: ['font', 'roundedBorders'],
lineHeight
}
})
var style = theme.getStyle('label', {size: 'xl'})
#####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)
:
theme.setSource('label', () => ({
color: 'lime',
bordered: {
borderStyle: 'double',
resize: 'both'
}
}))
theme.extendSource('label', () => ({
bordered: {
borderStyle: 'groove'
}
}))
var modifier = {bordered: true}
theme.getStyle('label', modifier)
// {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.
import defaultTheme from './defaultTheme'
class Button extends React.Component() {
static contextTypes = {
theme: React.PropTypes.object
}
getTheme() {
return this.context.theme || defaultTheme
}
reder() {
var {label, mod, style} = this.props;
var s = this.getTheme().getStyle('button', mod, style)
return <button style={s}>{label}</button>
}
}
#####JS Bin - Bootstrap buttons example
###API
####theme.getStyle(sourceName, modifier, additionalStyleObejct)
####theme.setSource(sourceName, sourceFunction)
see above
####theme.extendSource(sourceName, sourceFunction)
[see above](#extending source)
####theme.clone()
Returns a new Theme instance whit the same style sources.
FAQs
organise your inline styles flexible and forkable
The npm package react-theme receives a total of 4 weekly downloads. As such, react-theme popularity was classified as not popular.
We found that react-theme demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.