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

#148 Autoplay and stop on hover causes a warning. #149

Merged
merged 4 commits into from
Jul 4, 2017
Merged
Changes from 2 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
17 changes: 13 additions & 4 deletions src/components/Carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class Carousel extends Component {
this.state = {
initialized: false,
selectedItem: props.selectedItem,
hasMount: false
hasMount: false,
isMouseEntered: false
};
}

Expand Down Expand Up @@ -138,7 +139,7 @@ class Carousel extends Component {

if (this.props.stopOnHover && carouselWrapper) {
carouselWrapper.addEventListener('mouseenter', this.stopOnHover);
carouselWrapper.addEventListener('mouseleave', this.autoPlay);
carouselWrapper.addEventListener('mouseleave', this.startOnLeave);
}
}

Expand All @@ -148,7 +149,7 @@ class Carousel extends Component {

if (this.props.stopOnHover && carouselWrapper) {
carouselWrapper.removeEventListener('mouseenter', this.stopOnHover);
carouselWrapper.removeEventListener('mouseleave', this.autoPlay);
carouselWrapper.removeEventListener('mouseleave', this.startOnLeave);
}
}

Expand Down Expand Up @@ -203,9 +204,15 @@ class Carousel extends Component {
}

stopOnHover = () => {
this.setState({isMouseEntered: true});
Copy link
Owner

Choose a reason for hiding this comment

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

Can you please add test cases for these changes?

Copy link
Author

Choose a reason for hiding this comment

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

@leandrowd I've now added tests for the isMouseEntered state change and the stop/start auto play functionality if a mouse enters/leaves.

this.clearAutoPlay();
}

startOnLeave = () => {
this.setState({isMouseEntered: false});
this.autoPlay();
}

navigateWithKeyboard = (e) => {
const { axis } = this.props;
const isHorizontal = axis === 'horizontal';
Expand Down Expand Up @@ -365,7 +372,9 @@ class Carousel extends Component {
selectedItem: position
});

if (this.props.autoPlay) {
// don't reset auto play when stop on hover is enabled, doing so will trigger a call to auto play more than once
// and will result in the interval function not being cleared correctly.
if (this.props.autoPlay && this.state.isMouseEntered === false) {
this.resetAutoPlay();
}
}
Expand Down