Giter Club home page Giter Club logo

react-switch-theme's Introduction

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:

  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);

react-switch-theme's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.