VirtualList Demo

by tnhu

HTML

<script src="https://npmcdn.com/preact"></script>
<script src="https://npmcdn.com/preact-virtual-list"></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%;
	border: 1px solid #CCC;
	background: #FFF;
	overflow: auto;
	-webkit-overflow-scrolling: touch;
}

.row {
	position: relative;
	display: block;
	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=1e6; x--; ) DATA[x] = `Item #${x+1}`;

class Demo extends Component {
	rowHeight = 30;

	renderRow(row) {
		return <div class="row">{row}</div>;
	}

	render() {
		return (
			<VirtualList
				sync
				class="list"
				data={DATA}
				rowHeight={this.rowHeight}
				renderRow={this.renderRow}
			/>
		);
	}
}

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