-
Notifications
You must be signed in to change notification settings - Fork 3
[refactor] LazyImage 컴포넌트 기능 개선 및 성능 최적화 #913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lepitaaar
merged 5 commits into
develop-fe
from
refactor/#907-improve-lazy-image-MOA-401
Dec 7, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
745d530
refactor: LazyImage 컴포넌트 기능 개선 및 성능 최적화
seongwon030 0c3d9a9
refactor: LazyImage 컴포넌트 위치 변경 및 styled-components 적용
seongwon030 86b69b6
refactor: LazyImage에서 불필요한 rootMargin 옵션 제거
seongwon030 cf084d7
test: LazyImage 테스트 코드 리팩토링 및 data-testid 제거
seongwon030 377980b
fix: rootMargin을 ImageProps에서 제거
seongwon030 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
frontend/src/pages/ClubDetailPage/components/PhotoList/LazyImage/LazyImage.styles.ts
This file contains hidden or 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,18 @@ | ||
| import styled from 'styled-components'; | ||
|
|
||
| export const ImageContainer = styled.div<{ $isLoaded: boolean; $placeholder: string }>` | ||
| position: relative; | ||
| width: 100%; | ||
| height: 100%; | ||
| background-color: ${({ $isLoaded, $placeholder }) => | ||
| $isLoaded ? 'transparent' : $placeholder}; | ||
| transition: background-color 0.3s; | ||
| `; | ||
|
|
||
| export const StyledImage = styled.img<{ $isLoaded: boolean }>` | ||
| opacity: ${({ $isLoaded }) => ($isLoaded ? 1 : 0)}; | ||
| transition: opacity 0.3s ease-in; | ||
| width: 100%; | ||
| height: 100%; | ||
| object-fit: cover; | ||
| `; |
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
frontend/src/pages/ClubDetailPage/components/PhotoList/LazyImage/LazyImage.tsx
This file contains hidden or 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,64 @@ | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import * as Styled from './LazyImage.styles'; | ||
|
|
||
| interface LazyImageProps { | ||
| src: string; | ||
| alt: string; | ||
| onError?: () => void; | ||
| placeholder?: string; | ||
| threshold?: number; | ||
| isEager?: boolean; | ||
seongwon030 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const PLACEHOLDER_COLOR = '#f0f0f0'; | ||
|
|
||
| const LazyImage = ({ | ||
| src, | ||
| alt, | ||
| onError, | ||
| placeholder = PLACEHOLDER_COLOR, | ||
| threshold = 0.01, | ||
| isEager = false, | ||
| }: LazyImageProps) => { | ||
| const [isVisible, setIsVisible] = useState(isEager); | ||
| const [isLoaded, setIsLoaded] = useState(false); | ||
| const rootRef = useRef<HTMLDivElement | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (isEager) return; | ||
| if (!rootRef.current) return; | ||
|
|
||
| const observer = new IntersectionObserver( | ||
| ([entry]) => { | ||
| if (entry.isIntersecting) { | ||
| setIsVisible(true); | ||
| observer.disconnect(); | ||
| } | ||
| }, | ||
| { | ||
| threshold, | ||
| } | ||
| ); | ||
|
|
||
| observer.observe(rootRef.current); | ||
|
|
||
| return () => observer.disconnect(); | ||
| }, [threshold, isEager]); | ||
|
|
||
| return ( | ||
| <Styled.ImageContainer ref={rootRef} $isLoaded={isLoaded} $placeholder={placeholder}> | ||
| {isVisible && ( | ||
| <Styled.StyledImage | ||
| src={src} | ||
| alt={alt} | ||
| onError={onError} | ||
| onLoad={() => setIsLoaded(true)} | ||
| loading={isEager ? 'eager' : 'lazy'} | ||
| $isLoaded={isLoaded} | ||
| /> | ||
| )} | ||
| </Styled.ImageContainer> | ||
| ); | ||
| }; | ||
|
|
||
| export default LazyImage; | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.