@@ -4,21 +4,25 @@ import { findDOMNode } from 'react-dom';
44import throttle from 'lodash.throttle' ;
55import raf from 'raf' ;
66import getDisplayName from 'react-display-name' ;
7+ import { Consumer as DragDropContextConsumer } from 'react-dnd/lib/DragDropContext' ;
78import hoist from 'hoist-non-react-statics' ;
89import { noop , intBetween , getCoords } from './util' ;
910
1011const DEFAULT_BUFFER = 150 ;
1112
1213export function createHorizontalStrength ( _buffer ) {
13- return function defaultHorizontalStrength ( { x, w, y, h } , point ) {
14+ return function defaultHorizontalStrength ( {
15+ x, w, y, h,
16+ } , point ) {
1417 const buffer = Math . min ( w / 2 , _buffer ) ;
1518 const inRange = point . x >= x && point . x <= x + w ;
1619 const inBox = inRange && point . y >= y && point . y <= y + h ;
1720
1821 if ( inBox ) {
1922 if ( point . x < x + buffer ) {
2023 return ( point . x - x - buffer ) / buffer ;
21- } else if ( point . x > ( x + w - buffer ) ) {
24+ }
25+ if ( point . x > ( x + w - buffer ) ) {
2226 return - ( x + w - point . x - buffer ) / buffer ;
2327 }
2428 }
@@ -28,15 +32,18 @@ export function createHorizontalStrength(_buffer) {
2832}
2933
3034export function createVerticalStrength ( _buffer ) {
31- return function defaultVerticalStrength ( { y, h, x, w } , point ) {
35+ return function defaultVerticalStrength ( {
36+ y, h, x, w,
37+ } , point ) {
3238 const buffer = Math . min ( h / 2 , _buffer ) ;
3339 const inRange = point . y >= y && point . y <= y + h ;
3440 const inBox = inRange && point . x >= x && point . x <= x + w ;
3541
3642 if ( inBox ) {
3743 if ( point . y < y + buffer ) {
3844 return ( point . y - y - buffer ) / buffer ;
39- } else if ( point . y > ( y + h - buffer ) ) {
45+ }
46+ if ( point . y > ( y + h - buffer ) ) {
4047 return - ( y + h - point . y - buffer ) / buffer ;
4148 }
4249 }
@@ -50,12 +57,13 @@ export const defaultHorizontalStrength = createHorizontalStrength(DEFAULT_BUFFER
5057export const defaultVerticalStrength = createVerticalStrength ( DEFAULT_BUFFER ) ;
5158
5259
53- export default function createScrollingComponent ( WrappedComponent ) {
60+ export function createScrollingComponent ( WrappedComponent ) {
5461 class ScrollingComponent extends Component {
55-
5662 static displayName = `Scrolling(${ getDisplayName ( WrappedComponent ) } )` ;
5763
5864 static propTypes = {
65+ // eslint-disable-next-line react/forbid-prop-types
66+ dragDropManager : PropTypes . object . isRequired ,
5967 onScrollChange : PropTypes . func ,
6068 verticalStrength : PropTypes . func ,
6169 horizontalStrength : PropTypes . func ,
@@ -69,13 +77,33 @@ export default function createScrollingComponent(WrappedComponent) {
6977 strengthMultiplier : 30 ,
7078 } ;
7179
72- static contextTypes = {
73- dragDropManager : PropTypes . object ,
74- } ;
80+ // Update scaleX and scaleY every 100ms or so
81+ // and start scrolling if necessary
82+ updateScrolling = throttle ( ( evt ) => {
83+ const {
84+ left : x , top : y , width : w , height : h ,
85+ } = this . container . getBoundingClientRect ( ) ;
86+ const box = {
87+ x, y, w, h,
88+ } ;
89+ const coords = getCoords ( evt ) ;
90+
91+ // calculate strength
92+ const { horizontalStrength, verticalStrength } = this . props ;
93+ this . scaleX = horizontalStrength ( box , coords ) ;
94+ this . scaleY = verticalStrength ( box , coords ) ;
95+
96+ // start scrolling if we need to
97+ if ( ! this . frame && ( this . scaleX || this . scaleY ) ) {
98+ this . startScrolling ( ) ;
99+ }
100+ } , 100 , { trailing : false } )
75101
76102 constructor ( props , ctx ) {
77103 super ( props , ctx ) ;
78104
105+ this . wrappedInstance = React . createRef ( ) ;
106+
79107 this . scaleX = 0 ;
80108 this . scaleY = 0 ;
81109 this . frame = null ;
@@ -85,16 +113,17 @@ export default function createScrollingComponent(WrappedComponent) {
85113 }
86114
87115 componentDidMount ( ) {
88- this . container = findDOMNode ( this . wrappedInstance ) ;
116+ // eslint-disable-next-line react/no-find-dom-node
117+ this . container = findDOMNode ( this . wrappedInstance . current ) ;
89118 this . container . addEventListener ( 'dragover' , this . handleEvent ) ;
90119 // touchmove events don't seem to work across siblings, so we unfortunately
91120 // have to attach the listeners to the body
92121 window . document . body . addEventListener ( 'touchmove' , this . handleEvent ) ;
93122
94- this . clearMonitorSubscription = this . context
95- . dragDropManager
96- . getMonitor ( )
97- . subscribeToStateChange ( ( ) => this . handleMonitorChange ( ) ) ;
123+ const { dragDropManager } = this . props ;
124+ this . clearMonitorSubscription = dragDropManager
125+ . getMonitor ( )
126+ . subscribeToStateChange ( ( ) => this . handleMonitorChange ( ) ) ;
98127 }
99128
100129 componentWillUnmount ( ) {
@@ -112,7 +141,8 @@ export default function createScrollingComponent(WrappedComponent) {
112141 }
113142
114143 handleMonitorChange ( ) {
115- const isDragging = this . context . dragDropManager . getMonitor ( ) . isDragging ( ) ;
144+ const { dragDropManager } = this . props ;
145+ const isDragging = dragDropManager . getMonitor ( ) . isDragging ( ) ;
116146
117147 if ( ! this . dragging && isDragging ) {
118148 this . dragging = true ;
@@ -134,23 +164,6 @@ export default function createScrollingComponent(WrappedComponent) {
134164 window . document . body . removeEventListener ( 'touchmove' , this . updateScrolling ) ;
135165 }
136166
137- // Update scaleX and scaleY every 100ms or so
138- // and start scrolling if necessary
139- updateScrolling = throttle ( evt => {
140- const { left : x , top : y , width : w , height : h } = this . container . getBoundingClientRect ( ) ;
141- const box = { x, y, w, h } ;
142- const coords = getCoords ( evt ) ;
143-
144- // calculate strength
145- this . scaleX = this . props . horizontalStrength ( box , coords ) ;
146- this . scaleY = this . props . verticalStrength ( box , coords ) ;
147-
148- // start scrolling if we need to
149- if ( ! this . frame && ( this . scaleX || this . scaleY ) ) {
150- this . startScrolling ( ) ;
151- }
152- } , 100 , { trailing : false } )
153-
154167 startScrolling ( ) {
155168 let i = 0 ;
156169 const tick = ( ) => {
@@ -167,7 +180,8 @@ export default function createScrollingComponent(WrappedComponent) {
167180 // mousemove events from a container that also emits a scroll
168181 // event that same frame. So we double the strengthMultiplier and only adjust
169182 // the scroll position at 30fps
170- if ( i ++ % 2 ) {
183+ i += 1 ;
184+ if ( i % 2 ) {
171185 const {
172186 scrollLeft,
173187 scrollTop,
@@ -181,15 +195,15 @@ export default function createScrollingComponent(WrappedComponent) {
181195 ? container . scrollLeft = intBetween (
182196 0 ,
183197 scrollWidth - clientWidth ,
184- scrollLeft + scaleX * strengthMultiplier
198+ scrollLeft + scaleX * strengthMultiplier ,
185199 )
186200 : scrollLeft ;
187201
188202 const newTop = scaleY
189203 ? container . scrollTop = intBetween (
190204 0 ,
191205 scrollHeight - clientHeight ,
192- scrollTop + scaleY * strengthMultiplier
206+ scrollTop + scaleY * strengthMultiplier ,
193207 )
194208 : scrollTop ;
195209
@@ -220,12 +234,12 @@ export default function createScrollingComponent(WrappedComponent) {
220234 horizontalStrength,
221235 onScrollChange,
222236
223- ...props ,
237+ ...props
224238 } = this . props ;
225239
226240 return (
227241 < WrappedComponent
228- ref = { ( ref ) => { this . wrappedInstance = ref ; } }
242+ ref = { this . wrappedInstance }
229243 { ...props }
230244 />
231245 ) ;
@@ -234,3 +248,16 @@ export default function createScrollingComponent(WrappedComponent) {
234248
235249 return hoist ( ScrollingComponent , WrappedComponent ) ;
236250}
251+
252+ export default function createScrollingComponentWithConsumer ( WrappedComponent ) {
253+ const ScrollingComponent = createScrollingComponent ( WrappedComponent ) ;
254+ return props => (
255+ < DragDropContextConsumer >
256+ { ( { dragDropManager } ) => (
257+ dragDropManager === undefined
258+ ? null
259+ : < ScrollingComponent { ...props } dragDropManager = { dragDropManager } />
260+ ) }
261+ </ DragDropContextConsumer >
262+ ) ;
263+ }
0 commit comments