diff --git a/src/common/hooks/useScrollPosition.tsx b/src/common/hooks/useScrollPosition.tsx new file mode 100644 index 00000000..31b5756d --- /dev/null +++ b/src/common/hooks/useScrollPosition.tsx @@ -0,0 +1,31 @@ +import { useEffect, useRef, useState } from 'react'; + +const useScrollPosition = (topYScrollPosition: number = 20) => { + const lastScrollPositionRef = useRef(0); + const [isScrollingDown, setIsScrollingDown] = useState(true); + const [isScrollTop, setIsScrollTop] = useState(true); + + useEffect(() => { + const scrollHandler = () => { + const currentScrollPosition = window.scrollY; + + setIsScrollTop(currentScrollPosition < topYScrollPosition); + setIsScrollingDown(lastScrollPositionRef.current < currentScrollPosition); + + lastScrollPositionRef.current = currentScrollPosition; + }; + + window.addEventListener('scroll', scrollHandler); + + return () => { + window.removeEventListener('scroll', scrollHandler); + }; + }, [topYScrollPosition]); + + return { + isScrollingDown, + isScrollTop, + }; +}; + +export default useScrollPosition; diff --git a/src/views/ApplyPage/components/ApplyCategory/index.tsx b/src/views/ApplyPage/components/ApplyCategory/index.tsx index cdcebcbd..20f081c5 100644 --- a/src/views/ApplyPage/components/ApplyCategory/index.tsx +++ b/src/views/ApplyPage/components/ApplyCategory/index.tsx @@ -1,6 +1,8 @@ import { memo } from 'react'; import { Link } from 'react-router-dom'; +import useScrollPosition from '@hooks/useScrollPosition'; + import { CATEGORY } from './constant'; import { activeLinkStyle, categoryLinkStyle, categoryList, container } from './style.css'; @@ -8,8 +10,10 @@ interface ApplyCategoryProps { minIndex: number; } const ApplyCategory = memo(({ minIndex }: ApplyCategoryProps) => { + const { isScrollingDown, isScrollTop } = useScrollPosition(950); + return ( -