Skip to content
Snippets Groups Projects
Carousel.tsx 3.02 KiB
Newer Older
Tojo's avatar
Tojo committed
import React from "react";

interface DataImage {
Tojo's avatar
Tojo committed
  src: string;
Tojo's avatar
Tojo committed
}

interface Props {
Tojo's avatar
Tojo committed
  images: Array<DataImage>;
  isLogo: boolean;
  delay?: number;
Tojo's avatar
Tojo committed
}

Tojo's avatar
Tojo committed
const Carousel = (props: Props) => {
  const { images, isLogo, delay } = props;
  const [index, setIndex] = React.useState(1);
  // const [leftAndRightDisabled, setLeftAndRightDisabled] = React.useState(false);
  // const [stateSlides, setStateSlides] = React.useState<any[]>([]);
Tojo's avatar
Tojo committed

  const nbr_image: number = images.length;

  const updateIndex = (newIndex: number) => {
    if (newIndex < 0) {
      newIndex = nbr_image - 1;
    } else if (newIndex >= nbr_image) {
      newIndex = 0;
    }

    setIndex(newIndex);
  };

  // React.useEffect(() => {
  //   const slidesWithClones = [...images];
  //   slidesWithClones.unshift(slidesWithClones[slidesWithClones.length - 1]);
  //   slidesWithClones.push(slidesWithClones[1]);
  //   setStateSlides(slidesWithClones);

  //   // ...
  // }, []);

Tojo's avatar
Tojo committed
  React.useEffect(() => {
    const interval = setInterval(() => {
      if (delay != 0) {
        updateIndex(index + 1);
      }
    }, delay);

    return () => {
      if (interval) {
        clearInterval(interval);
      }
    };
  });
Tojo's avatar
Tojo committed

  return (
    <div className="block relative">
Tojo's avatar
Tojo committed
      <div
        className={`carousel ${isLogo ? "w-[50%] ml-20" : "w-full"} truncate`}
Tojo's avatar
Tojo committed
      >
        <div
          className={`whitespace-nowrap ${isLogo ? "w-[25%]" : "w-full"} `}
          style={{
            transform:
             isLogo ?
              nbr_image > 4
                ? `translateX(-${index * 60 }%)`
                : `translateX(0)`
                : `translateX(-${index * 100}%)`,
            transition: "transform 0.3s",
          }}
        >
          {images.map((item: any, key: any) => {
            return (
              <div
                className={`carousel-item relative w-full inline-block`}
                key={key}
              >
                {isLogo ? (
                  <div className="flex justify-center items-center w-full">
                    <img
                      src={item.src}
                      className="w-28 h-28  border border-gray-600 p-4"
                    />
                  </div>
                ) : (
                  <img src={item.src} className="w-full h-[384px]" />
                )}
              </div>
            );
          })}
        </div>
Tojo's avatar
Tojo committed
      </div>
Tojo's avatar
Tojo committed
      <div
        className={`absolute flex justify-between pl-5 pr-5 ${
          isLogo ? "w-[62%]" : "w-full"
        }  ${isLogo ? "mt-[0%] top-9" : " mt-[0%] top-[47%]"} `}
Tojo's avatar
Tojo committed
      >
        <button
          onClick={() => updateIndex(index - 1)}
          className={`bg-[#5A5C68] rounded-3xl  ${
            isLogo ? "w-5" : "w-10"
          } h-10 text-white`}
Tojo's avatar
Tojo committed
        >

        </button>
        <button
          onClick={() => updateIndex(index + 1)}
          className={`bg-[#5A5C68] rounded-3xl  ${
            isLogo ? "w-5" : "w-10"
          } h-10 text-white`}
Tojo's avatar
Tojo committed
        >

        </button>
      </div>
Tojo's avatar
Tojo committed
    </div>
  );
};

export default Carousel;