Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add web implementation for useScrollViewOffset (#5805)
## Summary Our `useScrollViewOffset` had no support for web, so I added it using some basic web scroll listeners. There are some funny typing shenanigans, if anyone comes up with better solutions I will happily use it. | before | after | | --- | --- | | <video src="https://github.com/software-mansion/react-native-reanimated/assets/77503811/fd61dd7d-3070-4d6e-a8e5-d00b25fe32db"/> | <video src="https://github.com/software-mansion/react-native-reanimated/assets/77503811/5b49a3dd-1a34-4d01-8c09-ae4bd723c9c3" /> | ## Test plan Check `useScrollViewOffset` example from Reanimated WebExample and watch console logs or copy [example code](https://docs.swmansion.com/react-native-reanimated/docs/scroll/useScrollViewOffset/#example) from our docs. More in-depth testing can be done using the following code - it tests switching and mouting/dismounting components that the hook refers to: <details><summary>Code</summary> ``` TYPESCRIPT import React, { useState } from 'react'; import Animated, { useAnimatedRef, useDerivedValue, useSharedValue, useScrollViewOffset, } from 'react-native-reanimated'; import { Button, StyleSheet, Text, View } from 'react-native'; export default function ScrollViewOffsetExample() { const aref = useAnimatedRef<Animated.ScrollView>(); const bref = useAnimatedRef<Animated.ScrollView>(); const scrollHandler = useSharedValue(0); const [scrollAMounted, setScrollAMounted] = useState(true); const [scrollBMounted, setScrollBMounted] = useState(true); const [scrollAPassed, setScrollAPassed] = useState(true); useDerivedValue(() => { console.log(scrollHandler.value); }); const onAMountPress = () => { setScrollAMounted(!scrollAMounted); }; const onBMountPress = () => { setScrollBMounted(!scrollBMounted); }; const onPassTogglePress = () => { setScrollAPassed(!scrollAPassed); }; useScrollViewOffset(scrollAPassed ? aref : bref, scrollHandler); return ( <> <View style={styles.positionView}> <Text>Test</Text> </View> <View style={styles.divider} /> <Button title={`${scrollAMounted ? 'Dismount' : 'Mount'} scroll A`} onPress={onAMountPress} /> <Button title={`${scrollBMounted ? 'Dismount' : 'Mount'} scroll B`} onPress={onBMountPress} /> <Button title={`Toggle the ref, currently passed to ${ scrollAPassed ? 'scroll A' : 'scroll B' }`} onPress={onPassTogglePress} /> {scrollAMounted ? ( <Animated.ScrollView ref={aref} style={[styles.scrollView, { backgroundColor: 'purple' }]}> {[...Array(100)].map((_, i) => ( <Text key={i} style={styles.text}> A: {i} </Text> ))} </Animated.ScrollView> ) : null} {scrollBMounted ? ( <Animated.ScrollView ref={bref} style={[styles.scrollView, { backgroundColor: 'lime' }]}> {[...Array(100)].map((_, i) => ( <Text key={i} style={styles.text}> B: {i} </Text> ))} </Animated.ScrollView> ) : null} </> ); } const styles = StyleSheet.create({ positionView: { margin: 20, alignItems: 'center', }, scrollView: { flex: 1, width: '100%', }, text: { fontSize: 50, textAlign: 'center', }, divider: { backgroundColor: 'black', height: 1, }, }); ```
- Loading branch information