This package has been deprecated

Author message:

This was really fun! I enjoyed reverse-engineering Among Us and learning a lot of new things. Right now I don't play the game anymore and I don't find it fun to work on this anymore. Luckily, it isn't yet developed enough that anyone depends on it.

styled-jsx-theming
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Styled-JSX Theming Library

An opinionated theming library for your Styled-JSX projects to develop stylesheets faster.

This can be easily used for other React styling libraries as well, but it was designed for Styled-JSX which I primarily use.


Getting Started

First, install the package:

npm i styled-jsx-theming

Alternatively, if you use Yarn:

yarn add styled-jsx-theming

Next, wrap your React app with ThemingProvider:

Next.js example

pages/_app.js

import App from 'next/app'
import ThemingProvider from 'styled-jsx-theming'
 
export default class extends App {
  render() {
    const { Component, pageProps } = this.props
    return (
      <ThemingProvider theme={{
        background: '#000000',
        color: '#ffffff'
      }}>
        <Component {...pageProps} />
      </ThemingProvider/>
    )
  }
}
Gatsby example

gatsby-browser.js

import React from 'react'
import ThemingProvider from 'styled-jsx-theming'
 
export const wrapRootElement = ({ element }) => {
  return (
    <ThemingProvider theme={{
      background: '#000000',
      color: '#ffffff'
    }}>
      {element}
    </ThemingProvider>
  );
}
Generic example
import React, { Component } from 'react'
import ThemingProvider from 'styled-jsx-theming'
// Import `App` or something
 
export default class extends Component {
  render() {
    return (
      <ThemingProvider theme={{
        background: '#000000',
        color: '#ffffff'
      }}>
        <App />
      </ThemingProvider/>
    )
  }
}

Using the Theme

Of course, the next step is actually using the theme in a component. There are two methods to this.

useTheme React Hook

import { useTheme } from 'styled-jsx-theming'
 
export default (props) => {
  const [ theme ] = useTheme()
 
  <div>
    {props.children}
 
    <style jsx>{`
      div {
        background: ${theme.background};
        color: ${theme.color};
      }
    `}</style>
  </div>
}

withTheme HOC

import { Component } from 'react'
import { withTheme } from 'styled-jsx-theming'
 
export withTheme(class extends Component {
  render() {
    <div>
      {this.props.children}
 
      <style jsx>{`
        div {
          background: ${this.props.theme.background};
          color: ${this.props.theme.color};
        }
      `}</style>
    </div>
  }
})

Multiple Themes

So far all the examples have involved just one theme that can be accessed, but sometimes you'll want to be able to switch between multiple.

First, modify the theming provider:

<ThemingProvider
  themes={{
    dark: {
      background: '#000000',
      color: '#ffffff'
    },
    light: {
      background: '#ffffff',
      color: '#000000'
    }
  }}
  default='dark'
>
  <App />
</ThemingProvider>

As you can see, we replaced the theme property with themes, that specifies multiple themes and their names, as well as default that chooses the defaultly selected theme.

To set the theme with the useTheme hook:

const [ theme, setTheme ] = useTheme()
 
setTheme('light')

Or if you're using the HOC:

this.props.setTheme('light')

Development

Styled-JSX Theming Library is open to contributions! I'll review pull requests anyone makes, so feel free to submit one.

Run yarn to install all dependencies.

I've written this library with TypeScript, so run yarn dev to automatically run a build when you change a file.

Package Sidebar

Install

npm i styled-jsx-theming

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

5.81 kB

Total Files

5

Last publish

Collaborators

  • archmaster