2018-07-18 03:00:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-07-14 20:38:57 +00:00
|
|
|
import { createElement, Component, createRef } from '@wordpress/element';
|
2018-07-18 03:00:20 +00:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { CSSTransition, TransitionGroup } from 'react-transition-group';
|
2018-11-15 18:16:23 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2018-07-18 03:00:20 +00:00
|
|
|
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* This component creates slideable content controlled by an animate prop to direct the contents to slide left or right.
|
|
|
|
* All other props are passed to `CSSTransition`. More info at http://reactcommunity.org/react-transition-group/css-transition
|
|
|
|
*/
|
2018-07-18 03:00:20 +00:00
|
|
|
class AnimationSlider extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
animate: null,
|
|
|
|
};
|
|
|
|
this.container = createRef();
|
|
|
|
this.onExited = this.onExited.bind( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
onExited() {
|
2019-03-14 23:38:41 +00:00
|
|
|
const { onExited } = this.props;
|
2018-07-18 03:00:20 +00:00
|
|
|
if ( onExited ) {
|
2019-03-14 23:38:41 +00:00
|
|
|
onExited( this.container.current );
|
2018-07-18 03:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { children, animationKey, animate } = this.props;
|
|
|
|
const containerClasses = classnames(
|
|
|
|
'woocommerce-slide-animation',
|
|
|
|
animate && `animate-${ animate }`
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<div className={ containerClasses } ref={ this.container }>
|
|
|
|
<TransitionGroup>
|
|
|
|
<CSSTransition
|
|
|
|
timeout={ 200 }
|
|
|
|
classNames="slide"
|
|
|
|
key={ animationKey }
|
|
|
|
{ ...this.props }
|
|
|
|
onExited={ this.onExited }
|
|
|
|
>
|
2020-02-14 02:23:21 +00:00
|
|
|
{ ( status ) => children( { status } ) }
|
2018-07-18 03:00:20 +00:00
|
|
|
</CSSTransition>
|
|
|
|
</TransitionGroup>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimationSlider.propTypes = {
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A function returning rendered content with argument status, reflecting `CSSTransition` status.
|
|
|
|
*/
|
2018-07-18 03:00:20 +00:00
|
|
|
children: PropTypes.func.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* A unique identifier for each slideable page.
|
|
|
|
*/
|
2018-07-18 03:00:20 +00:00
|
|
|
animationKey: PropTypes.any.isRequired,
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
|
|
|
* null, 'left', 'right', to designate which direction to slide on a change.
|
|
|
|
*/
|
2018-07-18 03:00:20 +00:00
|
|
|
animate: PropTypes.oneOf( [ null, 'left', 'right' ] ),
|
2018-08-31 17:27:21 +00:00
|
|
|
/**
|
2019-03-14 23:38:41 +00:00
|
|
|
* A function to be executed after a transition is complete, passing the containing ref as the argument.
|
2018-08-31 17:27:21 +00:00
|
|
|
*/
|
2019-03-14 23:38:41 +00:00
|
|
|
onExited: PropTypes.func,
|
2018-07-18 03:00:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default AnimationSlider;
|