Git Product home page Git Product logo

react-theme's Introduction

react-theme Build Status Coverage Status npm

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}

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'}

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: ?}

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'}

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'}

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'})

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)

  • 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) see above

####theme.extendSource(sourceName, sourceFunction) [see above](#extending source)

####theme.clone() Returns a new Theme instance whit the same style sources.

react-theme's People

Contributors

azazdeaz avatar bryant1410 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.