Skip to content

Commit d9e71f7

Browse files
Fran McDadeFran McDade
authored andcommitted
feat: added pause to carousel with mouse enter (#66)
1 parent 0238626 commit d9e71f7

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

app/components/Home/components/Section/components/SectionHero/components/Carousel/hooks/useInteractiveCarousel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function useInteractiveCarousel(): UseInteractiveCarousel {
3131
const swipeInteraction = useSwipeInteraction(
3232
interactiveIndexes.length,
3333
true,
34-
20000
34+
12000
3535
);
3636
return {
3737
interactiveCards: carouselCards,

app/hooks/useSwipeInteraction/useSwipeInteraction.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { SwipeAction, SwipeCoordinates, SWIPE_ACTION } from "./common/entities";
1414

1515
export interface InteractiveAction {
1616
onMouseDown: (mouseEvent: MouseEvent) => void;
17+
onMouseEnter: () => void;
18+
onMouseLeave: () => void;
1719
onMouseUp: (mouseEvent: MouseEvent) => void;
1820
onTouchEnd: (touchEvent: TouchEvent) => void;
1921
onTouchMove: (touchEvent: TouchEvent) => void;
@@ -39,6 +41,7 @@ export function useSwipeInteraction(
3941
swipeEnabled = true,
4042
swipeDelay = 0
4143
): UseSwipeInteraction {
44+
const [interactiveDelay, setInteractiveDelay] = useState<number>(swipeDelay);
4245
const swipeStartCoordsRef = useRef<SwipeCoordinates>(
4346
DEFAULT_SWIPE_COORDINATES
4447
);
@@ -52,6 +55,14 @@ export function useSwipeInteraction(
5255
swipeStartCoordsRef.current = getMouseCoords(mouseEvent);
5356
}, []);
5457

58+
const onMouseEnter = useCallback((): void => {
59+
setInteractiveDelay(0);
60+
}, []);
61+
62+
const onMouseLeave = useCallback((): void => {
63+
setInteractiveDelay(swipeDelay);
64+
}, [swipeDelay]);
65+
5566
const onMouseUp = useCallback((mouseEvent: MouseEvent): void => {
5667
const mouseStartCoords = swipeStartCoordsRef.current;
5768
const mouseEndCoords = getMouseCoords(mouseEvent);
@@ -123,12 +134,12 @@ export function useSwipeInteraction(
123134
}, [swipeAction, onSwipeToIndex]);
124135

125136
useEffect(() => {
126-
if (swipeDelay === 0) return;
137+
if (interactiveDelay === 0) return;
127138
const timeout = setTimeout(() => {
128139
onSwipeToIndex(1);
129-
}, swipeDelay);
140+
}, interactiveDelay);
130141
return () => clearTimeout(timeout);
131-
}, [activeIndex, swipeDelay, onSwipeToIndex]);
142+
}, [activeIndex, interactiveDelay, onSwipeToIndex]);
132143

133144
if (!swipeEnabled) {
134145
return {
@@ -142,6 +153,8 @@ export function useSwipeInteraction(
142153
activeIndex,
143154
interactiveAction: {
144155
onMouseDown,
156+
onMouseEnter,
157+
onMouseLeave,
145158
onMouseUp,
146159
onTouchEnd,
147160
onTouchMove,

0 commit comments

Comments
 (0)