Skip to content
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 web useScrollViewOffset on wrapped scrolls #5861

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/reanimated2/hook/useScrollViewOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function useScrollViewOffsetWeb(

const eventHandler = useCallback(() => {
'worklet';
const element = animatedRef.current as unknown as HTMLElement;
const element = getWebScrollableElement(animatedRef.current);
// scrollLeft is the X axis scrolled offset, works properly also with RTL layout
offset.value =
element.scrollLeft === 0 ? element.scrollTop : element.scrollLeft;
Expand All @@ -45,14 +45,14 @@ function useScrollViewOffsetWeb(
useEffect(() => {
// We need to make sure that listener for old animatedRef value is removed
if (scrollRef.current !== null) {
(scrollRef.current as unknown as HTMLElement).removeEventListener(
getWebScrollableElement(scrollRef.current).removeEventListener(
'scroll',
eventHandler
);
}
scrollRef.current = animatedRef.current;

const element = animatedRef.current as unknown as HTMLElement;
const element = getWebScrollableElement(animatedRef.current);
element.addEventListener('scroll', eventHandler);
return () => {
element.removeEventListener('scroll', eventHandler);
Expand All @@ -66,14 +66,6 @@ function useScrollViewOffsetWeb(
return offset;
}

const scrollNativeEventNames = [
'onScroll',
'onScrollBeginDrag',
'onScrollEndDrag',
'onMomentumScrollBegin',
'onMomentumScrollEnd',
];

function useScrollViewOffsetNative(
animatedRef: AnimatedRef<AnimatedScrollView>,
providedOffset?: SharedValue<number>
Expand Down Expand Up @@ -115,3 +107,20 @@ function useScrollViewOffsetNative(

return offset;
}

function getWebScrollableElement(
scrollComponent: AnimatedScrollView | null
): HTMLElement {
return (
(scrollComponent?.getScrollableNode() as unknown as HTMLElement) ??
scrollComponent
);
}

const scrollNativeEventNames = [
'onScroll',
'onScrollBeginDrag',
'onScrollEndDrag',
'onMomentumScrollBegin',
'onMomentumScrollEnd',
];