-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from clmntcrl/header-component
Header component
- Loading branch information
Showing
8 changed files
with
300 additions
and
268 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 |
---|---|---|
@@ -1,23 +1,86 @@ | ||
import React from 'react'; | ||
import {View, Image, StyleSheet, Text} from 'react-native'; | ||
import React from "react" | ||
import { SafeAreaView, ImageBackground, Animated, Easing, Dimensions, StyleSheet, ViewPropTypes } from "react-native" | ||
import PropTypes from "prop-types" | ||
|
||
const Header = () => { | ||
return ( | ||
<View> | ||
<Image source={require('../res/images/header-back.jpg')} style={{height:125, width:'100%'}} /> | ||
<Image source={require('../res/images/logo.png')} style={styles.positionLogo} /> | ||
</View> | ||
) | ||
} | ||
const windowSize = Dimensions.get("window") | ||
|
||
class Header extends React.Component { | ||
static propTypes = { | ||
style: ViewPropTypes.style, | ||
scrollContentYOffset: PropTypes.number, | ||
} | ||
static defaultProps = { | ||
scrollContentYOffset: 0, | ||
} | ||
// Header animation configuration | ||
static minHeight = 0 | ||
static maxHeight = 80 | ||
static deltaAnimationProgressThatDoesNotNeedTiming = 0.5 | ||
static computeAnimatedProgressValue = ({ scrollContentYOffset }) => | ||
scrollContentYOffset >= 0 | ||
? Math.max(0, 1 - scrollContentYOffset / (Header.maxHeight - Header.minHeight)) | ||
: 1 + (-scrollContentYOffset * 0.3) / (Header.maxHeight - Header.minHeight) // Bounce | ||
static animatedHeight = ({ animatedProgress }) => | ||
Animated.multiply(animatedProgress, Header.maxHeight - Header.minHeight) | ||
static animatedOpacity = ({ animatedProgress }) => animatedProgress | ||
|
||
constructor(props) { | ||
super(props) | ||
this.animatedProgressValue = Header.computeAnimatedProgressValue({ | ||
scrollContentYOffset: this.props.scrollContentYOffset, | ||
}) | ||
this.animatedProgress = new Animated.Value(this.animatedProgressValue) | ||
} | ||
|
||
componentDidUpdate() { | ||
const { scrollContentYOffset } = this.props | ||
const newAnimatedProgressValue = Header.computeAnimatedProgressValue({ scrollContentYOffset }) | ||
if ( | ||
Math.abs(this.animatedProgressValue - newAnimatedProgressValue) <= | ||
Header.deltaAnimationProgressThatDoesNotNeedTiming | ||
) { | ||
this.animatedProgress.setValue(newAnimatedProgressValue) | ||
} else { | ||
Animated.timing(this.animatedProgress, { | ||
toValue: newAnimatedProgressValue, | ||
duration: 200, | ||
easing: Easing.out(Easing.quad), | ||
}).start() | ||
} | ||
this.animatedProgressValue = newAnimatedProgressValue | ||
} | ||
|
||
render() { | ||
return ( | ||
<ImageBackground | ||
source={require("../res/images/header-back.jpg")} | ||
style={[this.props.styles, styles.container]} | ||
resizeMode="cover" | ||
> | ||
<SafeAreaView style={{ flex: 1 }}> | ||
<Animated.Image | ||
source={require("../res/images/logo.png")} | ||
style={{ | ||
height: Header.animatedHeight({ animatedProgress: this.animatedProgress }), | ||
opacity: Header.animatedOpacity({ animatedProgress: this.animatedProgress }), | ||
}} | ||
resizeMode="contain" | ||
/> | ||
</SafeAreaView> | ||
</ImageBackground> | ||
) | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
positionLogo: { | ||
width: 80, | ||
height: 80, | ||
position: "absolute", | ||
top: "25%", | ||
left: "40%" | ||
} | ||
}); | ||
container: { | ||
position: "absolute", | ||
top: 0, | ||
left: 0, | ||
right: 0, | ||
alignItems: "center", | ||
justifyContent: "center", | ||
}, | ||
}) | ||
|
||
export default Header |
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,42 @@ | ||
import React, { Component } from "react" | ||
import { SafeAreaView, StyleSheet, ViewPropTypes, ScrollView, View } from "react-native" | ||
import PropTypes from "prop-types" | ||
|
||
import Header from "./Header" | ||
|
||
export default class ScrollViewWithHeader extends Component { | ||
static propTypes = { | ||
style: ViewPropTypes.style, | ||
children: PropTypes.node, | ||
navigation: PropTypes.object, // Provide navigation object if header is in navigation stack | ||
} | ||
|
||
state = { | ||
offsetY: 0, | ||
} | ||
|
||
onScroll = ({ nativeEvent }) => | ||
this.props.navigation | ||
? this.props.navigation.setParams({ scrollContentYOffset: nativeEvent.contentOffset.y }) | ||
: this.setState({ offsetY: nativeEvent.contentOffset.y }) | ||
|
||
render() { | ||
return ( | ||
<SafeAreaView style={this.props.style}> | ||
<ScrollView style={{ flex: 1 }} scrollEventThrottle={16} onScroll={this.onScroll}> | ||
<View style={{ flex: 1, marginTop: Header.maxHeight }}>{this.props.children}</View> | ||
</ScrollView> | ||
{!this.props.navigation && <Header style={styles.header} scrollContentYOffset={this.state.offsetY} />} | ||
</SafeAreaView> | ||
) | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
header: { | ||
position: "absolute", | ||
top: 0, | ||
left: 0, | ||
right: 0, | ||
}, | ||
}) |
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 |
---|---|---|
@@ -1,81 +1,69 @@ | ||
import React, { Component } from "react"; | ||
import { | ||
StyleSheet, | ||
Text, | ||
View, | ||
Image, | ||
ScrollView, | ||
Button | ||
} from "react-native"; | ||
import { StackNavigator } from "react-navigation"; | ||
import React, { Component } from "react" | ||
import { StyleSheet, Text, View, Image, ScrollView, Button } from "react-native" | ||
|
||
//Component | ||
import Header from "../../components/Header"; | ||
import programs from "../../res/data/programs.json"; | ||
import { Routes } from "."; | ||
import programs from "../../res/data/programs.json" | ||
import { Routes } from "." | ||
|
||
import ScrollViewWithHeader from "../../components/ScrollViewWithHeader" | ||
|
||
export default class Programs extends Component { | ||
render() { | ||
return ( | ||
<View style={{ justifyContent: "center", alignItems: "center" }}> | ||
<ScrollView style={{ width: "100%" }}> | ||
<View style={{ flex: 1 }}> | ||
{programs.map(program => ( | ||
<View key={program.id} style={styles.container}> | ||
<View key={program.id} style={styles.emission}> | ||
<Image | ||
style={styles.logoEmission} | ||
source={{ uri: program.logo }} | ||
/> | ||
<View style={styles.containerDetail}> | ||
<Text style={styles.programName}>{program.name}</Text> | ||
<Button | ||
title="Description" | ||
onPress={() => | ||
this.props.navigation.navigate(Routes.programsDetails, { | ||
programDetail: program | ||
}) | ||
} | ||
/> | ||
</View> | ||
render() { | ||
return ( | ||
<ScrollViewWithHeader style={{ flex: 1 }} navigation={this.props.navigation}> | ||
<View style={{ flex: 1 }}> | ||
{programs.map((program) => ( | ||
<View key={program.id} style={styles.container}> | ||
<View key={program.id} style={styles.emission}> | ||
<Image style={styles.logoEmission} source={{ uri: program.logo }} /> | ||
<View style={styles.containerDetail}> | ||
<Text style={styles.programName}>{program.name}</Text> | ||
<Button | ||
title="Description" | ||
onPress={() => | ||
this.props.navigation.navigate(Routes.programsDetails, { | ||
programDetail: program, | ||
}) | ||
} | ||
/> | ||
</View> | ||
</View> | ||
</View> | ||
))} | ||
</View> | ||
</View> | ||
))} | ||
</View> | ||
</ScrollView> | ||
</View> | ||
); | ||
} | ||
</ScrollViewWithHeader> | ||
) | ||
} | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
backgroundColor: "#DDD", | ||
borderRadius: 80, | ||
marginHorizontal: 20, | ||
marginVertical: 10 | ||
}, | ||
emission: { | ||
flex: 1, | ||
flexDirection: "row", | ||
height: 80 | ||
}, | ||
logoEmission: { | ||
borderRadius: 40, | ||
height: 80, | ||
width: 80 | ||
}, | ||
containerDetail: { | ||
flexDirection: "column", | ||
justifyContent: "space-around", | ||
flex: 1, | ||
height: 60 | ||
}, | ||
programName: { | ||
flex: 1, | ||
textAlign: "center", | ||
alignItems: "center", | ||
padding: 20, | ||
fontWeight: "500" | ||
} | ||
}); | ||
container: { | ||
backgroundColor: "#DDD", | ||
borderRadius: 80, | ||
marginHorizontal: 20, | ||
marginVertical: 10, | ||
}, | ||
emission: { | ||
flex: 1, | ||
flexDirection: "row", | ||
height: 80, | ||
}, | ||
logoEmission: { | ||
borderRadius: 40, | ||
height: 80, | ||
width: 80, | ||
}, | ||
containerDetail: { | ||
flexDirection: "column", | ||
justifyContent: "space-around", | ||
flex: 1, | ||
height: 60, | ||
}, | ||
programName: { | ||
flex: 1, | ||
textAlign: "center", | ||
alignItems: "center", | ||
padding: 20, | ||
fontWeight: "500", | ||
}, | ||
}) |
Oops, something went wrong.