-
Notifications
You must be signed in to change notification settings - Fork 10
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
feat: 용어집 무한스크롤 #136
base: main
Are you sure you want to change the base?
feat: 용어집 무한스크롤 #136
Changes from all commits
3d402de
155eeb1
447aeea
7e0aac7
eaeae1a
c8c7df1
1efccad
a6fc028
da70de4
9ef2ba8
2ad8083
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
import React from 'react'; | ||
import useIntersectionObserver from 'hooks/useIntersectionObserver'; | ||
import React, { useEffect } from 'react'; | ||
|
||
import { WikiWord } from '../../types'; | ||
type wikiTableRowProps = { | ||
description: string; | ||
name: string; | ||
last: boolean; | ||
onNext: () => void; | ||
}; | ||
export default function WikiTableRow({ | ||
name, | ||
description, | ||
last, | ||
onNext, | ||
}: wikiTableRowProps) { | ||
const { ref, entry } = useIntersectionObserver({ freezeOnceVisible: true }); | ||
|
||
export default function WikiTableRow({ name, description }: WikiWord) { | ||
return ( | ||
<tr> | ||
useEffect(() => { | ||
if (entry?.isIntersecting) onNext(); | ||
}, [entry]); | ||
|
||
const content = ( | ||
<> | ||
<td>{name}</td> | ||
<td>{description}</td> | ||
</tr> | ||
</> | ||
); | ||
|
||
return last ? <tr ref={ref}>{content}</tr> : <tr>{content}</tr>; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { WikiWord } from 'types'; | ||
|
||
type Args = { | ||
source: WikiWord[]; | ||
limit: number; | ||
offset: number; | ||
}; | ||
|
||
type Returns = { | ||
result: WikiWord[]; | ||
currentOffest: number; | ||
isLastOffset: boolean; | ||
onNext: () => void; | ||
}; | ||
const usePagination = ({ | ||
source = [], | ||
limit = 10, | ||
offset = 0, | ||
}: Args): Returns => { | ||
const getResult = (): WikiWord[] => { | ||
const startIdx = 0; | ||
const endIdx: number = currentOffest; | ||
return source.slice(startIdx, endIdx); | ||
}; | ||
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. 타입 지정되면 좋을 것 같아요~ 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. 넵! 타입지정! |
||
|
||
const getMaxOffset = (): number => { | ||
return source.length; | ||
}; | ||
|
||
const [currentOffest, setCurrentOffest] = useState<number>(limit); | ||
|
||
useEffect(() => { | ||
if (currentOffest !== limit) setCurrentOffest(limit); | ||
else getResult(); | ||
}, [source]); | ||
|
||
const onNext = () => { | ||
if (currentOffest >= getMaxOffset()) return; | ||
setCurrentOffest(currentOffest + limit); | ||
}; | ||
|
||
return { | ||
result: getResult(), | ||
currentOffest, | ||
isLastOffset: currentOffest > getMaxOffset(), | ||
onNext, | ||
}; | ||
}; | ||
|
||
export default usePagination; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { RefObject, useEffect, useRef, useState } from 'react'; | ||
import 'intersection-observer'; | ||
interface Args extends IntersectionObserverInit { | ||
freezeOnceVisible?: boolean; | ||
delay?: number; | ||
} | ||
|
||
function useIntersectionObserver({ | ||
threshold = 0, | ||
root = null, | ||
rootMargin = '0%', | ||
freezeOnceVisible = false, | ||
delay = 0, | ||
}: Args): { | ||
ref: RefObject<Element> | null; | ||
entry: IntersectionObserverEntry | undefined; | ||
} { | ||
const ref = useRef<HTMLFormElement | null>(null); | ||
const [entry, setEntry] = useState<IntersectionObserverEntry>(); | ||
|
||
const frozen: boolean = entry?.isIntersecting && freezeOnceVisible; | ||
const updateEntry = ([entry]: IntersectionObserverEntry[]): void => { | ||
setTimeout(() => { | ||
setEntry(entry); | ||
}, delay); | ||
}; | ||
|
||
useEffect(() => { | ||
const node = ref?.current; // DOM Ref | ||
if (frozen || !node) return; | ||
|
||
const observerParams = { threshold, root, rootMargin }; | ||
const observer = new IntersectionObserver(updateEntry, observerParams); | ||
|
||
observer.observe(node); | ||
return () => observer.disconnect(); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ref, threshold, root, rootMargin, frozen]); | ||
|
||
return { ref, entry }; | ||
} | ||
|
||
export default useIntersectionObserver; | ||
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. so 깔끔한 훅이네요! 굿굿 |
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.
layer tree가 업데이트 될 때마다 table의 width 값이 계속 바뀌네요 ㅎㅎㅠ 새로 추가되는 콘텐츠에 따라 기존에 있던 table의 너비가 달라지니 당연한 거긴 한데 사용자 경험 측면에서 자연스럽지 않아 보이는군요.. 반응형을 생각했을 때 table 구조가 용어 사전을 보기에 적절한 UI인지도 논의가 필요할 것 같아요
2021-10-08.8.45.00.mov
.
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.
아하 영상보고 이해가 되었습니다! 이부분은 적절한 UI로 변경되야될것으로 보이네요!