11import { animateScroll , renderSlides } from '#utils' ;
22import PropTypes from 'prop-types' ;
3- import React , { Children , Component , createRef } from 'react' ;
3+ import React , { Children , Component , forwardRef } from 'react' ;
44import cx from 'classnames' ;
55import debounce from 'lodash/debounce' ;
66
7- class ScrollSlider extends Component {
7+ export class ScrollSlider extends Component {
88 constructor ( props ) {
99 super ( props ) ;
1010 this . _verifyActiveItemDebounce = debounce ( this . _verifyActiveItem , 25 ) ;
@@ -13,7 +13,6 @@ class ScrollSlider extends Component {
1313 this . activeItem = 0 ;
1414 this . shouldMoveToItem = false ;
1515 this . isPreventingScroll = false ;
16- this . containerRef = createRef ( ) ;
1716 }
1817
1918 componentDidMount ( ) {
@@ -36,6 +35,7 @@ class ScrollSlider extends Component {
3635 itemsToShow,
3736 limitScroll,
3837 setItemsLength,
38+ containerRef,
3939 ...othersProps
4040 } = this . props ;
4141
@@ -52,7 +52,7 @@ class ScrollSlider extends Component {
5252
5353 return (
5454 < div
55- ref = { this . containerRef }
55+ ref = { containerRef }
5656 className = { cx ( 'slider' , 'scrollSliderContainer' , className ) }
5757 onScroll = { this . _handleScroll }
5858 { ...othersProps }
@@ -63,14 +63,14 @@ class ScrollSlider extends Component {
6363 }
6464
6565 _moveToItem ( ) {
66- const el = this . containerRef . current ;
66+ const { activeItem, itemsToShow, isRTL, containerRef } = this . props ;
67+ const el = containerRef . current ;
6768
6869 if ( ! el ) {
6970 return null ;
7071 }
7172
7273 const { offsetWidth } = el ;
73- const { activeItem, itemsToShow, isRTL } = this . props ;
7474
7575 const itemToMoveTo = isRTL
7676 ? this . itemsLength - 2 - activeItem
@@ -84,7 +84,7 @@ class ScrollSlider extends Component {
8484
8585 /* Don't work on Chrome, but work on iOS */
8686 _forceScrollSnapStop ( newPosition , newIndex ) {
87- const el = this . containerRef . current ;
87+ const el = this . props . containerRef . current ;
8888
8989 if ( ! el ) {
9090 return null ;
@@ -103,8 +103,8 @@ class ScrollSlider extends Component {
103103 }
104104
105105 _verifyActiveItem ( ) {
106- const { goTo, itemsToShow, isRTL } = this . props ;
107- const { offsetWidth, scrollLeft } = this . containerRef . current ;
106+ const { goTo, itemsToShow, isRTL, containerRef } = this . props ;
107+ const { offsetWidth, scrollLeft } = containerRef . current ;
108108
109109 const activeFraction =
110110 Math . round ( ( scrollLeft / offsetWidth ) * 100 ) / 100 ;
@@ -135,10 +135,10 @@ class ScrollSlider extends Component {
135135 return null ;
136136 }
137137
138- const { limitScroll, itemsToShow } = this . props ;
138+ const { limitScroll, itemsToShow, containerRef } = this . props ;
139139
140140 if ( limitScroll ) {
141- const { offsetWidth, scrollLeft } = this . containerRef . current ;
141+ const { offsetWidth, scrollLeft } = containerRef . current ;
142142
143143 // When scrolling too fast, we want to prevent scrolling 2 slides at once...
144144 const newPrevIndex = this . activeItem - 1 ;
@@ -167,6 +167,12 @@ ScrollSlider.propTypes = {
167167 setItemsLength : PropTypes . func . isRequired ,
168168 goTo : PropTypes . func . isRequired ,
169169 limitScroll : PropTypes . bool ,
170+ containerRef : PropTypes . oneOfType ( [
171+ PropTypes . func ,
172+ PropTypes . shape ( { current : PropTypes . elementType } ) ,
173+ ] ) ,
170174} ;
171175
172- export default ScrollSlider ;
176+ export default forwardRef ( function ScrollSliderForwardingRef ( props , ref ) {
177+ return < ScrollSlider containerRef = { ref } { ...props } /> ;
178+ } ) ;
0 commit comments