Skip to content

Commit

Permalink
fix: [Carousel] separate touch and mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
akai committed Jan 19, 2021
1 parent 63109f0 commit a533584
Showing 1 changed file with 27 additions and 30 deletions.
57 changes: 27 additions & 30 deletions src/components/Carousel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState, useEffect, useRef, useCallback, useImperativeHandle }
import clsx from 'clsx';
import { CarouselItem } from './Item';
import { setTransform, setTransition } from '../../utils/style';
import canUse from '../../utils/canUse';

export interface CarouselProps {
children: React.ReactNode;
Expand Down Expand Up @@ -40,13 +41,13 @@ interface State {
endX: number;
startY: number;
canMove: boolean | null;
preventClick: boolean;
pressDown: boolean;
}

type DragEvent = React.TouchEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement, MouseEvent>;

const formElements = ['TEXTAREA', 'OPTION', 'INPUT', 'SELECT'];
const canTouch = canUse('touch');

export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props, ref) => {
const {
Expand Down Expand Up @@ -82,7 +83,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
startY: 0,
canMove: null,
pressDown: false,
preventClick: false,
});

const getIndex = useCallback(
Expand Down Expand Up @@ -248,7 +248,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
state.endX = 0;
state.startY = 0;
state.canMove = null;
state.preventClick = false;
state.pressDown = false;
};

Expand Down Expand Up @@ -284,8 +283,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
if (!state.canMove) {
return;
}
} else if ((e.target as Element).nodeName === 'A') {
state.preventClick = true;
}

e.preventDefault();
Expand Down Expand Up @@ -319,6 +316,9 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
enableTransition();
if (state.endX) {
updateAfterDrag();
} else {
// when clicked
doAutoPlay();
}
resetDrag();
};
Expand All @@ -334,7 +334,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,

if (state.pressDown) {
state.pressDown = false;
state.preventClick = false;
state.endX = e.pageX;

enableTransition();
Expand All @@ -345,14 +344,6 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
doAutoPlay();
};

const handleClickWrap = (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
const state = stateRef.current;
if (state.preventClick) {
e.preventDefault();
}
state.preventClick = false;
};

const handleClickDot = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
const { slideTo: i } = e.currentTarget.dataset;
if (i) {
Expand Down Expand Up @@ -412,22 +403,28 @@ export const Carousel = React.forwardRef<CarouselHandle, CarouselProps>((props,
};
}, [autoPlay, activeIndex, doAutoPlay]);

const events = draggable
? {
onTouchStart: dragStart,
onTouchMove: dragMove,
onTouchEnd: dragEnd,
onMouseDown: dragStart,
onMouseMove: dragMove,
onMouseUp: dragEnd,
onMouseEnter,
onMouseLeave,
onClick: handleClickWrap,
}
: {
onMouseEnter,
onMouseLeave,
};
let events;

if (draggable) {
events = canTouch
? {
onTouchStart: dragStart,
onTouchMove: dragMove,
onTouchEnd: dragEnd,
}
: {
onMouseDown: dragStart,
onMouseMove: dragMove,
onMouseUp: dragEnd,
onMouseEnter,
onMouseLeave,
};
} else {
events = {
onMouseEnter,
onMouseLeave,
};
}

return (
<div
Expand Down

0 comments on commit a533584

Please sign in to comment.