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

recustom

v0.0.2

Published

An agnostic approach to bind your styles to your react components.

Downloads

4

Readme

Recustom

written in: typescript code style: prettier

Yet another way to handle styles in React.

How to use

Recustom is an "HOC factory" which helps mapping a component props and state to CSS custom properties and classes. It is inspired by the react-redux connect API.

import custom from "recustom";

// will be computed as custom properties
const mapCustomProperties = (props, state) => ({
  color: props.important ? "red" : "black",
  textAlign: props.alignment // computed as --text-align
});

// will be computed as state classes
const mapStateClasses = (props, { active }) => ({ active });

export default custom(mapCustomProperties, mapStateClasses)(Section);

Your wrapped component must receive in its props the className and the style.

// wrapped component
class Section extends PureComponent {
  render() {
    const { className, style, bindState } = this.props;

    bindState(this.state); // allow the hoc to map state to custom properties and classes

    return (
      <div className={className} style={style}>
        <div className="subelement" />
      </div>
    );
  }
}

Your are now able to write powerful vanilla CSS :

.Section {
  padding: 20px;
  color: var(--color);
  text-align: var(--text-align);
}

/* a state */
.Section.active {
  font-weight: bold;
}

/* a sub element */
.Section .subelement {
  ...;
}

The .Section is generated from your component displayName. You can however force any class name by passing a string as the first argument.

export default custom("NotASection", mapCustomProperties, mapStateClasses)(
  Section
);

Motivations

One of the most interesting feature of styled-components is the ability to pass props carried in our react components to the actual style.

We can leverage props in three different ways :

  • the prop contains a value relevant for the style :

    const Section = styled.section`
      text-align: ${props => props.alignment}; // i.e. alignment = 'left'
    `;
  • the prop conditions a value :

    const Section = styled.section`
      color: ${props => (props.important ? "red" : "inherit")};
    `;
  • the prop conditions a set of properties :

    const Section = styled.section`
      color: grey;
      font-weight: 300;
      ${props =>
        props.active &&
        css`
          color: black;
          font-weight: 700;
        `};
    `;

Translate props to CSS custom properties

The first two use-cases can be handled with CSS custom properties.

.Section {
  text-align: var(--text-align);
  color: var(--color);
}

But how can we translate component's props into CSS custom properties ?

This approach by Dan Bahrami is really interesting. It allows us to define custom properties locally in the following way :

<CustomProperties properties={{ "--text-align": "left" }}>
  <section className="Section">this will be aligned left</section>
</CustomProperties>

However these declarative approach may lead to some unnecessary complexity on the render function. Thus I decided to take the HOC approach.

custom(
  ({ alignment, important }) => ({
    textAlign: alignment,
    color: important ? "red" : "inherit"
  }),
  mapStateClasses
)(Section);

It allows to easily map props received by our components to CSS custom properties. Note that textAlign will be translated to --text-align.

Translate props to classes

The last use-case is a bit different. In fact we cannot rely on CSS custom properties anymore. However we can rely on the most old school way to deal with dynamism with CSS : "state classes". It is a common way (still used in Bootstrap v4) to handle how an element change its display throughout interactions. And this is exactly what styled-components do in background.

mapStateClasses({ active }) {
    return { active }; // return anything 'classnames' npm module can handle
}

export default custom(
	mapCustomProperties,
	mapStateClasses
)(Section);

To deal with this use case, I decided to go for the same approach as the "props to custom properties" mapper. Here you actually map the props of your component to "state classes" (i.e. active, visible, ...).

Extension

It is possible to extend an already stylized component using the same HOC, the exact same way :

import Section from './Section';

mapStateClasses(state) {
    return {
        active: false // overide the inherited behavior
    }
}

export default custom(
    'AlternativeSection',
	mapCustomProperties,
	mapStateClasses
)(Section);

You may have noticed our first argument which is mandatory to avoid className collision. If not provided, the className will fallback to the displayName of the wrapped component (in this case Section).

The provided mappers allows us to override the inherited behavior. Think that the response of the upper mapper is merged with the inherited mapper. That mean if you want to disable the active "state class", you just need to return false for this key.