|
| 1 | +import React, {useCallback} from 'react'; |
| 2 | +import {ViewProps, ScrollView, ScrollViewProps, NativeSyntheticEvent, NativeScrollEvent} from 'react-native'; |
| 3 | +import Fader from '../../components/fader'; |
| 4 | +import withScrollEnabler, {WithScrollEnablerProps} from '../../commons/withScrollEnabler'; |
| 5 | +import withScrollReached, {WithScrollReachedProps} from '../../commons/withScrollReached'; |
| 6 | +import forwardRef, {ForwardRefInjectedProps} from '../../commons/forwardRef'; |
| 7 | + |
| 8 | +export type FadedScrollViewProps = ViewProps & ScrollViewProps & { |
| 9 | + children?: React.ReactNode | React.ReactNode[]; |
| 10 | +}; |
| 11 | + |
| 12 | +type ScrollReachedProps = FadedScrollViewProps & WithScrollReachedProps; |
| 13 | +type ScrollEnabledProps = ScrollReachedProps & WithScrollEnablerProps; |
| 14 | +type Props = ScrollEnabledProps & ForwardRefInjectedProps; |
| 15 | + |
| 16 | +const FADER_SIZE = 76; |
| 17 | + |
| 18 | +const FadedScrollView = (props: Props) => { |
| 19 | + const {scrollEnablerProps, scrollReachedProps, children, onScroll: propsOnScroll, ...other} = props; |
| 20 | + const fadeLeft = scrollEnablerProps.scrollEnabled && !scrollReachedProps.isScrollAtStart; |
| 21 | + const fadeRight = scrollEnablerProps.scrollEnabled && !scrollReachedProps.isScrollAtEnd; |
| 22 | + |
| 23 | + const onScroll = useCallback( |
| 24 | + (event: NativeSyntheticEvent<NativeScrollEvent>) => { |
| 25 | + scrollReachedProps.onScroll(event); |
| 26 | + propsOnScroll?.(event); |
| 27 | + }, |
| 28 | + [scrollReachedProps.onScroll, propsOnScroll] |
| 29 | + ); |
| 30 | + |
| 31 | + if (children) { |
| 32 | + return ( |
| 33 | + <> |
| 34 | + <ScrollView |
| 35 | + horizontal |
| 36 | + showsHorizontalScrollIndicator={false} |
| 37 | + scrollEventThrottle={16} |
| 38 | + decelerationRate={'fast'} |
| 39 | + {...other} |
| 40 | + scrollEnabled={scrollEnablerProps.scrollEnabled} |
| 41 | + onContentSizeChange={scrollEnablerProps.onContentSizeChange} |
| 42 | + onLayout={scrollEnablerProps.onLayout} |
| 43 | + onScroll={onScroll} |
| 44 | + ref={props.forwardedRef} |
| 45 | + > |
| 46 | + {children} |
| 47 | + </ScrollView> |
| 48 | + <Fader visible={fadeLeft} position={Fader.position.LEFT} size={FADER_SIZE} /> |
| 49 | + <Fader visible={fadeRight} position={Fader.position.RIGHT} size={FADER_SIZE} /> |
| 50 | + </> |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | +}; |
| 56 | + |
| 57 | +// TODO: fix withScrollEnabler props (add <>) |
| 58 | +export default withScrollReached<FadedScrollViewProps>(withScrollEnabler(forwardRef<Props>(FadedScrollView)), { |
| 59 | + horizontal: true, |
| 60 | + threshold: 50 |
| 61 | +}); |
0 commit comments