Skip to content

Commit 2f088c4

Browse files
author
Kiril Krsteski
committed
fix: add inertia swiping
1 parent 5997cc2 commit 2f088c4

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "@kkx64/react-simple-carousel",
3-
"version": "1.5.2",
3+
"version": "1.5.3",
44
"exports": "./src/index.ts"
55
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kkx64/react-simple-carousel",
3-
"version": "1.5.2",
3+
"version": "1.5.3",
44
"description": "Simple carousel component for React",
55
"author": "Kiril Krsteski",
66
"homepage": "https://github.com/kkx64/react-simple-carousel",

src/Carousel.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
9898
const [dragStart, setDragStart] = useState({
9999
x: 0,
100100
y: 0,
101+
timestamp: 0,
101102
});
102103
const [dragging, setDragging] = useState(false);
103104
const [scrolling, setScrolling] = useState(false);
@@ -207,7 +208,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
207208
clearInterval(autoPlayIntervalRef.current);
208209
}
209210

210-
setDragStart({ x: clientX, y: clientY });
211+
setDragStart({ x: clientX, y: clientY, timestamp: Date.now() });
211212
}, []);
212213

213214
const handleDragMove = useCallback(
@@ -264,9 +265,15 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
264265

265266
// Determine the new slide index based on drag offset
266267
let newSlide = currentSlide;
267-
if (dragOffset > containerWidth / 2) {
268+
if (
269+
dragOffset > containerWidth / 2 ||
270+
(Date.now() - dragStart.timestamp < 200 && dragOffset > 20)
271+
) {
268272
newSlide = Math.max(0, currentSlide - 1);
269-
} else if (dragOffset < -containerWidth / 2) {
273+
} else if (
274+
dragOffset < -containerWidth / 2 ||
275+
(Date.now() - dragStart.timestamp < 200 && dragOffset < -20)
276+
) {
270277
newSlide = Math.min(slides - shownSlides, currentSlide + 1);
271278
}
272279

@@ -298,7 +305,7 @@ const Carousel = forwardRef<CarouselRef, CarouselProps>(
298305
if (!disableTranslate) {
299306
return {
300307
transform: `translateX(${-translateX}px)`,
301-
transitionDuration: `${dragging ? 0.01 : transitionDuration}s`,
308+
transitionDuration: `${dragging ? 0.005 : transitionDuration}s`,
302309
transitionTimingFunction: dragging ? "linear" : undefined,
303310
height: fitHeight ? "fit-content" : undefined,
304311
};

0 commit comments

Comments
 (0)