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-themable-hoc

v0.1.9

Published

React higher-order-components that allow for css-in-js-style themes.

Downloads

23

Readme

react-themable-hoc

Higher-order-components for theming using css-in-js.

npm install --save react-themable-hoc

Usage

Simply add the themed HOC to the bottom of your component class.

import React from 'react';
import { themed } from 'react-themable-hoc';

const Button = ({ onClick, classNames }) => (
    <input className={classNames.button} type="button" onClick={onClick} />
);

export default themed(theme => ({
    button: {
        color: theme.fontColor,
        backgroundColor: theme.backgroundColor
    }
}))(Button);

Setup

Define your themes and add them to the ThemeManager

import { ThemeManager } from 'react-themable-hoc';

ThemeManager.addTheme('theme1', {
    primaryColor: '#eee',
    secondaryColor: '#bbb',
    fontColor: '#333',
    fontSize: '14px'
});
ThemeManager.addTheme('theme2', {
    // ...
});

Pick your css-in-js interface (See Supported Interfaces)

This example uses the AphroditeInterface

import AphroditeInterface from 'react-themable-hoc-aphrodite-interface';

//...

ThemeManager.setStyleInterface(new AphroditeInterface());

Setup the ThemeProvider. This allows all components under the ThemeProvider to have access to the current theme.

ReactDOM.render(
    <ThemeProvider theme="myTheme">
        <App />
    </ThemeProvider>,
    document.getElementById('app')
);

Options

You can pass options to the themed HOC.

export default themed(theme => ({
    button: {
        // ...
    }
}), { pure: true })(Button);

Available options

| Name | type | Default | Description | |------|------|---------|-------------| | pure | boolean | undefined | If true, the HOC will extend from React.PureComponent | | shouldUpdateStyles | function | undefined | Determine if stylesheets should be re-created on update. See Styling based on props| | classesPropName | string | 'classNames' | The name of the prop passed to the wrapped component with the generated classNames|

Styling based on props

The component's props are passed along with the theme when creating your styles. This allows you to specify inline styles based on the props passed in.

export default themed((theme, props) => ({
    button: {
        color: theme.fontColor,
        width: props.size
    }
}))(Button);

You can pass a function called shouldUpdateStyles as an option to control when the HOC will re-create the stylesheets when its props change.

const shouldUpdateStyles = (currProps, nextProps) => {
    return currProps.size !== nextProps.size;
};

export default themed((theme, props) => ({
    button: {
        color: theme.fontColor,
        width: props.size
    }
}), { shouldUpdateStyles })(Button);

If pure is true and no shouldUpdateStyles function is provided, themed will perform a shallow comparison on its props to determine whether or not the stylesheets should be re-created.

If pure is not set and shouldUpdateStyles is not provided, themed will always re-create stylesheets for this component when it updates.

Supported CSS-in-JS interfaces

License

MIT