Skip to content

Commit

Permalink
feat: expose virtualizer.resizeItem & update doc (#534)
Browse files Browse the repository at this point in the history
Co-authored-by: Ele4nor <>
  • Loading branch information
Ele4nor committed Aug 29, 2023
1 parent e91941c commit 4392822
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/api/virtualizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,18 @@ Measures the element using your configured `measureElement` virtualizer option.
By default the `measureElement` virtualizer option is configured to measure elements with `getBoundingClientRect()`.
### `resizeItem`
```tsx
resizeItem: (index: number, size: number) => void
```
Change the virtualized item's size manually. Use this function to manually set the size calculated for this item. Useful in occations when using some custom morphing transition and you know the morphed item's size beforehand.
You can also use this method with a throttled ResizeObserver instead of `Virtualizer.measureElement` to reduce re-rendering.
> 鈿狅笍 Please be aware that manually changing the size of an item when using `Virtualizer.measureElement` to monitor that item, will result in unpredictable behaviour as the `Virtualizer.measureElement` is also changing the size. However you can use one of resizeItem or measureElement in the same virtualizer instance but on different item indexes.
### `lanes`
```tsx
Expand Down
13 changes: 11 additions & 2 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,18 @@ export class Virtualizer<

const measuredItemSize = this.options.measureElement(node, entry, this)

this.resizeItem(index, measuredItemSize)
}

resizeItem = (index: number, size: number) => {
const item = this.measurementsCache[index]
if (!item) {
return
}

const itemSize = this.itemSizeCache.get(item.key) ?? item.size

const delta = measuredItemSize - itemSize
const delta = size - itemSize

if (delta !== 0) {
if (item.start < this.scrollOffset) {
Expand All @@ -667,7 +676,7 @@ export class Virtualizer<
this.pendingMeasuredCacheIndexes.push(index)

this.itemSizeCache = new Map(
this.itemSizeCache.set(item.key, measuredItemSize),
this.itemSizeCache.set(item.key, size),
)

this.notify()
Expand Down

0 comments on commit 4392822

Please sign in to comment.