• Stars
    star
    407
  • Rank 106,183 (Top 3 %)
  • Language
    JavaScript
  • License
    Other
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A performant virtual scrolling list utility capable of rendering millions of rows

HyperList

Build Status JavaScript Style Guide

This is a simple component that can be dropped into any JavaScript application and provide a virtual scrolling area that is highly performant and lightweight. With zero dependencies and well under 300 lines of code sans comments, it is easy to understand and use.

Demo

Demo

Installation

npm install hyperlist

Of course it can also just be added to any JavaScript project since it consists of a single JavaScript file.

Usage

Below are full code examples containing typical usage. Documentation supplements the code comments so hopefully everything makes sense!

Invocation

How to invoke an instance of HyperList

// Using create
const list = HyperList.create(document.body, requiredOptions);

// Using new
const list = new HyperList(document.body, requiredOptions);

Required Options

These configuration options are not optional. So set them to avoid runtime errors. You can mutate them by setting a new object in the refresh method.

list.refresh(element, newConfig);
  • itemHeight A single value that is the height for every single element in the list.
  • total The number of items in the list.
  • generate A function that is called with the index to render. You return an element to render in that position.

Basic example

A simple example with just the required options.

// Create a container element or find one that already exists in the DOM.
const container = document.createElement('div');

// Pass the container element and configuration to the HyperList constructor.
// You can optionally use the create method if you prefer to avoid `new`.
const list = HyperList.create(container, {
  // All items must be the exact same height currently. Although since there is
  // a generate method, in the future this should be configurable.
  itemHeight: 30,

  // Specify the total amount of items to render the virtual height.
  total: 10000,

  // Wire up the data to the index. The index is then mapped to a Y position
  // in the container.
  generate(index) {
    const el = document.createElement('div');
    el.innerHTML = `ITEM ${index + 1}`;
    return el;
  },
});

// Attach the container to the DOM.
document.body.appendChild(container);

Optional Options

These configuration options are totally optional. So set them when you need to go beyond the defaults and required options.

  • reverse This will render items from the bottom of the container instead of the top. This works much better for chat and notifications experiences. This option will automatically scroll the container to the bottom every time the refresh method is called and during instantiation.
  • horizontal Change the rendering orientation to horizontal
  • width The container width as a number or string (defaults to 100%)
  • height The container height as a number or string (defaults to 100%)
  • scrollerTagName Is a TR by default which works fine in most cases. If you need a different element tag name, specify it here.
  • rowClassName Any custom classes to add to the row.
  • overrideScrollPosition Pull the scrollTop value from somewhere else, this allows for binding range elements to the scroll position.
  • applyPatch Is called with the container element and the DocumentFragment which contains all the items being added. You can implement Virtual DOM patching with this hook.
  • afterRender - Triggered after applyPatch has returned.
  • scroller - Specify an element to be in the place of the scroller.
  • useFragment - Determines if a fragment is used internally or not, defaults to true.

Variable height items

When you are rendering a list of elements that have variable heights you may specific an object as the generate callback's return value that contains the signature: { element: domNode, height: 100 }.

For example:

// Wire up the data to the index. The index is then mapped to a Y position
// in the container, using some height.
generate(index) {
  const el = document.createElement('div');
  el.innerHTML = `ITEM ${index + 1}`;
  return { element: el, height: Math.random() * 1000 };
}

You can also find a working implementation in the examples directory.

Advanced example

An example with all the options, mounted to the entire page that refreshes when the browser resizes.

// Create a container element or find one that already exists in the DOM.
const container = document.createElement('div');

const config = {
  width: '100%',
  height: window.innerHeight,

  // All items must be the exact same height currently. Although since there is
  // a generate method, in the future this should be configurable.
  itemHeight: 30,

  // Specify the total amount of items to render the virtual height.
  total: 10000,

  // Reverse the list to start from the bottom instead of the top.
  reverse: true,
  
  // Customize the scroller tag name, defaults to tr.
  scrollerTagName: 'tr',

  // Or if you want, you can specify an element which has higher precedence.
  scroller: document.createElement('tr'),

  // Customize the virtual row class, defaults to vrow.
  rowClassName: 'vrow',

  // Whether or not childNodes are built up in an Array or Document Fragment.
  useFragment: false,

  // By default HyperList will determine scroll offset from the container
  // element. You can override this lookup by using this hook.
  overrideScrollPosition() {
    return document.body.scrollTop;
  },

  // Wire up the data to the index. The index is then mapped to a Y position
  // in the container.
  generate(index) {
    const el = document.createElement('div');
    el.innerHTML = `ITEM ${index + 1}`;
    return el;
  },

  // Triggerd after any items have been added into the DOM.
  afterRender() {
    console.log('Rendered some items');
  },

  // If you want to do some custom rendering with the container element and
  // the fragment, you can specify this method. The contents of this function
  // are the defaults. Look at examples/diffhtml.html for an example of using
  // this method with a Virtual DOM.
  applyPatch(element, fragment) {
    element.innerHTML = '';
    element.appendChild(fragment);
  },
};

// Pass the container element and configuration to the HyperList constructor.
// You can optionally use the create method if you prefer to avoid `new`.
const list = HyperList.create(container, config);

window.onresize = () => {
  config.height = window.innerHeight;
  list.refresh(container, config);
};

// Attach the container to the DOM.
document.body.appendChild(container);

Contributing

PRs are welcome, please ensure the tests pass and the code looks like the surrounding style:

npm test

Credits

This project is a fork of the existing (unmaintained) project: https://github.com/sergi/virtual-list

This README section, the LICENSE, and package.json will remain to ensure proper credit is always extended.

More Repositories

1

backbone-boilerplate

A workflow for building Backbone applications.
JavaScript
4,496
star
2

backbone.layoutmanager

UNMAINTAINED 7/31/18: A layout and template manager for Backbone applications.
JavaScript
1,681
star
3

diffhtml

diffHTML is a web framework that helps you build applications and other interactive content
JavaScript
869
star
4

github-viewer

GitHub Viewer.
JavaScript
370
star
5

use-amd

An AMD plugin for consuming globally defined JavaScript.
JavaScript
271
star
6

combyne

A template engine that works the way you expect.
JavaScript
144
star
7

backbone.routemanager

Better route management for Backbone.js projects.
JavaScript
111
star
8

salita

Automatically upgrade all NPM dependencies.
JavaScript
95
star
9

babel-plugin-transform-commonjs

A Babel 7 plugin transform to convert CommonJS into ES Modules
JavaScript
82
star
10

lodash-template-loader

A Lo-Dash template loader plugin for AMD projects.
JavaScript
54
star
11

backbone.cacheit

Fetch caching made super simple in Backbone.
JavaScript
47
star
12

vertebrae

Backbone transport management.
JavaScript
43
star
13

previewcode

previewcode.com public repository.
JavaScript
32
star
14

nodewii

Node.js cwiid asynchronous native bindings
C++
31
star
15

growing-up-with-backbone

Growing Up With Backbone.
JavaScript
28
star
16

localhub

A local hub of your Git repositories
JavaScript
15
star
17

redux-create-action-types

Easily create immutable, strict, and well formed action types
JavaScript
15
star
18

diffhtml-prollyfill

DEPRECATED MOVED TO MAIN REPO
14
star
19

scopedcss

Scoping your CSS to a specific selector or element.
JavaScript
14
star
20

nodefreckle

Node.js freckle api bindings
JavaScript
13
star
21

pigeonpost

Amazon SES E-Mail Scheduler & Delivery API
JavaScript
11
star
22

vim-typescript

VIM TypeScript Plugin
JavaScript
10
star
23

site-content

All site content.
JavaScript
10
star
24

hooks-theme-refs

A simple pattern for well structured React Components
JavaScript
10
star
25

todomvc

TodoMVC implemented with VDOM, Tagged templates, and Redux
JavaScript
8
star
26

talks

My Talks
JavaScript
8
star
27

diffhtml-inline-transitions

Inline transition hooks directly into tagged templates
JavaScript
7
star
28

miso.fs

A Miso Dataset FileSystem Importer.
JavaScript
7
star
29

cat-api-boilerplate

A starting point for creating your very own cat API.
JavaScript
7
star
30

react-babel-esm

Using React and Hooks without a bundler, from npm, and with native browser ESM
JavaScript
6
star
31

react-hyperlist

A React wrapper for HyperList
JavaScript
6
star
32

redux-simple-routing

Simple routing for React/Redux apps
JavaScript
5
star
33

babel-plugin-resolve-imports-for-browser

A Babel plugin to convert imports to support browser ESM
JavaScript
5
star
34

bserve

Static HTTP server which automatically transpiles JS using Babel
JavaScript
4
star
35

parallel-run

Run npm scripts multiplexed
JavaScript
4
star
36

babel-preset-browser-esm

Babel preset to make running JavaScript in the browser easier
TypeScript
4
star
37

lm-forms

Backbone LayoutManager and Backbone Forms integration
JavaScript
4
star
38

devoxx-2013

Backbone and Tools
JavaScript
4
star
39

app.toweatherproof.me

Basic Weather App.
JavaScript
4
star
40

chart-component

An experimental Web Component that renders a chart, using diffHTML
JavaScript
4
star
41

babel-ast-cache-perf-tests

Benchmarks various ways of getting a Babel AST
JavaScript
3
star
42

tbranyen.com

My personal site.
JavaScript
3
star
43

grunt-nodequnit

Run QUnit tests in the Node.js environment.
JavaScript
3
star
44

transform-tagged-diffhtml

A Babel plugin transform for diffHTML tagged template strings
JavaScript
3
star
45

diffhtml-synthetic-events

Swaps out inline events for synthetic event delegation.
JavaScript
3
star
46

synchronizer

Build AMD projects into a single UMD file.
JavaScript
3
star
47

consumare

Consume content from Git.
JavaScript
3
star
48

combyne-amd-loader

A Combyne template loader plugin for AMD projects.
JavaScript
3
star
49

combynexpress

Combyne view engine for Express.
JavaScript
3
star
50

extless-loader

Loads Node ESM in a more compatible way by making extensions optional.
JavaScript
3
star
51

d3-playground

D3 playground with webpack hot reloading & babel for prototyping
JavaScript
3
star
52

cropcircle

Pattern framework for building web sites and applications
HTML
2
star
53

quick-dom-node

Make DOM Nodes in Node quickly and they are also quick
JavaScript
2
star
54

make8bitart

an in-browser canvas tool which is great fun!
JavaScript
2
star
55

diffhtml-snowpack

Extremely simple demo using diffHTML and Snowpack
JavaScript
2
star
56

amd.js

Experimental AMD loader
JavaScript
2
star
57

static-sync

A static server that automatically keeps HTML files in sync using Virtual DOM.
JavaScript
2
star
58

awesome-config

My minimalistic Awesome WM configuration.
Lua
2
star
59

diffhtml-logger

DEPRECATED MOVED TO MAIN REPO
2
star
60

LiveEdit-CSS

JavaScript
2
star
61

amd-test-bed

A project test bed
JavaScript
2
star
62

turbopack-webpack-example

Example of turbopack integration with webpack
JavaScript
1
star
63

amd-next

If we were to have a v2 of the AMD specification what would it look like?
1
star
64

Charcoal_UI_v1.1

MIRROR of Charcoal UI 1.1
Lua
1
star
65

calendar.js

A long lost project
JavaScript
1
star
66

load-as-fake-workers

Loads scripts as fake workers for testing
JavaScript
1
star
67

ayo-node-esm-tests

Testing Node vs Ayo ES Modules
JavaScript
1
star
68

broken-npm-resolution

Works with updates to react-blessed-you (fork for react-blessed)
JavaScript
1
star
69

diffhtml-esm-test-app

Zero build test of diffHTML, Redux, and a simple event emitter
JavaScript
1
star
70

css-loader

CSS Loader.
JavaScript
1
star
71

cribbage

Follow me as I get incrementally updated from College-standards to Modern-standards.
JavaScript
1
star