Skip to content

Commit 818a8a7

Browse files
committed
Updated dependencies and support for react-dnd@6
1 parent 5177ba3 commit 818a8a7

File tree

9 files changed

+115
-79
lines changed

9 files changed

+115
-79
lines changed

.babelrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

.babelrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
presets: [
3+
'@babel/preset-env',
4+
'@babel/preset-react',
5+
],
6+
plugins: [
7+
'@babel/plugin-proposal-class-properties',
8+
],
9+
}

.eslintrc

Lines changed: 0 additions & 13 deletions
This file was deleted.

.eslintrc.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
parser: 'babel-eslint',
3+
extends: 'airbnb',
4+
5+
rules: {
6+
'react/jsx-filename-extension': 0,
7+
'no-unused-vars': 0,
8+
'react/prop-types': 0,
9+
},
10+
11+
env: {
12+
browser: true,
13+
mocha: true
14+
},
15+
}

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
examples
22
node_modules
3-
src
3+
#src
44
test
55

66
.babelrc

package.json

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,35 @@
3838
},
3939
"license": "MIT",
4040
"dependencies": {
41-
"hoist-non-react-statics": "^1.2.0",
41+
"hoist-non-react-statics": "^3.1.0",
4242
"lodash.throttle": "^4.0.1",
4343
"prop-types": "^15.5.9",
4444
"raf": "^3.2.0",
45-
"react-display-name": "^0.2.0"
45+
"react": "^16.3.0",
46+
"react-display-name": "^0.2.0",
47+
"react-dom": "^16.3.0"
4648
},
4749
"devDependencies": {
48-
"babel-cli": "^6.4.5",
49-
"babel-eslint": "^6.0.4",
50-
"babel-preset-es2015": "^6.3.13",
51-
"babel-preset-react": "^6.5.0",
52-
"babel-preset-stage-1": "^6.3.13",
53-
"babel-register": "^6.4.3",
54-
"chai": "^3.4.1",
55-
"eslint": "^2.12.0",
56-
"eslint-config-airbnb": "^9.0.1",
57-
"eslint-plugin-import": "^1.8.1",
58-
"eslint-plugin-jsx-a11y": "^1.4.2",
59-
"eslint-plugin-react": "^5.1.1",
50+
"@babel/cli": "^7.0.0",
51+
"@babel/core": "^7.0.0",
52+
"@babel/plugin-proposal-class-properties": "^7.0.0",
53+
"@babel/preset-env": "^7.0.0",
54+
"@babel/preset-react": "^7.0.0",
55+
"@babel/register": "^7.0.0",
56+
"babel-eslint": "^10.0.1",
57+
"chai": "^4.2.0",
58+
"eslint": "^5.9.0",
59+
"eslint-config-airbnb": "^17.1.0",
60+
"eslint-plugin-import": "^2.14.0",
61+
"eslint-plugin-jsx-a11y": "^6.1.2",
62+
"eslint-plugin-react": "^7.11.1",
6063
"in-publish": "^2.0.0",
61-
"mocha": "^2.3.4",
62-
"react": "^15.1.0",
63-
"react-dom": "^15.1.0",
64-
"sinon": "^1.17.2",
65-
"sinon-chai": "^2.8.0"
64+
"mocha": "^5.2.0",
65+
"react-dnd": "^6.0.0",
66+
"sinon": "^7.1.1",
67+
"sinon-chai": "^3.2.0"
68+
},
69+
"peerDependencies": {
70+
"react-dnd": "^6.0.0"
6671
}
6772
}

src/index.js

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ import { findDOMNode } from 'react-dom';
44
import throttle from 'lodash.throttle';
55
import raf from 'raf';
66
import getDisplayName from 'react-display-name';
7+
import { Consumer as DragDropContextConsumer } from 'react-dnd/lib/DragDropContext';
78
import hoist from 'hoist-non-react-statics';
89
import { noop, intBetween, getCoords } from './util';
910

1011
const DEFAULT_BUFFER = 150;
1112

1213
export 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

3034
export 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
5057
export 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+
}

src/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function noop() {
55

66
export function intBetween(min, max, val) {
77
return Math.floor(
8-
Math.min(max, Math.max(min, val))
8+
Math.min(max, Math.max(min, val)),
99
);
1010
}
1111

test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--require babel-register
1+
--require @babel/register
22
--require test/setup
33
--check-leaks
44
--throw-deprecation

0 commit comments

Comments
 (0)