Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Cell Decorators are an easy way to add common hover animations. For example, wra

It's possible to render multiple `DraggableFlatList` components within a single scrollable parent by wrapping one or more `NestableDraggableFlatList` components within an outer `NestableScrollContainer` component.

`NestableScrollContainer` extends the `ScrollView` from `react-native-gesture-handler`, and `NestableDraggableFlatList` extends `DraggableFlatList`, so all available props may be passed into both of them.
`NestableScrollContainer` extends the `ScrollView` from `react-native-gesture-handler`, with `onScrollOffsetChange` as a stand-in for `onScroll`, and `NestableDraggableFlatList` extends `DraggableFlatList`, so all available props may be passed into both of them.

> Note: When using NestableDraggableFlatLists, all React Native warnings about nested list performance will be disabled.

Expand Down
33 changes: 26 additions & 7 deletions src/components/NestableScrollContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from "react";

import { LayoutChangeEvent, ScrollViewProps } from "react-native";
import { ScrollView } from "react-native-gesture-handler";
import Animated, { useAnimatedScrollHandler } from "react-native-reanimated";
import Animated, {
runOnJS,
useAnimatedScrollHandler,
} from "react-native-reanimated";

import {
NestableScrollContainerProvider,
useSafeNestableScrollContainerContext,
Expand All @@ -10,7 +15,13 @@ import { useStableCallback } from "../hooks/useStableCallback";

const AnimatedScrollView = Animated.createAnimatedComponent(ScrollView);

function NestableScrollContainerInner(props: ScrollViewProps) {
type NestableScrollContainerInnerProps = {
onScrollOffsetChange?: (scrollOffset: number) => void;
} & Omit<ScrollViewProps, "onScroll">;

function NestableScrollContainerInner(
props: NestableScrollContainerInnerProps
) {
const {
outerScrollOffset,
containerSize,
Expand All @@ -19,9 +30,14 @@ function NestableScrollContainerInner(props: ScrollViewProps) {
outerScrollEnabled,
} = useSafeNestableScrollContainerContext();

const onScroll = useAnimatedScrollHandler({
onScroll: ({ contentOffset }) => {
outerScrollOffset.value = contentOffset.y;
const onScroll = useStableCallback((scrollOffset: number) => {
props.onScrollOffsetChange?.(scrollOffset);
});

const scrollHandler = useAnimatedScrollHandler({
onScroll: (event) => {
outerScrollOffset.value = event.contentOffset.y;
runOnJS(onScroll)(event.contentOffset.y);
},
});

Expand All @@ -45,13 +61,16 @@ function NestableScrollContainerInner(props: ScrollViewProps) {
scrollEnabled={outerScrollEnabled}
ref={scrollableRef}
scrollEventThrottle={1}
onScroll={onScroll}
onScroll={scrollHandler}
/>
);
}

export const NestableScrollContainer = React.forwardRef(
(props: ScrollViewProps, forwardedRef?: React.ForwardedRef<ScrollView>) => {
(
props: NestableScrollContainerInnerProps,
forwardedRef?: React.ForwardedRef<ScrollView>
) => {
return (
<NestableScrollContainerProvider
forwardedRef={
Expand Down