1"use client"
2
3import { memo, useEffect, useLayoutEffect, useMemo, useState } from "react"
4import {
5 AnimatePresence,
6 motion,
7 useAnimation,
8 useMotionValue,
9 useTransform,
10} from "motion/react"
11
12export const useIsomorphicLayoutEffect =
13 typeof window !== "undefined" ? useLayoutEffect : useEffect
14
15type UseMediaQueryOptions = {
16 defaultValue?: boolean
17 initializeWithValue?: boolean
18}
19
20const IS_SERVER = typeof window === "undefined"
21
22export function useMediaQuery(
23 query: string,
24 {
25 defaultValue = false,
26 initializeWithValue = true,
27 }: UseMediaQueryOptions = {}
28): boolean {
29 const getMatches = (query: string): boolean => {
30 if (IS_SERVER) {
31 return defaultValue
32 }
33 return window.matchMedia(query).matches
34 }
35
36 const [matches, setMatches] = useState<boolean>(() => {
37 if (initializeWithValue) {
38 return getMatches(query)
39 }
40 return defaultValue
41 })
42
43 const handleChange = () => {
44 setMatches(getMatches(query))
45 }
46
47 useIsomorphicLayoutEffect(() => {
48 const matchMedia = window.matchMedia(query)
49 handleChange()
50
51 matchMedia.addEventListener("change", handleChange)
52
53 return () => {
54 matchMedia.removeEventListener("change", handleChange)
55 }
56 }, [query])
57
58 return matches
59}
60
61const keywords = [
62 "night",
63 "city",
64 "sky",
65 "sunset",
66 "sunrise",
67 "winter",
68 "skyscraper",
69 "building",
70 "cityscape",
71 "architecture",
72 "street",
73 "lights",
74 "downtown",
75 "bridge",
76]
77
78const duration = 0.15
79const transition = {
80 duration,
81 ease: [0.32, 0.72, 0, 1] as const,
82 filter: "blur(4px)",
83}
84const transitionOverlay = { duration: 0.5, ease: [0.32, 0.72, 0, 1] as const }
85
86const Carousel = memo(
87 ({
88 handleClick,
89 controls,
90 cards,
91 isCarouselActive,
92 }: {
93 handleClick: (imgUrl: string, index: number) => void
94 controls: any
95 cards: string[]
96 isCarouselActive: boolean
97 }) => {
98 const isScreenSizeSm = useMediaQuery("(max-width: 640px)")
99 const cylinderWidth = isScreenSizeSm ? 1100 : 1800
100 const faceCount = cards.length
101 const faceWidth = cylinderWidth / faceCount
102 const radius = cylinderWidth / (2 * Math.PI)
103 const rotation = useMotionValue(0)
104 const transform = useTransform(
105 rotation,
106 (value) => `rotate3d(0, 1, 0, ${value}deg)`
107 )
108
109 return (
110 <div
111 className="flex h-full items-center justify-center bg-mauve-dark-2"
112 style={{
113 perspective: "1000px",
114 transformStyle: "preserve-3d",
115 willChange: "transform",
116 }}
117 >
118 <motion.div
119 drag={isCarouselActive ? "x" : false}
120 className="relative flex h-full origin-center cursor-grab justify-center active:cursor-grabbing"
121 style={{
122 transform,
123 rotateY: rotation,
124 width: cylinderWidth,
125 transformStyle: "preserve-3d",
126 }}
127 onDrag={(_, info) =>
128 isCarouselActive &&
129 rotation.set(rotation.get() + info.offset.x * 0.05)
130 }
131 onDragEnd={(_, info) =>
132 isCarouselActive &&
133 controls.start({
134 rotateY: rotation.get() + info.velocity.x * 0.05,
135 transition: {
136 type: "spring" as const,
137 stiffness: 100,
138 damping: 30,
139 mass: 0.1,
140 },
141 })
142 }
143 animate={controls}
144 >
145 {cards.map((imgUrl, i) => (
146 <motion.div
147 key={`key-${imgUrl}-${i}`}
148 className="absolute flex h-full origin-center items-center justify-center rounded-xl bg-mauve-dark-2 p-2"
149 style={{
150 width: `${faceWidth}px`,
151 transform: `rotateY(${
152 i * (360 / faceCount)
153 }deg) translateZ(${radius}px)`,
154 }}
155 onClick={() => handleClick(imgUrl, i)}
156 >
157 <motion.img
158 src={imgUrl}
159 alt={`keyword_${i} ${imgUrl}`}
160 layoutId={`img-${imgUrl}`}
161 className="pointer-events-none w-full rounded-xl object-cover aspect-square"
162 initial={{ filter: "blur(4px)" }}
163 layout="position"
164 animate={{ filter: "blur(0px)" }}
165 transition={transition}
166 />
167 </motion.div>
168 ))}
169 </motion.div>
170 </div>
171 )
172 }
173)
174
175const hiddenMask = `repeating-linear-gradient(to right, rgba(0,0,0,0) 0px, rgba(0,0,0,0) 30px, rgba(0,0,0,1) 30px, rgba(0,0,0,1) 30px)`
176const visibleMask = `repeating-linear-gradient(to right, rgba(0,0,0,0) 0px, rgba(0,0,0,0) 0px, rgba(0,0,0,1) 0px, rgba(0,0,0,1) 30px)`
177function ThreeDPhotoCarousel() {
178 const [activeImg, setActiveImg] = useState<string | null>(null)
179 const [isCarouselActive, setIsCarouselActive] = useState(true)
180 const controls = useAnimation()
181 const cards = useMemo(
182 () => keywords.map((keyword) => `https://picsum.photos/200/300?${keyword}`),
183 []
184 )
185
186 useEffect(() => {
187 console.log("Cards loaded:", cards)
188 }, [cards])
189
190 const handleClick = (imgUrl: string) => {
191 setActiveImg(imgUrl)
192 setIsCarouselActive(false)
193 controls.stop()
194 }
195
196 const handleClose = () => {
197 setActiveImg(null)
198 setIsCarouselActive(true)
199 }
200
201 return (
202 <motion.div layout className="relative">
203 <AnimatePresence mode="sync">
204 {activeImg && (
205 <motion.div
206 initial={{ opacity: 0, scale: 0 }}
207 animate={{ opacity: 1, scale: 1 }}
208 exit={{ opacity: 0, scale: 0 }}
209 layoutId={`img-container-${activeImg}`}
210 layout="position"
211 onClick={handleClose}
212 className="fixed inset-0 bg-black bg-opacity-10 flex items-center justify-center z-50 m-5 md:m-36 lg:mx-[19rem] rounded-3xl"
213 style={{ willChange: "opacity" }}
214 transition={transitionOverlay}
215 >
216 <motion.img
217 layoutId={`img-${activeImg}`}
218 src={activeImg}
219 className="max-w-full max-h-full rounded-lg shadow-lg"
220 initial={{ scale: 0.5 }} // Start with a smaller scale
221 animate={{ scale: 1 }} // Animate to full scale
222 transition={{
223 delay: 0.5,
224 duration: 0.5,
225 ease: [0.25, 0.1, 0.25, 1] as const,
226 }} // Clean ease-out curve
227 style={{
228 willChange: "transform",
229 }}
230 />
231 </motion.div>
232 )}
233 </AnimatePresence>
234 <div className="relative h-[500px] w-full overflow-hidden">
235 <Carousel
236 handleClick={handleClick}
237 controls={controls}
238 cards={cards}
239 isCarouselActive={isCarouselActive}
240 />
241 </div>
242 </motion.div>
243 )
244}
245
246export default ThreeDPhotoCarousel
247