forked from goshacmd/react-native-simple-onboarding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (114 loc) · 4.02 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import React, { Component, PropTypes } from 'react';
import { View, ScrollView, Dimensions, Text, TouchableOpacity, StyleSheet } from 'react-native';
import tinycolor from 'tinycolor2';
import PageData from './components/PageData';
import Paginator from './components/Paginator';
const HORIZONTAL_ORIENTATION = 'horizontal';
const VERTICAL_ORIENTATION = 'vertical';
export default class Onboarding extends Component {
constructor() {
super();
const { width, height } = Dimensions.get('window');
this.state = {
currentPage: 0,
height:height,
width:width,
orientation: height > width ? VERTICAL_ORIENTATION : HORIZONTAL_ORIENTATION,
};
}
updatePosition = (event) => {
const { contentOffset, layoutMeasurement } = event.nativeEvent;
const pageFraction = contentOffset.x / layoutMeasurement.width;
const page = Math.round(pageFraction);
const isLastPage = this.props.pages.length === page + 1;
if (isLastPage && pageFraction - page > 0.3) {
this.props.onEnd();
} else {
this.setState({ currentPage: page });
}
};
goNext = () => {
const { width } = this.state;
const { currentPage } = this.state;
const nextPage = currentPage + 1;
const offsetX = nextPage * width;
this.refs.scroll.scrollTo({ x: offsetX, animated: true });
this.setState({ currentPage: nextPage });
};
measureView(event) {
this.setState({
height: event.nativeEvent.layout.height,
width: event.nativeEvent.layout.width,
orientation: event.nativeEvent.layout.height > event.nativeEvent.layout.width ? VERTICAL_ORIENTATION : HORIZONTAL_ORIENTATION
});
};
render() {
const { width, height } = this.state;
const { pages, bottomOverlay, showSkip, showNext, showDone, nextButtonText, doneButtonText} = this.props;
const currentPage = pages[this.state.currentPage] || pages[0];
const { backgroundColor } = currentPage;
const isLight = tinycolor(backgroundColor).getBrightness() > 180;
return (
<View style={{ flex: 1, backgroundColor: backgroundColor, justifyContent: 'center' }} onLayout={(event) => this.measureView(event)}>
<ScrollView
ref="scroll"
pagingEnabled={true}
horizontal={true}
showsHorizontalScrollIndicator={false}
onScroll={this.updatePosition}
scrollEventThrottle={100}
>
{pages.map(({ image, title, subtitle }, idx) => (
<PageData
key={idx}
isLight={isLight}
image={image}
title={title}
subtitle={subtitle}
width={width}
height={height}
orientation={this.state.orientation}
/>
))}
</ScrollView>
<Paginator
isLight={isLight}
overlay={bottomOverlay}
showSkip={showSkip}
showNext={showNext}
showDone={showDone}
pages={pages.length}
currentPage={this.state.currentPage}
onEnd={this.props.onEnd}
onNext={this.goNext}
nextButtonText={nextButtonText}
doneButtonText={doneButtonText}
buttonStyle = {this.props.buttonStyle}
buttonTextStyle = {this.props.buttonTextStyle}
/>
</View>
);
}
}
Onboarding.propTypes = {
pages: PropTypes.arrayOf(PropTypes.shape({
backgroundColor: PropTypes.string.isRequired,
image: PropTypes.element.isRequired,
title: PropTypes.string.isRequired,
subtitle: PropTypes.string.isRequired,
})).isRequired,
bottomOverlay: PropTypes.bool,
showSkip: PropTypes.bool,
showNext: PropTypes.bool,
showDone: PropTypes.bool,
nextButtonText: PropTypes.string,
doneButtonText: PropTypes.string,
buttonStyle: PropTypes.object,
buttonTextStyle: PropTypes.object,
};
Onboarding.defaultProps = {
bottomOverlay: true,
showSkip: true,
showNext: true,
showDone: true,
};