use-spinner - Show loading spinners for your asynchronous calls

pinussilvestrus

Niklas Kiefer

Posted on April 3, 2021

use-spinner - Show loading spinners for your asynchronous calls

Hello community 👋

Sometimes calls can take longer, so showing a loading spinner becomes an option to fill the gap. I was tired of configuring such spinners times and times again.

Yesterday I built a tiny Javascript library called use-spinner that simply wraps asynchronous calls into a new function adding a loading spinner to the DOM.

Simply install the module



$ npm install --save use-spinner


Enter fullscreen mode Exit fullscreen mode

and embed it in your Node.js styled application.



import useSpinner from 'use-spinner';

import 'use-spinner/assets/use-spinner.css';

const mySlowCall = async () => {
  return await fetch(/*...*/);
}

const spinned = useSpinner(mySlowCall);

await spinned();


Enter fullscreen mode Exit fullscreen mode

Alt Text

Of course, it is rather rudimentary right now and the spinner itself is hardly customizable. However, I like the easiness of integration to existing functions without much configuration.

Enjoy ❤️

GitHub logo pinussilvestrus / use-spinner

Add a simple loading spinner to your async JS calls - built for the browser.

use-spinner

CI npm bundle size

Add a simple loading spinner to your async JS calls - built for the browser.

Installation

$ npm install --save use-spinner
Enter fullscreen mode Exit fullscreen mode

Usage

import useSpinner from 'use-spinner';

import 'use-spinner/assets/use-spinner.css';

const fn = async () => {
  await new Promise(resolve => setTimeout(() => {
    console.log('done.');
    resolve();
  }, 2000));
};

// wrap your asynchronous function
const spinnedFn = useSpinner(fn, {
  container: 'body'
});

// execute with a loading spinner
await spinnedFn();
Enter fullscreen mode Exit fullscreen mode

Screencast

Options

The API accepts a second argument configuring the wrapped function. This defaults to:

{
  container: 'body'
}
Enter fullscreen mode Exit fullscreen mode
  • container: a selector or a DOM element that appends the loading spinner.

License

MIT






💖 💪 🙅 🚩
pinussilvestrus
Niklas Kiefer

Posted on April 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related