|
| 1 | +import React, {useCallback, useImperativeHandle} from 'react'; |
| 2 | +import { |
| 3 | + ScrollView as RNScrollView, |
| 4 | + ScrollViewProps, |
| 5 | + NativeSyntheticEvent, |
| 6 | + NativeScrollEvent, |
| 7 | + LayoutChangeEvent |
| 8 | +} from 'react-native'; |
| 9 | +import Fader, {FaderProps} from '../fader'; |
| 10 | +import useScrollEnabler from '../../hooks/useScrollEnabler'; |
| 11 | +import useScrollReached from '../../hooks/useScrollReached'; |
| 12 | +import {forwardRef, ForwardRefInjectedProps} from '../../commons/new'; |
| 13 | +import {ScrollView as GestureScrollView} from 'react-native-gesture-handler'; |
| 14 | + |
| 15 | +export type FadedScrollViewProps = ScrollViewProps & { |
| 16 | + /** |
| 17 | + * Show a fader at the start of the scroll |
| 18 | + */ |
| 19 | + showStartFader?: boolean; |
| 20 | + /** |
| 21 | + * Additional props for the start fader |
| 22 | + */ |
| 23 | + startFaderProps?: Omit<FaderProps, 'visible' | 'position'>; |
| 24 | + /** |
| 25 | + * Show a fader at the end of the scroll |
| 26 | + */ |
| 27 | + showEndFader?: boolean; |
| 28 | + /** |
| 29 | + * Additional props for the end fader |
| 30 | + */ |
| 31 | + endFaderProps?: Omit<FaderProps, 'visible' | 'position'>; |
| 32 | + /** |
| 33 | + * Use the react-native-gesture-handler version, useful when using react-native-reanimated |
| 34 | + */ |
| 35 | + useGesture?: boolean; |
| 36 | + children?: React.ReactNode | React.ReactNode[]; |
| 37 | +}; |
| 38 | + |
| 39 | +type Props = FadedScrollViewProps & ForwardRefInjectedProps; |
| 40 | +interface Statics { |
| 41 | + scrollTo( |
| 42 | + y?: number | {x?: number | undefined; y?: number | undefined; animated?: boolean | undefined}, |
| 43 | + x?: number, |
| 44 | + animated?: boolean |
| 45 | + ): void; |
| 46 | + isScrollEnabled: () => boolean; |
| 47 | +} |
| 48 | + |
| 49 | +const FadedScrollView = (props: Props) => { |
| 50 | + const { |
| 51 | + children, |
| 52 | + onScroll: propsOnScroll, |
| 53 | + onContentSizeChange: propsOnContentSizeChange, |
| 54 | + onLayout: propsOnLayout, |
| 55 | + horizontal: propsHorizontal, |
| 56 | + showStartFader, |
| 57 | + startFaderProps, |
| 58 | + showEndFader, |
| 59 | + endFaderProps, |
| 60 | + useGesture, |
| 61 | + ...others |
| 62 | + } = props; |
| 63 | + const ScrollView = useGesture ? GestureScrollView : RNScrollView; |
| 64 | + const scrollViewRef = React.createRef<typeof ScrollView>(); |
| 65 | + const horizontal = propsHorizontal ?? false; |
| 66 | + const {onContentSizeChange, onLayout, scrollEnabled} = useScrollEnabler({horizontal}); |
| 67 | + const { |
| 68 | + onScroll: onScrollReached, |
| 69 | + isScrollAtStart, |
| 70 | + isScrollAtEnd |
| 71 | + } = useScrollReached({ |
| 72 | + horizontal, |
| 73 | + threshold: 50 |
| 74 | + }); |
| 75 | + |
| 76 | + const showStart = (showStartFader && scrollEnabled && !isScrollAtStart) ?? false; |
| 77 | + const showEnd = (showEndFader && scrollEnabled && !isScrollAtEnd) ?? false; |
| 78 | + |
| 79 | + const onScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => { |
| 80 | + onScrollReached(event); |
| 81 | + propsOnScroll?.(event); |
| 82 | + }, |
| 83 | + [onScrollReached, propsOnScroll]); |
| 84 | + |
| 85 | + const _onContentSizeChange = useCallback((w: number, h: number) => { |
| 86 | + propsOnContentSizeChange?.(w, h); |
| 87 | + onContentSizeChange?.(w, h); |
| 88 | + }, |
| 89 | + [propsOnContentSizeChange, onContentSizeChange]); |
| 90 | + |
| 91 | + const _onLayout = useCallback((event: LayoutChangeEvent) => { |
| 92 | + propsOnLayout?.(event); |
| 93 | + onLayout?.(event); |
| 94 | + }, |
| 95 | + [propsOnLayout, onLayout]); |
| 96 | + |
| 97 | + const isScrollEnabled = () => { |
| 98 | + return scrollEnabled; |
| 99 | + }; |
| 100 | + |
| 101 | + useImperativeHandle(props.forwardedRef, () => ({ |
| 102 | + // @ts-expect-error |
| 103 | + scrollTo: (...data: any) => scrollViewRef.current?.scrollTo?.(...data), |
| 104 | + isScrollEnabled |
| 105 | + })); |
| 106 | + |
| 107 | + if (children) { |
| 108 | + return ( |
| 109 | + <> |
| 110 | + <ScrollView |
| 111 | + scrollEventThrottle={16} |
| 112 | + decelerationRate={'fast'} |
| 113 | + {...others} |
| 114 | + horizontal={horizontal} |
| 115 | + scrollEnabled={scrollEnabled} |
| 116 | + onContentSizeChange={_onContentSizeChange} |
| 117 | + onLayout={_onLayout} |
| 118 | + onScroll={onScroll} |
| 119 | + ref={scrollViewRef} |
| 120 | + > |
| 121 | + {children} |
| 122 | + </ScrollView> |
| 123 | + <Fader |
| 124 | + visible={showStart} |
| 125 | + position={horizontal ? Fader.position.START : Fader.position.TOP} |
| 126 | + {...startFaderProps} |
| 127 | + /> |
| 128 | + <Fader |
| 129 | + visible={showEnd} |
| 130 | + position={horizontal ? Fader.position.END : Fader.position.BOTTOM} |
| 131 | + {...endFaderProps} |
| 132 | + /> |
| 133 | + </> |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + return null; |
| 138 | +}; |
| 139 | + |
| 140 | +FadedScrollView.displayName = 'IGNORE'; |
| 141 | +// @ts-expect-error |
| 142 | +export default forwardRef<FadedScrollViewProps, Statics>(FadedScrollView); |
0 commit comments