|
| 1 | +import React, { FunctionComponent, MutableRefObject, useEffect, useRef, useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { View, Animated, ViewProps } from 'react-native'; |
| 4 | + |
| 5 | +interface Props extends ViewProps { |
| 6 | + onBeginHidden: Function; |
| 7 | + onHide: Function; |
| 8 | + onBeginDisplayed: Function; |
| 9 | + onDisplay: Function; |
| 10 | + onTouchTop: Function; |
| 11 | + onTouchBottom: Function; |
| 12 | + bottomOffset?: number; |
| 13 | + topOffset?: number; |
| 14 | +} |
| 15 | + |
| 16 | +type Context = { |
| 17 | + scrollPageY?: number; |
| 18 | + scrollY: Animated.Value; |
| 19 | +}; |
| 20 | + |
| 21 | +const TriggeringView: FunctionComponent<Props> = ({ |
| 22 | + topOffset = 0, |
| 23 | + bottomOffset = 0, |
| 24 | + onDisplay, |
| 25 | + onBeginDisplayed, |
| 26 | + onHide, |
| 27 | + onBeginHidden, |
| 28 | + onTouchBottom, |
| 29 | + onTouchTop, |
| 30 | + onLayout, |
| 31 | + children, |
| 32 | + ...viewProps |
| 33 | +}) => { |
| 34 | + const [initialPageY, setInitialPageY] = useState(0); |
| 35 | + const ref = useRef<MutableRefObject<View>>(null).current; |
| 36 | + const [touched, setTouched] = useState(false); |
| 37 | + const [hidden, setHidden] = useState(false); |
| 38 | + const [context, setContext] = useState<Context>({ |
| 39 | + scrollPageY: 0, |
| 40 | + scrollY: new Animated.Value(0), |
| 41 | + }); |
| 42 | + |
| 43 | + const [height, setHeight] = useState(0); |
| 44 | + useEffect(() => { |
| 45 | + if (!context.scrollY) { |
| 46 | + return; |
| 47 | + } |
| 48 | + const listenerId = context.scrollY.addListener(onScroll); |
| 49 | + |
| 50 | + return () => { |
| 51 | + context.scrollY.removeListener(listenerId); |
| 52 | + }; |
| 53 | + }, []); |
| 54 | + |
| 55 | + const handleOnLayout = (e: any) => { |
| 56 | + if (onLayout) { |
| 57 | + onLayout(e); |
| 58 | + } |
| 59 | + if (!ref) { |
| 60 | + return; |
| 61 | + } |
| 62 | + const layout = e.nativeEvent.layout; |
| 63 | + setHeight(layout.height); |
| 64 | + |
| 65 | + ref.current.measure((_x, _y, _width, _height, _ageX, pageY) => { |
| 66 | + setInitialPageY(pageY); |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + const onScroll = (event: any) => { |
| 71 | + if (!context.scrollPageY) { |
| 72 | + return; |
| 73 | + } |
| 74 | + const pageY = initialPageY - event.value; |
| 75 | + triggerEvents(context.scrollPageY, pageY, pageY + height); |
| 76 | + }; |
| 77 | + |
| 78 | + const triggerEvents = (value: number, top: number, bottom: number) => { |
| 79 | + if (!touched && value >= top + topOffset) { |
| 80 | + setTouched(true); |
| 81 | + onBeginHidden(); |
| 82 | + onTouchTop(true); |
| 83 | + } else if (touched && value < top + topOffset) { |
| 84 | + setTouched(false); |
| 85 | + |
| 86 | + onDisplay(); |
| 87 | + onTouchTop(false); |
| 88 | + } |
| 89 | + |
| 90 | + if (!hidden && value >= bottom + bottomOffset) { |
| 91 | + setHidden(true); |
| 92 | + onHide(); |
| 93 | + onTouchBottom(true); |
| 94 | + } else if (hidden && value < bottom + bottomOffset) { |
| 95 | + setHidden(false); |
| 96 | + onBeginDisplayed(); |
| 97 | + onTouchBottom(false); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + return ( |
| 102 | + <View |
| 103 | + ref={ref} |
| 104 | + collapsable={false} |
| 105 | + {...viewProps} |
| 106 | + onLayout={handleOnLayout} |
| 107 | + style={{ backgroundColor: 'red' }} |
| 108 | + > |
| 109 | + {children} |
| 110 | + </View> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +TriggeringView.contextTypes = { |
| 115 | + scrollY: PropTypes.instanceOf(Animated.Value), |
| 116 | + scrollPageY: PropTypes.number, |
| 117 | +}; |
| 118 | + |
| 119 | +export default TriggeringView; |
0 commit comments