Skip to content

Commit af7a1e4

Browse files
committed
init.
1 parent 9afb9ee commit af7a1e4

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

src/components/AnglePicker/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const AnglePicker = ({ angle, setAngle, size = 48, snap = 5 }) => {
3636
});
3737

3838
return (
39-
<div className="ap" ref={pickerRef} onMouseDown={drag} style={sizeStyle}>
39+
<div className="ap" ref={pickerRef} onMouseDown={drag} onTouchStart={drag} style={sizeStyle}>
4040
<span className="apc" style={{ transform: `rotate(${angle}deg)`, height: size }}>
4141
<i className="aph"/>
4242
</span>

src/components/ColorStop/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const ColorStop = ({ stop, limits, onPosChange, onDeleteColor, onDragStart = noo
2222
<div className={isActive ? 'cs active' : 'cs'}
2323
ref={colorStopRef}
2424
style={{ left: offset }}
25-
onMouseDown={drag}>
25+
onMouseDown={drag}
26+
onTouchStart={drag}>
2627
<div style={{ backgroundColor: color }}/>
2728
</div>
2829
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* The touch start event, used to distinguish touch / mouse actions
3+
* @type {String}
4+
*/
5+
export const TOUCH_START_EVENT = 'touchstart';

src/components/hooks/useDragging/index.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,47 @@
11
import { useState, useEffect } from 'react';
22
import { noop } from '../../../lib';
3+
import { TOUCH_START_EVENT } from './constants';
4+
5+
const DRAG_HANLDERS = {
6+
MOUSE: {
7+
stop: (e) => e.preventDefault() && e.stopPropagation(),
8+
coordinates: ({ clientX, clientY }) => ({ clientX, clientY }),
9+
dragEvent: { name: 'mousemove' },
10+
dragEndEvent: { name: 'mouseup' }
11+
},
12+
TOUCH: {
13+
stop: noop,
14+
coordinates: (e) => {
15+
const [touch] = e.touches;
16+
return { clientX: touch.pageX, clientY: touch.pageY };
17+
},
18+
dragEvent: { name: 'touchmove', options: { cancelable: true, passive: true } },
19+
dragEndEvent: { name: 'touchend' }
20+
}
21+
};
22+
23+
const isTouch = (e) => e.type === TOUCH_START_EVENT;
324

425
const useDragging = ({ onDragStart = noop, onDrag, onDragEnd = noop }) => {
526
const [context, setContext] = useState({});
27+
const [handler, setHandler] = useState();
628
const [dragging, setDragging] = useState(false);
729

830
const handleMouseDown = (e) => {
9-
e.preventDefault();
10-
e.stopPropagation();
31+
const handler = isTouch(e) ? DRAG_HANLDERS.TOUCH : DRAG_HANLDERS.MOUSE;
32+
console.log('Drag start', handler);
33+
handler.stop(e);
1134

12-
if (!e.button) activate(e);
35+
if (!e.button) activate(e, handler);
1336
};
1437

15-
const activate = (e) => {
38+
const activate = (e, handler) => {
1639
setDragging(true);
40+
handler && setHandler(handler);
41+
42+
const coordinates = handler ? handler.coordinates(e) : e;
1743

18-
onDragStart(e);
44+
onDragStart(coordinates);
1945
};
2046

2147
const deactivate = () => {
@@ -28,21 +54,25 @@ const useDragging = ({ onDragStart = noop, onDrag, onDragEnd = noop }) => {
2854
const handleDrag = (e) => {
2955
if (!dragging) return;
3056

31-
context.change = onDrag(e);
57+
context.change = onDrag(handler.coordinates(e));
3258
};
3359

3460
useEffect(() => {
35-
if (dragging) {
61+
if (!handler) return;
62+
63+
const { dragEvent, dragEndEvent } = handler;
3664

37-
document.addEventListener('mousemove', handleDrag);
38-
document.addEventListener('mouseup', deactivate);
65+
if (dragging) {
66+
console.log('Bindiing:', handler)
67+
document.addEventListener(dragEvent.name, handleDrag, dragEndEvent.options);
68+
document.addEventListener(dragEndEvent.name, deactivate);
3969
}
4070

4171
return () => {
42-
document.removeEventListener('mousemove', handleDrag);
43-
document.removeEventListener('mouseup', deactivate);
72+
document.removeEventListener(dragEvent.name, handleDrag);
73+
document.removeEventListener(dragEndEvent.name, deactivate);
4474
};
45-
}, [dragging]);
75+
}, [dragging, handler]);
4676

4777
return [
4878
handleMouseDown,

0 commit comments

Comments
 (0)