UNPKG

react-native-reanimated

Version:

More powerful alternative to Animated library for React Native.

25 lines (22 loc) 1.09 kB
import { useEffect, useRef } from 'react'; // TODO: fixme? import { findNodeHandle } from 'react-native'; import { useEvent } from './utils'; import { useSharedValue } from './useSharedValue'; const scrollEventNames = ['onScroll', 'onScrollBeginDrag', 'onScrollEndDrag', 'onMomentumScrollBegin', 'onMomentumScrollEnd']; export function useScrollViewOffset(aref, initialRef) { const offsetRef = useRef(initialRef !== undefined ? initialRef : useSharedValue(0)); const event = useEvent(event => { 'worklet'; offsetRef.current.value = event.contentOffset.x === 0 ? event.contentOffset.y : event.contentOffset.x; }, scrollEventNames); useEffect(() => { var _event$current; const viewTag = findNodeHandle(aref.current); // @ts-ignore TODO TYPESCRIPT This happens because of // how we had to type `useEvent` to get rid of .d.ts file. (_event$current = event.current) === null || _event$current === void 0 ? void 0 : _event$current.registerForEvents(viewTag); }, [aref.current]); return offsetRef.current; } //# sourceMappingURL=useScrollViewOffset.js.map