forked from SWYP-team-2th/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
이미지 상세 모달을 제작한다. (SWYP-team-2th#122)
* 이미지 상세 모달을 제작한다. * 이미지 모달 연결 * 버튼 변경 및 로딩 추가
- Loading branch information
Showing
8 changed files
with
162 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/components/vote-detail/ImageDetailModal/ImageDetailModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import useImageDetailModal from './hooks'; | ||
import { Image } from '@/api/useGetVoteDetail'; | ||
import Icon from '@/components/common/Icon'; | ||
|
||
interface ImageDetailModalProps { | ||
images: Image[]; | ||
selectedImageId: number; | ||
} | ||
|
||
export default function ImageDetailModal({ | ||
images, | ||
selectedImageId, | ||
}: ImageDetailModalProps) { | ||
const { scrollContainerRef, currentIndex, handleScroll, closeDialog } = | ||
useImageDetailModal({ | ||
images, | ||
selectedImageId, | ||
}); | ||
|
||
return ( | ||
<div className="bg-gray-700 w-full h-[100dvh] max-w-[480px] flex flex-col"> | ||
<header className="h-[57px] flex items-center justify-between px-4 text-white z-10"> | ||
<button onClick={closeDialog}> | ||
<Icon name="ArrowLeft" size="medium" /> | ||
</button> | ||
<div className="text-white"> | ||
<span className="font-medium">{currentIndex + 1}</span> | ||
<span className="mx-1">/</span> | ||
<span>{images.length}</span> | ||
</div> | ||
<div className="w-[24px] h-full"></div> | ||
</header> | ||
|
||
<div | ||
ref={scrollContainerRef} | ||
className="flex-1 flex overflow-x-auto snap-x snap-mandatory" | ||
style={{ scrollBehavior: 'smooth' }} | ||
onScroll={handleScroll} | ||
> | ||
{images.map((image) => ( | ||
<div | ||
key={image.id} | ||
className="min-w-full h-full flex-shrink-0 snap-center flex items-center justify-center" | ||
> | ||
<img | ||
src={image.imageUrl} | ||
alt={`image-${image.id}`} | ||
className="w-full h-auto max-h-full object-cover" | ||
/> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { Image } from '@/api/useGetVoteDetail'; | ||
import { useDialog } from '@/components/common/Dialog/hooks'; | ||
|
||
interface UseImageDetailModalOptions { | ||
images: Image[]; | ||
selectedImageId: number; | ||
} | ||
|
||
export default function useImageDetailModal({ | ||
images, | ||
selectedImageId, | ||
}: UseImageDetailModalOptions) { | ||
const { closeDialog } = useDialog(); | ||
|
||
const [currentImageId, setCurrentImageId] = useState( | ||
selectedImageId || images[0]?.id, | ||
); | ||
|
||
const scrollContainerRef = useRef<HTMLDivElement>(null); | ||
|
||
const currentIndex = images.findIndex((img) => img.id === currentImageId); | ||
|
||
const handleScroll = (e: React.UIEvent<HTMLDivElement>) => { | ||
const container = e.currentTarget; | ||
const itemWidth = container.clientWidth; | ||
|
||
const visibleIndex = Math.round(container.scrollLeft / itemWidth); | ||
|
||
if (images[visibleIndex] && images[visibleIndex].id !== currentImageId) { | ||
setCurrentImageId(images[visibleIndex].id); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (scrollContainerRef.current && currentIndex !== -1) { | ||
scrollContainerRef.current.scrollLeft = | ||
currentIndex * scrollContainerRef.current.clientWidth; | ||
} | ||
}, [selectedImageId]); | ||
|
||
return { | ||
scrollContainerRef, | ||
imageNum: images.length, | ||
images, | ||
currentIndex, | ||
handleScroll, | ||
closeDialog, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './ImageDetailModal'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters