Skip to content

[DO NOT MERGE] Try to Go Back to the first implentation #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/Pages/Avignon.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BasicUsage extends React.Component {
minOverlayOpacity={0.4}
renderHeader={() =>
<Image source={require('../assets/avignon.jpg')} style={styles.image} />}
renderTouchableFixedForeground={() =>
renderFixedForeground={() =>
<View style={{ height: MAX_HEIGHT, justifyContent: 'center', alignItems: 'center' }}>
<TouchableOpacity onPress={() => console.log('tap!!')} style={styles.button}>
<Text style={styles.buttonText}>Discover Avignon now!</Text>
Expand Down
109 changes: 37 additions & 72 deletions src/ImageHeaderScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export type Props = {
renderFixedForeground: () => React.Element<any>,
renderForeground: () => React.Element<any>,
renderHeader: () => React.Element<any>,
renderTouchableFixedForeground: () => React.Element<any>,
};

export type DefaultProps = {
Expand All @@ -27,8 +26,6 @@ export type DefaultProps = {
maxOverlayOpacity: number,
minHeight: number,
minOverlayOpacity: number,
renderFixedForeground: () => React.Element<any>,
renderForeground: () => React.Element<any>,
renderHeader: () => React.Element<any>,
};

Expand All @@ -50,8 +47,6 @@ class ImageHeaderScrollView extends Component<DefaultProps, Props, State> {
maxOverlayOpacity: 0.3,
minHeight: 80,
minOverlayOpacity: 0,
renderFixedForeground: () => <View />,
renderForeground: () => <View />,
renderHeader: () => <View />,
};

Expand Down Expand Up @@ -106,13 +101,14 @@ class ImageHeaderScrollView extends Component<DefaultProps, Props, State> {
]);

const headerScale = this.state.scrollY.interpolate({
inputRange: [-this.props.maxHeight, 0],
outputRange: [3, 1],
inputRange: [-2 * this.props.maxHeight, 0],
outputRange: [5, 1],
extrapolate: 'clamp',
});
const height = this.interpolateOnImageHeight([this.props.maxHeight, this.props.minHeight]);

const headerTransformStyle = {
height: this.props.maxHeight,
height,
transform: [{ scale: headerScale }],
};

Expand All @@ -121,18 +117,33 @@ class ImageHeaderScrollView extends Component<DefaultProps, Props, State> {
{ opacity: overlayOpacity, backgroundColor: this.props.overlayColor },
];

const absoluteContainerStyle = {
position: 'absolute',
top: 0,
left: 0,
right: 0,
height: this.props.maxHeight,
};

return (
<Animated.View style={[styles.header, headerTransformStyle]}>
{this.props.renderHeader()}
<Animated.View style={overlayStyle} />
<View style={styles.fixedForeground}>
{this.props.renderFixedForeground()}
<View style={absoluteContainerStyle}>
{this.props.renderHeader()}
<Animated.View style={overlayStyle} />
{this.props.renderFixedForeground &&
<View style={styles.fixedForeground}>
{this.props.renderFixedForeground()}
</View>}
</View>
</Animated.View>
);
}

renderForeground() {
if (!this.props.renderForeground) {
return null;
}

const headerTranslate = this.state.scrollY.interpolate({
inputRange: [0, this.props.maxHeight * 2],
outputRange: [0, -this.props.maxHeight * 2 * this.props.foregroundParallaxRatio],
Expand All @@ -152,31 +163,6 @@ class ImageHeaderScrollView extends Component<DefaultProps, Props, State> {
);
}

renderTouchableFixedForeground() {
if (!this.props.renderTouchableFixedForeground) {
return <View />;
}

const height = this.interpolateOnImageHeight([this.props.maxHeight, this.props.minHeight]);

const headerScale = this.state.scrollY.interpolate({
inputRange: [-this.props.maxHeight, 0],
outputRange: [3, 1],
extrapolate: 'clamp',
});

const headerTransformStyle = {
height,
transform: [{ scale: headerScale }],
};

return (
<Animated.View style={[styles.header, styles.touchableFixedForeground, headerTransformStyle]}>
{this.props.renderTouchableFixedForeground()}
</Animated.View>
);
}

render() {
const {
children,
Expand All @@ -191,42 +177,29 @@ class ImageHeaderScrollView extends Component<DefaultProps, Props, State> {
renderFixedForeground,
renderForeground,
renderHeader,
renderTouchableFixedForeground,
...scrollViewProps
} = this.props;

const headerScrollDistance = this.interpolateOnImageHeight([maxHeight, maxHeight - minHeight]);
const topMargin = this.interpolateOnImageHeight([0, minHeight]);

const childrenContainerStyle = StyleSheet.flatten([
{ transform: [{ translateY: headerScrollDistance }] },
{ backgroundColor: 'white', paddingBottom: maxHeight },
childrenStyle,
]);
const childrenContainerStyle = StyleSheet.flatten([childrenStyle, { paddingTop: maxHeight }]);

return (
<View
style={styles.container}
ref={ref => (this.container = ref)}
onLayout={() => this.container.measureInWindow((x, y) => this.setState({ pageY: y }))}
>
<ScrollView
ref={ref => (this.scrollViewRef = ref)}
style={styles.container}
scrollEventThrottle={16}
onScroll={Animated.event([{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }])}
{...scrollViewProps}
>
<Animated.View style={childrenContainerStyle}>
{children}
</Animated.View>
</ScrollView>
{this.renderHeader()}
<Animated.View style={[styles.container, { transform: [{ translateY: topMargin }] }]}>
<ScrollView
ref={ref => (this.scrollViewRef = ref)}
style={styles.container}
scrollEventThrottle={16}
onScroll={Animated.event([
{ nativeEvent: { contentOffset: { y: this.state.scrollY } } },
])}
{...scrollViewProps}
>
<Animated.View style={childrenContainerStyle}>
{children}
</Animated.View>
</ScrollView>
</Animated.View>
{this.renderTouchableFixedForeground()}
{this.renderForeground()}
</View>
);
Expand Down Expand Up @@ -254,19 +227,11 @@ const styles = StyleSheet.create({
justifyContent: 'center',
},
overlay: {
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
...StyleSheet.absoluteFillObject,
zIndex: 100,
},
fixedForeground: {
position: 'absolute',
top: 0,
right: 0,
left: 0,
bottom: 0,
...StyleSheet.absoluteFillObject,
zIndex: 101,
},
touchableFixedForeground: {
Expand Down