Code Monkey home page Code Monkey logo

react-style's Introduction

React Style

Build Status Code Climate

React Style is an approach for styling React components.

Define styles using full power of JavaScript:

var ReactStyle = require('react-style')

var styles = ReactStyle({
  color: 'red',
  backgroundColor: 'white'
})

Style React components:

var React = require('react')

var HelloWorld = React.createClass({

  render() {
    var dynamicStyles = ReactStyle({color: this.props.color})
    return <div styles={[styles, dynamicStyles]}>Hello, world!</div>
  }
})

Now with these two lines you get your application styled and running:

ReactStyle.inject()
React.renderComponent(<HelloWorld color="red" />, document.body)

Styles which are found to be at the module level will be compiled to CSS classes and injected into DOM as <style> element. Dynamic styles (as dynamicStyles is defined inside render() method) will be applied to DOM as inline styles.

Extracting styles into CSS at build time

There's React Style Webpack plugin which extends Webpack with the ability to extract styles from your application at build time. The result is a bundle.css file which can be added to <head>.

Syntax helpers for writing styles

There's React Style syntax which allows you to write styles like this:

var styles = ReactStyle`
  color: red;
  background-color: white;
`

And have it transformed into:

var styles = ReactStyle({
  color: 'red',
  backgroundColor: 'white'
})

This syntax is consistent with ES6 tagged template literal

The syntax helpers are convenient when transitioning a large CSS code base to React Style. It makes it possible to directly copy paste styles from your CSS and later refactor them into a more modular form.

License

MIT

react-style's People

Contributors

andreypopp avatar ianobermiller avatar sanderspies avatar

Watchers

 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.