|
| 1 | +import useEmblaCarousel from 'embla-carousel-react' |
| 2 | +import { AnimatePresence, motion } from 'motion/react' |
| 3 | +import { useCallback, useEffect, useMemo, useRef, useState } from 'react' |
| 4 | +import { RadixThemePortal } from '#@/pages/components/radix-theme-portal.tsx' |
| 5 | +import { convertYouTubeIframeURL } from './youtube-embed.tsx' |
| 6 | + |
| 7 | +export interface MediaItem { |
| 8 | + type: 'image' | 'video' |
| 9 | + src: string |
| 10 | +} |
| 11 | + |
| 12 | +interface GameMediaCarouselProps { |
| 13 | + items: MediaItem[] |
| 14 | + initialIndex: number |
| 15 | + originRect: DOMRect | null |
| 16 | + open: boolean |
| 17 | + onClose: (currentIndex: number) => void |
| 18 | + onSlideChange: (index: number) => void |
| 19 | +} |
| 20 | + |
| 21 | +export function GameMediaCarousel({ |
| 22 | + items, |
| 23 | + initialIndex, |
| 24 | + originRect, |
| 25 | + open, |
| 26 | + onClose, |
| 27 | + onSlideChange, |
| 28 | +}: Readonly<GameMediaCarouselProps>) { |
| 29 | + const [emblaRef, emblaApi] = useEmblaCarousel({ startIndex: initialIndex }) |
| 30 | + const [canScrollPrev, setCanScrollPrev] = useState(false) |
| 31 | + const [canScrollNext, setCanScrollNext] = useState(false) |
| 32 | + const [currentIndex, setCurrentIndex] = useState(initialIndex) |
| 33 | + const currentIndexRef = useRef(currentIndex) |
| 34 | + currentIndexRef.current = currentIndex |
| 35 | + |
| 36 | + const onEmblaSelect = useCallback(() => { |
| 37 | + if (!emblaApi) { |
| 38 | + return |
| 39 | + } |
| 40 | + const idx = emblaApi.selectedScrollSnap() |
| 41 | + setCanScrollPrev(emblaApi.canScrollPrev()) |
| 42 | + setCanScrollNext(emblaApi.canScrollNext()) |
| 43 | + setCurrentIndex(idx) |
| 44 | + onSlideChange(idx) |
| 45 | + }, [emblaApi, onSlideChange]) |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (!emblaApi) { |
| 49 | + return |
| 50 | + } |
| 51 | + onEmblaSelect() |
| 52 | + emblaApi.on('select', onEmblaSelect) |
| 53 | + emblaApi.on('reInit', onEmblaSelect) |
| 54 | + return () => { |
| 55 | + emblaApi.off('select', onEmblaSelect) |
| 56 | + emblaApi.off('reInit', onEmblaSelect) |
| 57 | + } |
| 58 | + }, [emblaApi, onEmblaSelect]) |
| 59 | + |
| 60 | + const handleClose = useCallback(() => { |
| 61 | + onClose(currentIndexRef.current) |
| 62 | + }, [onClose]) |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + if (!open) { |
| 66 | + return |
| 67 | + } |
| 68 | + const abortController = new AbortController() |
| 69 | + |
| 70 | + document.addEventListener( |
| 71 | + 'keydown', |
| 72 | + (event) => { |
| 73 | + if (event.key === 'Escape') { |
| 74 | + event.preventDefault() |
| 75 | + handleClose() |
| 76 | + } else if (event.key === 'ArrowLeft') { |
| 77 | + event.preventDefault() |
| 78 | + emblaApi?.scrollPrev() |
| 79 | + } else if (event.key === 'ArrowRight') { |
| 80 | + event.preventDefault() |
| 81 | + emblaApi?.scrollNext() |
| 82 | + } |
| 83 | + }, |
| 84 | + { signal: abortController.signal }, |
| 85 | + ) |
| 86 | + |
| 87 | + document.body.style.overflow = 'hidden' |
| 88 | + |
| 89 | + return () => { |
| 90 | + abortController.abort() |
| 91 | + document.body.style.overflow = '' |
| 92 | + } |
| 93 | + }, [open, handleClose, emblaApi]) |
| 94 | + |
| 95 | + const panelAnimProps = useMemo(() => { |
| 96 | + if (!originRect) { |
| 97 | + return { |
| 98 | + animate: { opacity: 1, scale: 1 }, |
| 99 | + exit: { opacity: 0, scale: 0.95 }, |
| 100 | + initial: { opacity: 0, scale: 0.95 }, |
| 101 | + } |
| 102 | + } |
| 103 | + const left = Math.round(originRect.left) |
| 104 | + const top = Math.round(originRect.top) |
| 105 | + const width = Math.round(originRect.width) |
| 106 | + const height = Math.round(originRect.height) |
| 107 | + return { |
| 108 | + animate: { |
| 109 | + height: Math.round(window.innerHeight * 0.9), |
| 110 | + left: Math.round(window.innerWidth * 0.05), |
| 111 | + top: Math.round(window.innerHeight * 0.05), |
| 112 | + width: Math.round(window.innerWidth * 0.9), |
| 113 | + }, |
| 114 | + exit: { height, left, top, width }, |
| 115 | + initial: { height, left, top, width }, |
| 116 | + } |
| 117 | + }, [originRect]) |
| 118 | + |
| 119 | + return ( |
| 120 | + <RadixThemePortal> |
| 121 | + <AnimatePresence> |
| 122 | + {open ? ( |
| 123 | + <> |
| 124 | + {/* Backdrop */} |
| 125 | + <motion.div |
| 126 | + animate={{ opacity: 1 }} |
| 127 | + aria-hidden |
| 128 | + className='fixed inset-0 z-50 cursor-default bg-black/80' |
| 129 | + exit={{ opacity: 0 }} |
| 130 | + initial={{ opacity: 0 }} |
| 131 | + onClick={handleClose} |
| 132 | + /> |
| 133 | + {/* Panel */} |
| 134 | + <motion.div |
| 135 | + animate={panelAnimProps.animate} |
| 136 | + className='fixed z-50' |
| 137 | + exit={panelAnimProps.exit} |
| 138 | + initial={panelAnimProps.initial} |
| 139 | + onClick={(e) => e.stopPropagation()} |
| 140 | + > |
| 141 | + <div className='flex h-full flex-col overflow-hidden' ref={emblaRef}> |
| 142 | + <div className='flex h-full'> |
| 143 | + {items.map((item, index) => ( |
| 144 | + <div className='flex min-h-0 min-w-0 flex-[0_0_100%] items-center justify-center' key={index}> |
| 145 | + {item.type === 'video' ? ( |
| 146 | + <iframe |
| 147 | + allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share' |
| 148 | + allowFullScreen |
| 149 | + className='h-full w-full border-none bg-black' |
| 150 | + referrerPolicy='strict-origin-when-cross-origin' |
| 151 | + sandbox='allow-same-origin allow-scripts allow-forms' |
| 152 | + src={convertYouTubeIframeURL(item.src)} |
| 153 | + title='YouTube video player' |
| 154 | + /> |
| 155 | + ) : ( |
| 156 | + <img alt='' className='h-full w-full object-contain' src={item.src} /> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + ))} |
| 160 | + </div> |
| 161 | + </div> |
| 162 | + </motion.div> |
| 163 | + {/* Prev button */} |
| 164 | + {canScrollPrev ? ( |
| 165 | + <motion.button |
| 166 | + animate={{ opacity: 1 }} |
| 167 | + aria-label='Previous' |
| 168 | + className='fixed top-1/2 left-[calc(5vw+0.75rem)] z-50 -translate-y-1/2 cursor-pointer rounded-full bg-black/50 p-2 text-white hover:bg-black/70' |
| 169 | + exit={{ opacity: 0 }} |
| 170 | + initial={{ opacity: 0 }} |
| 171 | + onClick={() => emblaApi?.scrollPrev()} |
| 172 | + type='button' |
| 173 | + > |
| 174 | + <span className='icon-[mdi--chevron-left] block size-6' /> |
| 175 | + </motion.button> |
| 176 | + ) : null} |
| 177 | + {/* Next button */} |
| 178 | + {canScrollNext ? ( |
| 179 | + <motion.button |
| 180 | + animate={{ opacity: 1 }} |
| 181 | + aria-label='Next' |
| 182 | + className='fixed top-1/2 right-[calc(5vw+0.75rem)] z-50 -translate-y-1/2 cursor-pointer rounded-full bg-black/50 p-2 text-white hover:bg-black/70' |
| 183 | + exit={{ opacity: 0 }} |
| 184 | + initial={{ opacity: 0 }} |
| 185 | + onClick={() => emblaApi?.scrollNext()} |
| 186 | + type='button' |
| 187 | + > |
| 188 | + <span className='icon-[mdi--chevron-right] block size-6' /> |
| 189 | + </motion.button> |
| 190 | + ) : null} |
| 191 | + {/* Close button */} |
| 192 | + <motion.button |
| 193 | + animate={{ opacity: 1 }} |
| 194 | + aria-label='Close' |
| 195 | + className='fixed top-4 right-4 z-50 cursor-pointer rounded-full bg-black/50 p-2 text-white hover:bg-black/70' |
| 196 | + exit={{ opacity: 0 }} |
| 197 | + initial={{ opacity: 0 }} |
| 198 | + onClick={handleClose} |
| 199 | + type='button' |
| 200 | + > |
| 201 | + <span className='icon-[mdi--close] block size-6' /> |
| 202 | + </motion.button> |
| 203 | + </> |
| 204 | + ) : null} |
| 205 | + </AnimatePresence> |
| 206 | + </RadixThemePortal> |
| 207 | + ) |
| 208 | +} |
0 commit comments