ChopFresh / src /components /HeroCarousel.tsx
pranav8tripathi@gmail.com
init
56ea4c7
import { useState, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { ChevronLeft, ChevronRight } from "lucide-react";
import { Button } from "./ui/button";
const heroImages = [
"https://images.unsplash.com/photo-1610348725531-843dff563e2c?w=1200&q=80",
"https://images.unsplash.com/photo-1542838132-92c53300491e?w=1200&q=80",
"https://images.unsplash.com/photo-1464226184884-fa280b87c399?w=1200&q=80",
"https://images.unsplash.com/photo-1566385101042-1a0aa0c1268c?w=1200&q=80",
];
export default function HeroCarousel() {
const [currentIndex, setCurrentIndex] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setCurrentIndex((prev) => (prev + 1) % heroImages.length);
}, 5000);
return () => clearInterval(interval);
}, []);
const goToSlide = (index: number) => {
setCurrentIndex(index);
};
const goToPrevious = () => {
setCurrentIndex((prev) => (prev - 1 + heroImages.length) % heroImages.length);
};
const goToNext = () => {
setCurrentIndex((prev) => (prev + 1) % heroImages.length);
};
const scrollToContact = () => {
const element = document.getElementById("contact");
if (element) {
element.scrollIntoView({ behavior: "smooth" });
}
};
const scrollToResearch = () => {
const element = document.getElementById("research");
if (element) {
element.scrollIntoView({ behavior: "smooth" });
}
};
return (
<div className="relative h-screen w-full overflow-hidden bg-gray-900">
{/* Carousel Images */}
<AnimatePresence mode="wait">
<motion.div
key={currentIndex}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.7 }}
className="absolute inset-0"
>
<img
src={heroImages[currentIndex]}
alt={`Slide ${currentIndex + 1}`}
className="w-full h-full object-cover"
/>
<div className="absolute inset-0 bg-black/40" />
</motion.div>
</AnimatePresence>
{/* Content Overlay */}
<div className="relative z-10 h-full flex items-center justify-center">
<div className="container mx-auto px-4 sm:px-6 lg:px-8 text-center">
<motion.h1
initial={{ y: 30, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={{ delay: 0.2, duration: 0.8 }}
className="text-4xl sm:text-5xl md:text-6xl lg:text-7xl font-bold text-white mb-6 leading-tight"
>
Hibernate your veggies/fruits smartly
</motion.h1>
<motion.p
initial={{ y: 30, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={{ delay: 0.4, duration: 0.8 }}
className="text-lg sm:text-xl md:text-2xl text-white/90 mb-8 max-w-3xl mx-auto"
>
Expert consultation on produce hibernation research and preservation techniques
</motion.p>
<motion.div
initial={{ y: 30, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={{ delay: 0.6, duration: 0.8 }}
className="flex flex-col sm:flex-row gap-4 justify-center items-center"
>
<Button
size="lg"
onClick={scrollToContact}
className="bg-green-600 hover:bg-green-700 text-white px-8 py-6 text-lg font-semibold rounded-full shadow-lg hover:shadow-xl transition-all"
>
Get Started
</Button>
<Button
size="lg"
variant="outline"
onClick={scrollToResearch}
className="bg-white/10 backdrop-blur-sm hover:bg-white/20 text-white border-white/30 px-8 py-6 text-lg font-semibold rounded-full shadow-lg hover:shadow-xl transition-all"
>
View Research
</Button>
</motion.div>
</div>
</div>
{/* Navigation Arrows */}
<button
onClick={goToPrevious}
className="absolute left-4 top-1/2 -translate-y-1/2 z-20 bg-white/20 hover:bg-white/30 backdrop-blur-sm p-3 rounded-full transition-all"
aria-label="Previous slide"
>
<ChevronLeft className="w-6 h-6 text-white" />
</button>
<button
onClick={goToNext}
className="absolute right-4 top-1/2 -translate-y-1/2 z-20 bg-white/20 hover:bg-white/30 backdrop-blur-sm p-3 rounded-full transition-all"
aria-label="Next slide"
>
<ChevronRight className="w-6 h-6 text-white" />
</button>
{/* Dots Indicator */}
<div className="absolute bottom-8 left-1/2 -translate-x-1/2 z-20 flex gap-2">
{heroImages.map((_, index) => (
<button
key={index}
onClick={() => goToSlide(index)}
className={`w-3 h-3 rounded-full transition-all ${
index === currentIndex
? "bg-white w-8"
: "bg-white/50 hover:bg-white/75"
}`}
aria-label={`Go to slide ${index + 1}`}
/>
))}
</div>
</div>
);
}