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-styleable

v3.3.3

Published

Higher-order component for styling react

Downloads

502

Readme

react-styleable

Consistent, easy overrides for CSS Modules in React Components

Install

npm install react-styleable --save-dev

Usage

Styles in Props

react-styleable makes your styles from your CSS modules available on props.css.

Write your stylesheet with all the perks of css modules. For example:

.list {
  list-style: none;
  padding-left: 0;
  margin: 10px;
}
.item {
  outline: 1px solid red;
  padding: 10px;
}

Then in your reusable component, wrap your React.Component in react-styleable's higher-order component.

import styleable from 'react-styleable'

import css from './my-list.css'

class MyList extends React.Component {
  renderItem(item, i) {
    return (
      <li key={i} className={this.props.css.item}>{item}</li>
    )
  }
  renderList(items) {
    return items.map(this.renderItem)
  }
  render() {
    return (
      <ul className={this.props.css.list}>
        {this.renderList(this.props.items)}
      </ul>
    )
  }
}

export default styleable(css)(MyList)

Usage as a decorator is also nice:

@styleable(css)
class MyList extends React.Component { /* ... */ }

Your MyList component is now styled and ready to display!

Overriding Component Styles

This is the big payoff.

If you want to override this component's styles as the consumer, you can easily do so, through the same, consistent interface. First, define a new stylesheet:

.item {
  outline: 1px solid blue;
}

And use it to render MyList again, passing your new stylesheet via the props.css prop:

import MyList from './my-list'

import css from './client.css'

React.render(<MyList css={css} />, document.getElementById('app'))

Now the .items outline will be blue instead of the original red.

Composing Component Styles

If instead of just overriding the styles, you wanted to add to them with style composition, you can do that as well.

One method is via CSS modules' composes keyword. In your new stylesheet:

.item {
  composes: item from "./my-list.css";
  background: pink;
}

Now the original red outline will remain and a pink background will be present as well. This is the most surefire way to compose styles because it allows you to guarantee the order of the cascade.

But it has the downside of having to locate the original stylesheet location.

If you have enough assurances on the cascade order and selector specificity, all potential concerns, you can use the compose api via the react-styleable to accomplish the same thing (in [email protected]):

import MyList from './my-list'

import css from './client.css'

React.render(<MyList css={{ compose: css }} />, document.getElementById('app'))

Styled. Portable. Easily overridden. So, so good.