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-css-vars

v1.2.1

Published

A simple way to theme your React application using pure CSS variables.

Downloads

2,868

Readme

react-css-vars Build Status NPM npm bundle size (minified) Codacy Badge

A simple way to theme your React application using pure CSS variables.

Example

Why

Because CSS variables are fast. React doesn't have to lift a finger when the style changes globally.

Can I use it

If you support modern browsers, you can use it!

Have a look at caniuse.com to make sure.

Does it work with styled-components/emotion/glamor/glamorous?

Yes. As long as the browser supports CSS variables, then you should be able to use var(--yourVariable) in any CSS-in-JS library.

Install

npm install react-css-vars

or

yarn add react-css-vars

Basic usage

Simply import ThemeSwitcher and use it anywhere in your application, providing it a theme map.

import React from 'react'

import ThemeSwitcher from 'react-css-vars'

const myTheme = {
  myColor: 'red',
}

class Example extends React.Component {
  render() {
    return <ThemeSwitcher theme={myTheme} />
  }
}

In your CSS you can use these variables.

.anyCssClass {
  color: var(--myColor);
}

Props

ThemeSwitcher:

| Property | Type | Default | Description | | ------------- |:-------------:|:------------: | ----------- | | theme | Object | null | required { key: value }-map of variables and their values | elementId | string | null | used to apply the variables to a different element that html. The element is only referenced on component mount, so it needs to be a static DOM-node that doesn't change (i.e. probably not a React-element). Common use-case is the #root node that most React-apps renders into.

Careful with multiple instances

This project writes the CSS variables directly to the html element (see the "How does it work" section for more details). So if you have two or more ThemeSwitcher in your application they might work against eachother.

However, you may have it anywhere in your application, it does not need to wrap your application like typical Provider-pattern type solutions do. You can use ThemeSwitcher without providing any children as well.

Switching theme

To dynamically switch themes, simply provide a different theme as the theme-prop.

import React from 'react'

import ThemeSwitcher from 'react-css-vars'

const myFirstTheme = {
  myColor: 'darkslategray',
}

const coolTheme = {
  myColor: 'yellow',
}

export default class App extends Component {
  state = {
    basicTheme: true,
  }

  handleToggleTheme = () => {
    this.setState(prevState => ({ basicTheme: !prevState.basicTheme }))
  }

  render() {
    const { basicTheme } = this.state

    return (
      <ThemeSwitcher theme={basicTheme ? basicTheme : coolTheme}>
        <div>
          <div>Example</div>
          <button onClick={this.handleToggleTheme}>Change theme!</button>
        </div>
      </ThemeSwitcher>
    )
  }
}

This is just a basic example on how to switch themes. As long as the theme prop changes, the theme is going to be applied.

Overwriting CSS variables

If you would like to have your base theme in your CSS file, and only overwrite when the theme changes this is very simple.

In your CSS, provide your variables on the :root-pseudo-class

:root {
  --myColor: palevioletred;
}

Then your switcher passes in null to the ThemeSwitcher when no theme is selected.

import React from 'react'

import ThemeSwitcher from 'react-css-vars'

// Only one theme here, the base theme is declared in the CSS-file.
const coolTheme = {
  myColor: 'yellow',
}

export default class App extends Component {
  state = {
    basicTheme: true,
  }

  handleToggleTheme = () => {
    this.setState(prevState => ({ basicTheme: !prevState.basicTheme }))
  }

  render() {
    const { basicTheme } = this.state

    return (
      <ThemeSwitcher theme={basicTheme ? null : coolTheme}>
        <div>
          <div>Example</div>
          <button onClick={this.handleToggleTheme}>Change theme!</button>
        </div>
      </ThemeSwitcher>
    )
  }
}

For a full working example look at the example project.

How does it work

It's very simple! CSS variables are simply CSS properties that are assigned to a specific element. You can assign them to a psuedo-element known as :root like this:

:root {
  --myVariable: palevioletred;
}

This library simply takes a map of values that look like this:

const theme = {
  myVariable: 'red',
}

And simply sets them as CSS properties on the html element. Since this has higher precedence than the :root psuedo-element the browser will always select the programatically set values over the ones set on :root.

When the theme-prop changes, this library unsets the theme values that were previously set. If the theme-prop is set to null it will unset the previous values and not set any new ones. Any values on :root will then have the highest precedence again.

License

MIT © karl-run