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

v0.3.0

Published

Switch between your themes easily, uses react hooks. Just provide the colors object for themes and switch it. switch between light or dark theme, or so.

Downloads

36

Readme

npm NPM npm bundle size

react-switch-theme

Switch between your themes, (i.e. dark/light or so) with just one click. Uses react hooks. react-switch-theme on npm

Live demo: codesandbox

Install:

  npm i react-switch-theme

Updated usage:

This does not break anything, old usage is still valid.

  1. Import ThemeProvider and Theme. Theme is the Context which will be used with useContext to get theme and setTheme.
    import { ThemeProvider, Theme } from "react-switch-theme";
  2. Wrap your app with ThemeProvider. This takes just one children, It accepts three props.
  const colors = {
    light: {
      background: "#fff",
      color: "#000"
    },
    dark: {
      background: "#000",
      color: "#fff"
    }
  };
  const activeMode = "light";
  const offlineStorageKey = "react-random-key";

  // wrap your app
    <ThemeProvider
      colors={colors}
      activeMode={activeMode}
      offlineStorageKey={offlineStorageKey}
    >
      <App />
    </ThemeProvider>
  1. Now in app you can access theme by using:
  const [theme, toogleTheme] = useContext(Theme);
theme is current theme: string, toogleTheme is a function which changes your current theme.
  1. Access your css var from css-in-js or css:
  // You did the hardwork now just use your vars.
  background: var(--background);
  color: var(--color);

Old Usage:

Demo: CodeSandbox

  1. Import ( default import ):

    import useReactSwitchTheme from 'react-switch-theme'; 
  2. Pass one option to the hook

    const options = {
      colors: {
           
        // Object of two color objects. light and dark or whatever you call them.
        light: {
    
        // These properties will be converted to css vars
        // These can be named anything
        // You can access them in css, css-in-js or in other way by using 'var(--propName)'
        background: '#fff',
        color: '#000',
        },
        dark: {
    
        // Property name must be same for both objects
    
        background: '#000',
        color: '#fff',
        },
      },
           
      // This is the current active theme
      // It must be one of the key of colors object
      activeMode: 'light',
           
      // An unique key for your app to store themeMode in localStorage
      // It should be same for every load.
      // So, choosen theme stays even after a refresh
      offlineStorageKey: 'replace it with you app name or some hash'
           
    }
  3. Use it in your app:

      const [currentMode, setDiffMode] = useReactSwitchTheme(options)
           
      // currentMode is a string with the currentTheme as value
      // light or dark here.
           
      // setDiffMode is function which will toggle between two themes on call.
  4. Your css:

      // You did the hardwork now just use your vars.
      background: var(--background);
      color: var(--color);