Skip to content
Snippets Groups Projects
Carousel.tsx 4.32 KiB
Newer Older
Toavina's avatar
Toavina committed
import React, { useEffect, useState } from "react"
import Image from "./Image"
Tojo's avatar
Tojo committed
interface DataImage {
Toavina's avatar
Toavina committed
    src: string
Tojo's avatar
Tojo committed
}

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

Tojo's avatar
Tojo committed
const Carousel = (props: Props) => {
    const { images, isLogo, delay, fullSize } = props
Toavina's avatar
Toavina committed
    const [index, setIndex] = useState(1)
Tojo's avatar
Tojo committed

Toavina's avatar
Toavina committed
    const nbr_image: number = images.length
Tojo's avatar
Tojo committed

Toavina's avatar
Toavina committed
    const updateIndex = (newIndex: number) => {
        if (newIndex < 0) {
            newIndex = nbr_image - 1
        } else if (newIndex >= nbr_image) {
            newIndex = 0
        }
Tojo's avatar
Tojo committed

Toavina's avatar
Toavina committed
        setIndex(newIndex)
    }
Tojo's avatar
Tojo committed

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

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

Toavina's avatar
Toavina committed
    return (
        <div
            className={`block relative ${
                isLogo
                    ? "flex items-center w-1/2 box-content float-none"
                    : "flex items-center"
            }`}
        >
            <div className="btn-carousel">
                <button
                    onClick={() => updateIndex(index - 1)}
                    className={`bg-neutral rounded-3xl  ${
                        isLogo ? "w-5" : "w-10 absolute z-10 left-2"
                    } h-10 text-base-100`}
                >

                </button>
            </div>
Toavina's avatar
Toavina committed
            <div
                className={`inline-block align-middle carousel ${
                    isLogo ? "w-1/2 ml-2 mr-2" : "w-full"
Toavina's avatar
Toavina committed
                } truncate`}
            >
                <div
                    className={`whitespace-nowrap ${
                        isLogo ? (fullSize ? "w-full" : "w-4/12") : "w-full"
Toavina's avatar
Toavina committed
                    } `}
                    style={{
                        transform: isLogo
                            ? nbr_image > 2
                                ? fullSize
                                    ? `translateX(-${index * 100}%)`
                                    : `translateX(-${index * 60}%)`
Toavina's avatar
Toavina committed
                                : `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 ${
                                    isLogo
                                        ? fullSize
                                            ? "mr-0"
                                            : "mr-5"
                                        : "mr-0"
                                }`}
Toavina's avatar
Toavina committed
                                key={key}
                            >
                                {isLogo ? (
                                    <div
                                        className={`flex justify-center items-center ${
                                            fullSize ? "w-full" : "w-full h-28"
                                        } `}
                                    >
                                        <Image
                                            image={item.src}
                                            className="w-full border border-gray-600"
Toavina's avatar
Toavina committed
                                        />
                                    </div>
                                ) : (
                                    <Image
                                        image={item.src}
                                        className="w-full"
Toavina's avatar
Toavina committed
                                    />
                                )}
                            </div>
                        )
                    })}
                </div>
            </div>
            <div className="btn-carousel">
Toavina's avatar
Toavina committed
                <button
                    onClick={() => updateIndex(index + 1)}
                    className={`bg-neutral rounded-3xl  ${
                        isLogo ? "w-5" : "w-10 absolute z-10 right-2"
Toavina's avatar
Toavina committed
                    } h-10 text-base-100`}
                >

                </button>
            </div>
Tojo's avatar
Tojo committed

Toavina's avatar
Toavina committed
export default Carousel