-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
useSwipeableIndexes.ts
177 lines (154 loc) · 4.23 KB
/
useSwipeableIndexes.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
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
172
173
174
175
176
177
import { useCallback, useReducer } from "react";
import {
SwipeableHandlers,
SwipeEventData,
useSwipeable,
} from "react-swipeable";
interface State {
/**
* Boolean if the user is currently swiping. This is used to disable transitions
* as needed and apply the `transform` to the panels.
*/
swiping: boolean;
/**
* The current swipe distance. When the user is not swiping, this will be the
* empty string.
*/
distance: string;
/**
* The current active index for the tabs. This is now controlled instead of allowing
* the TabsManager to handle it all since we'll need to update the active index once
* the user completes a swipe.
*/
activeIndex: number;
}
const CHANGE = "CHANGE";
const MOVE = "MOVE";
const JUMP = "JUMP";
interface ChangeAction {
type: typeof CHANGE;
activeIndex: number;
}
interface MoveAction {
type: typeof MOVE;
data: SwipeEventData;
tabs: number;
}
interface JumpAction {
type: typeof JUMP;
data: SwipeEventData;
tabs: number;
}
type Action = ChangeAction | MoveAction | JumpAction;
/**
* This does a shallow compare of the state objects and only returns the nextState
* when one of the values are different. This prevents unneeded re-renders since
* different state objects will always be shallow-different.
*/
const update = (state: State, nextState: State): State => {
if (
(Object.keys(state) as (keyof State)[]).some(
(key) => state[key] !== nextState[key]
)
) {
return nextState;
}
return state;
};
/**
* This is called whenever a swipe has been completed to update the active
* index to the next slide.
*
* You'll probably want to update this to only update the slide if a specific
* relative distance has been exceeded. Right now it'll be fired even if it's
* only 10px.
*/
function jump(state: State, data: SwipeEventData, tabs: number): State {
const { activeIndex, swiping } = state;
const incrementor = data.dir === "Left" ? 1 : -1;
const nextIndex = Math.max(0, Math.min(tabs, activeIndex + incrementor));
return update(state, {
swiping,
distance: "",
activeIndex: nextIndex,
});
}
/**
* This is fired whenever the user is swiping the panels and is used to update
* the distance string used in a `transform: translateX(${diustance})` style.
*
* This should eventually be updated to ensure that the distance is "normalized"
* based on the panel width, but it's too much work for an example demo.
*/
function move(state: State, data: SwipeEventData, tabs: number): State {
let { deltaX } = data;
if (state.activeIndex === 0) {
deltaX = Math.max(-30, deltaX);
} else if (state.activeIndex === tabs) {
deltaX = Math.min(30, deltaX);
}
const distance = `${-deltaX}px`;
return update(state, {
...state,
distance,
swiping: true,
});
}
function reducer(state: State, action: Action): State {
switch (action.type) {
case CHANGE:
return update(state, {
activeIndex: action.activeIndex,
distance: "",
swiping: false,
});
case JUMP:
return jump(state, action.data, action.tabs);
case MOVE:
return move(state, action.data, action.tabs);
default:
return state;
}
}
interface ReturnValue extends State {
onActiveIndexChange: (activeIndex: number) => void;
handlers: SwipeableHandlers;
}
export default function useSwipeableIndexes(tabs: number): ReturnValue {
const [state, dispatch] = useReducer<typeof reducer>(reducer, {
activeIndex: 0,
distance: "",
swiping: false,
});
const move = useCallback(
(data: SwipeEventData) => {
const sel = window.getSelection();
if (sel) {
sel.removeAllRanges();
}
dispatch({ type: MOVE, data, tabs });
},
[tabs]
);
const jump = useCallback(
(data: SwipeEventData) => {
dispatch({ type: JUMP, data, tabs });
},
[tabs]
);
const change = useCallback((activeIndex: number) => {
dispatch({ type: CHANGE, activeIndex });
}, []);
const handlers = useSwipeable({
// this is just for demo purposes, and you probably don't want mouse sliding.
trackMouse: true,
onSwiping: move,
onSwipedLeft: jump,
onSwipedRight: jump,
});
return {
...state,
handlers,
onActiveIndexChange: change,
};
}