GithubHelp home page GithubHelp logo

smeijer / spin-delay Goto Github PK

View Code? Open in Web Editor NEW
337.0 4.0 10.0 726 KB

Smart spinner helper for React, to manage the duration of loading states.

License: MIT License

JavaScript 55.19% TypeScript 44.81%
spinner react hacktoberfest

spin-delay's Introduction

spin-delay

All Contributors

Smart Spinner Helper for React

demo animation of spin-delay

There are a few annoyances when working with spinners. Network request can be so fast that rendering a spinner does more harm than good. Why render a spinner when loading the data only takes like 50ms?

This can be fixed by adding a delay. Only show the spinner when the request takes longer than 200ms for example. And what happens when the request takes 210ms? Right, we see a spinner for 10ms. This flicker can be annoying.

spin-delay solves these issues by wrapping your booleans, and only returning true after the delay, and for a minimum time of minDuration. This way you're sure that you don't show unnecessary or very short living spinners.

Demo

Sandbox -> https://codesandbox.io/s/spin-delay-jlp2c

Installation

With npm:

npm install --save spin-delay

With yarn:

yarn add spin-delay

API

The examples below use the following data object:

import { useSpinDelay } from 'spin-delay';

function MyComponent() {
  const [{ loading }] = useFetch('http://example.com');

  // options are optional, and default to these values
  const showSpinner = useSpinDelay(loading, { delay: 500, minDuration: 200 });

  if (showSpinner) {
    return <Spinner />;
  }

  // ...
}

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Stephan Meijer
Stephan Meijer

🤔 💻 🚇 🚧 ⚠️
Peter Hozák
Peter Hozák

🤔 ⚠️
Eric Hosick
Eric Hosick

📖
Supachai Dev
Supachai Dev

💻
Kent C. Dodds
Kent C. Dodds

💻
Phong Chu
Phong Chu

💻
Joe Porpeglia
Joe Porpeglia

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

spin-delay's People

Contributors

allcontributors[bot] avatar aprillion avatar erichosick avatar joeporpeglia avatar kentcdodds avatar smeijer avatar supachaidev avatar vidhill avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

spin-delay's Issues

[Feature Request]: Delay mutations

Hey, thanks for the cool library. I wonder if it makes sense to also support delaying mutations. I.e. always showing the spinner for at least 300ms, like in this example? I'd be willing to contribute a PR.

Delay is always ignored on mount (defeating the purpose of delay)

The useIsSSR hook is hardcoded to always return true when the component mounts, which isn't the worst assumption given the state of the React ecosystem.

However, the ssr option is not considered when deciding if the delay option should be respected or not.

image

This means that the spinner will always show immediately unless you are also using the loading prop you pass in as a backup like the setup below:

image

Steps to reproduce

  1. Have the following:
    • Use useSpinDelay in a component and ensure the options argument at least has { delay: <some value>, ssr: false }
    • A loading useState that will start false and is passed as the loading argument to useSpinDelay
    • A button that will set loading to true, and then adds a timeout for a period of time that will set loading back to false
    • A span/div/etc. that shows the state of loading for verification
  2. Ensure the timeout to set loading to false is relatively short (<1 sec) and the delay option is longer (1-2 sec)
  3. Click the button

Expected behavior

The loading spinner will not show and the loading verification will show true then false.

Actual behavior

The loading spinner appears immediately and will hide when loading gets set back to false
If the minDuration option is specified, then the spinner will appear for only the full length of the minDuration, not including the delay amount.


Unrelated: the code sandbox in the README is really useful! However, it is still using version 0.0.1 by default, which means it doesn't show off the current behaviors properly.

Node version requirements

Does this require node version greater than or equal to 18? I received the following error when I tried to do a yarn add spin-delay:

error @azure/[email protected]: The engine "node" is incompatible with this module. Expected version ">=18.0.0". Got "16.20.2"
error Found incompatible module.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.