From b00bbf27e6c454cb69e37f346d7a1503647ad2ce Mon Sep 17 00:00:00 2001 From: Kakabay <2kakabayashyrberdyew@gmail.com> Date: Fri, 31 Jan 2025 18:01:02 +0500 Subject: [PATCH] logic for the click on dot --- src/components/shared/home/home-offers.tsx | 38 +++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/components/shared/home/home-offers.tsx b/src/components/shared/home/home-offers.tsx index 1e59cd8..568526e 100644 --- a/src/components/shared/home/home-offers.tsx +++ b/src/components/shared/home/home-offers.tsx @@ -1,10 +1,33 @@ -import { FC } from "react"; +import { FC, useCallback, useEffect, useState } from "react"; import useEmblaCarousel from "embla-carousel-react"; import { Container } from "@/components/layout"; -import { OfferCard } from "../"; +import { EmblaDots, OfferCard } from "../"; export const HomeOffers: FC = () => { - const [emblaRef] = useEmblaCarousel({ align: "start" }); + const [emblaRef, emblaApi] = useEmblaCarousel({ align: "start" }); + const [activeIndex, setActiveIndex] = useState(0); + + const scrollTo = useCallback( + (index: number) => { + if (emblaApi) { + emblaApi.scrollTo(index); + } + }, + [emblaApi] + ); + + useEffect(() => { + if (!emblaApi) return; + + const onSelect = () => { + setActiveIndex(emblaApi.selectedScrollSnap()); + }; + + emblaApi.on("select", onSelect); + return () => { + emblaApi.off("select", onSelect); + }; + }, [emblaApi]); return (
@@ -20,10 +43,17 @@ export const HomeOffers: FC = () => { + +