Skip to content

Commit 6b3bd20

Browse files
committed
Touch support.
1 parent 917a00d commit 6b3bd20

File tree

6 files changed

+50
-43
lines changed

6 files changed

+50
-43
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-linear-gradient-picker",
3-
"version": "1.0.8",
3+
"version": "1.1.0",
44
"description": "React linear gradient picker",
55
"main": "dist/index.js",
66
"scripts": {

src/components/ColorStop/hooks/useStopDragging.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react';
1+
import { useState } from 'react';
22
import useDragging from '../../hooks/useDragging';
33

44
/**
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/**
2-
* The touch start event, used to distinguish touch / mouse actions
3-
* @type {String}
2+
* The drag supported events
3+
* Object<String>
44
*/
5-
export const TOUCH_START_EVENT = 'touchstart';
5+
export const EVENTS = {
6+
MOUSEDOWN: 'mousedown',
7+
MOUSEMOVE: 'mousemove',
8+
MOUSEUP: 'mouseup',
9+
TOUCHSTART: 'touchstart',
10+
TOUCHMOVE: 'touchmove',
11+
TOUCHEND: 'touchend'
12+
};

src/components/hooks/useDragging/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import { useState, useEffect } from 'react';
22
import { noop } from '../../../lib';
3-
import { TOUCH_START_EVENT } from './constants';
3+
import { EVENTS } from './constants';
44

55
const DRAG_HANDLERS = {
66
MOUSE: {
77
stop: (e) => {
88
e.preventDefault();
9-
e.stopPropagation();
9+
e.stopPropagation();
1010
},
1111
coordinates: ({ clientX, clientY }) => ({ clientX, clientY }),
12-
dragEvent: { name: 'mousemove' },
13-
dragEndEvent: { name: 'mouseup' }
12+
dragEvent: { name: EVENTS.MOUSEMOVE },
13+
dragEndEvent: { name: EVENTS.MOUSEUP }
1414
},
1515
TOUCH: {
1616
stop: noop,
1717
coordinates: (e) => {
1818
const [touch] = e.touches;
1919
return { clientX: touch.clientX, clientY: touch.clientY };
2020
},
21-
dragEvent: { name: 'touchmove', options: { cancelable: true, passive: true } },
22-
dragEndEvent: { name: 'touchend' }
21+
dragEvent: { name: EVENTS.TOUCHMOVE, options: { cancelable: true, passive: true } },
22+
dragEndEvent: { name: EVENTS.TOUCHEND }
2323
}
2424
};
2525

26-
const isTouch = (e) => e.type === TOUCH_START_EVENT;
26+
const isTouch = (e) => e.type === EVENTS.TOUCHSTART;
2727

2828
const useDragging = ({ onDragStart = noop, onDrag, onDragEnd = noop }) => {
2929
const [context, setContext] = useState({});
3030
const [dragging, setDragging] = useState(false);
3131

32-
const handleMouseDown = (e) => {
32+
const dragHandler = (e) => {
3333
const handler = isTouch(e) ? DRAG_HANDLERS.TOUCH : DRAG_HANDLERS.MOUSE;
3434

3535
handler.stop(e);
@@ -70,13 +70,13 @@ const useDragging = ({ onDragStart = noop, onDrag, onDragEnd = noop }) => {
7070
}
7171

7272
return () => {
73-
document.removeEventListener(dragEvent.name, handleDrag);
73+
document.removeEventListener(dragEvent.name, handleDrag, dragEndEvent.options);
7474
document.removeEventListener(dragEndEvent.name, deactivate);
7575
};
7676
}, [dragging]);
7777

7878
return [
79-
handleMouseDown,
79+
dragHandler,
8080
activate,
8181
deactivate
8282
];

stories/GradientPicker/index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import 'rc-color-picker/assets/index.css';
99
/**
1010
* (c) https://stackoverflow.com/questions/21646738/convert-hex-to-rgba
1111
*/
12-
function addOpacityToHex(hex, a = 1){
13-
var c;
14-
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
15-
c= hex.substring(1).split('');
16-
if(c.length== 3){
17-
c= [c[0], c[0], c[1], c[1], c[2], c[2]];
18-
}
19-
c= '0x'+c.join('');
20-
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+','+a+')';
21-
}
22-
if (/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/.test(hex)) { /** RGB color */
23-
return hex.replace('rgb', 'rgba').replace(')', `, ${a})`);
24-
}
25-
throw new Error('Bad Hex');
12+
function addOpacityToHex(hex, a = 1) {
13+
let c;
14+
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
15+
c = hex.substring(1).split('');
16+
if (c.length === 3) {
17+
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
18+
}
19+
c = '0x' + c.join('');
20+
return 'rgba(' + [(c >> 16) & 255, (c >> 8) & 255, c & 255].join(',') + ',' + a + ')';
21+
}
22+
if (/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/.test(hex)) { /** RGB color */
23+
return hex.replace('rgb', 'rgba').replace(')', `, ${a})`);
24+
}
25+
throw new Error('Bad Hex');
2626
}
2727

2828
const WrapperPropTypes = {

stories/Popover/index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ import './index.css';
77
/**
88
* (c) https://stackoverflow.com/questions/21646738/convert-hex-to-rgba
99
*/
10-
function addOpacityToHex(hex, a = 1){
11-
var c;
12-
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
13-
c= hex.substring(1).split('');
14-
if(c.length== 3){
15-
c= [c[0], c[0], c[1], c[1], c[2], c[2]];
16-
}
17-
c= '0x'+c.join('');
18-
return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+','+a+')';
19-
}
20-
if (/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/.test(hex)) { /** RGB color */
21-
return hex.replace('rgb', 'rgba').replace(')', `, ${a})`);
22-
}
23-
throw new Error('Bad Hex');
10+
function addOpacityToHex(hex, a = 1) {
11+
let c;
12+
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
13+
c = hex.substring(1).split('');
14+
if (c.length === 3) {
15+
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
16+
}
17+
c = '0x' + c.join('');
18+
return 'rgba(' + [(c >> 16) & 255, (c >> 8) & 255, c & 255].join(',') + ',' + a + ')';
19+
}
20+
if (/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/.test(hex)) { /** RGB color */
21+
return hex.replace('rgb', 'rgba').replace(')', `, ${a})`);
22+
}
23+
throw new Error('Bad Hex');
2424
}
2525

2626
const WrappedSketchPicker = ({ onSelect, ...rest }) => (

0 commit comments

Comments
 (0)