Skip to content
Open
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
34 changes: 26 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,38 @@ import tinycolor from 'tinycolor2';
import PageData from './components/PageData';
import Paginator from './components/Paginator';

var { height, width } = Dimensions.get('window');
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: can you switch to const please?


export default class Onboarding extends Component {
constructor() {
super();

this.state = {
currentPage: 0,
layout:{
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Styling nitpick: please add a space after the colon :)

height: height,
width: width,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both of these can use the ES6 shorthand syntax:

layout: {
  height,
  width,
}

}
};
}

onLayout = (event) => {
this.setState({
layout: {
height: event.nativeEvent.layout.height,
width: event.nativeEvent.layout.width,
}
});

// adjust the scrolling
const { currentPage } = this.state;
const offsetX = currentPage * event.nativeEvent.layout.width;
this.refs.scroll.scrollTo({ x: offsetX, animated: false });
};

updatePosition = (event) => {
const { contentOffset, layoutMeasurement } = event.nativeEvent;
const pageFraction = contentOffset.x / layoutMeasurement.width;
const { contentOffset } = event.nativeEvent;
const pageFraction = contentOffset.x / this.state.layout.width;
const page = Math.round(pageFraction);
const isLastPage = this.props.pages.length === page + 1;
if (isLastPage && pageFraction - page > 0.3) {
Expand All @@ -27,23 +47,21 @@ export default class Onboarding extends Component {
};

goNext = () => {
const { width } = Dimensions.get('window');
const { currentPage } = this.state;
const nextPage = currentPage + 1;
const offsetX = nextPage * width;
const offsetX = nextPage * this.state.layout.width;
this.refs.scroll.scrollTo({ x: offsetX, animated: true });
this.setState({ currentPage: nextPage });
};

render() {
const { width, height } = Dimensions.get('window');
const { pages, bottomOverlay, showSkip, showNext, showDone } = 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' }}>
<View style={{ flex: 1, backgroundColor: backgroundColor, justifyContent: 'center' }} onLayout={this.onLayout}>
<ScrollView
ref="scroll"
pagingEnabled={true}
Expand All @@ -59,8 +77,8 @@ export default class Onboarding extends Component {
image={image}
title={title}
subtitle={subtitle}
width={width}
height={height}
width={this.state.layout.width}
height={this.state.layout.height}
/>
))}
</ScrollView>
Expand Down