import { useCallback, useEffect, useMemo, useState } from "react"; import type { EmblaCarouselType, EmblaOptionsType } from "embla-carousel"; import useEmblaCarousel from "embla-carousel-react"; import { WheelGesturesPlugin } from "embla-carousel-wheel-gestures"; import { getCarouselEmblaOptions, getCarouselWheelGestureOptions } from "@/lib/carouselEmbla"; interface UseCarouselEmblaOptions { options?: EmblaOptionsType; } export function useCarouselEmbla({ options }: UseCarouselEmblaOptions = {}) { const [canScrollPrev, setCanScrollPrev] = useState(false); const [canScrollNext, setCanScrollNext] = useState(false); const emblaOptions = useMemo(() => getCarouselEmblaOptions(options), [options]); const emblaPlugins = useMemo(() => [WheelGesturesPlugin(getCarouselWheelGestureOptions())], []); const [emblaRef, emblaApi] = useEmblaCarousel(emblaOptions, emblaPlugins); const updateScrollState = useCallback((api: EmblaCarouselType) => { setCanScrollPrev(api.canScrollPrev()); setCanScrollNext(api.canScrollNext()); }, []); useEffect(() => { if (!emblaApi) return; updateScrollState(emblaApi); emblaApi.on("select", updateScrollState); emblaApi.on("reInit", updateScrollState); return () => { emblaApi.off("select", updateScrollState); emblaApi.off("reInit", updateScrollState); }; }, [emblaApi, updateScrollState]); const scrollPrev = useCallback(() => { emblaApi?.scrollPrev(); }, [emblaApi]); const scrollNext = useCallback(() => { emblaApi?.scrollNext(); }, [emblaApi]); return { emblaApi, emblaRef, canScrollPrev, canScrollNext, scrollPrev, scrollNext, }; }