"use client" import { useOutsideClick } from "@/hooks/out-side-click" import { cn } from "@/lib/utils" import { IconArrowNarrowLeft, IconArrowNarrowRight } from "@tabler/icons-react" import { motion } from "framer-motion" import Image, { ImageProps } from "next/image" import { createContext, Fragment, JSX, useEffect, useRef, useState, } from "react" interface CarouselProps { items: JSX.Element[] initialScroll?: number cardGap?: number scrollOffset?: number } interface Card { src: string title: string category: string } const CarouselContext = createContext<{ onCardClick: (index: number) => void currentIndex: number }>({ onCardClick: () => { }, currentIndex: 0, }) export const Carousel = ({ items, initialScroll = 0, cardGap = 16, scrollOffset = 300, }: CarouselProps) => { const carouselRef = useRef(null) const [canScrollLeft, setCanScrollLeft] = useState(false) const [canScrollRight, setCanScrollRight] = useState(true) const [currentIndex, setCurrentIndex] = useState(0) const checkScrollAbility = () => { if (carouselRef.current) { const { scrollLeft, scrollWidth, clientWidth } = carouselRef.current setCanScrollLeft(scrollLeft > 0) setCanScrollRight(scrollLeft < scrollWidth - clientWidth) } } useEffect(() => { if (carouselRef.current) { carouselRef.current.scrollLeft = initialScroll checkScrollAbility() } }, [initialScroll]) const scroll = (direction: "left" | "right") => { if (carouselRef.current) { carouselRef.current.scrollBy({ left: direction === "left" ? -scrollOffset : scrollOffset, behavior: "smooth", }) } } const handleCarsouelClick = (index: number) => { setCurrentIndex(index) } return (
{items.map((item, index) => ( {item} ))}
) } export const Card = ({ card, index, layout = false, }: { card: Card index: number layout?: boolean }) => { const [isOpen, setIsOpen] = useState(false) const containerRef = useRef(null) const cardRef = useRef(null) useOutsideClick(containerRef, () => setIsOpen(false)) const handleCardClick = () => { setIsOpen(true) if (cardRef.current) { cardRef.current.scrollIntoView({ behavior: "smooth", block: "nearest", inline: "center", }) } } return (
{card.category} {card.title}
) } export const BlurImage = ({ height, width, src, className, alt, ...rest }: ImageProps) => { const [isLoading, setLoading] = useState(true) return ( setLoading(false)} src={src} width={width} height={height} loading="lazy" decoding="async" blurDataURL={typeof src === "string" ? src : undefined} alt={alt ? alt : "Background of a beautiful view"} {...rest} /> ) }