Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

styled-jss

cssinjs2.1kMIT2.2.3

Styled Components on top of JSS.

jss, styled, react, styled-components, primitives

readme

styled-jss

Styled Components on top of JSS

Travis branch Coverage Status branch npm version npm license

Styled-JSS implements a styled-primitives interface on top of JSS. Its API is similar to styled-components but thanks to the JSS core, it supports all features and plugins JSS does. For e.g. you can use full JSON Syntax inside.

Try it out on playground.

Default styled function

import styled from 'styled-jss'

const Button = styled('button')({
  fontSize: 12,
  color: (props) => props.theme.textColor
})

// You can also use curried interface this way.
const div = styled('div')

const Container = div({
  padding: 20
})

// Composition.
const PrimaryButton = styled(Button)({
  color: 'red'
})

// Composition with unstyled React Components too.
const Button = styled(UnstyledButton)({
  color: 'blue'
})

// Component Selectors.
const ButtonContainer = styled(Container)({
  [`& ${PrimaryButton}`]: {
    color: 'green'
  }
})

Theming

styled-jss has out of the box support for theme customization with the unified theming package.

import styled, {ThemeProvider} from 'styled-jss'

const Button = styled('button')(({margin, theme}) => ({
  margin,
  color: theme.color,
  backgroundColor: theme.backgroundColor,
}))

const themes = {
  light: {
    color: 'black',
    backgroundColor: 'yellow',
  },
}

const App = () => (
  <ThemeProvider theme={themes.light}>
    <Button margin={20}>This is themed Button</Button>
  </ThemeProvider>
)

export default App

Composable styles

Example on the CodeSandbox

You can compose your style-objects and style-functions.

Let's say this is our mods.js:

export const theme = ({ theme }) => ({
  color: theme.colors.primary,
  backgroundColor: theme.colors.secondary,
})

export const font = ({ bold }) => ({
  font: {
    weight: bold ? 'bold' : 'normal',
    family: 'Arial',
  },
})

export const size = ({ size = 'm' }) => ({
  s: {
    fontSize: 12,
    lineHeight: 1.2,
  },
  m: {
    fontSize: 16,
    lineHeight: 1.5
  }
})[size]

export const rounded = ({ rounded }) => rounded && { borderRadius: 5 }

Now we can mix them to our Button Component:

import styled from 'styled-jss'
import {theme, font, size, rounded} from 'mods'

const Button = styled('button')(
  {
    border: 0,
    padding: [5, 10],
    display: 'inline-block',
  },
  theme,
  font,
  size,
  rounded,
)

export default Button

And Usage:

import {ThemeProvider} from 'styled-jss'
import Button from './components/Button'

const theme = {
  dark: {
    colors: {
      primary: 'white',
      secondary: 'purple'
    }
  }
}

export default () => (
  <ThemeProvider theme={theme.dark}>
    <Button>normal button</Button>
    <Button bold>bold button</Button>
    <Button size="s">small button</Button>
    <Button rounded>rounded button</Button>
  </ThemeProvider>
)

Base Style Sheet

Using base Style Sheet we can reuse classes in the render function and inside of a styled component.

import { Styled, injectStyled } from 'styled-jss'

// Base styles, like a regular jss object.
const styled = Styled({
  root: {
    margin: 10,
    '& $baseButton': {
      fontSize: 16
    }
  },
  baseButton: {
    padding: 10,
    '& + &': {
      marginLeft: 10
    }
  }
})

const NormalButton = styled('button')({
  composes: '$baseButton',
  border: [1, 'solid', 'grey'],
  color: 'black'
})

// Composition - same way.
const PrimaryButton = styled(NormalButton)({
  color: 'red'
})

// One can use classes AND styled primitives.
const MyComponent = ({classes}) => (
  <div className={classes.root}>
    <NormalButton>normal button</NormalButton>
    <PrimaryButton>primary button</PrimaryButton>
  </div>
)

const MyStyledComponent = injectStyled(styled)(MyComponent)

Custom JSS setup

Styled-JSS uses jss-preset-default by default. You can require createStyled function and provide your custom JSS instance.

import { create as createJss } from 'jss'
import vendorPrefixer from 'jss-vendor-prefixer'
import createStyled from 'styled-jss/createStyled'

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

// Create a custom Styled function, that allows to set BaseStyles.
export const Styled = createStyled(jss)

// Create a custom styled function that allows to create styled components.
const styled = Styled()

export default styled

Install

npm install --save styled-jss

Install peer dependencies react and react-dom in your project.

License

MIT

changelog

Change Log

v2.2.3 (2018-05-15)

Full Changelog

Fixed bugs:

  • Bug: Nested composition causes invalid tag to be rendered #67

Merged pull requests:

v2.2.2 (2018-03-02)

Full Changelog

Merged pull requests:

  • Fix function values in composition #64 (lttb)

v2.2.1 (2018-03-02)

Full Changelog

Merged pull requests:

  • Add ThemeProvider, fix Readme #63 (lttb)

v2.2.0 (2018-03-02)

Full Changelog

Implemented enhancements:

  • Set styled object as a whole based on props #41

v2.2.0-0 (2018-03-02)

Full Changelog

Closed issues:

  • Integrate ThemeProvider #20
  • What about dynamic themes? #16

Merged pull requests:

v2.1.2 (2017-11-13)

Full Changelog

Fixed bugs:

  • Higher order component name #50

Merged pull requests:

  • Fix #50, escape Components name #58 (lttb)

v2.1.1 (2017-11-12)

Full Changelog

Closed issues:

  • Add plugins without re-exporting styled #56

Merged pull requests:

  • Resolve #56, provide jss instance via styled #57 (lttb)

v2.1.0 (2017-10-24)

Full Changelog

Fixed bugs:

  • Support Observables from JSS9 #53

Closed issues:

  • Border property #52

Merged pull requests:

v2.0.2 (2017-10-13)

Full Changelog

Fixed bugs:

  • Props not passed #47

Merged pull requests:

  • Pass props to composed React Components #48 (lttb)

v2.0.1 (2017-10-13)

Full Changelog

Merged pull requests:

  • Fix React Component classes #46 (lttb)

v2.0.0 (2017-10-13)

Full Changelog

Implemented enhancements:

  • Components selectors #40
  • Compose React Components with styled-jss #39

Closed issues:

  • React 16 compatibility #42
  • Update to the latest jss and preset #38
  • Styled-jss component not passing props down #37
  • Setting default values for prop types #36
  • SC v2 #32

Merged pull requests:

v1.2.0 (2017-06-28)

Full Changelog

Merged pull requests:

v1.1.3 (2017-06-02)

Full Changelog

Closed issues:

  • react-jss vs styled-jss recommendations #31

Merged pull requests:

v1.1.2 (2017-05-28)

Full Changelog

Fixed bugs:

  • Props don't work with component remount #26

Merged pull requests:

v1.1.1 (2017-05-28)

Full Changelog

v1.1.0 (2017-05-28)

Full Changelog

Fixed bugs:

  • Function don't work with nested rules #23
  • Get rid from dynamicStyleSheet, fix nested dynamic rules #24 (lttb)

v1.0.1 (2017-05-18)

Full Changelog

Closed issues:

  • Extend doesn't seem to work #21

v1.0.0 (2017-05-09)

Full Changelog

Implemented enhancements:

  • Currying interface #18

Merged pull requests:

  • Implement currying interface, resolve #18 #19 (lttb)

v0.6.3 (2017-04-30)

Full Changelog

Implemented enhancements:

  • Use Component instead of PureComponent for StyledElement #15 (lttb)

v0.6.2 (2017-04-30)

Full Changelog

Merged pull requests:

  • Rewrite filterProps and createStyled, fix tests #14 (lttb)

v0.6.1 (2017-04-26)

Full Changelog

Implemented enhancements:

  • babel-plugin-styled-jss #5

Merged pull requests:

v0.6.0 (2017-04-26)

Full Changelog

Implemented enhancements:

  • Code Style: express acronyms in capital space #8
  • Improve mount performance #12 (lttb)

v0.5.2 (2017-04-25)

Full Changelog

Merged pull requests:

v0.5.1 (2017-04-24)

Full Changelog

Merged pull requests:

  • Add Travis CI and update build #10 (lttb)

v0.5.0 (2017-04-24)

Full Changelog

Implemented enhancements:

  • Implement injectStyled #4
  • Implement injectStyled #6 (lttb)

v0.4.0 (2017-04-23)

Full Changelog

v0.3.1 (2017-04-22)

Full Changelog

v0.3.0 (2017-04-21)

Full Changelog

Merged pull requests:

  • Update docs, remove createStyledCreator #2 (lttb)
  • Create JSS-styled prototype #1 (lttb)

* This Change Log was automatically generated by github_changelog_generator