tech-tracker-expo/components/default/ParallaxScrollView.tsx
2025-01-28 13:24:20 -06:00

93 lines
2.4 KiB
TypeScript

import type { PropsWithChildren, ReactElement } from 'react';
import { StyleSheet, ViewStyle, StyleProp } from 'react-native';
import Animated, {
interpolate,
useAnimatedRef,
useAnimatedStyle,
useScrollViewOffset,
} from 'react-native-reanimated';
import { ThemedText, ThemedView } from '@/components/theme/Theme';
import { useBottomTabOverflow } from '@/components/ui/TabBarBackground';
import { Colors } from '@/constants/Colors';
import { useColorScheme } from '@/hooks/useColorScheme';
const HEADER_HEIGHT = 150;
type Props = PropsWithChildren<{
headerImage?: ReactElement;
headerBackgroundColor?: { dark: string; light: string };
headerHeight?: number;
headerTitle?: ReactElement;
}>;
const ParallaxScrollView = ({
children,
headerImage = <ThemedView />,
headerBackgroundColor = { light: Colors.light.accent, dark: Colors.dark.accent },
headerHeight = HEADER_HEIGHT,
headerTitle = <ThemedText />,
}: Props) => {
const scheme = useColorScheme() ?? 'dark';
const scrollRef = useAnimatedRef<Animated.ScrollView>();
const scrollOffset = useScrollViewOffset(scrollRef);
const bottom = useBottomTabOverflow();
const headerAnimatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateY: interpolate(
scrollOffset.value,
[-headerHeight, 0, headerHeight],
[-headerHeight / 2, 0, headerHeight * 0.75],
),
},
{
scale: interpolate(scrollOffset.value, [-headerHeight, 0, headerHeight], [2, 1, 1]),
},
],
};
});
return (
<ThemedView style={styles.container}>
<Animated.ScrollView
ref={scrollRef}
scrollEventThrottle={16}
scrollIndicatorInsets={{ bottom }}
contentContainerStyle={{ paddingBottom: bottom }}
>
<Animated.View
style={[
styles.header,
{
backgroundColor: headerBackgroundColor[scheme],
height: headerHeight,
},
headerAnimatedStyle,
]}
>
{headerImage}
{headerTitle}
</Animated.View>
<ThemedView style={styles.content}>{children}</ThemedView>
</Animated.ScrollView>
</ThemedView>
);
};
export default ParallaxScrollView;
const styles = StyleSheet.create({
container: {
flex: 1,
},
header: {
overflow: 'hidden',
},
content: {
flex: 1,
padding: 32,
gap: 16,
overflow: 'hidden',
},
});