'use client'; import { useCallback, useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import { ChevronLeft, ChevronRight, X } from 'lucide-react'; type Props = { urls: string[]; startIndex: number; onClose: () => void; }; export default function FeedLightbox({ urls, startIndex, onClose }: Props) { const [index, setIndex] = useState(startIndex); const [mounted, setMounted] = useState(false); useEffect(() => { setMounted(true); }, []); const next = useCallback(() => { setIndex((i) => (i + 1) % urls.length); }, [urls.length]); const prev = useCallback(() => { setIndex((i) => (i - 1 + urls.length) % urls.length); }, [urls.length]); useEffect(() => { const handleKey = (e: KeyboardEvent) => { if (e.key === 'Escape') { onClose(); } else if (e.key === 'ArrowRight') { next(); } else if (e.key === 'ArrowLeft') { prev(); } }; document.addEventListener('keydown', handleKey); document.body.style.overflow = 'hidden'; return () => { document.removeEventListener('keydown', handleKey); document.body.style.overflow = ''; }; }, [next, prev, onClose]); if (!mounted || urls.length === 0) { return null; } const body = (
{urls.length > 1 && ( )}
e.stopPropagation()}> {`이미지 {urls.length > 1 && (
{index + 1} / {urls.length}
)}
{urls.length > 1 && ( )}
); return createPortal(body, document.body); }