-
-
Notifications
You must be signed in to change notification settings - Fork 313
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
Observe element for size changes with ResizeObserver #405
Conversation
109fc06
to
b50ee10
Compare
9c444d4
to
d99d6e6
Compare
6ec5b8d
to
7cbc5cd
Compare
As note, for example having two lists and keeping only one visible at the time, other hidden by display none. In this context we would like to skip updating sizes for the hidden one as ResizeObserver will be called on display change. One solution could be, if virtualizer would expose export const useVirtualizer2 = <TScrollElement extends Element, TItemElement extends HTMLElement>({
updateSize = true,
...options
}: VirtualizerOptions<TScrollElement, TItemElement>) => {
const virtualizer = useVirtualizer({
...options,
measureElement: (element: TItemElement, instance: Virtualizer<TScrollElement, TItemElement>) => {
const getSize = () => measureElement(element, instance)
if (updateSize) {
return getSize()
} else {
const item = instance.measurementsCache[instance.indexFromElement(element)]
return item ? item.size : getSize()
}
},
})
return virtualizer
} |
0f8e5aa
to
48cfbc2
Compare
Thank you, this is amazing! I can delete so much logic to trigger resize manually |
This was due to a bug in react-virtual: TanStack/virtual#391 TanStack/virtual#405 Upgrade to v3 solves the issue
@piecyk this change is nice overall, but makes a specific use case a bit more complicated. Previously we could ignore the API of measureElement and provide our own. For instance This is useful if you want to measure height in a custom way, and the element instance alone doesn't provide enough information to compute the height. I'm curious if you would be open to PRs implementing some sort of custom measure element function. Either extending the current API to allow for user provided options |
@lukesmurray hi, you can still provide |
ahh gotcha that makes sense thank you @piecyk. |
Was looking on our options how to add observing element for size changes using ResizeObserver fix #376. What if we would change a bit the api, end expose
measureElement
on top level, not on the item. This would allow use to memo few of them for example in grid and fix #391Then we nee to bound
measureElement
with index, we can do it viaattribute
by addingdata-index={virtualRow.index}
.