Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(cdk/scrolling): Docs for new virtual scroll features added in #24394 #25043

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/cdk/scrolling/scrolling.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,21 @@ to the list as the user scrolls without removing rendered views. The `appendOnly
views that are already rendered persist in the DOM after they scroll out of view.

<!-- example(cdk-virtual-scroll-append-only) -->

### Separate viewport and scrolling element
The virtual scroll viewport itself acts as the scrolling element by default. However, there may be
some cases where you want to have the viewport scroll one of its parent elements. For example,
if you want to have some non-virtualized content that the user can scroll through before or after
the virtualized content.

To configure a `cdk-vritual-scroll-viewport` to use one of its parent elements as the scrolling
element, apply `cdkVirtualScrollingElement` to the scrolling parent element.

<!-- example(cdk-virtual-scroll-parent-scrolling) -->

Another common scenario is using the window itself as the scrolling element. This often a better
user experience on mobile devices, as it allows the browser chrome to scroll away. To use the
window as the scrolling element, add the `scrollWindow` attribute to the
`cdk-virtual-scroll-viewport`.

<!-- example(cdk-virtual-scroll-window-scrolling) -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.example-viewport {
flex: 1;
width: 200px;
min-height: 200px;
border: 1px solid black;
}

.example-item {
height: 50px;
}

.example-header,
.example-footer {
height: 100px;
background: lightgray;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class="example-viewport" cdkVirtualScrollingElement>
<div class="example-header">Content before</div>
<cdk-virtual-scroll-viewport itemSize="50">
<div *cdkVirtualFor="let item of items" class="example-item">{{item}}</div>
</cdk-virtual-scroll-viewport>
<div class="example-footer">Content after</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';

/** @title Virtual scrolling viewport parent element */
@Component({
selector: 'cdk-virtual-scroll-parent-scrolling-example',
styleUrls: ['cdk-virtual-scroll-parent-scrolling-example.css'],
templateUrl: 'cdk-virtual-scroll-parent-scrolling-example.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CdkVirtualScrollParentScrollingExample {
items = Array.from({length: 100000}).map((_, i) => `Item #${i}`);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.example-item {
height: 50px;
}

.example-header,
.example-footer {
height: 100px;
background: lightgray;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<ng-container *ngIf="shouldRun">
<div class="example-header">Content before</div>
<cdk-virtual-scroll-viewport scrollWindow itemSize="50">
<div *cdkVirtualFor="let item of items" class="example-item">{{item}}</div>
</cdk-virtual-scroll-viewport>
<div class="example-footer">Content after</div>
</ng-container>

<div *ngIf="!shouldRun">Please open on StackBlitz to see result</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {ChangeDetectionStrategy, Component, Input} from '@angular/core';

/** @title Virtual scrolling window */
@Component({
selector: 'cdk-virtual-scroll-window-scrolling-example',
styleUrls: ['cdk-virtual-scroll-window-scrolling-example.css'],
templateUrl: 'cdk-virtual-scroll-window-scrolling-example.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CdkVirtualScrollWindowScrollingExample {
@Input() shouldRun = /(^|.)(stackblitz|webcontainer).(io|com)$/.test(window.location.host);

items = Array.from({length: 100000}).map((_, i) => `Item #${i}`);
}
9 changes: 8 additions & 1 deletion src/components-examples/cdk/scrolling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {CdkVirtualScrollDlExample} from './cdk-virtual-scroll-dl/cdk-virtual-scr
import {CdkVirtualScrollFixedBufferExample} from './cdk-virtual-scroll-fixed-buffer/cdk-virtual-scroll-fixed-buffer-example';
import {CdkVirtualScrollHorizontalExample} from './cdk-virtual-scroll-horizontal/cdk-virtual-scroll-horizontal-example';
import {CdkVirtualScrollOverviewExample} from './cdk-virtual-scroll-overview/cdk-virtual-scroll-overview-example';
import {CdkVirtualScrollParentScrollingExample} from './cdk-virtual-scroll-parent-scrolling/cdk-virtual-scroll-parent-scrolling-example';
import {CdkVirtualScrollTemplateCacheExample} from './cdk-virtual-scroll-template-cache/cdk-virtual-scroll-template-cache-example';
import {CdkVirtualScrollWindowScrollingExample} from './cdk-virtual-scroll-window-scrolling/cdk-virtual-scroll-window-scrolling-example';
import {CommonModule} from '@angular/common';

export {
CdkVirtualScrollAppendOnlyExample,
Expand All @@ -20,6 +23,8 @@ export {
CdkVirtualScrollHorizontalExample,
CdkVirtualScrollOverviewExample,
CdkVirtualScrollTemplateCacheExample,
CdkVirtualScrollParentScrollingExample,
CdkVirtualScrollWindowScrollingExample,
};

const EXAMPLES = [
Expand All @@ -32,10 +37,12 @@ const EXAMPLES = [
CdkVirtualScrollHorizontalExample,
CdkVirtualScrollOverviewExample,
CdkVirtualScrollTemplateCacheExample,
CdkVirtualScrollParentScrollingExample,
CdkVirtualScrollWindowScrollingExample,
];

@NgModule({
imports: [ScrollingModule],
imports: [CommonModule, ScrollingModule],
declarations: EXAMPLES,
exports: EXAMPLES,
})
Expand Down
1 change: 1 addition & 0 deletions src/dev-app/virtual-scroll/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ng_module(
deps = [
"//src/cdk-experimental/scrolling",
"//src/cdk/scrolling",
"//src/components-examples/cdk/scrolling",
"//src/material/button",
"//src/material/form-field",
"//src/material/input",
Expand Down
28 changes: 7 additions & 21 deletions src/dev-app/virtual-scroll/virtual-scroll-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,10 @@ <h2>Append only</h2>
</div>
</cdk-virtual-scroll-viewport>

<h2>Custom virtual scroller</h2>

<div class="demo-viewport" cdkVirtualScrollingElement>
<p>Content before virtual scrolling items</p>
<cdk-virtual-scroll-viewport itemSize="50">
<div *cdkVirtualFor="let size of fixedSizeData; let i = index" class="demo-item"
[style.height.px]="size">
Item #{{i}} - ({{size}}px)
</div>
</cdk-virtual-scroll-viewport>
<p>Content after virtual scrolling items</p>
</div>

<h2>Window virtual scroller</h2>

<cdk-virtual-scroll-viewport scrollWindow itemSize="50">
<div *cdkVirtualFor="let size of fixedSizeData; let i = index" class="demo-item"
[style.height.px]="size">
Item #{{i}} - ({{size}}px)
</div>
</cdk-virtual-scroll-viewport>
<h2>Virtual scroller with scrolling parent</h2>
<cdk-virtual-scroll-parent-scrolling-example class="demo-resize-example">
</cdk-virtual-scroll-parent-scrolling-example>

<h2>Virtual scroller with scrolling window</h2>
<cdk-virtual-scroll-window-scrolling-example [shouldRun]="true">
</cdk-virtual-scroll-window-scrolling-example>
11 changes: 11 additions & 0 deletions src/dev-app/virtual-scroll/virtual-scroll-demo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@
.demo-td {
border: 1px solid gray;
}

.demo-resize-example {
display: flex;
width: 500px;
height: 500px;
}

cdk-virtual-scroll-window-scrolling-example {
display: block;
width: 500px;
}
2 changes: 2 additions & 0 deletions src/dev-app/virtual-scroll/virtual-scroll-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {MatFormFieldModule} from '@angular/material/form-field';
import {MatInputModule} from '@angular/material/input';
import {MatSelectModule} from '@angular/material/select';
import {BehaviorSubject} from 'rxjs';
import {CdkScrollingExamplesModule} from '@angular/components-examples/cdk/scrolling';

type State = {
name: string;
Expand All @@ -38,6 +39,7 @@ type State = {
MatInputModule,
MatSelectModule,
ScrollingModule,
CdkScrollingExamplesModule,
],
})
export class VirtualScrollDemo implements OnDestroy {
Expand Down