-
Notifications
You must be signed in to change notification settings - Fork 0
[#10] 홈페이지 가상스크롤 적용 #14
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49585b5
✨ feat: scroll의 Y 좌표를 리턴하는 useScroll 커스텀 훅 추가
lee0jae330 6e3dd9e
🚚 rename: hooks/css에 있던 useWindowSize 훅 폴더 위치 변경
lee0jae330 a277c68
✨ feat: 가상스크롤 구현 중
lee0jae330 531adc3
🙀 chore: 함수명 오타 수정
lee0jae330 3d4410c
✨ feat: 가상스크롤 커스텀훅 추가
lee0jae330 47625b3
✨ feat: WorkspaceGrid 컴포넌트에 가상스크롤 적용
lee0jae330 7146d1a
🙀 chore: grid의 총 높이 계산할 때 반올림 적용
lee0jae330 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 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
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,25 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
export const useScrollPosition = () => { | ||
const [scrollPosition, setScrollPosition] = useState<number>(0); | ||
const ticking = useRef(false); | ||
|
||
useEffect(() => { | ||
const onScroll = () => { | ||
if (!ticking.current) { | ||
requestAnimationFrame(() => { | ||
setScrollPosition(window.scrollY); | ||
ticking.current = false; | ||
}); | ||
ticking.current = true; | ||
} | ||
}; | ||
window.addEventListener('scroll', onScroll); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', onScroll); | ||
}; | ||
}, []); | ||
|
||
return { scrollPosition }; | ||
}; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 계산 로직이 복잡했을텐데 척척 다 잘하셨군요 💯 nodePadding 계산 로직이 따로 쓰이는 부분이 없으니, 파일분리는 안하고 같은 파일에서 함수로만 따로뺀거 좋네용 🥇 |
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,56 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useScrollPosition, useWindowSize } from '@/shared/hooks'; | ||
|
||
const calculateNumOfNodePadding = (windowWidth: number): number => { | ||
if (windowWidth < 640) { | ||
return 1; | ||
} else if (windowWidth < 768) { | ||
return 2; | ||
} else if (windowWidth < 1024) { | ||
return 3; | ||
} | ||
return 4; | ||
}; | ||
|
||
export const useVirtualScroll = <T>({ | ||
data, | ||
topSectionHeight = 0, | ||
renderedItemHeight, | ||
gapY = 0, | ||
}: { | ||
data: T[] | undefined; | ||
topSectionHeight?: number; | ||
renderedItemHeight: number; | ||
gapY: number; | ||
}) => { | ||
const { scrollPosition } = useScrollPosition(); | ||
const { screenHeight, screenWidth } = useWindowSize(); | ||
|
||
const [renderedData, setRenderedData] = useState<T[]>([]); | ||
|
||
const nodePadding = calculateNumOfNodePadding(screenWidth); | ||
const start = Math.max( | ||
0, | ||
Math.floor((scrollPosition - topSectionHeight) / (renderedItemHeight + gapY)) * nodePadding | ||
); | ||
const offsetY = Math.floor(start / nodePadding) * (renderedItemHeight + gapY); | ||
const [totalHeight, setTotalHeight] = useState(0); | ||
|
||
useEffect(() => { | ||
if (!data) { | ||
return; | ||
} | ||
|
||
setRenderedData( | ||
data.slice( | ||
start, | ||
start + | ||
Math.floor(screenHeight / (renderedItemHeight + gapY)) * nodePadding + | ||
2 * nodePadding | ||
) | ||
); | ||
setTotalHeight(Math.ceil(data!.length / nodePadding) * (renderedItemHeight + gapY)); | ||
}, [start, screenHeight, data]); | ||
|
||
return { renderedData, offsetY, totalHeight }; | ||
}; |
File renamed without changes.
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
15 changes: 12 additions & 3 deletions
15
apps/client/src/widgets/home/WorkspaceGrid/WorkspaceGrid.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
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.
쓰로틀링 좋네요!
커스텀 훅으로 계속 잘 관리해 주시네용 👍