Skip to content
Open
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
16 changes: 16 additions & 0 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ export interface VirtualizerOptions<
enabled?: boolean
isRtl?: boolean
useAnimationFrameWithResizeObserver?: boolean
keepAliveOnHidden?: boolean
}

export class Virtualizer<
Expand Down Expand Up @@ -447,6 +448,7 @@ export class Virtualizer<
isRtl: false,
useScrollendEvent: false,
useAnimationFrameWithResizeObserver: false,
keepAliveOnHidden: false,
...opts,
}
}
Expand Down Expand Up @@ -793,6 +795,14 @@ export class Virtualizer<
},
)

private isVisible = () => {
if (!this.scrollElement) return false
return (
this.scrollElement instanceof Window ||
(this.scrollElement as HTMLElement).offsetWidth > 0
)
}

calculateRange = memo(
() => [
this.getMeasurements(),
Expand All @@ -801,6 +811,9 @@ export class Virtualizer<
this.options.lanes,
],
(measurements, outerSize, scrollOffset, lanes) => {
if (this.options.keepAliveOnHidden && !this.isVisible()) {
return this.range
}
return (this.range =
measurements.length > 0 && outerSize > 0
? calculateRange({
Expand Down Expand Up @@ -869,6 +882,9 @@ export class Virtualizer<
node: TItemElement,
entry: ResizeObserverEntry | undefined,
) => {
if (this.options.keepAliveOnHidden && !this.isVisible()) {
return
}
const index = this.indexFromElement(node)
const item = this.measurementsCache[index]
if (!item) {
Expand Down