0.3.0-alpha.1 • Published 16 days ago

rgjs6-virtual-scroller v0.3.0-alpha.1

Weekly downloads
-
License
MIT
Repository
-
Last release
16 days ago

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

ParameterTypeDefaultDescription
elementHTMLElementThe container element holding the items
itemsArray[]An array of items
itemRenderCbFunctionnullCalled for each item that needs to render. Should return an HTMLElement.
optsObject{}Options (optional)
opts.viewport.elementHTMLElementundefinedIf the container is not the scrolling element, you can provide it here
opts.viewport.rootMarginModeString'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.rootMarginNumber0Similar 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.itemsPerChunkNumber8The number of items a single chunk should contain.
opts.chunk.keepAliveNumber0Determines how long a detached chunk should be kept around before allowing garbage collection. 0 means forever.
opts.item.intrinsicSizeNumber40The assumed size of an item until it's rendered, added to DOM and measured.
opts.item.keepAliveNumber10*1000Determines how long a detached item should be kept around before allowing garbage collection. 0 means forever.
opts.item.cacheBooleantrueEnables caching an Item's outerHTML for better performance, trading some memory usage.
opts.debugBooleanfalseEnables 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 nameDescription
'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, ...]}.
0.3.0-alpha.1

16 days ago

0.2.27

9 months ago

0.2.26

9 months ago

0.2.25

9 months ago

0.3.0-alpha.0

9 months ago

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.1.10

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

0.1.13

2 years ago

0.1.14

2 years ago

0.1.15

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.30

2 years ago

0.1.32

2 years ago

0.1.33

2 years ago

0.1.27

2 years ago

0.1.28

2 years ago

0.1.29

2 years ago

0.1.20

2 years ago

0.1.21

2 years ago

0.1.22

2 years ago

0.1.23

2 years ago

0.1.24

2 years ago

0.1.25

2 years ago

0.1.26

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.16

2 years ago

0.2.6

2 years ago

0.1.17

2 years ago

0.2.9

2 years ago

0.1.18

2 years ago

0.2.8

2 years ago

0.1.19

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.1.6

2 years ago

0.1.4

3 years ago

0.1.5

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago