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

feat: add useAnimationFrameWithResizeObserver option #923

Merged
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
10 changes: 10 additions & 0 deletions docs/api/virtualizer.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ isRtl: boolean

Whether to invert horizontal scrolling to support right-to-left language locales.

### `useAnimationFrameWithResizeObserver`

```tsx
useAnimationFrameWithResizeObserver: boolean
```

This option enables wrapping ResizeObserver measurements in requestAnimationFrame for smoother updates and reduced layout thrashing. The default value is `false`.

It helps prevent the "ResizeObserver loop completed with undelivered notifications" error by ensuring that measurements align with the rendering cycle. This can improve performance and reduce UI jitter, especially when resizing elements dynamically. However, since ResizeObserver already runs asynchronously, adding requestAnimationFrame may introduce a slight delay in measurements, which could be noticeable in some cases. If resizing operations are lightweight and do not cause reflows, enabling this option may not provide significant benefits.

## Virtualizer Instance

The following properties and methods are available on the virtualizer instance:
Expand Down
29 changes: 21 additions & 8 deletions packages/virtual-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ export const observeElementRect = <T extends Element>(
}

const observer = new targetWindow.ResizeObserver((entries) => {
const entry = entries[0]
if (entry?.borderBoxSize) {
const box = entry.borderBoxSize[0]
if (box) {
handler({ width: box.inlineSize, height: box.blockSize })
return
const run = () => {
const entry = entries[0]
if (entry?.borderBoxSize) {
const box = entry.borderBoxSize[0]
if (box) {
handler({ width: box.inlineSize, height: box.blockSize })
return
}
}
handler(element.getBoundingClientRect())
}
handler(element.getBoundingClientRect())

instance.options.useAnimationFrameWithResizeObserver
? requestAnimationFrame(run)
: run()
})

observer.observe(element, { box: 'border-box' })
Expand Down Expand Up @@ -337,6 +343,7 @@ export interface VirtualizerOptions<
useScrollendEvent?: boolean
enabled?: boolean
isRtl?: boolean
useAnimationFrameWithResizeObserver?: boolean
}

export class Virtualizer<
Expand Down Expand Up @@ -378,7 +385,12 @@ export class Virtualizer<

return (_ro = new this.targetWindow.ResizeObserver((entries) => {
entries.forEach((entry) => {
this._measureElement(entry.target as TItemElement, entry)
const run = () => {
this._measureElement(entry.target as TItemElement, entry)
}
this.options.useAnimationFrameWithResizeObserver
? requestAnimationFrame(run)
: run()
})
}))
}
Expand Down Expand Up @@ -427,6 +439,7 @@ export class Virtualizer<
enabled: true,
isRtl: false,
useScrollendEvent: true,
useAnimationFrameWithResizeObserver: false,
...opts,
}
}
Expand Down