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

rgjs6-virtual-scroller

v0.3.0-alpha.2

Published

A virtual scroller for the DOM

Downloads

28

Readme

RGJS6 Virtual Scroller

A virtual scroller for the DOM, inspired by virtual-scroller.

Getting started

Install via npm:

npm install rgjs6-virtual-scroller

Usage

See https://unpkg.com/rgjs6-virtual-scroller@latest/jsdoc/index.html

HTML:

<!DOCTYPE html>
<html lang="en">
<body>

  <div class="items-container"></div>

</body>
</html>

Javascript:

import VirtualScroller from 'rgjs6-virtual-scroller';

const body = document.body;
const element = document.querySelector('.items-container');

const items = [
  {
    title: 'Item #1',
    subtitle: 'This is the 1st item!',
    href: 'https://www.example.com/',
  },
  // etc...
];

const itemRenderCb = (itemData) => {
  const element = document.createElement('a');
  a.setAttribute('href', itemData.href);
  a.innerHTML = `
    <strong>${itemData.title}</strong><br />
    ${itemData.subtitle}
  `;
  return a;
};

const scroller = new VirtualScroller(element, items, itemRenderCb, {
  viewport: { element: body },
}).init();

Result:

<!DOCTYPE html>
<html lang="en">
<body>

  <div class="items-container">
    <div data-chunk-index="0">
      <a data-item-index="0" href="..."> ... </a>
      <a data-item-index="1" href="..."> ... </a>
      ...
    </div>
    <div data-chunk-index="1">
      <a data-item-index="8" href="..."> ... </a>
      <a data-item-index="9" href="..."> ... </a>
      ...
    </div>
  </div>

</body>
</html>

Overview

Parameters

| Parameter | Type | Default | Description | | --- | --- | --- | --- | | element | HTMLElement | | The container element holding the items | | items | Array | [] | An array of items | | itemRenderCb | Function | null | Called for each item that needs to render. Should return an HTMLElement. | | opts | Object | {} | Options (optional) | | opts.viewport.element | HTMLElement | undefined | If the container is not the scrolling element, you can provide it here | | opts.viewport.rootMarginMode | String | 'auto' | Describes how the Viewport.rootMargin value should be interpreted. Allowed values: 'px|pixel|pixels', '$|percent' or 'auto'. 'auto' causes Viewport.rootMargin to be ignored and automatically sets it to 50% of the height of the viewport. | | opts.viewport.rootMargin | Number | 0 | Similar to how IntersectObserver.rootMargin works. The value is added to the offsetTop of a chunk and used to calculate if an item is in the ViewPort | | opts.chunk.itemsPerChunk | Number | 8 | The number of items a single chunk should contain. | | opts.chunk.keepAlive | Number | 0 | Determines how long a detached chunk should be kept around before allowing garbage collection. 0 means forever. | | opts.item.intrinsicSize | Number | 40 | The assumed size of an item until it's rendered, added to DOM and measured. | | opts.item.keepAlive | Number | 10*1000 | Determines how long a detached item should be kept around before allowing garbage collection. 0 means forever. | | opts.item.cache | Boolean | true | Enables caching an Item's outerHTML for better performance, trading some memory usage. | | opts.debug | Boolean | false | Enables debug output in the console. |

Events

Listen for events using the on() and off() methods, example:

const scroller = new VirtualScroller( /* ... */ )
scroller.on('chunk-change', () => { /* ... */ })
scroller.init();

| Event name | Description | | --- | --- | | 'viewport-resize' | Fired after the viewport size changed and all components have updated.Details: {viewport: Viewport}. | | 'container-resize' | Fired after the container size changed and all components have updated.Details: {container: Container}. | | 'chunk-change' | Fired after chunks was added or removed and all components have updated.Details: {added: [Chunk, ...], removed: [Chunk, ...]}. |