Skip to content

Commit 61cc6bc

Browse files
authored
feat(horizontal virtualized layout): Support overscan property (#1470)
1 parent 55c458d commit 61cc6bc

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

web_ui/packages/ui/src/virtualized-horizontal-grid/horizontal-layout.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ import { Key, Layout, LayoutInfo, Rect, Size } from 'react-aria-components';
66

77
const DEFAULT_GAP = 8;
88
const DEFAULT_SIZE = 100;
9+
const DEFAULT_OVERSCAN = 0;
910

1011
export interface HorizontalLayoutOptions {
1112
gap?: number;
1213
size?: number;
14+
overscan?: number;
1315
}
1416

1517
export class HorizontalLayout extends Layout {
1618
protected gap: number;
1719
protected size: number;
20+
protected overscan: number;
1821

1922
constructor(options: HorizontalLayoutOptions = {}) {
2023
super();
2124
this.gap = options.gap ?? DEFAULT_GAP;
2225
this.size = options.size ?? DEFAULT_SIZE;
26+
this.overscan = options.overscan ?? DEFAULT_OVERSCAN;
2327
}
2428

2529
// Determine which items are visible within the given rectangle.
@@ -30,8 +34,8 @@ export class HorizontalLayout extends Layout {
3034

3135
const sizeWithGap = this.size + this.gap;
3236
const keys = Array.from(this.virtualizer.collection.getKeys());
33-
const startIndex = Math.max(0, Math.floor(rect.x / sizeWithGap));
34-
const endIndex = Math.min(keys.length - 1, Math.ceil(rect.maxX / sizeWithGap));
37+
const startIndex = Math.max(0, Math.floor(rect.x / sizeWithGap) - this.overscan);
38+
const endIndex = Math.min(keys.length - 1, Math.ceil(rect.maxX / sizeWithGap) + this.overscan);
3539
const layoutInfos: LayoutInfo[] = [];
3640

3741
for (let i = startIndex; i <= endIndex; i++) {
@@ -84,5 +88,6 @@ export class HorizontalLayout extends Layout {
8488
update(invalidationContext: InvalidationContext<HorizontalLayoutOptions>): void {
8589
this.gap = invalidationContext?.layoutOptions?.gap ?? this.gap;
8690
this.size = invalidationContext?.layoutOptions?.size ?? this.size;
91+
this.overscan = invalidationContext?.layoutOptions?.overscan ?? this.overscan;
8792
}
8893
}

0 commit comments

Comments
 (0)