npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-theme

v0.1.4

Published

organise your inline styles flexible and forkable

Downloads

459

Readme

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

#####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)

  • 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.