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

@loopmode/stateful

v3.0.1

Published

Unobtrusive react wrapper component that takes care of state micro-magement.

Downloads

107

Readme

@loopmode/stateful

Unobtrusive react wrapper component that takes care of state micro-magement.

Check out some examples at https://loopmode.github.io/stateful/.

Installation

yarn add @loopmode/stateful

# or
npm install --save-dev @loopmode/stateful

Usage

Wrap components in a <Stateful> parent, and the wrapped components will receive "stateful props":

import { Stateful } from "@loopmode/stateful";

export default function Example() {
  const callback = async () => {
    const stuff = await fetch("https://yourproject.com/api");
    console.log(`Do something with stuff`, stuff);
  };

  return (
    <div>
      <Stateful pendingClass="btn-loading">
        <button className="btn btn-primary" onClick={callback}>
          Successfull operation
        </button>
      </Stateful>
    </div>
  );
}

What it does

The default monitored callback is onClick (Can be changed via monitor prop, e.g. <Stateful monitor="onSubmit">). If that callback returns a promise, then a series of states will be handled internally:

  • pending - as soon as the promise was created and until status becomes busy.
    • This state can be omitted completely by setting busyDelay={0}
  • busy - after status has been pending for busyDelay milliseconds
  • success - after the promise was resolved, and for successDuration milliseconds
  • error - after the promise was rejected, and for errorDuration milliseconds
  • idle - the initial state, after successDuration/errorDuration/hintDuration milliseconds have passed since the promise was settled

Your wrapped children will receive props and/or classNames based on the current state or Status:

  • Each status has corresponding options for props and classes, for example you can specify busyClasses and/or busyProps
    • Classes will be merged into the existing classes of your component
    • Props will simply be applied, overriding any previous values
  • Props will typically be a boolean flag, e.g. if you define busyProps="disabled", the wrapped children will receive disabled={true} while status is busy
    • You can provide more complex configuration than strings to achieve different kinds of values
  • You can provide multiple values in various ways: busyClasses="disabled is-loading default-cursor", busyClasses={["disabled", "is-loading default-cursor"]} or busyClasses={() => (['disabled', 'is-loading default-cursor'])}

Motivation

Imagine a button that triggers some operation on the server.
How many times have you manually written handling for the pending state of such operations?
Setting a boolean flag here, setting it back after success or error there, maybe setting up some more more flags to indicate success or error, adding some timers to revert those flags...

Good UX

In the philosophy of this project, good button UX goes something like this:

  • On click
    • Disable the button (to prevent multiple requests while the first one is still pending)
    • Display an animation on the button (to give visual feedback to the user)
  • On success:
    • Reset disabled state (to make the button operational again)
    • Display success state by adding classes or props to the button (for visual feedback)
      • Set a timeout that resets the indicator state after a moment
  • On error: Same as on success, but make the button e.g. red instead of green

Probably the button should be disabled immediately after the click to prevent multiple overlapping requests, but maybe the animation itself ("busy state") should be delayed for a while, as to avoid spinners flashing up briefly on fast networks or short operations.

UX vs DX

However, good UX like that might come at the cost of bad developer experience. Also, readability and maintainability if your codebase might suffer greatly, unless you have a good abstraction.

If you start implementing this in your components ad-hoc, you'll probably end up with a cluttered and noisy codebase. Reading the code and understanding the business case will become hard, simply because there's all that UX stuff like multiple boolean flags and timers all over the place.

Solution

Use this library and you'll get good button UX for free :)