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

theme-wrap

v1.0.3

Published

Package your React apps with your own hand crafted Theme Wrap

Downloads

47

Readme

Theme Wrap

Build Status npm

Package your React apps with your own hand crafted Theme Wrap

Basic Usage

In the quick overview below, you will be guided with a simple walk-through of how to configure a sample theme and have your components be able to access this data. In short:

  • You wrap your components inside of the <ThemeWrapProvider theme={theme}> and pass the theme via a prop.
  • You wrap your components with applyThemeWrap(componentStyles) and pass your styles as the argument.
  • Access your styles via your component's styles.

It's a simple process that provides an elegant way of theme your entire React app.

Furthermore, in the advanced section, we will cover more advanced topics, such as being able to access your component'sprops in your component's styles, hot-swap different themes on the fly (in cases where an action could trigger an entire theme update), mixins support, and more...

Lets get started...

Similar to other React providers, ThemeWrapProvider provides data to the entire app. For example, you may have the following sample theme:

// theme.js

module.exports = {
  primaryColor: 'red',
  secondaryColor: 'green',
  fontSizes: {
    sm: '0.8em',
    md: '1.1em',
    lg: '1.8em'
  }
};

After creating and configuring your theme, you provide the theme as a prop to the ThemeWrapProvider, as seen below:

// App.jsx

import React, { Component } from 'react';

import theme from './theme';

class App extends Component {
  
  render() {
    return (
      <ThemeWrapProvider theme={theme}>
        <Navigation />
        ...
      </ThemeWrapProvider>
    );
  }
}

export default App;

As you can see above, we've decided to wrap our entire app signified by the inside of our ThemeWrapProvider. However, you will later see that you don't necessarily have to wrap your entire app if not necessary and you can also nest ThemeWrapProviders for the ability to have sub sections of your app respond to different themes.

Now, all components within ThemeWrapProvider have access to theme. But how do our components get access to the theme? You can try to manage the context data yourself or use our applyThemeWrap higher order utility.

applyThemeWrap()

This higher order function wraps your component and provides the theme along with other data as props to your wrapped component. Following our example above, we have a <Navigation />:

// Navigation.jsx

import React, { Component } from 'react';
import { applyThemeWrap } from 'theme-wrap';

import styles from './Navigation.styles';

class Navigation extends Component {
  
  render() {
    return (
      <div>
        <h1>
          Sample Component Header
        </h1>
      </div>
    );
  }
}

export default applyThemeWrap(styles)(Navigation);

Please note the component's styles which is passed into applyThemeWrap that wraps your entire component. You can also use ES7 decorators, if you'd like:

@applyThemeWrap(styles)
class Navigation extends Component {
  ...
}

export default Navigation;

So lets now see how our component's styles could look like. We can either include our inline styles within the same file, but we'll separate it to help keep our code modular. So, lets define the styles for our <Navigation />:

// Navigation.styles.js

export default function(theme) {
  return {
    container: {
      // green
      backgroundColor: theme.secondaryColor,
      padding: '20px'
    },
    header: { 
      // red
      color: theme.primaryColor
    }
  };
}

Our component's styles now has the theme within its scope which you can see we've used to define our colors. Now that our component's styles are defined, lets use them within our component. As mentioned earlier, your styles are passed down as props to the component. Lets revisit our <Navigation />:

// Navigation.jsx

import React, { Component } from 'react';
import { applyThemeWrap } from 'theme-wrap';

import styles from './Navigation.styles';

@applyThemeWrap(styles)
class Navigation extends Component {
  
  render() {
    const { tmStyles } = this.props;
    return (
      <div style={tmStyles.container}>
        <h1 style={tmStyles.header}>
          Sample Component Header
        </h1>
      </div>
    );
  }
}

export default Navigation;

In the render function, you will see that we are deconstructing our props and accessing tmStyles, which signified our theme wrapped styles from our applyThemeWrap(styles) wrapper. We then use the keys from our styles as we please to style the different sections of our app. It's as easy as that!

Contribute

I welcome contributors. If you have ideas to improve this library, go for it! Please use GitHub's pull request and issues features. Here are a few scripts to help you get started:

  • Run the repo in development mode. npm run dev

  • Run the repo in development mode with all outputs on a single line. npm run dev -s

  • Prepare and build the repo for distribution. npm run build

  • Lint your project. npm run lint.

  • Lint and watch your project npm run lint:watch.

  • Run tests npm run test.

  • Run and watch tests npm run test:watch.

  • Run tests with coverage report npm run test:cover.

  • Run tests with coverage report and travis npm run test:cover:travis.

TODO

  • lint
  • applyThemeWrap tests
  • favicon
  • Guides
  • Documentation
  • Static build for demo
  • more demos
  • screenshots