This repository has been archived by the owner on Nov 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
271 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import * as React from 'react'; | ||
import { | ||
View, | ||
StyleSheet, | ||
Platform, | ||
StatusBar, | ||
StyleProp, | ||
ViewStyle, | ||
} from 'react-native'; | ||
import Animated from 'react-native-reanimated'; | ||
import HeaderSheet from './HeaderSheet'; | ||
import { Route, Layout } from '../Stack'; | ||
import HeaderAnimatedItem, { | ||
HeaderAnimationPreset, | ||
InterpolationProps, | ||
} from './HeaderAnimatedItem'; | ||
|
||
type Scene<T extends Route> = { | ||
title: string; | ||
route: T; | ||
progress: Animated.Node<number>; | ||
}; | ||
|
||
type Props<T extends Route> = { | ||
layout: Layout; | ||
onGoBack: (props: { route: T }) => void; | ||
preset: HeaderAnimationPreset; | ||
scenes: Scene<T>[]; | ||
style?: StyleProp<ViewStyle>; | ||
}; | ||
|
||
const { interpolate, multiply } = Animated; | ||
|
||
const FadePreset: HeaderAnimationPreset = { | ||
styleInterpolator: ({ current, next }: InterpolationProps) => { | ||
const progress = next | ||
? multiply( | ||
current, | ||
interpolate(next, { | ||
inputRange: [0, 1], | ||
outputRange: [1, 0], | ||
}) | ||
) | ||
: current; | ||
|
||
return { | ||
leftButtonStyle: { opacity: progress }, | ||
titleStyle: { opacity: progress }, | ||
}; | ||
}, | ||
}; | ||
|
||
export default class HeaderAnimated<T extends Route> extends React.Component< | ||
Props<T> | ||
> { | ||
static defaultProps = { | ||
preset: FadePreset, | ||
}; | ||
|
||
render() { | ||
const { preset, scenes, layout, onGoBack } = this.props; | ||
|
||
return ( | ||
<HeaderSheet> | ||
<View style={styles.container}> | ||
{scenes.map((scene, i, self) => { | ||
const previous = self[i - 1]; | ||
const next = self[i + 1]; | ||
|
||
return ( | ||
<HeaderAnimatedItem | ||
key={scene.route.key} | ||
preset={preset} | ||
layout={layout} | ||
scene={scene} | ||
previous={previous} | ||
next={next} | ||
onGoBack={() => onGoBack({ route: scene.route })} | ||
/> | ||
); | ||
})} | ||
</View> | ||
</HeaderSheet> | ||
); | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
height: Platform.OS === 'ios' ? 44 : 56, | ||
marginTop: Platform.OS === 'ios' ? 20 : StatusBar.currentHeight, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as React from 'react'; | ||
import { View, StyleSheet, StyleProp, ViewStyle } from 'react-native'; | ||
import Animated from 'react-native-reanimated'; | ||
import HeaderTitle from './HeaderTitle'; | ||
import { Route, Layout } from '../Stack'; | ||
import HeaderBackButton from './HeaderBackButton'; | ||
import memoize from '../../utils/memoize'; | ||
|
||
export type InterpolationProps = { | ||
current: Animated.Node<number>; | ||
next?: Animated.Node<number>; | ||
layout: Layout; | ||
}; | ||
|
||
export type StyleInterpolator = ( | ||
props: InterpolationProps | ||
) => { | ||
leftButtonStyle: any; | ||
titleStyle: any; | ||
}; | ||
|
||
export type HeaderAnimationPreset = { | ||
styleInterpolator: StyleInterpolator; | ||
}; | ||
|
||
export type Scene<T extends Route> = { | ||
title: string; | ||
route: T; | ||
progress: Animated.Node<number>; | ||
}; | ||
|
||
type Props<T extends Route> = { | ||
layout: Layout; | ||
onGoBack: () => void; | ||
preset: HeaderAnimationPreset; | ||
scene: Scene<T>; | ||
previous?: Scene<T>; | ||
next?: Scene<T>; | ||
style?: StyleProp<ViewStyle>; | ||
}; | ||
|
||
export default class HeaderAnimatedItem< | ||
T extends Route | ||
> extends React.Component<Props<T>> { | ||
private getInterpolatedStyle = memoize( | ||
( | ||
styleInterpolator: StyleInterpolator, | ||
layout: Layout, | ||
current: Animated.Node<number>, | ||
next?: Animated.Node<number> | ||
) => styleInterpolator({ current, next, layout }) | ||
); | ||
|
||
render() { | ||
const { | ||
scene, | ||
previous, | ||
next, | ||
preset, | ||
layout, | ||
onGoBack, | ||
style, | ||
} = this.props; | ||
|
||
const { titleStyle, leftButtonStyle } = this.getInterpolatedStyle( | ||
preset.styleInterpolator, | ||
layout, | ||
scene.progress, | ||
next ? next.progress : undefined | ||
); | ||
|
||
return ( | ||
<View style={[styles.content, style]}> | ||
{previous ? ( | ||
<Animated.View style={[styles.left, leftButtonStyle]}> | ||
<HeaderBackButton onPress={onGoBack} title={previous.title} /> | ||
</Animated.View> | ||
) : null} | ||
<HeaderTitle style={[previous ? styles.title : null, titleStyle]}> | ||
{scene.title} | ||
</HeaderTitle> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
content: { | ||
...StyleSheet.absoluteFillObject, | ||
paddingHorizontal: 4, | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
}, | ||
left: { | ||
position: 'absolute', | ||
left: 0, | ||
top: 0, | ||
bottom: 0, | ||
justifyContent: 'center', | ||
}, | ||
title: { | ||
marginHorizontal: 48, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.