Skip to content
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

[FE] feat: NavigationTab 컴포넌트 구현 #1037

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions frontend/src/components/common/NavigationTab/NavItem/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as S from './styles';

interface NavItemProps {
label: string;
$isSelected: boolean;
onClick: () => void;
}

const NavItem = ({ label, $isSelected, onClick }: NavItemProps) => {
return (
<S.NavItem $isSelected={$isSelected}>
<button onClick={onClick}>{label}</button>
</S.NavItem>
);
};

export default NavItem;
33 changes: 33 additions & 0 deletions frontend/src/components/common/NavigationTab/NavItem/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import styled from '@emotion/styled';

import media from '@/utils/media';

interface NavItemProps {
$isSelected: boolean;
}

export const NavItem = styled.li<NavItemProps>`
border-bottom: 0.3rem solid ${({ theme, $isSelected }) => ($isSelected ? theme.colors.primary : 'none')};
padding: 0 1rem 1.3rem 1rem;

button {
font-weight: ${({ theme }) => theme.fontWeight.semibold};
color: ${({ theme, $isSelected }) => ($isSelected ? theme.colors.black : theme.colors.disabled)};

&:hover {
color: ${({ theme }) => theme.colors.black};
}
}

${media.xSmall} {
display: flex;
flex: 1;
justify-content: center;

margin: 0 2rem;
}

${media.xxSmall} {
margin: 0 1.6rem;
}
`;
74 changes: 15 additions & 59 deletions frontend/src/components/common/NavigationTab/index.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,25 @@
import { useEffect, useRef, useState } from 'react';
import { useLocation, useNavigate } from 'react-router';
import useNavigationTabs from '@/hooks/useNavigationTabs';

import NavItem from './NavItem';
import * as S from './styles';

interface TabInfoItem {
name: string;
path: string;
param: string;
}

interface NavigationTabProps {
tabInfoList: TabInfoItem[];
}

const NavigationTab = ({ tabInfoList }: NavigationTabProps) => {
const activeTab = sessionStorage.getItem('activeTab');

const [currentIndex, setCurrentIndex] = useState(Number(activeTab) || 0);
const [currentTabWidth, setCurrentTabWidth] = useState(0);
const [currentTabLeft, setCurrentTabLeft] = useState(0);
const [isTransitionEnabled, setIsTransitionEnabled] = useState(false);

const location = useLocation();
const navigate = useNavigate();

const currentItemRef = useRef<HTMLUListElement>(null);

// URL의 쿼리 파라미터 값을 읽어, 탭 인덱스 업데이트
useEffect(() => {
const queryParams = new URLSearchParams(location.search);
const tabParam = queryParams.get('tab');
const tabIndex = tabInfoList.findIndex((item) => item.param === tabParam);

if (tabIndex >= 0) {
setCurrentIndex(tabIndex);
sessionStorage.setItem('activeTab', String(tabIndex));
}
}, [location.search]);

// 탭이 변경될 때마다 현재 탭의 크기와 위치 업데이트
useEffect(() => {
if (currentItemRef.current) {
const currentTab = currentItemRef.current.children[currentIndex];
const { width, left } = currentTab.getBoundingClientRect();
setCurrentTabWidth(width);
setCurrentTabLeft(left);
}
}, [currentIndex]);

const handleTabClick = (path: string, index: number) => {
setCurrentIndex(index);
setIsTransitionEnabled(true);
navigate(`${path}?tab=${tabInfoList[index].param}`);
};
const NavigationTab = () => {
const { currentTabIndex, tabList } = useNavigationTabs();

return (
<S.NavContainer>
<S.NavList ref={currentItemRef}>
{tabInfoList.map((item, index) => (
<S.NavItem key={index} selected={currentIndex === index}>
<button onClick={() => handleTabClick(item.path, index)}>{item.name}</button>
</S.NavItem>
))}
<S.NavList>
{tabList.map((tab, index) => {
return (
<NavItem
key={tab.label}
label={tab.label}
$isSelected={currentTabIndex === index}
onClick={tab.handleTabClick}
/>
);
})}
</S.NavList>
<S.CurrentNavBar width={currentTabWidth} left={currentTabLeft} isTransitionEnabled={isTransitionEnabled} />
</S.NavContainer>
);
};
Expand Down
36 changes: 0 additions & 36 deletions frontend/src/components/common/NavigationTab/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,3 @@ export const NavList = styled.ul`
padding: 0;
}
`;

export const NavItem = styled.li<{ selected: boolean }>`
margin-bottom: 1rem;
padding: 0.7rem 1rem;
border-radius: 0.5rem;

button {
font-weight: ${({ theme }) => theme.fontWeight.semibold};
color: ${({ theme, selected }) => (selected ? theme.colors.black : theme.colors.disabled)};

&:hover {
color: ${({ theme }) => theme.colors.black};
}
}

${media.xSmall} {
display: flex;
flex: 1;
justify-content: center;
padding: 0.7rem 0;
}
`;

export const CurrentNavBar = styled.div<{ width: number; left: number; isTransitionEnabled: boolean }>`
position: absolute;
bottom: 0;
left: ${({ left }) => `${left}px`};

width: ${({ width }) => `${width}px`};
height: 0.3rem;

background-color: ${({ theme }) => theme.colors.primary};
border-radius: 0.1rem;

transition: ${({ isTransitionEnabled }) => (isTransitionEnabled ? 'all 0.2s ease-in-out' : 'none')};
`;
33 changes: 33 additions & 0 deletions frontend/src/hooks/useNavigationTabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useLocation, useNavigate } from 'react-router';

const useNavigationTabs = () => {
const { pathname } = useLocation();
const navigate = useNavigate();

const navigateReviewLinkManagementPage = () => {
navigate('/user/review-link-management');
};

const navigateWrittenReviewConfirmPage = () => {
navigate('/user/written-review-confirm');
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아직 path가 확정이 아니라서, ROUTE 상수를 이용하지 않은 건가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵,,, 일단 임시로,, 넣어놨어요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

경로 거의 확정된 것 같아서 추가했습니다~


const tabList = [
{
label: '리뷰 링크 관리',
path: '/user/review-link-management',
handleTabClick: navigateReviewLinkManagementPage,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabList의 요소안에 path가 있으니, tabList 사용시 onClick={()=> navigate(tab.path)}로 해도 될 것 같은데, tabList에 handleTabClick으로 선언한 이유가 있을까요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 같은 의문이 있었는데, navigate 코드에 더해 페이지 방문 amplitude 코드를 추가한다고 생각하면 nav 함수를 따로 파도 괜찮을 것 같아요. 지금 useReviewDisplayLayoutOptions도 이런 식으로 되어 있습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tabList의 요소안에 path가 있으니, tabList 사용시 onClick={()=> navigate(tab.path)}로 해도 될 것 같은데, tabList에 handleTabClick으로 선언한 이유가 있을까요?

amplitude 코드 추가될 것 같아서 함수 따로 팠어요~

지금 useReviewDisplayLayoutOptions도 이런 식으로 되어 있습니다!

맞아요. useReviewDisplayLayoutOptions 참고했어요!

},
{
label: '작성한 리뷰 확인',
path: '/user/written-review-confirm',
handleTabClick: navigateWrittenReviewConfirmPage,
},
];

const currentTabIndex = tabList.findIndex((tab) => tab.path === pathname);

return { currentTabIndex, tabList };
};

export default useNavigationTabs;