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(useInfiniteScroll): improve visibility check #3212
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem still exists.
@@ -61,7 +61,7 @@ export function useInfiniteScroll( | |||
state.measure() | |||
|
|||
const el = toValue(element) as HTMLElement | |||
if (!el || !el.offsetParent) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails when el
is a document
instance.
We need to check if el
is a instance of HTMLDocument
first, instead of assuming its an HTMLElement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discovered that although the approach of determining the element offsetParent solves the issue of an infinite loop when v-show is false #3143, it also results in the load more functionality not working when v-show is switched to true. The solution I came up with is to use MutationObserver to listen for changes and trigger the load more. do you have any better suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to review the code more, but I think the issue in #3143 needs to be solved in a different way.
The changes that were made by @antfu coerces the element
to be an HTMLElement
:
const el = toValue(element) as HTMLElement
if (!el || !el.offsetParent)
return
This is not correct because the function definition itself tells us that element
could take 4 different shapes:
export function useInfiniteScroll(
element: MaybeRefOrGetter<HTMLElement | SVGElement | Window | Document | null | undefined>,
...
)
coincidentally only HTMLElemewnt
has an offsetParent
Pinging @antfu for feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not very familiar with this, but I guess we could do !el || (('offsetParent' in el) && !el.offsetParent)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes can, but it will have another problem that load more callback will not trigger when the v-show from false to true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My recommendation is to re-open that issue and revert the fix that was made because It had caused a regression. Then we can move the discussion on how to fix it there or another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@genu Any update on this? :)
If I understand correctly, you just need a PR that reverts the offsetParent check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR was updated and I reviewed again. In my testing, everything looks good now.
…ent to check if element is visible
hi genu , I've switched to using the IntersectionObserver to check if the element is visible as a fix for this issue. I've also added some tests. please review them and let me know if you have any suggestions for improvement. thanks |
Hope this fix will live soon 🫂 |
Thanks @erikkkwu I tested the PR on my local and it seems to be good now -- particularly the handling of Should be all good now 🎉 💯 for the extra tests! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
sorry, I accidentally hit the review request.😱 |
Before submitting the PR, please make sure you do the following
fixes #123
).Description
adjust element offsetParent check to fix useInfiniteScroll broken on v10.2.1 #3201
Additional context
🤖 Generated by Copilot at 0181645
Fixed a bug in
useInfiniteScroll
that caused excessive data loading for some elements. Changed the hidden element detection logic to useel.offsetParent === null
instead of!el.offsetParent
.🤖 Generated by Copilot at 0181645
el.offsetParent === null
instead of!el.offsetParent
(link). This prevents unnecessary loading of data when the element is not hidden but has a falsyoffsetParent
, such as when the element is fixed or the offset parent is the root element. This change affects the filepackages/core/useInfiniteScroll/index.ts
.