@@ -14,13 +14,19 @@ import {
1414} from 'react-native' ;
1515import Animated , {
1616 measure ,
17+ runOnJS ,
1718 runOnUI ,
1819 useAnimatedRef ,
1920 useDerivedValue ,
2021 useScrollViewOffset ,
2122 useSharedValue ,
2223} from 'react-native-reanimated' ;
2324import { AnimatedContext } from '../context/AnimatedContext' ;
25+ import { logger } from '../utils/logger' ;
26+
27+ const log = ( ...args : Parameters < typeof console . log > ) => {
28+ logger . log ( '<LazyScrollView>' , ...args ) ;
29+ } ;
2430
2531export interface LazyScrollViewMethods {
2632 scrollTo : typeof ScrollView . prototype . scrollTo ;
@@ -38,6 +44,10 @@ export interface LazyScrollViewProps {
3844 * Ref to the LazyScrollView. Exposes scrollTo, scrollToStart, and scrollToEnd methods.
3945 */
4046 ref ?: MutableRefObject < LazyScrollViewMethods > ;
47+ /**
48+ * When true, console.logs scrollview measurements. Even if true, will not call console.log in production.
49+ */
50+ debug ?: boolean ;
4151}
4252
4353type Props = LazyScrollViewProps &
@@ -50,14 +60,15 @@ type Props = LazyScrollViewProps &
5060 * ScrollView to wrap Lazy Children in.
5161 */
5262const LazyScrollView = forwardRef < LazyScrollViewMethods , Props > (
53- ( { children, offset : injectedOffset , ...rest } , ref ) => {
63+ ( { children, offset : injectedOffset , debug = false , ...rest } , ref ) => {
5464 const _scrollRef = useAnimatedRef < Animated . ScrollView > ( ) ;
5565 const _wrapperRef = useRef < View > ( null ) ;
5666 const _offset = useSharedValue ( injectedOffset || 0 ) ;
5767 const _containerDimensions = useSharedValue ( { width : 0 , height : 0 } ) ;
5868 const _containerCoordinates = useSharedValue ( { x : 0 , y : 0 } ) ;
5969 const _contentDimensions = useSharedValue ( { width : 0 , height : 0 } ) ;
6070 const _statusBarHeight = useSharedValue ( StatusBar . currentHeight || 0 ) ;
71+ const _debug = useSharedValue ( debug ) ;
6172 const horizontal = useSharedValue ( ! ! rest . horizontal ) ;
6273
6374 useImperativeHandle ( ref , ( ) => ( {
@@ -107,17 +118,30 @@ const LazyScrollView = forwardRef<LazyScrollViewMethods, Props>(
107118 width : e . nativeEvent . layout . width ,
108119 } ;
109120
121+ if ( debug ) {
122+ log ( 'dimensions:' , {
123+ height : _containerDimensions . value . height ,
124+ width : _containerDimensions . value . width ,
125+ } ) ;
126+ }
127+
110128 // onLayout runs when RN finishes render, but native layout may not be fully settled until the next frame.
111129 requestAnimationFrame ( ( ) => {
112130 runOnUI ( ( ) => {
113131 'worklet' ;
114132 const measurement = measure ( _scrollRef ) ;
115133
116134 if ( measurement ) {
117- _containerCoordinates . value = {
135+ const coordinates = {
118136 x : measurement . pageX ,
119137 y : measurement . pageY + _statusBarHeight . value ,
120138 } ;
139+
140+ if ( _debug . value ) {
141+ runOnJS ( log ) ( 'coordinates:' , coordinates ) ;
142+ }
143+
144+ _containerCoordinates . value = coordinates ;
121145 }
122146 } ) ( ) ;
123147 } ) ;
@@ -128,10 +152,16 @@ const LazyScrollView = forwardRef<LazyScrollViewMethods, Props>(
128152
129153 const measureContent = useCallback (
130154 ( e : LayoutChangeEvent ) => {
131- _contentDimensions . value = {
155+ const dimensions = {
132156 height : e . nativeEvent . layout . height ,
133157 width : e . nativeEvent . layout . width ,
134158 } ;
159+
160+ if ( debug ) {
161+ log ( 'content dimensions:' , dimensions ) ;
162+ }
163+
164+ _contentDimensions . value = dimensions ;
135165 } ,
136166 // eslint-disable-next-line react-hooks/exhaustive-deps -- shared values do not trigger re-renders
137167 [ ]
0 commit comments