-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
73 lines (63 loc) · 2.12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {createSignal} from "solid-js";
import {useEventListener} from "../use-event-listener";
export type UseMouseOptions = {
/**
* @default "page"
* */
type: 'page' | 'client',
/**
* @default true
* */
touch: boolean
}
/**
* It returns an object with the current x and y coordinates of the mouse, whether or not the user is touching the screen,
* and the source of the event (mouse or touch)
* @param options - Partial<UseMouseOptions> = {}
* @returns An object with the following properties:
* x: number | null
* y: number | null
* isTouching: boolean | null
* sourceType: "touch" | "mouse" | null
*/
export const useMouse = (options: Partial<UseMouseOptions> = {}) => {
const {type = 'page', touch = true} = options;
const passive = {passive: true};
const [sourceType, setSourceType] = createSignal<"touch" | "mouse" | null>(null);
const [x, setX] = createSignal<number | null>(null);
const [y, setY] = createSignal<number | null>(null);
const [isTouching, setIsTouching] = createSignal<boolean | null>(null);
const setCoordinates = (xCord: number, yCord: number) => {
setX(xCord)
setY(yCord)
}
const touchHandler = (event: TouchEvent) => {
if (event.touches.length > 0) {
const touch = event.touches[0]
if (type === 'page') {
setCoordinates(touch.pageX, touch.pageY);
} else if (type === 'client') {
setCoordinates(touch.clientX, touch.clientY);
}
setIsTouching(true)
setSourceType('touch')
}
}
const mouseHandler = (event: MouseEvent) => {
if (type === 'page') {
setCoordinates(event.pageX, event.pageY);
} else if (type === 'client') {
setCoordinates(event.clientX, event.clientY);
}
setSourceType('mouse');
}
const resetTouch = () => setIsTouching(false);
useEventListener('mousemove', mouseHandler, passive)
useEventListener('dragover', mouseHandler, passive)
if (touch) {
useEventListener('touchstart', touchHandler, passive)
useEventListener('touchmove', touchHandler, passive)
useEventListener('touchend', resetTouch, passive)
}
return {x, y, isTouching, sourceType};
}