[@mantine/hooks]: fix unstable pinning of item by useHeadroom
on mobile devices
#5793
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.
fixes #4563
In order to fix the issue, I actually had to change the code logic for checking pin status. The
isPinned
function was previously used every time thescrollPosition
returned byuseWindowScroll
changed. This is useless as causes the issue as when an address bar hides on scroll in mobile browser, aresize
event is fired by the browser, but thescrollPosition
hasn't actually changed.In the above code, both
scrollRef.current
andscrollPosition
hold the same value and don't offer a clear comparison.This line of code is useless because the next condition call after it which calls
isFixed
withscrollPosition
and thefixedAt
value is the only true logic for this hook's implementation.I also changed the logic for calling
onPin
andonRelease
callbacks. The ref which was previously used to hold thescrollPosition
was actually used for wrong comparison. TheonPin
callback in my opinion should get called only when the pinning element has switched state from pinned to unpinned or vice versa, when the scrollPosition changes. In the current code,onPin
is getting called so many times as user scrolls, this doesn't make sense. So, I have merged the logic of two pastuseEffects
into a single one. I store the pinning status in a ref to prevent extra re-render.I have marked this PR as a draft to make sure you inspect those changes and I hope this explanation clarifies them for you.