Skip to content

Commit fe07341

Browse files
committed
chore: remove unnecessary animation variables to reduce length
1 parent 285838d commit fe07341

File tree

1 file changed

+43
-47
lines changed

1 file changed

+43
-47
lines changed

example/src/screens/usage/TwitterProfile.tsx

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import {
88
TouchableOpacity,
99
useWindowDimensions,
1010
View,
11+
Platform,
1112
} from 'react-native';
1213
import { useSafeAreaInsets } from 'react-native-safe-area-context';
1314
import { useNavigation } from '@react-navigation/native';
1415
import {
1516
FadingView,
1617
Header,
1718
LargeHeader,
18-
ScalingView,
1919
SectionListWithHeaders,
2020
} from '@codeherence/react-native-header';
2121
import type { ScrollHeaderProps, ScrollLargeHeaderProps } from '@codeherence/react-native-header';
@@ -26,12 +26,12 @@ import Animated, {
2626
interpolate,
2727
useAnimatedStyle,
2828
useDerivedValue,
29+
useSharedValue,
2930
} from 'react-native-reanimated';
30-
import { Avatar, AVATAR_SIZE_MAP } from '../../components';
3131
import { BlurView } from '@react-native-community/blur';
32+
import { Avatar, AVATAR_SIZE_MAP } from '../../components';
3233
import TwitterVerifiedSvg from '../../../assets/twitter-verified.svg';
3334
import type { TwitterProfileScreenNavigationProps } from '../../navigation';
34-
import { Platform } from 'react-native';
3535

3636
// From reading comments online, the BlurView does not work properly for Android <= 11.
3737
// We will have a boolean to check if we can use the BlurView.
@@ -55,87 +55,83 @@ const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar, scrollY }) =
5555
const navigation = useNavigation();
5656
const { left, right } = useSafeAreaInsets();
5757
const { width, height } = useWindowDimensions();
58-
59-
const blurOpacity = useDerivedValue(() => {
60-
return interpolate(Math.abs(scrollY.value), [0, 40], [0, 1], Extrapolate.CLAMP);
61-
});
58+
const bannerHeight = useSharedValue(48 + BANNER_BOTTOM_HEIGHT_ADDITION);
6259

6360
const blurStyle = useAnimatedStyle(() => {
64-
return { opacity: blurOpacity.value };
61+
const blurOpacity = interpolate(Math.abs(scrollY.value), [0, 40], [0, 1], Extrapolate.CLAMP);
62+
63+
return { opacity: blurOpacity };
6564
});
6665

67-
const bannerTranslation = useDerivedValue(() => {
66+
const profileImageScale = useDerivedValue(() => {
6867
return interpolate(
6968
scrollY.value,
7069
[0, BANNER_BOTTOM_HEIGHT_ADDITION],
71-
[0, -BANNER_BOTTOM_HEIGHT_ADDITION],
70+
[AVATAR_START_SCALE, AVATAR_END_SCALE],
7271
Extrapolate.CLAMP
7372
);
7473
});
7574

7675
const bannerTranslationStyle = useAnimatedStyle(() => {
77-
return { transform: [{ translateY: bannerTranslation.value }] };
78-
});
79-
80-
const profileContainerTranslationY = useDerivedValue(() => {
81-
return -scrollY.value + BANNER_BOTTOM_HEIGHT_ADDITION / 2;
82-
});
83-
84-
const profileImageScale = useDerivedValue(() => {
85-
return interpolate(
76+
const bannerTranslation = interpolate(
8677
scrollY.value,
8778
[0, BANNER_BOTTOM_HEIGHT_ADDITION],
88-
[AVATAR_START_SCALE, AVATAR_END_SCALE],
79+
[0, -BANNER_BOTTOM_HEIGHT_ADDITION],
8980
Extrapolate.CLAMP
9081
);
91-
});
9282

93-
const profileImageTranslationY = useDerivedValue(() => {
94-
return interpolate(
95-
profileImageScale.value,
96-
[AVATAR_START_SCALE, AVATAR_END_SCALE],
97-
[0, AVATAR_SIZE_VALUE / 2],
98-
Extrapolate.CLAMP
99-
);
83+
return { transform: [{ translateY: bannerTranslation }] };
10084
});
10185

10286
// This allows the profile container to translate as the user scrolls.
10387
const profileContainerTranslationStyle = useAnimatedStyle(() => {
104-
return {
105-
transform: [{ translateY: profileContainerTranslationY.value }],
106-
};
88+
const translateY = -scrollY.value + BANNER_BOTTOM_HEIGHT_ADDITION / 2;
89+
90+
return { transform: [{ translateY }] };
10791
});
10892

10993
// Once the profile image has been scaled down, we allow the profile container to be
11094
// hidden behind the banner. This is done by setting the zIndex to -1.
11195
const rootProfileRowZIndexStyle = useAnimatedStyle(() => {
112-
return {
113-
zIndex: profileImageScale.value <= AVATAR_END_SCALE ? -1 : 1,
114-
};
96+
return { zIndex: profileImageScale.value <= AVATAR_END_SCALE ? -1 : 1 };
11597
});
11698

11799
// Slow down the avatar's translation to allow it to scale down and
118100
// still stay at its position.
119101
const profileImageScaleStyle = useAnimatedStyle(() => {
102+
const profileImageTranslationY = interpolate(
103+
profileImageScale.value,
104+
[AVATAR_START_SCALE, AVATAR_END_SCALE],
105+
[0, AVATAR_SIZE_VALUE / 2],
106+
Extrapolate.CLAMP
107+
);
108+
120109
return {
121-
transform: [
122-
{ scale: profileImageScale.value },
123-
{ translateY: profileImageTranslationY.value },
124-
],
110+
transform: [{ scale: profileImageScale.value }, { translateY: profileImageTranslationY }],
125111
};
126112
});
127113

114+
const animatedScaleStyle = useAnimatedStyle(() => {
115+
const bannerHeightRatio = height / bannerHeight.value;
116+
117+
const scaleY = interpolate(
118+
scrollY.value,
119+
[0, -(height + bannerHeight.value)],
120+
[1, bannerHeightRatio],
121+
Extrapolate.CLAMP
122+
);
123+
124+
return {
125+
transform: [{ scaleY }, { scaleX: scaleY }],
126+
};
127+
}, [height]);
128+
128129
return (
129130
<View style={styles.smallHeaderContainer}>
130131
<Animated.View style={[StyleSheet.absoluteFill, bannerTranslationStyle]}>
131-
<ScalingView
132-
scrollY={scrollY}
133-
translationDirection="none"
134-
// These two values were arbitrarily chosen.
135-
// Will need to adjust scaling view to allow for scale translations to separate
136-
// x, y and z.
137-
endScale={6}
138-
endRange={height * 0.43}
132+
<Animated.View
133+
onLayout={(e) => (bannerHeight.value = e.nativeEvent.layout.height)}
134+
style={animatedScaleStyle}
139135
>
140136
<View style={{ marginBottom: -BANNER_BOTTOM_HEIGHT_ADDITION }}>
141137
{canUseBlurView ? (
@@ -166,7 +162,7 @@ const HeaderComponent: React.FC<ScrollHeaderProps> = ({ showNavBar, scrollY }) =
166162
style={[styles.imageStyle, { width }]}
167163
/>
168164
</View>
169-
</ScalingView>
165+
</Animated.View>
170166
</Animated.View>
171167

172168
<Header

0 commit comments

Comments
 (0)