choigirang / portfolio

포트폴리오 v1
https://v1.choigirang-portfolio.site
0 stars 0 forks source link

Main : Turtle 페이지의 pathname 추적 #10

Open choigirang opened 8 months ago

choigirang commented 8 months ago
// Header.tsx
// 주소 목록
const lists = ['Home', 'About', 'Skills', 'Project', 'Contact'];

/**
 *
 * @returns router에 따른 헤더
 */
export default function Index() {
  const [scroll, setScroll] = useState<boolean>(false);
  const [activeSection, setActiveSection] = useState<string>('Home');

  const currentPath = window.location.pathname;
  const theme = useTheme().palette.mode === 'light';

  const { scrollToTop, scrollToPage } = useMoveScroll();

  // scroll에 따른 Header bg 변경
  useEffect(() => {
    let animationFrameId: number;

    const scrollHandler = () => {
      cancelAnimationFrame(animationFrameId);

      animationFrameId = requestAnimationFrame(() => {
        if (window.scrollY > 100 && !scroll) {
          setScroll(true);
        } else if (window.scrollY <= 100 && scroll) {
          setScroll(false);
        }
      });
    };
    scrollHandler();
    window.addEventListener('scroll', scrollHandler);

    return () => {
      window.removeEventListener('scroll', scrollHandler);
      cancelAnimationFrame(animationFrameId);
    };
  }, [scroll]);

  // 화면에 들어오는 컴포넌트에 따른 주솟값 변경
  useEffect(() => {
    const scrollChangeRouter = () => {
      const section = lists.find(list => {
        const element = document.getElementById(list);
        if (element) {
          const rect = element.getBoundingClientRect();
          return rect.top <= window.innerHeight / 2 && rect.bottom >= window.innerHeight / 2;
        }
        return false;
      });

      if (section && section !== activeSection) {
        setActiveSection(section);
        window.history.replaceState(null, '', `/${section}`);
      }
    };

    window.addEventListener('scroll', scrollChangeRouter);

    return () => {
      window.removeEventListener('scroll', scrollChangeRouter);
    };
  }, [activeSection]);

  return (
    <Header $scroll={scroll}>
      <Logo variant="h1" onClick={scrollToTop}>
        {theme ? (
          <WbSunnyIcon
            sx={{
              animation: `${sunAni} 2s linear infinite`,
            }}
          />
        ) : (
          <DarkModeIcon
            sx={{
              animation: `${reverseAni} 3s linear infinite`,
            }}
          />
        )}
        Girang's
      </Logo>
      <Tabs
        value={currentPath !== '/' ? currentPath : '/Home'}
        sx={{ '& .MuiTabs-indicator': { backgroundColor: 'transparent' } }}>
        {lists.map(list => (
          <TabList
            key={list}
            label={list.replace('/', '')}
            value={'/' + list}
            to={list}
            component={Link}
            onClick={() => scrollToPage(list)}
          />
        ))}
      </Tabs>
    </Header>
  );
}
choigirang commented 8 months ago
// Header.tsx

...
useEffect(() => {
    const scrollChangeRouter = () => {
      const section = lists.find(list => {
        const element = document.getElementById(list);
        if (element) {
          const rect = element.getBoundingClientRect();
          return rect.top <= window.innerHeight / 2 && rect.bottom >= window.innerHeight / 2;
        }
        return false;
      });

      if (section && section !== activeSection) {
        setActiveSection(section);
        window.history.replaceState(null, '', `/${section}`);
      }
    };

    window.addEventListener('scroll', scrollChangeRouter);

    return () => {
      window.removeEventListener('scroll', scrollChangeRouter);
    };
  }, [activeSection]);