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-finite-loader

v0.1.0

Published

React progress loaders for loading a finite resource

Downloads

4

Readme

React Finite Loader

Finite loader components for React.

React finite loader provides loader components for loading resources where the size of the resource is known. For example downloading a file.

React finite loader provides a set of loading UI components as well as a set of container components to interface with resource loading methods such as XMLHttpRequest objects.

Contents


  1. Getting Started
  2. UI Components
  3. API Documentation

Getting Started


Install and save as a dependency. npm install --save react-finite-loader

Using the XMLHttpRequest container

In the example below we make use of the provided XmlHttpRequestContainer component which accepts an XMLHttpRequest instance and a loader type as a child for the UI.

In this instance we simply start the XMLHttpRequest once the component has been loaded. For our UI component (Bar) we have passed some styling attributes. Styling can be handled within the component via Javascript or externally via CSS, see API documentation for more details.

import React from 'react'
import { XmlHttpRequestContainer, Bar } from 'react-finite-loader'

export default class ResourceDownloader extends React.Component {
  componentDidMount () {
    const { xmlHttpRequest, url } = this.props
    xmlHttpRequest.open('GET', url)
    xmlHttpRequest.send()
  }

  render () {
    const { xmlHttpRequest } = this.props
    return (
      <XmlHttpRequestContainer xmlHttpRequest={xmlHttpRequest}>
        <Bar
          style={{
            width: '100%',
            height: '20px',
            loadedColor: '#2c69cc',
            unloadedColor: '#9cbbed'
          }}
        />
      </XmlHttpRequestContainer>
    )
  }
}

In this example our ResourceDownloader takes two props; xmlHttpRequest and url. Where xmlHttpRequest is our XMLHttpRequest instance and url is the URL location of the resource we are running our GET request against.

Using the ReactFiniteLoader component

If there are no suitable container components you can use the ReactFiniteLoader component directly, this is the component used by the container components to interface with the various resource loading methods.

In this example we imitate a resource loading in by setting a randomly incrementing loading value at random intervals.

import React from 'react'
import { ReactFiniteLoader, Bar } from 'react-finite-loader'

export default class ResourceDownloader extends React.Component {
  constructor (props) {
    super(props)
    this.state = { value: 0 }
    this.incrementValue = this.incrementValue.bind(this)
    this.getRand = this.getRand.bind(this)
  }

  incrementValue () {
    setTimeout(() => {
      const value = this.state.value + this.getRand(1, 12)
      if (this.state.value >= 100) {
        this.setState({ value: 100 })
      } else {
        this.setState({ value })
        this.incrementValue()
      }
    }, this.getRand(600, 1800))
  }

  componentDidMount () {
    this.incrementValue()
  }

  getRand (min, max) {
    return Math.floor(Math.random() * (max - min)) + min
  }

  render () {
    const { value } = this.state
    return (
      <ReactFiniteLoader value={value}>
        <Bar
          style={{
            width: '100%',
            height: '20px',
            loadedColor: '#2c69cc',
            unloadedColor: '#9cbbed'
          }}
        />
      </ReactFiniteLoader>
    )
  }
}

We use the same UI component as in the previous example, but this time instead of using the XmlHttpRequestContainer component we are using the ReactFiniteLoader component instead. This allows us to control the progress. In this example we only pass a value prop, by default our inital and final values are 0 and 100 respectively. See the API docs for more details.

Once our component has mounted we start our progress value incrementer which recursively increments our value until it has reached 100 at random time intervals and random increments.

UI Components

As well as being able to use the bundled loader UI components you can also create your own custom UI components.

All UI components receive a progressPercentage prop which is the progress percentage value between 0% and 100% inclusive. Any additional props are component specific. The progressPercentage prop is handled by the ReactFiniteLoader component.

Therefore to create a custom UI loader component all you need to do is create a component which accepts a progressPercentage prop and handle the value on progressPercentage accordingly.

See the API documentation for further detail on the bundled UI components.

API Documentation


  1. Container Components
  2. UI Loader Components