// @ts-nocheck 'use client'; import { IconArrowNarrowLeft, IconArrowNarrowRight, IconX } from '@tabler/icons-react'; import { AnimatePresence, motion } from 'framer-motion'; import Image, { ImageProps } from 'next/image'; import React, { createContext, useContext, useEffect, useRef, useState } from 'react'; import { useOutsideClick } from '../../hooks/use-outside-click'; import { cn } from '../../lib/utils'; interface CarouselProps { items: JSX.Element[]; initialScroll?: number; } type Card = { src: string; title: string; category: string; content: React.ReactNode; }; export const CarouselContext = createContext<{ onCardClose: (index: number) => void; currentIndex: number; }>({ onCardClose: () => {}, currentIndex: 0, }); export const Carousel = ({ items, initialScroll = 0 }: CarouselProps) => { const carouselRef = React.useRef(null); const [canScrollLeft, setCanScrollLeft] = React.useState(false); const [canScrollRight, setCanScrollRight] = React.useState(true); const [currentIndex, setCurrentIndex] = useState(0); useEffect(() => { if (carouselRef.current) { carouselRef.current.scrollLeft = initialScroll; checkScrollability(); } }, [initialScroll]); const checkScrollability = () => { if (carouselRef.current) { const { scrollLeft, scrollWidth, clientWidth } = carouselRef.current; setCanScrollLeft(scrollLeft > 0); setCanScrollRight(scrollLeft < scrollWidth - clientWidth); } }; const scrollLeft = () => { if (carouselRef.current) { carouselRef.current.scrollBy({ left: -300, behavior: 'smooth' }); } }; const scrollRight = () => { if (carouselRef.current) { carouselRef.current.scrollBy({ left: 300, behavior: 'smooth' }); } }; const handleCardClose = (index: number) => { if (carouselRef.current) { const cardWidth = isMobile() ? 230 : 384; // (md:w-96) const gap = isMobile() ? 4 : 8; const scrollPosition = (cardWidth + gap) * (index + 1); carouselRef.current.scrollTo({ left: scrollPosition, behavior: 'smooth', }); setCurrentIndex(index); } }; const isMobile = () => { return window && window.innerWidth < 768; }; return (
{items.map((item, index) => ( {item} ))}
); }; export const Card = ({ card, index, layout = false, }: { card: Card; index: number; layout?: boolean; }) => { const [open, setOpen] = useState(false); const containerRef = useRef(null); const { onCardClose, currentIndex } = useContext(CarouselContext); useEffect(() => { function onKeyDown(event: KeyboardEvent) { if (event.key === 'Escape') { handleClose(); } } if (open) { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = 'auto'; } window.addEventListener('keydown', onKeyDown); return () => window.removeEventListener('keydown', onKeyDown); }, [open]); useOutsideClick(containerRef, () => handleClose()); const handleOpen = () => { setOpen(true); }; const handleClose = () => { setOpen(false); onCardClose(index); }; return ( <> {open && (
{card.category} {card.title}
{card.content}
)}
{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} /> ); };