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

v0.2.0

Published

Simple feature toggle library for React.

Downloads

3

Readme

Caterpillar 🐛 📦 🦋

A simple feature toggle library for React ⚛️.

Install

yarn add react-caterpillar

or

npm install react-caterpillar

Usage

// feature-toggles.ts

import {initCaterpillar} from 'caterpillar';
import type {FeatureGroup} from 'caterpillar';

export enum FeatureName {
  blueButton = 'blueButton',
  displayVersion = 'displayVersion',
}

const FEATURES: FeatureGroup<FeatureName> = {
  [FeatureName.blueButton]: {
    name: FeatureName.blueButton,
    description: 'Blue button',
    active: true,
  },
  [FeatureName.displayVersion]: {
    name: FeatureName.displayVersion,
    description: 'Display app version on Home screen',
    active: false,
  },
};

const Caterpillar = initCaterpillar(FEATURES);

export default Caterpillar;
// app.tsx

import React from 'react';
import Caterpillar, {FeatureName} from './feature-toggles';

function HomeScreen() {
  const displayVersion = Caterpillar.useFeature(FeatureName.displayVersion);

  return (
    <div>
      <main>
        <div>Hi there!</div>
      </main>
      <footer>{displayVersion.active ? <p>Version 1.0.0</p> : null}</footer>
    </div>
  );
}

function App() {
  return (
    <Caterpillar.Provider>
      <HomeScreen />
    </Caterpillar.Provider>
  );
}

Documentation

This section explains the rest of the API.

Using the Feature component

You can access feature toggles using either the React Hooks API or the higher order Caterpillar.Feature component.

import Caterpillar, {FeatureName} from './feature-toggles';

function AboutScreen() {
  const [count, setCount] = React.useState(0);

  return (
    <main>
      <Caterpillar.Feature name={FeatureName.blueButton} fallback={null}>
        <button
          style={{backgroundColor: 'blue'}}
          onClick={() => setCount(count => count + 1)}
        >
          Count is: {count}
        </button>
      </Caterpillar.Feature>
    </main>
  );
}

Listing and updating all feature toggles

You can use the Caterpillar.useFeatures-hook to access all feature toggles. This is especially useful for implementing a (hidden) feature toggle screen where users can enable features to preview them locally.

import Caterpillar from './feature-toggles';

function HiddenScreen() {
  const [features, setFeature] = Caterpillar.useFeatures();

  return (
    <div>
      <h2>All features</h2>

      <ul>
        {features.map(feature => (
          <li key={feature.name}>
            <label>
              <input
                type="checkbox"
                name={feature.name}
                checked={feature.active}
                onChange={event =>
                  setFeature(feature.name, event.target.checked)
                }
              />
              {feature.description}
            </label>
          </li>
        ))}
      </ul>
    </div>
  );
}

Enabling features on start-up

You can pass a list of initially enabled features to the provider component. One idea would be to read the enabled features from the URL.

import React from 'react';
import parse from 'url-parse';
import Caterpillar, {FeatureName} from './feature-toggles';

function App() {
  const features = React.useMemo(() => {
    const url = parse(window.location.href, true);
    return url.query['enable']
  }, [])

  return (
    <Caterpillar.Provider initiallyEnabled={features}>
      <HomeScreen />
    </Caterpillar.Provider>
  );
}