Giter VIP home page Giter VIP logo

react-jss's Introduction

React integration of JSS

Gitter

There is a number of benefits when using react-jss instead of JSS directly:

  • Lazy evaluation - sheet is created only when component will mount.
  • Auto attach/detach - sheet will be rendered to the dom when component is about to mount and will be removed when no element needs it.
  • A sheet gets shared between all elements.
  • You want to use it with React Hot Loader.

Also you may need this module if you build a big application where leaving all styles in the DOM or compiling all styles at once may have a performance overhead or you are going to hit IE limits.

Usage

You can use it as a higher-order component to inject JSS. It can act both as a simple wrapping function and as a ES7 decorator.

React JSS wraps your React component and injects props.sheet, which is just a regular JSS style sheet, as a prop into your component. This is a common pattern that is used for composition in React instead of mixins, and works equally well with old-style createClass classes, as well as the ES6 classes.

Because JSS class names are namespaced by default, you will need to reach into this.props.sheet.classes to get their real names. For example, if you define a button class in your JSS stylesheet, its real name will be available as props.sheet.classes.button.

By default react-jss comes with jss and presets.

import React from 'react'
import injectSheet from 'react-jss'

const styles = {
  button: {
    backgroundColor: 'yellow'
  },
  label: {
    fontWeight: 'bold'
  }
}

const Button = ({sheet: {classes}, children}) => (
  <button className={classes.button}>
    <span className={classes.label}>
      {children}
    </span>
  </button>
)

export default injectSheet(styles)(Button)

Custom setup.

If you want to specify a jss version and plugins to use, you should create your own jss instance, setup plugins and create a injectSheet function which has your jss version bound.

import {create as createJss} from 'jss'
import {create as createInjectSheet} from 'react-jss'
import vendorPrefixer from 'jss-vendor-prefixer'

const jss = createJss()
jss.use(vendorPrefixer())

export const injectSheet = createInjectSheet(jss)

You can also access the Jss instance being used by default.

import {jss} from 'react-jss'

Using decorators.

You can use ES7 with decorators (using babel-plugin-transform-decorators-legacy).

import React, {Component} from 'react'
import injectSheet from 'react-jss'

const styles = {
  button: {
    backgroundColor: 'yellow'
  },
  label: {
    fontWeight: 'bold'
  }
}

@injectSheet(styles)
export default class Button extends Component {
  render() {
    const {sheet: {classes}, children} = this.props
    return (
      <button className={classes.button}>
        <span className={classes.label}>
          {children}
        </span>
      </button>
    )
  }
}

Using classNames helper.

You can use classNames together with JSS same way you do it with global CSS.

import classNames from 'classnames'

const Component = ({sheet: {classes}, children, isActive}) => (
  <div
    className={classNames({
      [classes.normal]: true,
      [classes.active]: isActive
    })}>
    {children}
  </div>
)

Using SheetsRegistryProvider for server-side rendering

import {renderToString} from 'react-dom/server'
import {SheetsRegistryProvider, SheetsRegistry} from 'react-jss'
import MyApp from './MyApp'

export default function render(req, res) {
  const sheets = new SheetsRegistry()

  const body = renderToString(
    <SheetsRegistryProvider registry={sheets}>
      <MyApp />
    </SheetsRegistryProvider>
  )

  // any instances of `injectStyle` within `<MyApp />` will have gotten `sheets`
  // from `context` and added their style sheets to it by now.

  return res.send(renderToString(
    <html>
      <head>
        <style type="text/css">
          {sheets.toString()}
        </style>
      </head>
      <body>
        {body}
      </body>
    </html>
  ))
}

Installation.

npm install --save react-jss

Contributing

See our contribution guidelines.

License

MIT

react-jss's People

Contributors

kof avatar gaearon avatar avocadowastaken avatar jedwards1211 avatar sapegin avatar isaiahdw avatar cvle avatar adriantoine avatar alexkuz avatar cheapsteak avatar typical000 avatar dlwalsh avatar nathanmarks avatar

Watchers

James Cloos 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.