Skip to content

Commit fac9838

Browse files
committed
✨ Allow the carousel to be scrollable via wheel gestures
1 parent e622ef3 commit fac9838

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

bun.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/home/featured-jargon-carousel.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { useRef, useState, useEffect, useCallback } from "react";
44
import Autoplay from "embla-carousel-autoplay";
5+
import WheelGestures from "embla-carousel-wheel-gestures";
56
import { Sparkles } from "lucide-react";
67
import {
78
Carousel,
@@ -25,14 +26,16 @@ export type FeaturedJargonCarouselItem = {
2526
comment_count: number;
2627
};
2728

29+
const autoplayPlugin = Autoplay({ delay: 2000, stopOnInteraction: true });
30+
const wheelGesturesPlugin = WheelGestures({ forceWheelAxis: "x" });
31+
2832
export default function FeaturedJargonCarousel({
2933
featuredJargons,
3034
className,
3135
}: {
3236
featuredJargons: FeaturedJargonCarouselItem[];
3337
className?: string;
3438
}) {
35-
const plugin = useRef(Autoplay({ delay: 2000, stopOnInteraction: true }));
3639
const [api, setApi] = useState<CarouselApi>();
3740
const [canScrollPrev, setCanScrollPrev] = useState(false);
3841
const [canScrollNext, setCanScrollNext] = useState(false);
@@ -130,10 +133,10 @@ export default function FeaturedJargonCarousel({
130133
</h2>
131134
<Carousel
132135
setApi={setApi}
133-
plugins={[plugin.current]}
136+
plugins={[autoplayPlugin, wheelGesturesPlugin]}
134137
className="relative w-full"
135-
onMouseEnter={plugin.current.stop}
136-
onMouseLeave={plugin.current.reset}
138+
onMouseEnter={autoplayPlugin.stop}
139+
onMouseLeave={autoplayPlugin.reset}
137140
>
138141
{/* Left fade overlay */}
139142
{canScrollPrev && (

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dayjs": "^1.11.13",
3939
"embla-carousel-autoplay": "^8.6.0",
4040
"embla-carousel-react": "^8.6.0",
41+
"embla-carousel-wheel-gestures": "^8.1.0",
4142
"fast-deep-equal": "^3.1.3",
4243
"lucide-react": "^0.539.0",
4344
"match-sorter": "^8.1.0",

0 commit comments

Comments
 (0)