-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathtypes.ts
More file actions
171 lines (160 loc) · 3.88 KB
/
types.ts
File metadata and controls
171 lines (160 loc) · 3.88 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as React from "react";
export const LEFT = "Left";
export const RIGHT = "Right";
export const UP = "Up";
export const DOWN = "Down";
export type HandledEvents = React.MouseEvent | TouchEvent | MouseEvent;
export type Vector2 = [number, number];
export type SwipeDirections =
| typeof LEFT
| typeof RIGHT
| typeof UP
| typeof DOWN;
export interface SwipeEventData {
/**
* Absolute displacement of swipe in x. Math.abs(deltaX);
*/
absX: number;
/**
* Absolute displacement of swipe in y. Math.abs(deltaY);
*/
absY: number;
/**
* Displacement of swipe in x. (current.x - initial.x)
*/
deltaX: number;
/**
* Displacement of swipe in y. (current.y - initial.y)
*/
deltaY: number;
/**
* Direction of swipe - Left | Right | Up | Down
*/
dir: SwipeDirections;
/**
* Source event.
*/
event: HandledEvents;
/**
* True for the first event of a tracked swipe.
*/
first: boolean;
/**
* Location where swipe started - [x, y].
*/
initial: Vector2;
/**
* "Absolute velocity" (speed) - √(absX^2 + absY^2) / time
*/
velocity: number;
/**
* Velocity per axis - [ deltaX/time, deltaY/time ]
*/
vxvy: Vector2;
}
export type SwipeCallback = (eventData: SwipeEventData) => void;
export type TapCallback = ({ event }: { event: HandledEvents }) => void;
export type SwipeableDirectionCallbacks = {
/**
* Called after a DOWN swipe
*/
onSwipedDown: SwipeCallback;
/**
* Called after a LEFT swipe
*/
onSwipedLeft: SwipeCallback;
/**
* Called after a RIGHT swipe
*/
onSwipedRight: SwipeCallback;
/**
* Called after a UP swipe
*/
onSwipedUp: SwipeCallback;
};
export type SwipeableCallbacks = SwipeableDirectionCallbacks & {
/**
* Called at start of a tracked swipe.
*/
onSwipeStart: SwipeCallback;
/**
* Called after any swipe.
*/
onSwiped: SwipeCallback;
/**
* Called for each move event during a tracked swipe.
*/
onSwiping: SwipeCallback;
/**
* Called after a tap. A touch under the min distance, `delta`.
*/
onTap: TapCallback;
/**
* Called for `touchstart` and `mousedown`.
*/
onTouchStartOrOnMouseDown: TapCallback;
/**
* Called for `touchend` and `mouseup`.
*/
onTouchEndOrOnMouseUp: TapCallback;
};
// Configuration Options
export type ConfigurationOptionDelta =
| number
| { [key in Lowercase<SwipeDirections>]?: number };
export interface ConfigurationOptions {
/**
* Min distance(px) before a swipe starts. **Default**: `10`
*/
delta: ConfigurationOptionDelta;
/**
* Prevents scroll during swipe in most cases. **Default**: `false`
*/
preventScrollOnSwipe: boolean | ((eventData: SwipeEventData) => boolean);
/**
* Set a rotation angle. **Default**: `0`
*/
rotationAngle: number;
/**
* Track mouse input. **Default**: `false`
*/
trackMouse: boolean;
/**
* Track touch input. **Default**: `true`
*/
trackTouch: boolean;
/**
* Allowable duration of a swipe (ms). **Default**: `Infinity`
*/
swipeDuration: number;
/**
* Options for touch event listeners
*/
touchEventOptions: { passive: boolean };
}
export type SwipeableProps = Partial<SwipeableCallbacks & ConfigurationOptions>;
export type SwipeablePropsWithDefaultOptions = Partial<SwipeableCallbacks> &
ConfigurationOptions;
export interface SwipeableHandlers {
ref(element: HTMLElement | null): void;
onMouseDown?(event: React.MouseEvent): void;
}
export type SwipeableState = {
cleanUpTouch?: () => void;
el?: HTMLElement;
eventData?: SwipeEventData;
first: boolean;
initial: Vector2;
start: number;
swiping: boolean;
xy: Vector2;
};
export type StateSetter = (
state: SwipeableState,
props: SwipeablePropsWithDefaultOptions
) => SwipeableState;
export type Setter = (stateSetter: StateSetter) => void;
export type AttachTouch = (
el: HTMLElement,
props: SwipeablePropsWithDefaultOptions
) => () => void;