|
| 1 | +import * as React from 'react'; |
| 2 | +import { View, StyleSheet, StyleProp, ViewStyle } from 'react-native'; |
| 3 | +import Animated from 'react-native-reanimated'; |
| 4 | +import HeaderTitle from './HeaderTitle'; |
| 5 | +import { Route, Layout } from '../Stack'; |
| 6 | +import HeaderBackButton from './HeaderBackButton'; |
| 7 | +import memoize from '../../utils/memoize'; |
| 8 | + |
| 9 | +export type InterpolationProps = { |
| 10 | + current: Animated.Node<number>; |
| 11 | + next?: Animated.Node<number>; |
| 12 | + layout: Layout; |
| 13 | +}; |
| 14 | + |
| 15 | +export type StyleInterpolator = ( |
| 16 | + props: InterpolationProps |
| 17 | +) => { |
| 18 | + leftButtonStyle: any; |
| 19 | + titleStyle: any; |
| 20 | +}; |
| 21 | + |
| 22 | +export type HeaderAnimationPreset = { |
| 23 | + styleInterpolator: StyleInterpolator; |
| 24 | +}; |
| 25 | + |
| 26 | +export type Scene<T extends Route> = { |
| 27 | + title: string; |
| 28 | + route: T; |
| 29 | + progress: Animated.Node<number>; |
| 30 | +}; |
| 31 | + |
| 32 | +type Props<T extends Route> = { |
| 33 | + layout: Layout; |
| 34 | + onGoBack: () => void; |
| 35 | + preset: HeaderAnimationPreset; |
| 36 | + scene: Scene<T>; |
| 37 | + previous?: Scene<T>; |
| 38 | + next?: Scene<T>; |
| 39 | + style?: StyleProp<ViewStyle>; |
| 40 | +}; |
| 41 | + |
| 42 | +export default class HeaderAnimatedItem< |
| 43 | + T extends Route |
| 44 | +> extends React.Component<Props<T>> { |
| 45 | + private getInterpolatedStyle = memoize( |
| 46 | + ( |
| 47 | + styleInterpolator: StyleInterpolator, |
| 48 | + layout: Layout, |
| 49 | + current: Animated.Node<number>, |
| 50 | + next?: Animated.Node<number> |
| 51 | + ) => styleInterpolator({ current, next, layout }) |
| 52 | + ); |
| 53 | + |
| 54 | + render() { |
| 55 | + const { |
| 56 | + scene, |
| 57 | + previous, |
| 58 | + next, |
| 59 | + preset, |
| 60 | + layout, |
| 61 | + onGoBack, |
| 62 | + style, |
| 63 | + } = this.props; |
| 64 | + |
| 65 | + const { titleStyle, leftButtonStyle } = this.getInterpolatedStyle( |
| 66 | + preset.styleInterpolator, |
| 67 | + layout, |
| 68 | + scene.progress, |
| 69 | + next ? next.progress : undefined |
| 70 | + ); |
| 71 | + |
| 72 | + return ( |
| 73 | + <View style={[styles.content, style]}> |
| 74 | + {previous ? ( |
| 75 | + <Animated.View style={[styles.left, leftButtonStyle]}> |
| 76 | + <HeaderBackButton onPress={onGoBack} title={previous.title} /> |
| 77 | + </Animated.View> |
| 78 | + ) : null} |
| 79 | + <HeaderTitle style={[previous ? styles.title : null, titleStyle]}> |
| 80 | + {scene.title} |
| 81 | + </HeaderTitle> |
| 82 | + </View> |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +const styles = StyleSheet.create({ |
| 88 | + content: { |
| 89 | + ...StyleSheet.absoluteFillObject, |
| 90 | + paddingHorizontal: 4, |
| 91 | + flexDirection: 'row', |
| 92 | + alignItems: 'center', |
| 93 | + }, |
| 94 | + left: { |
| 95 | + position: 'absolute', |
| 96 | + left: 0, |
| 97 | + top: 0, |
| 98 | + bottom: 0, |
| 99 | + justifyContent: 'center', |
| 100 | + }, |
| 101 | + title: { |
| 102 | + marginHorizontal: 48, |
| 103 | + }, |
| 104 | +}); |
0 commit comments