|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import { AnimatePresence, motion } from "framer-motion"; |
| 3 | +import { Calendar as CalendarIcon, DotIcon } from "lucide-react"; |
| 4 | + |
| 5 | +const monthArray = [ |
| 6 | + "", |
| 7 | + "January", |
| 8 | + "February", |
| 9 | + "March", |
| 10 | + "April", |
| 11 | + "May", |
| 12 | + "June", |
| 13 | + "July", |
| 14 | + "August", |
| 15 | + "September", |
| 16 | + "October", |
| 17 | + "November", |
| 18 | + "December", |
| 19 | +]; |
| 20 | + |
| 21 | +interface EventType { |
| 22 | + date: number; |
| 23 | + title: string; |
| 24 | + time: string; |
| 25 | +} |
| 26 | + |
| 27 | +export default function CalendarWidget({ |
| 28 | + initialSelectedDate = 1, |
| 29 | + initialShowEvents = true, |
| 30 | + eventsData = [], |
| 31 | + month = 1, |
| 32 | + year = new Date().getFullYear(), |
| 33 | +}: { |
| 34 | + initialSelectedDate?: number; |
| 35 | + initialShowEvents?: boolean; |
| 36 | + eventsData?: EventType[]; |
| 37 | + month?: number; |
| 38 | + year?: number; |
| 39 | +}) { |
| 40 | + const [selectedDate, setSelectedDate] = useState(initialSelectedDate); |
| 41 | + const [showEvents, setShowEvents] = useState(initialShowEvents); |
| 42 | + const scrollRef = useRef<HTMLDivElement>(null); |
| 43 | + |
| 44 | + const dates = Array.from({ length: 30 }, (_, i) => i + 1); |
| 45 | + const daySymbols = ["S", "M", "T", "W", "T", "F", "S"]; |
| 46 | + |
| 47 | + const filteredEvents = eventsData.filter((event: EventType) => event.date === selectedDate); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (scrollRef.current) { |
| 51 | + const selectedElement = scrollRef.current.querySelector(`[data-date="${selectedDate}"]`); |
| 52 | + if (selectedElement) { |
| 53 | + selectedElement.scrollIntoView({ behavior: "smooth", inline: "center", block: "nearest" }); |
| 54 | + } |
| 55 | + } |
| 56 | + }, [selectedDate]); |
| 57 | + |
| 58 | + return ( |
| 59 | + <motion.div |
| 60 | + className="mx-auto max-w-sm rounded-3xl bg-slate-200 shadow-lg" |
| 61 | + initial={{ scale: 0.9, opacity: 0 }} |
| 62 | + animate={{ scale: 1, opacity: 1 }} |
| 63 | + transition={{ duration: 0.3 }} |
| 64 | + > |
| 65 | + <div className="mx-auto max-w-sm rounded-3xl px-6 py-2"> |
| 66 | + <h2 className="mb-4 text-2xl font-bold">{`${monthArray[month]} ${year}`}</h2> |
| 67 | + <div |
| 68 | + ref={scrollRef} |
| 69 | + className="scrollbar-hide flex items-start space-x-2 overflow-x-auto py-2" |
| 70 | + style={{ scrollbarWidth: "none", msOverflowStyle: "none" }} |
| 71 | + > |
| 72 | + {dates.map((date, index) => ( |
| 73 | + <motion.button |
| 74 | + key={date} |
| 75 | + data-date={date} |
| 76 | + className="flex w-10 flex-shrink-0 flex-col items-center justify-center gap-y-2 rounded-lg" |
| 77 | + onClick={() => { |
| 78 | + setSelectedDate(date); |
| 79 | + setShowEvents(true); |
| 80 | + }} |
| 81 | + whileHover={{ scale: 1.05 }} |
| 82 | + whileTap={{ scale: 0.95 }} |
| 83 | + > |
| 84 | + <span className="mb-1 text-xs">{daySymbols[(index + 4) % 7]}</span> |
| 85 | + <AnimatePresence> |
| 86 | + {selectedDate === date ? ( |
| 87 | + <motion.span |
| 88 | + layoutId="highlighted-date" |
| 89 | + className="flex h-8 w-8 items-center justify-center rounded-full bg-gray-100 text-black shadow-lg" |
| 90 | + transition={{ |
| 91 | + type: "spring", |
| 92 | + stiffness: 300, |
| 93 | + damping: 20, |
| 94 | + }} |
| 95 | + > |
| 96 | + {date} |
| 97 | + </motion.span> |
| 98 | + ) : ( |
| 99 | + <span className="flex h-8 w-8 items-center justify-center">{date}</span> |
| 100 | + )} |
| 101 | + </AnimatePresence> |
| 102 | + <span> |
| 103 | + {eventsData.find((alldates: EventType) => alldates.date === date) ? ( |
| 104 | + <DotIcon /> |
| 105 | + ) : ( |
| 106 | + "" |
| 107 | + )} |
| 108 | + </span> |
| 109 | + </motion.button> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + |
| 114 | + <div className="w-full rounded-3xl bg-[#fefefe] px-6 shadow-lg"> |
| 115 | + <AnimatePresence mode="wait"> |
| 116 | + {showEvents && ( |
| 117 | + <motion.div |
| 118 | + key="events" |
| 119 | + className="scrollbar-hide mt-2 h-[150px] overflow-scroll border-t pt-4" |
| 120 | + initial={{ opacity: 0, y: 20 }} |
| 121 | + animate={{ opacity: 1, y: 0 }} |
| 122 | + exit={{ opacity: 0, y: -20 }} |
| 123 | + transition={{ duration: 0.2 }} |
| 124 | + > |
| 125 | + {filteredEvents.length > 0 ? ( |
| 126 | + filteredEvents.map((event: EventType, index: number) => ( |
| 127 | + <motion.div |
| 128 | + key={index} |
| 129 | + className="mb-2 border-b-2 border-slate-200" |
| 130 | + initial={{ opacity: 0, x: -20 }} |
| 131 | + animate={{ opacity: 1, x: 0 }} |
| 132 | + transition={{ delay: index * 0.1 }} |
| 133 | + > |
| 134 | + <h4 className="font-medium">{event.title}</h4> |
| 135 | + <p className="text-sm text-gray-500">{event.time}</p> |
| 136 | + </motion.div> |
| 137 | + )) |
| 138 | + ) : ( |
| 139 | + <motion.div |
| 140 | + className="flex flex-col items-center text-gray-500" |
| 141 | + initial={{ opacity: 0 }} |
| 142 | + animate={{ opacity: 1 }} |
| 143 | + > |
| 144 | + <CalendarIcon className="mb-2 h-8 w-8" /> |
| 145 | + <p>No Events</p> |
| 146 | + </motion.div> |
| 147 | + )} |
| 148 | + </motion.div> |
| 149 | + )} |
| 150 | + </AnimatePresence> |
| 151 | + </div> |
| 152 | + </motion.div> |
| 153 | + ); |
| 154 | +} |
0 commit comments