/** * External dependencies */ import { useRef, useState, useEffect } from '@wordpress/element'; import classnames from 'classnames'; import { CSSTransition, TransitionGroup } from 'react-transition-group'; import PropTypes from 'prop-types'; import { debounce } from 'lodash'; /** * Internal dependencies */ import './style.scss'; const Slider = ( { children, animationKey, animate } ) => { const [ height, updateHeight ] = useState( null ); const container = useRef(); const containerClasses = classnames( 'woocommerce-marketing-slider', animate && `animate-${ animate }` ); const style = {}; if ( height ) { style.height = height; } // timeout should be slightly longer than the CSS animation const timeout = 320; const updateSliderHeight = () => { const slide = container.current.querySelector( '.woocommerce-marketing-slider__slide' ); updateHeight( slide.clientHeight ); }; const debouncedUpdateSliderHeight = debounce( updateSliderHeight, 50 ); useEffect( () => { // Update the slider height on Resize window.addEventListener( 'resize', debouncedUpdateSliderHeight ); return () => { window.removeEventListener( 'resize', debouncedUpdateSliderHeight ); }; }, [] ); /** * Fix slider height before a slide enters because slides are absolutely position */ const onEnter = () => { const newSlide = container.current.querySelector( '.slide-enter' ); updateHeight( newSlide.clientHeight ); }; return (