Fix: don't save scrollbarWidth while zero offsetWidth and clientWidth #40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi! We use react-custom-scrollbars-2 in our project and we found a rare bug, so here is the fix.
Context:
We use custom scrollbars in widget which is opened inside an iframe. And it works fine. But we have a case when we open this iframe inside another iframe and our scroll bars are looking like this:
This is happening with Google Chrome on Windows and on Mac (if "Show scroll bars" parameter is set to "Always" in Mac General settings). Safari and FireFox are okay.
Reason:
In this case
margin-right
andmargin-bottom
of the scrollbar are supposed to be -15px (the exact number depends on the platform). But we have 0px instead. Why? Because during first render iterations we havediv.offsetWidth === 0
anddiv.clientWidth === 0
. I'm not sure about the initial reason, but I suppose there are might be some Chrome optimizations. Thisdiv
really exists in DOM with all styles and computed styles also say it is 100x100 px, but if you call its client and offset width by DevTools console, you'll get 0.Solution:
In this abnormal case we just shouldn't set up
scrollbarWidth
to 0 and save in cache immediately, we should wait untildiv.offsetWidth
anddiv.clientWidth
become normal. We tested it and saw that it renders okay after several iterations. Here is the console log of div.offsetWidth and div.clientWidth right after this line:The fix doesn't affect usual behaviour, because normally you have non zero width at the very first iteration.
It would be great to have a new version of the package with the fix. If you have any concerns or better ideas, I will be thankful!