-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (64 loc) · 2.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { Component } from 'react';
import { View, Animated, Easing, Dimensions } from 'react-native';
import { styles, backgroundColor, spaceNavigationHeight, iconSize } from './styles';
import SpaceNavigationItem from './item';
import Svg, { Polygon, Defs, Stop, LinearGradient } from 'react-native-svg';
export default class SpaceNavigation extends Component{
constructor(props){
super(props);
this.state = {
nav: this.props.navigation
};
this.changeNav = this.changeNav.bind(this);
this.activeAnimation = new Animated.Value(-1);
this.singleWidth = Dimensions.get('window').width / this.props.navigation.length;
this.startPoint = (this.singleWidth - iconSize) / 2;
this.endPoint = this.singleWidth - this.startPoint;
this.points = this.startPoint + ",0 0," + spaceNavigationHeight + " " + this.singleWidth + "," + spaceNavigationHeight + " " + this.endPoint + ",0";
}
componentDidMount(){
this.changeNav(this.props.firstPage, false);
}
changeNav(id, change = true){
let nav = [];
this.state.nav.map((item, index) => nav.push({ ...item, active: index === id }) );
this.setState({ nav }, () => {
this.animation(id);
if ( change ) this.props.changeNavigation(id);
});
}
animation(val){
Animated.timing( this.activeAnimation, {
toValue: val, duration: 250, useNativeDriver: true, easing: Easing.ease
}).start();
}
render(){
const animationLeft = this.activeAnimation.interpolate({
inputRange: [-1, 0, 1],
outputRange: [-this.singleWidth, 0, this.singleWidth]
});
return (
<View style={styles.spaceNavigation}>
<View style={styles.spaceIcons}>
{ this.state.nav.map((item, index) =>
<SpaceNavigationItem key={index} id={index} singleWidth={this.singleWidth} clickFunction={this.changeNav} {...item} />
) }
</View>
<Animated.View style={[styles.active, { width: this.singleWidth, transform: [{ translateX: animationLeft }] }]}>
<View style={styles.activeSmall} />
<View style={styles.activeBackground}>
<Svg width={this.singleWidth} height={spaceNavigationHeight}>
<Defs>
<LinearGradient id="grad" x1="0" y1="0" x2="0" y2={spaceNavigationHeight + 10}>
<Stop offset="0" stopColor="#606060" stopOpacity="1" />
<Stop offset="1" stopColor={backgroundColor} stopOpacity="1" />
</LinearGradient>
</Defs>
<Polygon points={this.points} fill="url(#grad)" />
</Svg>
</View>
</Animated.View>
</View>
);
}
}