Skip to content

Commit

Permalink
feat: added function innerRef
Browse files Browse the repository at this point in the history
  • Loading branch information
le-ar committed May 27, 2024
1 parent a107323 commit 98a340e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/components/AutoScrollScrollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export interface ScrollAnimInfo {

export function AutoScrollScrollView(
props: Omit<ComponentProps<Animated.ScrollView>, 'ref'> & {
innerRef?: AnimatedRef<Animated.ScrollView>;
innerRef?: ((ref: Animated.ScrollView) => void) | AnimatedRef<Animated.ScrollView>;
manualActivate?: boolean;
manualScrollBy?: AutoScrollBy<number>;
}
) {
const {
innerRef,
innerRef: innerRefProps,
horizontal: horizontalProps,
manualActivate,
manualScrollBy,
Expand All @@ -62,6 +62,8 @@ export function AutoScrollScrollView(
const scrollAnimInfo = useSharedValue<ScrollAnimInfo | null>(null);

const animatedRef = useAnimatedRef<Animated.ScrollView>();
const innerRef = typeof innerRefProps === 'function' && 'current' in innerRefProps ? innerRefProps : void 0
const innerRefFn = typeof innerRefProps === 'function' && !('current' in innerRefProps) ? innerRefProps : void 0
const scrollRef = innerRef ?? animatedRef;
const scrollHandler = useScrollViewOffset(scrollRef);
const scrollSize = useSharedValue(0);
Expand All @@ -70,6 +72,11 @@ export function AutoScrollScrollView(
height: 0,
});

const onRef = useCallback((ref: Animated.ScrollView) => {
innerRefFn?.(ref)
scrollRef.current = ref
}, [innerRefFn, scrollRef])

const needScroll = useCallback<AutoScrollHandler['needScroll']>(
(scrollBy) => {
'worklet';
Expand Down Expand Up @@ -258,7 +265,7 @@ export function AutoScrollScrollView(
<AutoScrollContext.Provider value={value}>
<Animated.ScrollView
{...otherProps}
ref={scrollRef}
ref={onRef}
horizontal={horizontalProps}
onLayout={({ nativeEvent }) => {
scrollSize.value = nativeEvent.layout.height;
Expand Down

0 comments on commit 98a340e

Please sign in to comment.