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

fix: measureElement handle elements when keys change #801

Merged
merged 1 commit into from
Aug 19, 2024
Merged
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
62 changes: 14 additions & 48 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,38 +615,6 @@ export class Virtualizer<
const measurements = this.measurementsCache.slice(0, min)

for (let i = min; i < count; i++) {
let measureElement = this.measurementsCache[i]?.measureElement

if (!measureElement) {
measureElement = (node: TItemElement | null | undefined) => {
const key = getItemKey(i)
const prevNode = this.elementsCache.get(key)

if (!node) {
if (prevNode) {
this.observer.unobserve(prevNode)
this.elementsCache.delete(key)
}
return
}

if (prevNode !== node) {
if (prevNode) {
this.observer.unobserve(prevNode)
}
this.observer.observe(node)
this.elementsCache.set(key, node)
}

if (node.isConnected) {
this.resizeItem(
i,
this.options.measureElement(node, undefined, this),
)
}
}
}

const key = getItemKey(i)

const furthestMeasurement =
Expand Down Expand Up @@ -677,7 +645,7 @@ export class Virtualizer<
end,
key,
lane,
measureElement,
measureElement: this.measureElement,
}
}

Expand Down Expand Up @@ -750,30 +718,22 @@ export class Virtualizer<
node: TItemElement,
entry: ResizeObserverEntry | undefined,
) => {
const i = this.indexFromElement(node)
const item = this.getMeasurements()[i]
const index = this.indexFromElement(node)
const key = this.options.getItemKey(index)

if (!item || !node.isConnected) {
this.elementsCache.forEach((cached, key) => {
if (cached === node) {
this.observer.unobserve(node)
this.elementsCache.delete(key)
}
})
return
}

const prevNode = this.elementsCache.get(item.key)
const prevNode = this.elementsCache.get(key)

if (prevNode !== node) {
if (prevNode) {
this.observer.unobserve(prevNode)
}
this.observer.observe(node)
this.elementsCache.set(item.key, node)
this.elementsCache.set(key, node)
}

this.resizeItem(i, this.options.measureElement(node, entry, this))
if (node.isConnected) {
this.resizeItem(index, this.options.measureElement(node, entry, this))
}
}

resizeItem = (index: number, size: number) => {
Expand Down Expand Up @@ -809,6 +769,12 @@ export class Virtualizer<

measureElement = (node: TItemElement | null | undefined) => {
if (!node) {
this.elementsCache.forEach((cached, key) => {
if (!cached.isConnected) {
this.observer.unobserve(cached)
this.elementsCache.delete(key)
}
})
return
}

Expand Down