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-js2css-theme

v2.0.0

Published

Converts JS theme object into CSS Variables on the fly to be consumed by your components

Downloads

4

Readme

React CSS Theming (with JS objects)

This tiny 500byte (not gzipped) component allows JS theme objects to be passed into your component where they're converted into CSS Variables for internal use.

https://codesandbox.io/s/react-js2css-theme-demo-s6xlx

  • Switching CSS Variables for themes is unpleasant for users of your component, JS objects are easier to deal with.

  • CSS Variable changes are much faster than React re-renders via Context which is what CSS-in-JS solutions do.

  • If you care about performance and bundle size and want to avoid CSS-in-JS solutions altogether you can still provide a nice JS theme object interface to your users.

Install

yarn add react-js2css-theme
const yourTheme = {
  background: 'black',
  textColor: 'white',
  fontFamily: '"Roboto", sans-serif',
  fontWeight: 500,
  button: {
    padding: '10px',
  },
};
import JSToCSSTheme from 'react-js2css-theme';

<JSToCSSTheme namespace="xx" theme={yourTheme}>
  <YourComponent />
</JSToCSSTheme>;

You may not wish to create an extra wrapping element, in this case you can use the hook:

import { useJsToCss } from 'react-js2css-theme';

function YourComponent() {
  const themeStyle = useJsToCss('xx', yourTheme);

  return (
    <div>
      <style>{themeStyle}</style>
      {/* ... your component JSX */}
    </div>
  );
}

Your component can now make use of the following CSS Variables:

:root {
  --xx-background: black;
  --xx-textColor: white;
  --xx-fontFamily: 'Roboto', sans-serif;
  --xx-fontWeight: 500;
  --xx-buttonPadding: 10px;
}

Now try changing the theme object and watch your component's theme change :)

  • All browsers except IE