VirtualList Demo

by Kye Hohenberger

HTML

<script src="https://npmcdn.com/preact"></script>
VirtualList renders only the visible items in a list. It updates on resize/scroll.

CSS

body { font:16px/1.21 'Helvetica Neue',arial,sans-serif; font-weight:300; }

.list {
	position: absolute;
	top: 20%;
	left: 20%;
	height: 60%;
	width: 60%;
  display: 'flex';
  flex-direction: 'row';
	border: 1px solid #CCC;
	background: #FFF;
	overflow: auto;
	-webkit-overflow-scrolling: touch;
}

.row {
	position: relative;
	height: 30px;
	line-height: 30px;
	padding: 0 20px;
	box-shadow: inset 0 -1px 0 #DDD;
	overflow: hidden;
}

Babel + JSX

const { h, Component, render } = preact;
/** @jsx h */

// static data
const DATA = [];
for (let x=1e3; x--; ) DATA[x] = `Item #${x+1}`;

class Demo extends Component {
	rowHeight = 30;

	renderRow(row, order) {
		return h(
    	'div', 
      { 
      	class: 'row',
        style: { order: order }
      }, 
      row
   	)
	}

	render() {
		return h('div', { class: 'list' },
    	DATA.map(this.renderRow)
    )
	}
}

render(<Demo />, document.body);