Skip to content

Commit 917a00d

Browse files
committed
stable.
1 parent af7a1e4 commit 917a00d

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/components/ColorStop/hooks/useStopDragging.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const useStopDragging = ({ limits, stop, initialPos, colorStopRef, onPosChange,
3636
onPosChange({ id, offset: limitedPos });
3737
};
3838

39-
const [handleMouseDown, activate] = useDragging({
39+
const [drag] = useDragging({
4040
onDragStart: ({ clientX }) => {
4141
setPosStart(clientX);
4242

@@ -46,13 +46,8 @@ const useStopDragging = ({ limits, stop, initialPos, colorStopRef, onPosChange,
4646
onDragEnd: () => onDragEnd(stop.id)
4747
});
4848

49-
useEffect(() => {
50-
const { pointX } = stop;
51-
pointX && activate({ clientX: pointX });
52-
}, []);
53-
5449
return [
55-
handleMouseDown,
50+
drag,
5651
];
5752
};
5853

src/components/hooks/useDragging/index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { useState, useEffect } from 'react';
22
import { noop } from '../../../lib';
33
import { TOUCH_START_EVENT } from './constants';
44

5-
const DRAG_HANLDERS = {
5+
const DRAG_HANDLERS = {
66
MOUSE: {
7-
stop: (e) => e.preventDefault() && e.stopPropagation(),
7+
stop: (e) => {
8+
e.preventDefault();
9+
e.stopPropagation();
10+
},
811
coordinates: ({ clientX, clientY }) => ({ clientX, clientY }),
912
dragEvent: { name: 'mousemove' },
1013
dragEndEvent: { name: 'mouseup' }
@@ -13,7 +16,7 @@ const DRAG_HANLDERS = {
1316
stop: noop,
1417
coordinates: (e) => {
1518
const [touch] = e.touches;
16-
return { clientX: touch.pageX, clientY: touch.pageY };
19+
return { clientX: touch.clientX, clientY: touch.clientY };
1720
},
1821
dragEvent: { name: 'touchmove', options: { cancelable: true, passive: true } },
1922
dragEndEvent: { name: 'touchend' }
@@ -24,24 +27,21 @@ const isTouch = (e) => e.type === TOUCH_START_EVENT;
2427

2528
const useDragging = ({ onDragStart = noop, onDrag, onDragEnd = noop }) => {
2629
const [context, setContext] = useState({});
27-
const [handler, setHandler] = useState();
2830
const [dragging, setDragging] = useState(false);
2931

3032
const handleMouseDown = (e) => {
31-
const handler = isTouch(e) ? DRAG_HANLDERS.TOUCH : DRAG_HANLDERS.MOUSE;
32-
console.log('Drag start', handler);
33+
const handler = isTouch(e) ? DRAG_HANDLERS.TOUCH : DRAG_HANDLERS.MOUSE;
34+
3335
handler.stop(e);
3436

3537
if (!e.button) activate(e, handler);
3638
};
3739

3840
const activate = (e, handler) => {
3941
setDragging(true);
40-
handler && setHandler(handler);
41-
42-
const coordinates = handler ? handler.coordinates(e) : e;
42+
context.handler = handler;
4343

44-
onDragStart(coordinates);
44+
onDragStart(handler.coordinates(e));
4545
};
4646

4747
const deactivate = () => {
@@ -52,18 +52,19 @@ const useDragging = ({ onDragStart = noop, onDrag, onDragEnd = noop }) => {
5252
};
5353

5454
const handleDrag = (e) => {
55+
const { handler } = context;
5556
if (!dragging) return;
5657

5758
context.change = onDrag(handler.coordinates(e));
5859
};
5960

6061
useEffect(() => {
62+
const { handler } = context;
6163
if (!handler) return;
6264

6365
const { dragEvent, dragEndEvent } = handler;
6466

6567
if (dragging) {
66-
console.log('Bindiing:', handler)
6768
document.addEventListener(dragEvent.name, handleDrag, dragEndEvent.options);
6869
document.addEventListener(dragEndEvent.name, deactivate);
6970
}
@@ -72,7 +73,7 @@ const useDragging = ({ onDragStart = noop, onDrag, onDragEnd = noop }) => {
7273
document.removeEventListener(dragEvent.name, handleDrag);
7374
document.removeEventListener(dragEndEvent.name, deactivate);
7475
};
75-
}, [dragging, handler]);
76+
}, [dragging]);
7677

7778
return [
7879
handleMouseDown,

0 commit comments

Comments
 (0)