Skip to content

Commit

Permalink
Use proper interpolateColor in composedHandler example (#6031)
Browse files Browse the repository at this point in the history
## Summary

Randomly found out that my "interpolateColor at home" in one of
`composedEventHandler` examples from example app doesn't really work on
Web. Thus the change.

## Test plan

Check out Composed Handler Internal Merging Example from example app on
any platform.
:shipit:
  • Loading branch information
szydlovsky committed May 20, 2024
1 parent e46d4ec commit cbd5ff1
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions app/src/examples/ComposedHandlerInternalMergingExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Text, View, StyleSheet } from 'react-native';
import type { NativeSyntheticEvent, NativeScrollEvent } from 'react-native';
import Animated, {
EventHandlerProcessed,
interpolateColor,
useAnimatedScrollHandler,
useAnimatedStyle,
useComposedEventHandler,
Expand Down Expand Up @@ -40,16 +41,17 @@ const ColorChangingList = ({
const internalHandler = useAnimatedScrollHandler({
onScroll(e) {
const scaledValue = e.contentOffset.y % 600;
offsetSv.value =
scaledValue <= 300
? (scaledValue / 600) * 255
: ((600 - scaledValue) / 600) * 255;
offsetSv.value = scaledValue <= 300 ? scaledValue : 600 - scaledValue;
},
});

const animatedStyle = useAnimatedStyle(() => {
return {
backgroundColor: `rgb(${offsetSv.value},30,${255 - offsetSv.value})`,
backgroundColor: interpolateColor(
offsetSv.value,
[0, 300],
['blue', 'purple']
),
};
});

Expand All @@ -59,28 +61,18 @@ const ColorChangingList = ({
]);

return (
<Animated.FlatList
<Animated.ScrollView
onScroll={composedHandler}
style={[styles.list, animatedStyle]}
data={items}
renderItem={({ item }) => <Item title={item.title} />}
keyExtractor={(item) => `A:${item.title}`}
/>
style={[styles.list, animatedStyle]}>
{[...Array(100)].map((_, num: number) => (
<View key={`${num}`} style={styles.item}>
<Text>{`${num}`}</Text>
</View>
))}
</Animated.ScrollView>
);
};

type ItemProps = { title: string };
const Item = ({ title }: ItemProps) => (
<View style={styles.item}>
<Text style={styles.title}>{title}</Text>
</View>
);

type ItemValue = { title: string };
const items: ItemValue[] = [...new Array(101)].map((_each, index) => {
return { title: `${index}` };
});

const styles = StyleSheet.create({
container: {
flex: 1,
Expand All @@ -101,7 +93,4 @@ const styles = StyleSheet.create({
marginHorizontal: 16,
borderRadius: 20,
},
title: {
fontSize: 32,
},
});

0 comments on commit cbd5ff1

Please sign in to comment.