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

bpk-component-infinite-scroll-css

v4.1.8

Published

Backpack infinite scroll component.

Downloads

91

Readme

bpk-component-infinite-scroll

Backpack infinite scroll component.

Installation

npm install bpk-component-infinite-scroll --save-dev

Usage

import React from 'react';
import PropTypes from 'prop-types';
import BpkButton from 'bpk-component-button';
import BpkSpinner, { SPINNER_TYPES } from 'bpk-component-spinner';
import withInfiniteScroll, {
  ArrayDataSource,
} from 'bpk-component-infinite-scroll';

const SomeList = ({ elements }) => (
  <div id="list">
    {elements.map(element => (
      <div key={element} style={{ height: '50px' }}>
        {element}
      </div>
    ))}
  </div>
);

const elementsArray = [
  'element 1',
  'element 2',
  'element 3',
  'element 4',
  'element 5',
  'element 6',
  'element 7',
  'element 8',
  'element 9',
  'element 10',
];

const CustomLoading = () => (
  <div>
    <BpkSpinner type={SPINNER_TYPES.primary} />
  </div>
);

const CustomSeeMore = ({ onSeeMoreClick }) => (
  <div>
    <BpkButton onClick={onSeeMoreClick}>See More</BpkButton>
  </div>
);

const InfiniteList = withInfiniteScroll(SomeList);
const dataSource = new ArrayDataSource(elementsArray);

export default () => (
  <InfiniteList
    dataSource={dataSource}
    renderLoadingComponent={CustomLoading}
    renderSeeMoreComponent={CustomSeeMore}
    seeMoreAfter={1}
  />
);

Accompanying classes

DataSource

DataSource is a class used by the BpkInfiniteScroll component to fetch items and listen for changes in the data and react to it by reloading the current items in the list.

Methods

fetchItems(index, nElements)

Called by the InfiniteScroll component every time new data is requested (by scrolling down) and should return the data starting from index plus nElements (number of elements). It should return a promise object.

Example:

fetchItems(0, 5); // should return 5 items starting from position 0
fetchItems(5, 5); // should return 5 items starting from position 5

Calling this method directly in the DataSource class will result in an error, it should be implemented by the subclass extending DataSource.

onDataChange(cb)

Adds a new change listener to this DataSource, to be called when data is updated. Returns true if added or false otherwise.

removeListener(cb)

Removes the callback from the list of change listeners. Returns true if removed or false otherwise.

triggerListeners(...args)

Triggers all listeners in response to data changes. This method should be used by subclasses to tell the InfiniteScroll component it should refresh its data.

import React from 'react';
import PropTypes from 'prop-types';
import withInfiniteScroll, { DataSource } from 'bpk-component-infinite-scroll';

const SomeList = ({ elements }) => (
  <div id="list">
    {elements.map(element => (
      <div key={element} style={{ height: '50px' }}>
        {element}
      </div>
    ))}
  </div>
);

class RemoteFlightsDataSource extends DataSource {
  constructor() {
    super();
    myWebSocketConnection.on('dataChange', () => {
      // tell the `InfiniteScroll` component to refresh its data
      this.triggerListeners();
    });
  }

  fetchItems(index, nElements) {
    return fetch(`https://my-api/flights?start=${index}&count=${nElements}`);
  }
}

const InfiniteList = withInfiniteScroll(SomeList);

export default () => (
  <InfiniteList dataSource={new RemoteFlightsDataSource()} />
);

ArrayDataSource

ArrayDataSource is a class that extends from DataSource. Accepts an array as a parameter in the constructor and uses it as source for the infinite scroll.

See Usage for an example of this class in use.

Methods

refer to the DataSource methods section for a list of all methods

fetchItems(index, nElements)

Returns a subset of the array data.

updateData(newData)

Updates the internal array and triggers all listeners.

Props

| Property | PropType | Required | Default Value | | ----------------------- | -------------------------------- | -------- | ------------- | | dataSource | instanceOf(DataSource) | true | - | | elementsPerScroll | number | false | 5 | | initiallyLoadedElements | number | false | 5 | | loaderMinDisplay | oneOf(['small', 'half', 'full']) | false | 'full' | | onScroll | func | false | null | | onScrollFinished | func | false | null | | renderLoadingComponent | func | false | null | | renderSeeMoreComponent | func | false | null | | seeMoreAfter | number | false | null |

elementsPerScroll

How many more elements to load every time the user reaches the bottom of the list.

initiallyLoadedElements

How many more elements to load every time the user reaches the bottom of the list.

seeMoreAfter

seeMoreAfter is how many scrolls should happen before a 'See more' button is displayed. This only happens once.