|
@@ -42,7 +42,7 @@ const useCursorPosition: UseCursorPositionType = (time = defaultTime) => {
|
|
|
const entered = useRef(false);
|
|
|
const isDown = useRef(false);
|
|
|
|
|
|
- const onMoveEvent = useCallback((e: MouseEvent | Event): void => {
|
|
|
+ const onMouseMoveEvent = useCallback((e: MouseEvent | Event): void => {
|
|
|
if (element) {
|
|
|
const {
|
|
|
clientX,
|
|
@@ -63,39 +63,84 @@ const useCursorPosition: UseCursorPositionType = (time = defaultTime) => {
|
|
|
}
|
|
|
}, [element]);
|
|
|
|
|
|
+ const onTouchMoveEvent = useCallback((e: TouchEvent | Event): void => {
|
|
|
+ if (element) {
|
|
|
+ const {
|
|
|
+ clientX,
|
|
|
+ clientY,
|
|
|
+ screenX,
|
|
|
+ screenY,
|
|
|
+ } = (e as TouchEvent).touches[0];
|
|
|
+ const coordinate = getAbsoluteCoordinate(element, e as TouchEvent);
|
|
|
+
|
|
|
+ setState({
|
|
|
+ x: coordinate.x,
|
|
|
+ y: coordinate.y,
|
|
|
+ clientX,
|
|
|
+ clientY,
|
|
|
+ screenX,
|
|
|
+ screenY,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }, [element]);
|
|
|
+
|
|
|
useEffect(() => {
|
|
|
+ let mouseSubscription: any = null;
|
|
|
+ let touchSubscription: any = null;
|
|
|
+
|
|
|
+ const onEnter = (): void => {
|
|
|
+ entered.current = true;
|
|
|
+ };
|
|
|
+
|
|
|
+ const onLeave = (): void => {
|
|
|
+ entered.current = false;
|
|
|
+ setState(initialState);
|
|
|
+ };
|
|
|
+
|
|
|
+ const onDown = (e: any): void => {
|
|
|
+ entered.current = true;
|
|
|
+ isDown.current = true;
|
|
|
+ onMouseMoveEvent(e);
|
|
|
+ };
|
|
|
+
|
|
|
+ const onTouch = (e: any): void => {
|
|
|
+ entered.current = true;
|
|
|
+ isDown.current = true;
|
|
|
+ onTouchMoveEvent(e);
|
|
|
+ };
|
|
|
+
|
|
|
+ const onUp = (): void => {
|
|
|
+ entered.current = false;
|
|
|
+ isDown.current = false;
|
|
|
+ setState(initialState);
|
|
|
+ if (mouseSubscription) mouseSubscription.unsubscribe();
|
|
|
+ if (touchSubscription) touchSubscription.unsubscribe();
|
|
|
+ };
|
|
|
+
|
|
|
if (element) {
|
|
|
- let subscription: any = null;
|
|
|
- const onEnter = (): void => {
|
|
|
- entered.current = true;
|
|
|
- };
|
|
|
-
|
|
|
- const onLeave = (): void => {
|
|
|
- entered.current = false;
|
|
|
- setState(initialState);
|
|
|
- };
|
|
|
-
|
|
|
- const onDown = (e: any): void => {
|
|
|
- entered.current = true;
|
|
|
- isDown.current = true;
|
|
|
- onMoveEvent(e);
|
|
|
- };
|
|
|
-
|
|
|
- subscription = fromEvent(element, 'mousemove').pipe(throttleTime(time)).subscribe(onMoveEvent);
|
|
|
-
|
|
|
- const onUp = (): void => {
|
|
|
- entered.current = false;
|
|
|
- isDown.current = false;
|
|
|
- setState(initialState);
|
|
|
- if (subscription) subscription.unsubscribe();
|
|
|
- };
|
|
|
+ mouseSubscription = fromEvent(element, 'mousemove').pipe(throttleTime(time)).subscribe(onMouseMoveEvent);
|
|
|
+ touchSubscription = fromEvent(element, 'touchmove').pipe(throttleTime(time)).subscribe(onTouchMoveEvent);
|
|
|
|
|
|
const addEvent = element.addEventListener.bind(element);
|
|
|
addEvent('mouseenter', onEnter);
|
|
|
addEvent('mouseleave', onLeave);
|
|
|
addEvent('mousedown', onDown);
|
|
|
+ addEvent('touchstart', onTouch);
|
|
|
addEvent('mouseup', onUp);
|
|
|
+ addEvent('touchend', onUp);
|
|
|
}
|
|
|
+
|
|
|
+ return (): void => {
|
|
|
+ if (element) {
|
|
|
+ const removeEvent = element.removeEventListener.bind(element);
|
|
|
+ removeEvent('mouseenter', onEnter);
|
|
|
+ removeEvent('mouseleave', onLeave);
|
|
|
+ removeEvent('mousedown', onDown);
|
|
|
+ removeEvent('touchstart', onTouch);
|
|
|
+ removeEvent('mouseup', onUp);
|
|
|
+ removeEvent('touchend', onUp);
|
|
|
+ }
|
|
|
+ };
|
|
|
}, [element]);
|
|
|
|
|
|
return [state, setElement];
|