You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Contents
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
Infinite Virtual Scroll With the Angular CDK
Episode 145 written by Jeff DelaneyHealth Check: This lesson was last reviewed on and tested with these packages:
- Angular v7
- RxJS v6.3
Find an issue? Let's fix it
Source code for Infinite Virtual Scroll With the Angular CDK on Github
The release of Angular v7 gives us access to a new virtual scroll behavior in the Material Component Development Kit (CDK). It provides tools for looping over a lists that only render elements when they are visible in the viewport, preventing lag an janky-ness in the browser. As an added bonus, it exposes a reliable API for building an infinite scroll where new batches of data are retrieved automatically when the user scrolls to the bottom of the list.
Installation
First, make sure you’re updated to Angular v7.0 or later, then add Angular Material to your project.
npm i @angular/cli@latest -g |
Angular CDK Virtual Scroll Basics
Let’s start by reviewing a few important concepts with virtual scroll. First, you declare the cdk-virtual-scroll-viewport
component to provide a context for virtual scrolling. It should have an itemSize
input property defined as the pixel height of each item. The *cdkVirtualFor
is a replacement for *ngFor
that you can use to loop over a list.
<cdk-virtual-scroll-viewport itemSize="100"> |
CSS Requirements
The cdk-virtual-scroll-viewport
must have a height and the items it loops over should also have a fixed height. The component needs this information to calculate when an item should be rendered or removed.
cdk-virtual-scroll-viewport { |
Custom Events
The component emits a custom event whenever the scrolled index changes. This allows you to run code when a specific item is scrolled to.
<cdk-virtual-scroll-viewport itemSize="100" (scrolledIndexChange)="handler($event)"> |
Accessing the Component API
The CdkVirtualScrollComponent
component class contains a suite of API methods that can be called to scroll programmatically or to measure the size of the viewport. You can gain access to these methods by grabbing the virtual scroll component with ViewChild
.
import { Component, ViewChild } from '@angular/core'; |
Building a Realtime Infinite Virtual Scroll
You will need @angular/fire and Firebase installed to follow along with the next section.
Building a realtime infinite scroll is a challenging requirement. We have tackled infinite scroll with Firestore in the past, but opted out of realtime listeners to simplify the code. Today, the CDK makes our life so much easier that we will make the extra effort to make our infinite list respond to realtime updates.
The code below gets fairly complex, so let’s look at the main instructions step-by-step.
- Make a paginated query to Firestore using
ref.orderBy(name).startAt(lastSeen).limit(batch)
. - Map the documents array to an object, where each key is the document ID (this mapping is needed for realtime updates).
- Scan the source observable and merge in new batches.
- Flatten the object values into a single array for looping in the HTML.
Keep in mind, this strategy works well for realtime data changes, but does not automatically reorder the list or remove deleted items. Additional clientside monkey patching will be needed to resolve these limitations.
Full Infinite Scroll Code
import { Component, ViewChild } from '@angular/core'; |
HTML
<ng-container *ngIf="infinite | async as people"> |
The End
The CDK dramatically improves the handling of scroll-able lists in Angular. In this demo, we managed to convert a Firestore Collection into an animated, realtime, infinite, virtual list with less than 100 lines of code. That’s pretty amazing considering how complex a feature like this would be without the help of Angular + Firebase.