-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,054 additions
and
259 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
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
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 |
---|---|---|
@@ -1,51 +1,37 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { reviewCountState } from '@recoil/review'; | ||
import { TopIcon } from '@components/common/icons/Icons'; | ||
|
||
export default function DetailTopButton({ parentRef }: any) { | ||
const [isVisible, setIsVisible] = useState<boolean>(false); | ||
const [scrollPosition, setScrollPosition] = useState<number>(0); | ||
const [viewportHeight, setViewportHeight] = useState<number>(0); | ||
|
||
const scrollButtonRef = useRef<HTMLDivElement>(null); | ||
export default function DetailTopButton() { | ||
const [visible, setVisible] = useState<boolean>(false); | ||
const getReviewCount = useRecoilValue(reviewCountState); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
if (scrollButtonRef.current && parentRef.current) { | ||
setViewportHeight(screen.height); | ||
|
||
// 부모 요소의 높이보다 적을 때까지 스크롤 허용 | ||
if (window.scrollY < parentRef.current.clientHeight - 50) { | ||
// 기기 높이의 절반 이상 스크롤 했을 때 | ||
if (window.scrollY >= viewportHeight / 2) { | ||
setIsVisible(true); | ||
setScrollPosition(window.scrollY); | ||
} else { | ||
setIsVisible(false); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
window.addEventListener('scroll', handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, [parentRef, scrollPosition, setScrollPosition]); | ||
|
||
const scrollToTop = () => { | ||
if (getReviewCount > 2) { | ||
setVisible(true); | ||
} else { | ||
setVisible(false); | ||
} | ||
}, [getReviewCount]); | ||
|
||
const scrollToTop = (e: React.MouseEvent<HTMLElement>) => { | ||
e.stopPropagation(); | ||
window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
}; | ||
|
||
if (!visible) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div | ||
ref={scrollButtonRef} | ||
className={`absolute right-2 flex h-12 w-12 cursor-pointer items-center justify-center rounded-full bg-white shadow-md transition-opacity duration-500 ${ | ||
isVisible ? 'opacity-100' : 'opacity-0' | ||
}`} | ||
onClick={scrollToTop} | ||
style={{ top: `${scrollPosition}px` }}> | ||
<TopIcon /> | ||
className="sticky bottom-5 | ||
mt-[-50px] flex h-12 w-12 cursor-pointer items-center justify-center rounded-full bg-white shadow-md" | ||
style={{ left: 'calc(100%)' }}> | ||
<button onClick={scrollToTop}> | ||
<TopIcon /> | ||
</button> | ||
</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
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.