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

v1.1.4

Published

A simple theme management library for React applications, allowing developers to manage themes using React Context API. It supports saving themes to local storage, detecting system preferences, and applying themes to the root html element.

Downloads

76

Readme

React Easy Theme

A simple theme management library for React applications, allowing developers to manage themes using React Context API. It supports saving themes to local storage, detecting system preferences, and applying themes to the root html element.

Installation:

Install the package via npm or yarn:

npm install react-easy-theme

or

yarn add react-easy-theme

Usage:

To use react-easy-theme, wrap your application with the ThemeProvider component and access the current theme or theme-switching functionality through the useTheme hook.

Example:

Wrap your app in <ThemeProvider>:

import React from 'react';
import { ThemeProvider } from 'react-easy-theme';

const App = () => { 
    return (
        <ThemeProvider themes={['light', 'dark', 'dark-blue']}> 
            <Component /> 
        </ThemeProvider> 
    ); 
};

export default App;

Create themes in your main css file (Note: this file should not be a module):

.light {
    --bg-color: #ffffff;
    --text-color: #000000;
}

.dark {
    --bg-color: #000000;
    --text-color: #ffffff;
}

.dark-blue {
    --bg-color: #0a1229;
    --text-color: #d8dce8;
}

/* Example of usage variables, you can use them in any css element */
body {
    background: var(--bg-color);
    color: var(--text-color);
}

Usage of useTheme():

import React from 'react';
import { useTheme } from 'react-easy-theme';

const Component = () => {
    const { currentTheme, changeTheme, getThemes } = useTheme();
    return (
        <div>
            <h1>Current Theme: {currentTheme}</h1>
            <button onClick={() => changeTheme('dark')}>Switch to Dark</button>
            <button onClick={() => changeTheme('light')}>Switch to Light</button>
            <button onClick={() => changeTheme('dark-blue')}>Switch to Dark Blue</button>
            <p>Available themes: {getThemes().join(', ')}</p>
        </div>
    );
};

export default App;

Props for <ThemeProvider>:

initialTheme (optional): The initial theme to use if no theme is saved in local storage. Defaults to light. detectPreferences (optional): If set to true, the system's preferred theme (dark/light) will be detected and used initially. Defaults to true. localStorageKey (optional): The key used to save the theme to local storage. Defaults to app-theme. themes (optional): An array of valid theme names. Defaults to.

Example:

<ThemeProvider
    localStorageKey="app-theme" // You can set key of the local storage item
    initialTheme="light" // You may use it if detectPreferences is set to false
    detectPreferences={true} // Detects user preference, and after the first load - sets the theme (light or dark)
    themes={['light', 'dark']} // possible themes (you may add additional ones), you wont be able to change the theme if its not statet here
>
    <Component />
</ThemeProvider>

Values of useTheme():

const { 
    currentTheme, // react state, the current theme
    changeTheme, // function that takes new theme as a string 
    getThemes // returns an array of all possible themes
} = useTheme();