@@ -2,6 +2,7 @@ import React, { useState, useEffect, useRef, useCallback, useImperativeHandle }
2
2
import clsx from 'clsx' ;
3
3
import { CarouselItem } from './Item' ;
4
4
import { setTransform , setTransition } from '../../utils/style' ;
5
+ import canUse from '../../utils/canUse' ;
5
6
6
7
export interface CarouselProps {
7
8
children : React . ReactNode ;
@@ -40,13 +41,13 @@ interface State {
40
41
endX : number ;
41
42
startY : number ;
42
43
canMove : boolean | null ;
43
- preventClick : boolean ;
44
44
pressDown : boolean ;
45
45
}
46
46
47
47
type DragEvent = React . TouchEvent < HTMLDivElement > | React . MouseEvent < HTMLDivElement , MouseEvent > ;
48
48
49
49
const formElements = [ 'TEXTAREA' , 'OPTION' , 'INPUT' , 'SELECT' ] ;
50
+ const canTouch = canUse ( 'touch' ) ;
50
51
51
52
export const Carousel = React . forwardRef < CarouselHandle , CarouselProps > ( ( props , ref ) => {
52
53
const {
@@ -82,7 +83,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
82
83
startY : 0 ,
83
84
canMove : null ,
84
85
pressDown : false ,
85
- preventClick : false ,
86
86
} ) ;
87
87
88
88
const getIndex = useCallback (
@@ -248,7 +248,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
248
248
state . endX = 0 ;
249
249
state . startY = 0 ;
250
250
state . canMove = null ;
251
- state . preventClick = false ;
252
251
state . pressDown = false ;
253
252
} ;
254
253
@@ -284,8 +283,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
284
283
if ( ! state . canMove ) {
285
284
return ;
286
285
}
287
- } else if ( ( e . target as Element ) . nodeName === 'A' ) {
288
- state . preventClick = true ;
289
286
}
290
287
291
288
e . preventDefault ( ) ;
@@ -319,6 +316,9 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
319
316
enableTransition ( ) ;
320
317
if ( state . endX ) {
321
318
updateAfterDrag ( ) ;
319
+ } else {
320
+ // when clicked
321
+ doAutoPlay ( ) ;
322
322
}
323
323
resetDrag ( ) ;
324
324
} ;
@@ -334,7 +334,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
334
334
335
335
if ( state . pressDown ) {
336
336
state . pressDown = false ;
337
- state . preventClick = false ;
338
337
state . endX = e . pageX ;
339
338
340
339
enableTransition ( ) ;
@@ -345,14 +344,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
345
344
doAutoPlay ( ) ;
346
345
} ;
347
346
348
- const handleClickWrap = ( e : React . MouseEvent < HTMLDivElement , MouseEvent > ) => {
349
- const state = stateRef . current ;
350
- if ( state . preventClick ) {
351
- e . preventDefault ( ) ;
352
- }
353
- state . preventClick = false ;
354
- } ;
355
-
356
347
const handleClickDot = ( e : React . MouseEvent < HTMLButtonElement , MouseEvent > ) => {
357
348
const { slideTo : i } = e . currentTarget . dataset ;
358
349
if ( i ) {
@@ -412,22 +403,28 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
412
403
} ;
413
404
} , [ autoPlay , activeIndex , doAutoPlay ] ) ;
414
405
415
- const events = draggable
416
- ? {
417
- onTouchStart : dragStart ,
418
- onTouchMove : dragMove ,
419
- onTouchEnd : dragEnd ,
420
- onMouseDown : dragStart ,
421
- onMouseMove : dragMove ,
422
- onMouseUp : dragEnd ,
423
- onMouseEnter,
424
- onMouseLeave,
425
- onClick : handleClickWrap ,
426
- }
427
- : {
428
- onMouseEnter,
429
- onMouseLeave,
430
- } ;
406
+ let events ;
407
+
408
+ if ( draggable ) {
409
+ events = canTouch
410
+ ? {
411
+ onTouchStart : dragStart ,
412
+ onTouchMove : dragMove ,
413
+ onTouchEnd : dragEnd ,
414
+ }
415
+ : {
416
+ onMouseDown : dragStart ,
417
+ onMouseMove : dragMove ,
418
+ onMouseUp : dragEnd ,
419
+ onMouseEnter,
420
+ onMouseLeave,
421
+ } ;
422
+ } else {
423
+ events = {
424
+ onMouseEnter,
425
+ onMouseLeave,
426
+ } ;
427
+ }
431
428
432
429
return (
433
430
< div
0 commit comments