@kolserdav/swiper 4.3.0 → 4.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Swiper.d.ts +2 -2
- package/dist/Swiper.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +4 -2
- package/.eslintrc.json +0 -24
- package/.prettierrc +0 -9
- package/.travis.yml +0 -4
- package/.vscode/extensions.json +0 -7
- package/.vscode/settings.json +0 -21
- package/README copy.md +0 -40
- package/docs/CHANGELOG.md +0 -5
- package/next.config.mjs +0 -6
- package/pages/_app.tsx +0 -16
- package/pages/_document.tsx +0 -23
- package/pages/api/hello.ts +0 -23
- package/pages/fonts/GeistMonoVF.woff +0 -0
- package/pages/fonts/GeistVF.woff +0 -0
- package/pages/index.tsx +0 -74
- package/public/favicon.ico +0 -0
- package/src/components/Swiper.module.css +0 -139
- package/src/components/Swiper.tsx +0 -812
- package/styles/Home.module.css +0 -5
- package/styles/globals.css +0 -35
- package/tsconfig.json +0 -36
- package/webpack.config.js +0 -40
|
@@ -1,812 +0,0 @@
|
|
|
1
|
-
/******************************************************************************************
|
|
2
|
-
* Repository: https://github.com/kolserdav/react-swiper.git
|
|
3
|
-
* File name: Swiper.tsx
|
|
4
|
-
* Author: Sergey Kolmiller
|
|
5
|
-
* Email: <kolserdav@conhos.ru>
|
|
6
|
-
* License: MIT
|
|
7
|
-
* License text: The code is distributed as is. There are no guarantees regarding the functionality of the code or parts of it.
|
|
8
|
-
* Copyright: kolserdav, All rights reserved (c)
|
|
9
|
-
* Create Date: Fri Sep 06 2024 18:44:44 GMT+0700 (Krasnoyarsk Standard Time)
|
|
10
|
-
******************************************************************************************/
|
|
11
|
-
import React, {
|
|
12
|
-
useMemo,
|
|
13
|
-
useState,
|
|
14
|
-
useRef,
|
|
15
|
-
createRef,
|
|
16
|
-
RefObject,
|
|
17
|
-
useEffect,
|
|
18
|
-
TouchEvent,
|
|
19
|
-
Fragment,
|
|
20
|
-
MouseEventHandler,
|
|
21
|
-
} from 'react';
|
|
22
|
-
import clsx from 'clsx';
|
|
23
|
-
import s from './Swiper.module.css';
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Time to miliseconds of change animation by card left value
|
|
27
|
-
*/
|
|
28
|
-
export const SWIPE_TRANSITION_TIMEOUT = 400;
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Minimum swipe speed regardless of distance
|
|
32
|
-
*/
|
|
33
|
-
export const SWIPE_ON_EVENT_SPEED = 0.7;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* One of swipe card
|
|
37
|
-
*/
|
|
38
|
-
export interface Swipe {
|
|
39
|
-
id: number | null;
|
|
40
|
-
nextRef?: RefObject<HTMLButtonElement | HTMLDivElement | undefined>;
|
|
41
|
-
closeRef?: RefObject<HTMLButtonElement | HTMLDivElement | undefined>;
|
|
42
|
-
children: React.ReactElement | React.ReactElement[];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Callback for get next or previous card content
|
|
47
|
-
*/
|
|
48
|
-
export type GetSwipeHandler = (oldId: number) => Promise<Swipe>;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* One of swipe card internal
|
|
52
|
-
*/
|
|
53
|
-
interface SwipeFull extends Swipe {
|
|
54
|
-
type: 'next' | 'current' | 'prev';
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Event touch name
|
|
59
|
-
*/
|
|
60
|
-
type TouchName = 'onTouchMove' | 'onTouchStart' | 'onTouchEnd';
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Props of Swiper component
|
|
64
|
-
*/
|
|
65
|
-
interface SwiperProps {
|
|
66
|
-
/**
|
|
67
|
-
* Current card content
|
|
68
|
-
*/
|
|
69
|
-
defaultCurrent: Swipe;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Get next card handler
|
|
73
|
-
*/
|
|
74
|
-
getNext: GetSwipeHandler;
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Get previous card handler
|
|
78
|
-
*/
|
|
79
|
-
getPrev: GetSwipeHandler;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Class name for content block
|
|
83
|
-
*/
|
|
84
|
-
className?: string;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* invitation animation
|
|
88
|
-
*/
|
|
89
|
-
invitationAnimation?: boolean;
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* On swipe callback
|
|
93
|
-
*/
|
|
94
|
-
onSwipe?: (currentId: number | null | undefined) => void;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Auto slide if provieded
|
|
98
|
-
*/
|
|
99
|
-
durationAnimation?: number;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Blocked swipe event
|
|
103
|
-
*/
|
|
104
|
-
blockSwipe?: boolean;
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Show dots
|
|
108
|
-
*/
|
|
109
|
-
dots?: {
|
|
110
|
-
list: number[];
|
|
111
|
-
active: number;
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Create swipe list from values
|
|
117
|
-
*/
|
|
118
|
-
const getSwipes = (prev: Swipe, current: Swipe, next: Swipe, swipes: SwipeFull[]): SwipeFull[] => {
|
|
119
|
-
if (
|
|
120
|
-
(prev?.id === current?.id || current?.id === next?.id || next?.id === prev?.id) &&
|
|
121
|
-
swipes.length
|
|
122
|
-
) {
|
|
123
|
-
return swipes;
|
|
124
|
-
}
|
|
125
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
126
|
-
const result: any[] = [1, 2, 3].map((id) => {
|
|
127
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
|
-
const item: any = id === 1 ? { ...prev } : id === 2 ? current : { ...next };
|
|
129
|
-
item.type = id === 1 ? 'prev' : id === 2 ? 'current' : 'next';
|
|
130
|
-
return item;
|
|
131
|
-
});
|
|
132
|
-
return result;
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Get default swipe with random id
|
|
137
|
-
*/
|
|
138
|
-
const getDefaultSwipe = (): Swipe => ({
|
|
139
|
-
id: Math.ceil(Math.random() * 1000),
|
|
140
|
-
children: <div />,
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
const refs: {
|
|
144
|
-
[key: number]: RefObject<HTMLDivElement>;
|
|
145
|
-
} = {};
|
|
146
|
-
|
|
147
|
-
let startClientX: number;
|
|
148
|
-
let startClientY: number;
|
|
149
|
-
let lastLeft: number;
|
|
150
|
-
let animated = false;
|
|
151
|
-
let _defaultCurrent: Swipe;
|
|
152
|
-
let oldSwipes: SwipeFull[] = [];
|
|
153
|
-
let startTime = new Date().getTime();
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Swiper component
|
|
157
|
-
*/
|
|
158
|
-
export const Swiper = (props: SwiperProps): React.ReactElement => {
|
|
159
|
-
const {
|
|
160
|
-
defaultCurrent,
|
|
161
|
-
getNext,
|
|
162
|
-
getPrev,
|
|
163
|
-
onSwipe,
|
|
164
|
-
className,
|
|
165
|
-
invitationAnimation,
|
|
166
|
-
durationAnimation,
|
|
167
|
-
blockSwipe,
|
|
168
|
-
dots,
|
|
169
|
-
} = props;
|
|
170
|
-
|
|
171
|
-
const [current, setCurrent] = useState<Swipe>();
|
|
172
|
-
const [prev, setPrev] = useState<Swipe>();
|
|
173
|
-
const [next, setNext] = useState<Swipe>();
|
|
174
|
-
const [prePrev, setPrePrev] = useState<Swipe>();
|
|
175
|
-
const [postNext, setPostNext] = useState<Swipe>();
|
|
176
|
-
const [windowWidth, setWindowWidth] = useState<number>(0);
|
|
177
|
-
const [height, setHeight] = useState<number>(0);
|
|
178
|
-
const [width, setWidth] = useState<number>(0);
|
|
179
|
-
const [_left, _setLeft] = useState<number>(0);
|
|
180
|
-
const [left, setLeft] = useState<number>(0);
|
|
181
|
-
const [isMobile, setIsMobile] = useState<boolean>();
|
|
182
|
-
const [load, setLoad] = useState<boolean>(false);
|
|
183
|
-
|
|
184
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
185
|
-
|
|
186
|
-
if (typeof _defaultCurrent === 'undefined') {
|
|
187
|
-
_defaultCurrent = defaultCurrent;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Create memoized swipes
|
|
191
|
-
*/
|
|
192
|
-
const swipes = useMemo(
|
|
193
|
-
() =>
|
|
194
|
-
getSwipes(
|
|
195
|
-
prev || getDefaultSwipe(),
|
|
196
|
-
current || defaultCurrent,
|
|
197
|
-
next || getDefaultSwipe(),
|
|
198
|
-
oldSwipes
|
|
199
|
-
),
|
|
200
|
-
|
|
201
|
-
[next, prev, current]
|
|
202
|
-
);
|
|
203
|
-
|
|
204
|
-
oldSwipes = swipes;
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Get new or exists ref by id
|
|
208
|
-
*/
|
|
209
|
-
const getRef = (id: number): typeof refs[0] => {
|
|
210
|
-
refs[id] = refs[id] ? refs[id] : createRef<HTMLDivElement>();
|
|
211
|
-
return refs[id];
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* Set to cards classes for in animation
|
|
216
|
-
*/
|
|
217
|
-
const setGoClassHandler = useMemo(
|
|
218
|
-
() =>
|
|
219
|
-
(_next?: Swipe, _prev?: Swipe): void => {
|
|
220
|
-
getRef(_next?.id || next?.id || 0).current?.classList.add(s.go);
|
|
221
|
-
getRef(_prev?.id || prev?.id || 0).current?.classList.add(s.go);
|
|
222
|
-
getRef(current?.id || 0).current?.classList.add(s.go);
|
|
223
|
-
setTimeout(() => {
|
|
224
|
-
getRef(_next?.id || next?.id || 0).current?.classList.remove(s.go);
|
|
225
|
-
getRef(_prev?.id || prev?.id || 0).current?.classList.remove(s.go);
|
|
226
|
-
getRef(current?.id || 0).current?.classList.remove(s.go);
|
|
227
|
-
}, SWIPE_TRANSITION_TIMEOUT);
|
|
228
|
-
},
|
|
229
|
-
[current?.id, next?.id, prev?.id]
|
|
230
|
-
);
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Set to cards classes for out animation
|
|
234
|
-
*/
|
|
235
|
-
const setBackClassHandler = useMemo(
|
|
236
|
-
() => (): void => {
|
|
237
|
-
getRef(next?.id || 0).current?.classList.add(s.back);
|
|
238
|
-
getRef(prev?.id || 0).current?.classList.add(s.back);
|
|
239
|
-
getRef(current?.id || 0).current?.classList.add(s.back);
|
|
240
|
-
setTimeout(() => {
|
|
241
|
-
getRef(next?.id || 0).current?.classList.remove(s.back);
|
|
242
|
-
getRef(prev?.id || 0).current?.classList.remove(s.back);
|
|
243
|
-
getRef(current?.id || 0).current?.classList.remove(s.back);
|
|
244
|
-
}, SWIPE_TRANSITION_TIMEOUT);
|
|
245
|
-
},
|
|
246
|
-
[current?.id, next?.id, prev?.id]
|
|
247
|
-
);
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Wait helper
|
|
251
|
-
*/
|
|
252
|
-
const wait = async (miliseconds: number): Promise<void> => {
|
|
253
|
-
await new Promise((resolve) => {
|
|
254
|
-
setTimeout(() => {
|
|
255
|
-
resolve(0);
|
|
256
|
-
}, miliseconds);
|
|
257
|
-
});
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Set pre previos or post next values
|
|
262
|
-
*/
|
|
263
|
-
const setPreValues = useMemo(
|
|
264
|
-
() =>
|
|
265
|
-
(_p: Swipe | undefined, _n: Swipe | undefined): void => {
|
|
266
|
-
if (_p) {
|
|
267
|
-
getPrev(_p?.id || 0).then((d) => {
|
|
268
|
-
setPrePrev(d);
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
if (_n) {
|
|
272
|
-
getNext(_n?.id || 0).then((d) => {
|
|
273
|
-
setPostNext(d);
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
},
|
|
277
|
-
[getNext, getPrev]
|
|
278
|
-
);
|
|
279
|
-
|
|
280
|
-
const goSwipeBack = useMemo(
|
|
281
|
-
() => () => {
|
|
282
|
-
setBackClassHandler();
|
|
283
|
-
setLeft(0);
|
|
284
|
-
setLoad(false);
|
|
285
|
-
},
|
|
286
|
-
[setBackClassHandler, setLeft, setLoad]
|
|
287
|
-
);
|
|
288
|
-
|
|
289
|
-
const startSwipe = useMemo(
|
|
290
|
-
() => (_next: Swipe | undefined, _prev: Swipe | undefined, _left: number) => {
|
|
291
|
-
setGoClassHandler(_next, _prev);
|
|
292
|
-
setLeft(_left);
|
|
293
|
-
},
|
|
294
|
-
[setGoClassHandler]
|
|
295
|
-
);
|
|
296
|
-
|
|
297
|
-
const endSwipe = useMemo(
|
|
298
|
-
() =>
|
|
299
|
-
({
|
|
300
|
-
_prev,
|
|
301
|
-
_current,
|
|
302
|
-
_next,
|
|
303
|
-
}: {
|
|
304
|
-
_prev: Swipe | undefined;
|
|
305
|
-
_current: Swipe | undefined;
|
|
306
|
-
_next: Swipe | undefined;
|
|
307
|
-
}) => {
|
|
308
|
-
setLeft(0);
|
|
309
|
-
setPrev(_prev);
|
|
310
|
-
setCurrent(_current);
|
|
311
|
-
setNext(_next);
|
|
312
|
-
},
|
|
313
|
-
[]
|
|
314
|
-
);
|
|
315
|
-
|
|
316
|
-
const goSwipe = useMemo(
|
|
317
|
-
() =>
|
|
318
|
-
async (
|
|
319
|
-
{
|
|
320
|
-
newNext,
|
|
321
|
-
newPrev,
|
|
322
|
-
coeff,
|
|
323
|
-
}: {
|
|
324
|
-
newPrev: Swipe | undefined;
|
|
325
|
-
newNext: Swipe | undefined;
|
|
326
|
-
coeff: 1 | -1;
|
|
327
|
-
},
|
|
328
|
-
_next: Swipe | undefined,
|
|
329
|
-
_prev: Swipe | undefined
|
|
330
|
-
) => {
|
|
331
|
-
if (_next?.id === null || _prev?.id === null) {
|
|
332
|
-
goSwipeBack();
|
|
333
|
-
return 1;
|
|
334
|
-
}
|
|
335
|
-
if (onSwipe !== undefined) {
|
|
336
|
-
onSwipe(_next?.id || _prev?.id);
|
|
337
|
-
}
|
|
338
|
-
startSwipe(_next, _prev, windowWidth * coeff);
|
|
339
|
-
await wait(SWIPE_TRANSITION_TIMEOUT);
|
|
340
|
-
endSwipe({ _prev: newPrev, _current: _next || _prev, _next: newNext });
|
|
341
|
-
return 0;
|
|
342
|
-
},
|
|
343
|
-
[endSwipe, goSwipeBack, onSwipe, startSwipe, windowWidth]
|
|
344
|
-
);
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Run swipe animation
|
|
348
|
-
*/
|
|
349
|
-
const swipe = useMemo(
|
|
350
|
-
() =>
|
|
351
|
-
async ({
|
|
352
|
-
_lastLeft,
|
|
353
|
-
_next,
|
|
354
|
-
_prev,
|
|
355
|
-
speed,
|
|
356
|
-
}: {
|
|
357
|
-
_lastLeft: number;
|
|
358
|
-
_next: Swipe | undefined;
|
|
359
|
-
_prev: Swipe | undefined;
|
|
360
|
-
speed?: number;
|
|
361
|
-
}): Promise<1 | 0> => {
|
|
362
|
-
setLoad(true);
|
|
363
|
-
if (
|
|
364
|
-
Math.abs(_lastLeft) > width / 3 ||
|
|
365
|
-
(speed !== undefined && speed > SWIPE_ON_EVENT_SPEED)
|
|
366
|
-
) {
|
|
367
|
-
if (_lastLeft < 0) {
|
|
368
|
-
await goSwipe({ newPrev: current, newNext: postNext, coeff: -1 }, _next, undefined);
|
|
369
|
-
} else {
|
|
370
|
-
await goSwipe({ newPrev: prePrev, newNext: current, coeff: 1 }, undefined, _prev);
|
|
371
|
-
}
|
|
372
|
-
} else {
|
|
373
|
-
goSwipeBack();
|
|
374
|
-
}
|
|
375
|
-
setLoad(false);
|
|
376
|
-
return 0;
|
|
377
|
-
},
|
|
378
|
-
[current, goSwipeBack, width, prePrev, postNext, goSwipe]
|
|
379
|
-
);
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* Touch event handler
|
|
383
|
-
*/
|
|
384
|
-
const onTouchHandler = useMemo(
|
|
385
|
-
() =>
|
|
386
|
-
async (name: TouchName, e: TouchEvent): Promise<void> => {
|
|
387
|
-
const { touches } = e;
|
|
388
|
-
const clientX = touches[0]?.clientX;
|
|
389
|
-
const clientY = touches[0]?.clientY;
|
|
390
|
-
startClientX = startClientX || 0;
|
|
391
|
-
startClientY = startClientY || 0;
|
|
392
|
-
let absX = 0;
|
|
393
|
-
let absY = 0;
|
|
394
|
-
switch (name) {
|
|
395
|
-
case 'onTouchStart':
|
|
396
|
-
startClientX = clientX;
|
|
397
|
-
startClientY = clientY;
|
|
398
|
-
lastLeft = _left;
|
|
399
|
-
startTime = new Date().getTime();
|
|
400
|
-
|
|
401
|
-
break;
|
|
402
|
-
case 'onTouchMove':
|
|
403
|
-
if (!blockSwipe) {
|
|
404
|
-
absY = Math.abs(startClientY - clientY);
|
|
405
|
-
absX = Math.abs(startClientX - clientX);
|
|
406
|
-
if (absX > absY) {
|
|
407
|
-
lastLeft = _left - (startClientX - clientX);
|
|
408
|
-
setLeft(lastLeft);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
break;
|
|
412
|
-
case 'onTouchEnd':
|
|
413
|
-
if (!blockSwipe) {
|
|
414
|
-
await swipe({
|
|
415
|
-
_lastLeft: lastLeft,
|
|
416
|
-
speed: Math.abs(lastLeft / (new Date().getTime() - startTime)),
|
|
417
|
-
_next: next,
|
|
418
|
-
_prev: prev,
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
break;
|
|
422
|
-
default:
|
|
423
|
-
}
|
|
424
|
-
},
|
|
425
|
-
[_left, blockSwipe, swipe, next, prev]
|
|
426
|
-
);
|
|
427
|
-
|
|
428
|
-
/**
|
|
429
|
-
* Touch event wrapper
|
|
430
|
-
*/
|
|
431
|
-
const onTouchWrapper =
|
|
432
|
-
(name: TouchName): ((event: TouchEvent<HTMLDivElement>) => void) =>
|
|
433
|
-
(e): void => {
|
|
434
|
-
onTouchHandler(name, e);
|
|
435
|
-
};
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* On next or previous button click handler
|
|
439
|
-
*/
|
|
440
|
-
const clickHandler = useMemo(
|
|
441
|
-
() =>
|
|
442
|
-
(origin: 'next' | 'prev'): (() => Promise<1 | 0>) => {
|
|
443
|
-
const isNext = origin === 'next';
|
|
444
|
-
return async (): Promise<1 | 0> => {
|
|
445
|
-
const coeff = isNext ? -1 : 1;
|
|
446
|
-
const leftVal = width * coeff;
|
|
447
|
-
if (isNext) {
|
|
448
|
-
setGoClassHandler();
|
|
449
|
-
} else {
|
|
450
|
-
setBackClassHandler();
|
|
451
|
-
}
|
|
452
|
-
await wait(SWIPE_TRANSITION_TIMEOUT);
|
|
453
|
-
await swipe({
|
|
454
|
-
_lastLeft: leftVal,
|
|
455
|
-
_next: next,
|
|
456
|
-
_prev: prev,
|
|
457
|
-
});
|
|
458
|
-
return 0;
|
|
459
|
-
};
|
|
460
|
-
},
|
|
461
|
-
[setBackClassHandler, setGoClassHandler, swipe, width, next, prev]
|
|
462
|
-
);
|
|
463
|
-
|
|
464
|
-
/**
|
|
465
|
-
* Wrapper for next button click handler
|
|
466
|
-
*/
|
|
467
|
-
const clickNextHandler = clickHandler('next');
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
* Wrapper for previous button click handler
|
|
471
|
-
*/
|
|
472
|
-
const clickPrevHandler = clickHandler('prev');
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* optional animation handler
|
|
476
|
-
*/
|
|
477
|
-
const infitationAnimationHandler = useMemo(
|
|
478
|
-
() =>
|
|
479
|
-
async (shift: number): Promise<void> => {
|
|
480
|
-
if (shift < 0) {
|
|
481
|
-
setGoClassHandler();
|
|
482
|
-
} else {
|
|
483
|
-
setBackClassHandler();
|
|
484
|
-
}
|
|
485
|
-
setLeft(shift);
|
|
486
|
-
await wait(SWIPE_TRANSITION_TIMEOUT);
|
|
487
|
-
if (shift < 0) {
|
|
488
|
-
setBackClassHandler();
|
|
489
|
-
} else {
|
|
490
|
-
setGoClassHandler();
|
|
491
|
-
}
|
|
492
|
-
setLeft(0);
|
|
493
|
-
await wait(SWIPE_TRANSITION_TIMEOUT);
|
|
494
|
-
},
|
|
495
|
-
[setBackClassHandler, setGoClassHandler]
|
|
496
|
-
);
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* Initial start cards
|
|
500
|
-
*/
|
|
501
|
-
const setStartCards = useMemo(
|
|
502
|
-
() => async (): Promise<void> => {
|
|
503
|
-
const _n = await getNext(defaultCurrent.id || 0);
|
|
504
|
-
const _p = await getPrev(defaultCurrent.id || 0);
|
|
505
|
-
setCurrent(defaultCurrent);
|
|
506
|
-
setPrev(_p);
|
|
507
|
-
setNext(_n);
|
|
508
|
-
_defaultCurrent = defaultCurrent;
|
|
509
|
-
setLoad(false);
|
|
510
|
-
},
|
|
511
|
-
[defaultCurrent, getNext, getPrev]
|
|
512
|
-
);
|
|
513
|
-
|
|
514
|
-
/**
|
|
515
|
-
* Invitation animation
|
|
516
|
-
*/
|
|
517
|
-
const startInvitationAnimation = useMemo(
|
|
518
|
-
() =>
|
|
519
|
-
async (prev: Swipe, next: Swipe): Promise<void> => {
|
|
520
|
-
if (prev.id) {
|
|
521
|
-
await infitationAnimationHandler(windowWidth / 5.5);
|
|
522
|
-
}
|
|
523
|
-
if (next.id) {
|
|
524
|
-
await infitationAnimationHandler((windowWidth / 5.5) * -1);
|
|
525
|
-
}
|
|
526
|
-
},
|
|
527
|
-
[infitationAnimationHandler, windowWidth]
|
|
528
|
-
);
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
* Handler on click by dot
|
|
532
|
-
*/
|
|
533
|
-
const clickDotHandler: MouseEventHandler<HTMLDivElement> = useMemo(
|
|
534
|
-
() => async (e: any) => {
|
|
535
|
-
const { target } = e;
|
|
536
|
-
const tabindex = target?.getAttribute('tabindex');
|
|
537
|
-
const active = parseInt(tabindex, 10);
|
|
538
|
-
const currentId = current?.id || 0;
|
|
539
|
-
let newCurrent: Swipe | undefined;
|
|
540
|
-
let newPrev: Swipe | undefined;
|
|
541
|
-
let newNext: Swipe | undefined;
|
|
542
|
-
if (currentId === next?.id) {
|
|
543
|
-
newCurrent = next;
|
|
544
|
-
newNext = postNext;
|
|
545
|
-
newPrev = current;
|
|
546
|
-
} else if (currentId === prev?.id) {
|
|
547
|
-
newCurrent = prev;
|
|
548
|
-
newNext = current;
|
|
549
|
-
newPrev = prePrev;
|
|
550
|
-
} else if (currentId === prePrev?.id) {
|
|
551
|
-
newCurrent = prePrev;
|
|
552
|
-
newNext = prev;
|
|
553
|
-
newPrev = await getPrev(prePrev?.id);
|
|
554
|
-
} else if (currentId === postNext?.id) {
|
|
555
|
-
newCurrent = postNext;
|
|
556
|
-
newPrev = next;
|
|
557
|
-
newNext = await getPrev(postNext?.id);
|
|
558
|
-
} else {
|
|
559
|
-
newCurrent = await getNext(active - 1);
|
|
560
|
-
newNext = await getNext(newCurrent?.id || 0);
|
|
561
|
-
newPrev = await getPrev(newCurrent?.id || 0);
|
|
562
|
-
}
|
|
563
|
-
if (currentId < active) {
|
|
564
|
-
await goSwipe({ newPrev, newNext, coeff: -1 }, newCurrent, undefined);
|
|
565
|
-
} else {
|
|
566
|
-
await goSwipe({ newPrev, newNext, coeff: 1 }, undefined, newCurrent);
|
|
567
|
-
}
|
|
568
|
-
},
|
|
569
|
-
[getNext, getPrev, goSwipe, current, next, postNext, prePrev, prev]
|
|
570
|
-
);
|
|
571
|
-
|
|
572
|
-
/**
|
|
573
|
-
* Set button listeners
|
|
574
|
-
*/
|
|
575
|
-
useEffect(() => {
|
|
576
|
-
const buttonNext = current?.nextRef?.current;
|
|
577
|
-
const buttonClose = current?.closeRef?.current;
|
|
578
|
-
buttonNext?.addEventListener('click', clickNextHandler);
|
|
579
|
-
buttonClose?.addEventListener('click', clickNextHandler);
|
|
580
|
-
return (): void => {
|
|
581
|
-
buttonNext?.removeEventListener('click', clickNextHandler);
|
|
582
|
-
buttonClose?.removeEventListener('click', clickNextHandler);
|
|
583
|
-
};
|
|
584
|
-
}, [clickNextHandler, current]);
|
|
585
|
-
|
|
586
|
-
/**
|
|
587
|
-
* Set width and height
|
|
588
|
-
*/
|
|
589
|
-
useEffect(() => {
|
|
590
|
-
const _width = containerRef?.current?.parentElement?.getBoundingClientRect()?.width;
|
|
591
|
-
const _height = containerRef?.current?.parentElement?.getBoundingClientRect()?.height;
|
|
592
|
-
if (_width && !width && _height && !height) {
|
|
593
|
-
setWidth(_width);
|
|
594
|
-
setHeight(_height);
|
|
595
|
-
}
|
|
596
|
-
}, [containerRef, width, height]);
|
|
597
|
-
|
|
598
|
-
/**
|
|
599
|
-
* Save container left
|
|
600
|
-
*/
|
|
601
|
-
useEffect(() => {
|
|
602
|
-
const __left = containerRef?.current?.parentElement?.getBoundingClientRect()?.left;
|
|
603
|
-
if (__left && !left) {
|
|
604
|
-
_setLeft(__left);
|
|
605
|
-
}
|
|
606
|
-
}, [containerRef, left]);
|
|
607
|
-
|
|
608
|
-
/**
|
|
609
|
-
* Auto slide
|
|
610
|
-
*/
|
|
611
|
-
useEffect(() => {
|
|
612
|
-
let clearAnimate: NodeJS.Timeout;
|
|
613
|
-
if (durationAnimation) {
|
|
614
|
-
clearAnimate = setInterval(() => {
|
|
615
|
-
clickNextHandler();
|
|
616
|
-
}, durationAnimation);
|
|
617
|
-
}
|
|
618
|
-
return () => {
|
|
619
|
-
if (clearAnimate) {
|
|
620
|
-
clearInterval(clearAnimate);
|
|
621
|
-
}
|
|
622
|
-
};
|
|
623
|
-
}, [durationAnimation, clickNextHandler]);
|
|
624
|
-
|
|
625
|
-
/**
|
|
626
|
-
* Set start cards
|
|
627
|
-
*/
|
|
628
|
-
useEffect(() => {
|
|
629
|
-
if (!current || !prev || !next || _defaultCurrent !== defaultCurrent) {
|
|
630
|
-
setStartCards();
|
|
631
|
-
}
|
|
632
|
-
}, [current, next, prev, defaultCurrent, setStartCards]);
|
|
633
|
-
|
|
634
|
-
/**
|
|
635
|
-
* Save window width
|
|
636
|
-
*/
|
|
637
|
-
useEffect(() => {
|
|
638
|
-
const _windowWidth = document.body.clientWidth;
|
|
639
|
-
if (!windowWidth && _windowWidth) {
|
|
640
|
-
setWindowWidth(_windowWidth);
|
|
641
|
-
}
|
|
642
|
-
}, [windowWidth]);
|
|
643
|
-
|
|
644
|
-
/**
|
|
645
|
-
* Run invitation animation
|
|
646
|
-
*/
|
|
647
|
-
useEffect(() => {
|
|
648
|
-
if (invitationAnimation && width && !animated) {
|
|
649
|
-
if (prev && next) {
|
|
650
|
-
animated = true;
|
|
651
|
-
startInvitationAnimation(prev, next);
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
}, [invitationAnimation, next, prev, startInvitationAnimation, width]);
|
|
655
|
-
|
|
656
|
-
/**
|
|
657
|
-
* Listen resize
|
|
658
|
-
*/
|
|
659
|
-
useEffect(() => {
|
|
660
|
-
const resizeHandler = (): void => {
|
|
661
|
-
const _width = containerRef?.current?.parentElement?.getBoundingClientRect()?.width;
|
|
662
|
-
const _height = containerRef?.current?.parentElement?.getBoundingClientRect()?.height;
|
|
663
|
-
const __left = containerRef?.current?.parentElement?.getBoundingClientRect()?.left;
|
|
664
|
-
const _windowWidth = document.body.clientWidth;
|
|
665
|
-
if (_width && _height) {
|
|
666
|
-
setWidth(_width);
|
|
667
|
-
setHeight(_height);
|
|
668
|
-
}
|
|
669
|
-
if (__left) {
|
|
670
|
-
setLeft(__left);
|
|
671
|
-
}
|
|
672
|
-
if (_windowWidth) {
|
|
673
|
-
setWindowWidth(_windowWidth);
|
|
674
|
-
}
|
|
675
|
-
};
|
|
676
|
-
window.addEventListener('resize', resizeHandler);
|
|
677
|
-
return (): void => {
|
|
678
|
-
window.removeEventListener('resize', resizeHandler);
|
|
679
|
-
};
|
|
680
|
-
}, []);
|
|
681
|
-
|
|
682
|
-
/**
|
|
683
|
-
* Set is mobile
|
|
684
|
-
*/
|
|
685
|
-
useEffect(() => {
|
|
686
|
-
if (typeof isMobile === 'undefined') {
|
|
687
|
-
// @ts-ignore
|
|
688
|
-
setIsMobile('ontouchstart' in window || typeof navigator?.msMaxTouchPoints !== 'undefined');
|
|
689
|
-
}
|
|
690
|
-
}, [isMobile]);
|
|
691
|
-
|
|
692
|
-
/**
|
|
693
|
-
* Set pre values
|
|
694
|
-
*/
|
|
695
|
-
useEffect(() => {
|
|
696
|
-
setPreValues(prev, next);
|
|
697
|
-
}, [prev, next, setPreValues]);
|
|
698
|
-
|
|
699
|
-
return (
|
|
700
|
-
<div className={s.container} ref={containerRef}>
|
|
701
|
-
{/** absolute position cards */}
|
|
702
|
-
{swipes.map((item) => (
|
|
703
|
-
<Fragment key={item.id}>
|
|
704
|
-
{item.id !== null && item.children !== null && width && (
|
|
705
|
-
<div
|
|
706
|
-
onTouchMove={onTouchWrapper('onTouchMove')}
|
|
707
|
-
onTouchStart={onTouchWrapper('onTouchStart')}
|
|
708
|
-
onTouchEnd={onTouchWrapper('onTouchEnd')}
|
|
709
|
-
id={item.id?.toString()}
|
|
710
|
-
style={
|
|
711
|
-
item.type === 'current'
|
|
712
|
-
? { width, height, left }
|
|
713
|
-
: item.type === 'prev'
|
|
714
|
-
? { width, height, left: left - window.innerWidth }
|
|
715
|
-
: { width, height, left: left + window.innerWidth }
|
|
716
|
-
}
|
|
717
|
-
className={clsx(
|
|
718
|
-
s.card,
|
|
719
|
-
item.type === 'prev' ? s.prev : item.type === 'next' ? s.next : ''
|
|
720
|
-
)}
|
|
721
|
-
ref={getRef(item.id)}
|
|
722
|
-
>
|
|
723
|
-
{/** Block of content */}
|
|
724
|
-
<div className={clsx(s.content, className)}>{item.children}</div>
|
|
725
|
-
</div>
|
|
726
|
-
)}
|
|
727
|
-
{item.id === null && (
|
|
728
|
-
<div
|
|
729
|
-
style={
|
|
730
|
-
item.type === 'current'
|
|
731
|
-
? { width, height, left }
|
|
732
|
-
: item.type === 'prev'
|
|
733
|
-
? { width, height, left: left - windowWidth }
|
|
734
|
-
: { width, height, left: left + windowWidth }
|
|
735
|
-
}
|
|
736
|
-
className={clsx(
|
|
737
|
-
s.card,
|
|
738
|
-
item.type === 'prev' ? s.prev : item.type === 'next' ? s.next : ''
|
|
739
|
-
)}
|
|
740
|
-
>
|
|
741
|
-
<svg
|
|
742
|
-
xmlns="http://www.w3.org/2000/svg"
|
|
743
|
-
height="24px"
|
|
744
|
-
viewBox="0 0 24 24"
|
|
745
|
-
width="24px"
|
|
746
|
-
fill="#000000"
|
|
747
|
-
>
|
|
748
|
-
<path d="M0 0h24v24H0V0z" fill="none" />
|
|
749
|
-
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zM4 12c0-4.42 3.58-8 8-8 1.85 0 3.55.63 4.9 1.69L5.69 16.9C4.63 15.55 4 13.85 4 12zm8 8c-1.85 0-3.55-.63-4.9-1.69L18.31 7.1C19.37 8.45 20 10.15 20 12c0 4.42-3.58 8-8 8z" />
|
|
750
|
-
</svg>
|
|
751
|
-
</div>
|
|
752
|
-
)}
|
|
753
|
-
</Fragment>
|
|
754
|
-
))}
|
|
755
|
-
{typeof isMobile !== 'undefined' && !isMobile && prev?.id !== null && (
|
|
756
|
-
<div className={clsx(s.button, s.button__prev)}>
|
|
757
|
-
<button
|
|
758
|
-
className={s.icon__button}
|
|
759
|
-
type="button"
|
|
760
|
-
disabled={load || isMobile}
|
|
761
|
-
onClick={clickPrevHandler}
|
|
762
|
-
>
|
|
763
|
-
<svg
|
|
764
|
-
xmlns="http://www.w3.org/2000/svg"
|
|
765
|
-
height="24px"
|
|
766
|
-
viewBox="0 0 24 24"
|
|
767
|
-
width="24px"
|
|
768
|
-
fill="#000000"
|
|
769
|
-
>
|
|
770
|
-
<path d="M0 0h24v24H0V0z" fill="none" />
|
|
771
|
-
<path d="M10.02 6L8.61 7.41 13.19 12l-4.58 4.59L10.02 18l6-6-6-6z" />
|
|
772
|
-
</svg>
|
|
773
|
-
</button>
|
|
774
|
-
</div>
|
|
775
|
-
)}
|
|
776
|
-
{typeof isMobile !== 'undefined' && !isMobile && next?.id !== null && (
|
|
777
|
-
<div className={clsx(s.button, s.button__next)}>
|
|
778
|
-
<button
|
|
779
|
-
className={s.icon__button}
|
|
780
|
-
type="button"
|
|
781
|
-
disabled={load || isMobile}
|
|
782
|
-
onClick={clickNextHandler}
|
|
783
|
-
>
|
|
784
|
-
<svg
|
|
785
|
-
xmlns="http://www.w3.org/2000/svg"
|
|
786
|
-
height="24px"
|
|
787
|
-
viewBox="0 0 24 24"
|
|
788
|
-
width="24px"
|
|
789
|
-
fill="#000000"
|
|
790
|
-
>
|
|
791
|
-
<path d="M0 0h24v24H0V0z" fill="none" />
|
|
792
|
-
<path d="M10.02 6L8.61 7.41 13.19 12l-4.58 4.59L10.02 18l6-6-6-6z" />
|
|
793
|
-
</svg>
|
|
794
|
-
</button>
|
|
795
|
-
</div>
|
|
796
|
-
)}
|
|
797
|
-
{dots && (
|
|
798
|
-
<div className={s.dots}>
|
|
799
|
-
{dots.list.map((item) => (
|
|
800
|
-
<div
|
|
801
|
-
role="button"
|
|
802
|
-
onClick={clickDotHandler}
|
|
803
|
-
tabIndex={item}
|
|
804
|
-
key={item}
|
|
805
|
-
className={clsx(s.dot, current?.id === item ? s.active : '')}
|
|
806
|
-
/>
|
|
807
|
-
))}
|
|
808
|
-
</div>
|
|
809
|
-
)}
|
|
810
|
-
</div>
|
|
811
|
-
);
|
|
812
|
-
};
|