-
Notifications
You must be signed in to change notification settings - Fork 92
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
Update client width to use getBoundingClientRect #573
Update client width to use getBoundingClientRect #573
Conversation
lib/KListWithOverflow.vue
Outdated
if (!element) { | ||
return 0; | ||
} | ||
return element.getBoundingClientRect().width; |
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.
One thing to flag, getBoundingClientRect forces a reflow, so this can cause performance issues. More info here: https://toruskit.com/blog/how-to-get-element-bounds-without-reflow/
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.
Thanks Richard, I will batch all getWidth calls before changing anything in the DOM
Hi @rtibbles I have batched all calls to getBoundingClientRect before setting the items visibilities. Hope this helps to optimize the performance and avoids reflows. |
It will still be quite a few, as we are calling it for each element in the list, it seems? I wonder if it might be better to take the Intersection Observer approach here, and require that Kolibri integrate the Intersection Observer polyfill for unsupported browsers. I think we can merge this as is, but maybe we file a follow up issue for a potential performance improvement. |
Oh I had read here that if there is no changes to the DOM between calls to properties that needs the layout to be recalculated, then the layout is no recalculated as is already up to date. What causes reflow is when we invalidate the layout by changing the DOM nodes, so I think batching all these calls at the beginning would do the work. This is a similar approach they do in their example with multiple different DOM nodes in a list. |
Oh, great - thanks for doing the due dilligence here! No need for the follow up, which would add some complexity. |
Description
This PR replaces the use of
clientWidth
to usegetBoundingClientRect().width
to have a more accurate measurement of the width of the KListWithOverflow items. It also recalculates the width of the moreButton element on each overflow calculation to make sure this value hasnt changed.Before/after screenshots
In very specific cases, using
clientWith
can lead to cases like this due to the lack of decimals:Now this is fixed:
Changelog
Testing checklist
Reviewer guidance
After review
CHANGELOG.md
Comments