Skip to content
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

Web: fix on scroll end #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
13 changes: 13 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default class AppIntroSlider<ItemT = any> extends React.Component<
height: 0,
activeIndex: 0,
};
_timeout = 0;
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
_timeout = 0;
_webScrollEndTimeout = null;

flatList: FlatList<ItemT> | undefined;

goToSlide = (pageNum: number, triggerOnSlideChange?: boolean) => {
Expand Down Expand Up @@ -245,6 +246,10 @@ export default class AppIntroSlider<ItemT = any> extends React.Component<
};

_onMomentumScrollEnd = (e: {nativeEvent: NativeScrollEvent}) => {
this._onScrollEnd(e);
};
Comment on lines 248 to +250
Copy link
Owner

Choose a reason for hiding this comment

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

Looks like this function now serves no purpose, I'd remove it and just set onMomentumScrollEnd={this._onScrollEnd} in the render


_onScrollEnd = (e: {nativeEvent: NativeScrollEvent}) => {
const offset = e.nativeEvent.contentOffset.x;
// Touching very very quickly and continuous brings about
// a variation close to - but not quite - the width.
Expand All @@ -260,6 +265,13 @@ export default class AppIntroSlider<ItemT = any> extends React.Component<
this.props.onSlideChange && this.props.onSlideChange(newIndex, lastIndex);
};

_onScroll = (e: {nativeEvent: NativeScrollEvent}) => {
if (Platform.OS === 'web') {
clearTimeout(this._timeout);
this._timeout = setTimeout(() => this._onScrollEnd(e), 150);
}
};

_onLayout = ({nativeEvent}: LayoutChangeEvent) => {
const {width, height} = nativeEvent.layout;
if (width !== this.state.width || height !== this.state.height) {
Expand Down Expand Up @@ -311,6 +323,7 @@ export default class AppIntroSlider<ItemT = any> extends React.Component<
onMomentumScrollEnd={this._onMomentumScrollEnd}
extraData={extra}
onLayout={this._onLayout}
onScroll={this._onScroll}
// make sure all slides are rendered so we can use dots to navigate to them
initialNumToRender={data.length}
{...otherProps}
Expand Down