import { createContext, useContext } from "react"; import { Dimensions, View } from "react-native"; import { Gesture, GestureDetector } from "react-native-gesture-handler"; import Animated, { useSharedValue, useAnimatedScrollHandler, useAnimatedStyle, interpolate, Extrapolation, } from "react-native-reanimated"; import { cn } from "@turbostarter/ui"; import type { FlatList } from "react-native"; import type { ViewProps } from "react-native"; import type { SharedValue, ScrollHandlerProcessed, FlatListPropsWithLayout, } from "react-native-reanimated"; interface SliderContextType { threshold: number; scrollX: SharedValue | null; onScroll: ScrollHandlerProcessed>; } const SliderContext = createContext({ threshold: Dimensions.get("window").width, scrollX: null, onScroll: () => null, }); const Slider = ({ className, threshold = Dimensions.get("window").width, ...props }: ViewProps & { threshold?: number }) => { const scrollX = useSharedValue(0); const onScroll = useAnimatedScrollHandler({ onScroll: ({ contentOffset: { x } }) => { scrollX.value = x; }, }); return ( ); }; const SliderList = ( props: FlatListPropsWithLayout & { ref?: React.ForwardedRef; }, ) => { const { onScroll, threshold } = useContext(SliderContext); const native = Gesture.Native(); return ( ); }; const SliderListItem = ({ index, style, ...props }: ViewProps & { index: number }) => { const { scrollX, threshold } = useContext(SliderContext); const opacity = useAnimatedStyle(() => ({ opacity: interpolate( scrollX?.value ?? 0, [(index - 1) * threshold, index * threshold, (index + 1) * threshold], [0, 1, 0], Extrapolation.CLAMP, ), })); return ; }; const SliderPaginationDots = ({ className, ...props }: ViewProps) => { return ; }; const SliderPaginationDot = ({ className, index = 0, ...props }: ViewProps & { index?: number }) => { const { scrollX, threshold } = useContext(SliderContext); const animatedDot = useAnimatedStyle(() => ({ opacity: interpolate( scrollX?.value ?? 0, [(index - 1) * threshold, index * threshold, (index + 1) * threshold], [0.7, 1, 0.7], ), width: interpolate( scrollX?.value ?? 0, [(index - 1) * threshold, index * threshold, (index + 1) * threshold], [11, 28, 11], Extrapolation.CLAMP, ), })); return ( ); }; export { Slider, SliderList, SliderListItem, SliderPaginationDots, SliderPaginationDot, };