-
Notifications
You must be signed in to change notification settings - Fork 2
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
[FE] feat : 이미지 스켈레톤 적용 #1055
Merged
Merged
[FE] feat : 이미지 스켈레톤 적용 #1055
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7ddf926
feat : 이미지 스켈레톤 컴포넌트 구현
BadaHertz52 134eb9c
feat : 리뷰 연결 페이지 이미지에 스켈레톤 적용
BadaHertz52 5d20bcd
style : 이미지 스켈레톤 스타일 변경
BadaHertz52 ad3a345
feat : 홈페이지 캐러셀에 이미지 스켈레톤 적용
BadaHertz52 33d59cd
chore : 불필요한 주석 삭제 및 경로 수정
BadaHertz52 37e9a9f
refactor : 가독성을 위해 줄 바꿈 추가
BadaHertz52 8ddbb38
feat : ImageSkeleton에서 img onLoad 확장할 수 있게 변경
BadaHertz52 310568e
fix : 스켈레톤 event 타입으로 인한 테스트 오류 수정
BadaHertz52 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 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
33 changes: 33 additions & 0 deletions
33
frontend/src/components/skeleton/ImgWithSkeleton/index.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,33 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import * as S from './style'; | ||
|
||
interface ImgWithSkeletonProps { | ||
children: React.ReactElement<React.ImgHTMLAttributes<HTMLImageElement>>; | ||
imgWidth: string; | ||
imgHeight: string; | ||
} | ||
|
||
const ImgWithSkeleton = ({ children, imgWidth, imgHeight }: ImgWithSkeletonProps) => { | ||
const [isLoaded, setIsLoaded] = useState(false); | ||
|
||
const handleImgLoad = (event: React.SyntheticEvent<HTMLImageElement, Event>) => { | ||
if (children.props.onLoad) { | ||
children.props.onLoad(event); | ||
} | ||
setIsLoaded(true); | ||
}; | ||
|
||
return ( | ||
<S.Container $width={imgWidth} $height={imgHeight}> | ||
{!isLoaded && <S.ImgSkeleton />} | ||
<S.ImgWrapper $isLoaded={isLoaded}> | ||
{React.cloneElement(children, { | ||
onLoad: (event: React.SyntheticEvent<HTMLImageElement, Event>) => handleImgLoad(event), | ||
})} | ||
</S.ImgWrapper> | ||
</S.Container> | ||
); | ||
}; | ||
|
||
export default ImgWithSkeleton; |
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,47 @@ | ||
import styled from '@emotion/styled'; | ||
|
||
interface ContainerProps { | ||
$width: string; | ||
$height: string; | ||
} | ||
export const Container = styled.div<ContainerProps>` | ||
position: relative; | ||
width: ${(props) => props.$width}; | ||
height: ${(props) => props.$height}; | ||
`; | ||
export const ImgWrapper = styled.div<{ $isLoaded: boolean }>` | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
|
||
width: 100%; | ||
height: 100%; | ||
|
||
opacity: ${(props) => (props.$isLoaded ? 1 : 0)}; | ||
|
||
transition: opacity 300ms; | ||
`; | ||
export const ImgSkeleton = styled.div` | ||
width: 100%; | ||
height: 100%; | ||
|
||
background-image: linear-gradient( | ||
135deg, | ||
${({ theme }) => theme.colors.lightGray} 40%, | ||
rgba(246, 246, 246, 0.89) 50%, | ||
${({ theme }) => theme.colors.lightGray} 85% | ||
); | ||
background-size: 200% 100%; | ||
border-radius: ${({ theme }) => theme.borderRadius.basic}; | ||
|
||
animation: skeleton-animation 1.5s infinite linear; | ||
|
||
@keyframes skeleton-animation { | ||
0% { | ||
background-position: 200% 0; | ||
} | ||
100% { | ||
background-position: -200% 0; | ||
} | ||
} | ||
`; |
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 as ImgWithSkeleton } from './ImgWithSkeleton'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { debounce } from '@/utils'; | ||
|
||
const DEBOUNCE_TIME = 300; | ||
|
||
interface UseSlideImgSizeProps { | ||
slideRef: React.RefObject<HTMLDivElement>; | ||
} | ||
const useSlideImgSize = ({ slideRef }: UseSlideImgSizeProps) => { | ||
interface ImgSize { | ||
width: string; | ||
height: string; | ||
} | ||
const [imgSize, setImgSize] = useState<ImgSize>({ width: '', height: '' }); | ||
|
||
const updateImgSize = () => { | ||
if (!slideRef.current) return; | ||
|
||
const slideDomRect = slideRef.current.getBoundingClientRect(); | ||
const width = Math.ceil(slideDomRect.width * 0.8 * 0.1); | ||
const height = width * 0.61; | ||
|
||
setImgSize({ width: `${width}rem`, height: `${height}rem` }); | ||
}; | ||
|
||
const debouncedUpdateImgSize = debounce(updateImgSize, DEBOUNCE_TIME); | ||
|
||
useEffect(() => { | ||
updateImgSize(); | ||
|
||
document.addEventListener('resize', debouncedUpdateImgSize); | ||
|
||
return () => { | ||
document.removeEventListener('resize', debouncedUpdateImgSize); | ||
}; | ||
}, [slideRef]); | ||
|
||
return { imgSize }; | ||
}; | ||
|
||
export default useSlideImgSize; |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
구현 배경
홈 페이지의 경우, 이미지가 퍼센트로 설정되어 있고 768px이하에서 height가 설정되어 있지 않아서 이미지에 따라 자동으로 설정되는 상황이었어요. slideRef로 width를 받아올 수 있어서 캐러셀 이미지 비율을 사용해 height를 지정하는 방식을 사용했어요.