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

toolazy-react-theme

v1.0.28

Published

Theme manager for React and React Native projects.

Downloads

55

Readme

toolazy-react-theme

Theme manager for React and React Native projects. Make sure you are using React version 16.8.0 / React Native version 0.59 or higher.

Quick start

We expect you having an existing react application - if not give create-react-app / react-native init a try. I will show you step-by-step to use this library.

First: Installation

toolazy-react-theme is delivered primarily via npm . (https://www.npmjs.com/package/toolazy-react-theme)

# using npm install
$ npm i toolazy-react-theme

Second: Configure toolazy-react-theme

Create a new file theme.js beside your index.js containing following content:

import {toolazyReactTheme} from 'toolazy-react-theme';

const DarkTheme = {
  tabBarActive: '#FFF',
  tabBarInactive: 'green',
};

const LightTheme = {
  tabBarActive: 'red',
  tabBarInactive: 'rgba(0,0,0,.27)',
};

toolazyReactTheme.init({
  themesResource: {
    dark: DarkTheme,
    light: LightTheme,
  },
  currentTheme: 'dark',
});

export default toolazyReactTheme;

Then import that in index.js:

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import './theme'; // I'm here.

AppRegistry.registerComponent(appName, () => App);

Everything is ready, but how to use it? Let's go...

Third: Usage example

The components you need to use theme variable must be wrapped by withToolazyReactTheme HOC.

With Class component:

import React from 'react';
import {Text} from 'react-native';
// the Higher-order component (HOC)
import {withToolazyReactTheme} from 'toolazy-react-theme';

class MyComponent extends React.Component {
 constructor(props) {
  super(props);
 }
 render() {
  console.log(this.props); // You theme should be here.
  const {theme} = this.props;
  return <Text style={{color: theme.tabBarActive}}>Hi friend</Text>
 }
}

// Wrap your component with HOC
export default withToolazyReactTheme()(MyComponent);

With Function component:

import React from 'react';
import {Text} from 'react-native';

// the Higher-order component (HOC)
import {withToolazyReactTheme} from 'toolazy-react-theme';

const MyComponent = (props) => {
  return (
   <>
    <Text style={{color: props.theme.tabBarActive}}>Hi friend</Text>
   </>
  );
}

// Wrap your component with HOC
export default withToolazyReactTheme()(MyComponent);

Fourth: Change theme

The interesting part here is Theme.changeTheme(theme) which will make current theme you pass available for all the components wrapped by withToolazyReactTheme()(MyComponent)

At the Configure step, you have been created a config file called theme.js, you can import to anywhere and use it:

import React from 'react';
import {View, Button} from 'react-native';
import Theme from 'path/to/theme.js';

class MyComponent extends React.Component {
 constructor(props) {
  super(props);
 }
 
 render() {
  return (
    <View>
      <Button 
        onPress={
          () => {
            Theme.changeTheme(Theme.getCurrentTheme() === 'light'? 'dark' : 'light');
          }
        } 
        title={'Press me to Change Your Theme'} 
      />
    </View>
  )
 }
 
}

Methods

| Methods | Description | | :---: | --- | | Theme.changeTheme(theme) | theme: String Ex: 'dark' or 'light'  This function make your app change theme variable and apply to all the components wrapped by withToolazyReactTheme Higher-order Component | | Theme.getThemesResource() | Return your theme resource. It's an object. | | Theme.getCurrentTheme() | Return the current theme name. It's a string. | | Theme.getTheme() | Return the theme you're using. It's an object |

   

Tips Tricks

You no need to use Redux to manage your themes. Say no to connect() mapStateToProps mapDispatchToProps if it's not needed. You can import Theme from 'path/to/theme.js' everywhere and use all methods above.

Tip 1: Detect your theme has been changed.

Let's say you have 2 components called MyComponent and ToggleComponent. MyComponent is using theme and ToogleComponent is not. ToggleComponent make a change theme action. You need to detect and do something in MyComponent.

Create toogle-component.js file


import React from 'react';
import Theme from 'path/to/theme.js';

const ToggleComponent = () => {
  return (
    <Button 
      onPress={
        () => {
          Theme.changeTheme(Theme.getCurrentTheme() === 'light'? 'dark' : 'light');
        }
      } 
      title={'Press me to Change Your Theme'} 
    />
  );
}

export default ToggleComponent

Create my-component.js file


import React, {useEffect, useRef} from 'react';
import {Text} from 'react-native';
import {withToolazyReactTheme} from 'toolazy-react-theme';

const MyComponent = (props) => {
  const previousTheme = useRef(props.theme);
  useEffect(() => {
    if (previousTheme.current !== props.theme) {
      console.log('Theme in MyComponent updated');
    }
    previousTheme.current = props.theme;
  });

  return (
    <>
      <Text style={{color: props.theme.tabBarActive}}>Hi friend</Text>
    </>
  );
};

export default withToolazyReactTheme()(MyComponent);

As you can see, we have two separate components but When you click on the "Press me to Change Your Theme" button, theme will be changed and MyComponet updated. You can detect that change by using useRef and useEffect like above if it's React function component or you can use componentDidUpdate(prevProps) for React class component and compare if(prevProps.theme !== this.props.theme) as well.

     

License

Licensed under MIT license, see LICENSE for the full license.