Skip to content

Commit

Permalink
Add onTransitionEnd callback. Resolve #111
Browse files Browse the repository at this point in the history
  • Loading branch information
amio committed Oct 24, 2018
1 parent f6d06ef commit 3bbbaad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ All attributes are optional.
- `auto` {Boolean} `true` or `false` (`false` by default) toggle auto sliding.
- `interval` {Number} (`4000`ms by default) interval for auto sliding.
- `duration` {Number} (`300`ms by default) duration for animation.
- `onTransitionEnd` {Function({ prev: HTMLElement, current: HTMLElement, next: HTMLElement})} on frames transition end callback.
- `widgets` {Array of ReactClass} Indicator and switcher could be various,
so it's not builtin. Here's some example custom widgets
([dots indicator](src/indicator-dots.js),
Expand Down
15 changes: 11 additions & 4 deletions src/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,16 @@ class Carousel extends React.Component {
}, cb)
}

prepareSiblingFrames () {
const siblings = {
getSiblingFrames () {
return {
current: this.refs['f' + this.getFrameId()],
prev: this.refs['f' + this.getFrameId('prev')],
next: this.refs['f' + this.getFrameId('next')]
}
}

prepareSiblingFrames () {
const siblings = this.getSiblingFrames()

if (!this.props.loop) {
this.state.current === 0 && (siblings.prev = undefined)
Expand Down Expand Up @@ -289,7 +293,7 @@ class Carousel extends React.Component {

transitFramesTowards (direction) {
const { prev, current, next } = this.state.movingFrames
const { duration, axis } = this.props
const { duration, axis, onTransitionEnd } = this.props

let newCurrentId = this.state.current
switch (direction) {
Expand Down Expand Up @@ -324,6 +328,8 @@ class Carousel extends React.Component {
}
}

onTransitionEnd && setTimeout(() => onTransitionEnd(this.getSiblingFrames()), duration)

this.setState({ current: newCurrentId })
}

Expand Down Expand Up @@ -382,7 +388,8 @@ Carousel.propTypes = {
widgets: PropTypes.arrayOf(PropTypes.func),
frames: PropTypes.arrayOf(PropTypes.element),
style: PropTypes.object,
minMove: PropTypes.number
minMove: PropTypes.number,
onTransitionEnd: PropTypes.func
}

Carousel.defaultProps = {
Expand Down

0 comments on commit 3bbbaad

Please sign in to comment.