feat: allow external links in navbar and sidebar

This commit is contained in:
Kim, Jimin 2023-06-26 00:58:52 +09:00
parent 12c246dcd3
commit 3af0fd7056
2 changed files with 38 additions and 14 deletions

View file

@ -18,11 +18,17 @@ const Nav = styled.div`
export default () => { export default () => {
return ( return (
<Nav> <Nav>
{NavbarData.map((item, index) => ( {NavbarData.map(({ path, title }, index) => {
<Link key={index} to={item.path}> return path.at(0) === "/" ? (
<HeaderButton>{item.title}</HeaderButton> <Link key={index} to={path}>
</Link> <HeaderButton>{title}</HeaderButton>
))} </Link>
) : (
<a key={index} target="_blank" href={path}>
<HeaderButton>{title}</HeaderButton>
</a>
)
})}
</Nav> </Nav>
) )
} }

View file

@ -6,11 +6,11 @@ import type { Item } from "../../data/NavbarData"
import { useCallback, useState } from "react" import { useCallback, useState } from "react"
import { Link } from "react-router-dom" import { Link } from "react-router-dom"
import styled from "styled-components" import styled, { css } from "styled-components"
import button from "../../styles/button" import button from "../../styles/button"
const SidebarLink = styled(Link)` const sharedStyle = css`
${button}; ${button};
display: flex; display: flex;
width: 100%; width: 100%;
@ -31,6 +31,14 @@ const SidebarLink = styled(Link)`
} }
` `
const SidebarLink = styled(Link)`
${sharedStyle}
`
const SidebarAnchor = styled.a`
${sharedStyle}
`
const SidebarLabel = styled.span` const SidebarLabel = styled.span`
margin-left: 1rem; margin-left: 1rem;
` `
@ -39,21 +47,31 @@ interface Props {
item: Item item: Item
} }
const SubMenu = (props: Props) => { const SubMenu = ({ item }: Props) => {
const { path, icon, title } = item
const [isSubNavOpen, setSubNavOpen] = useState(false) const [isSubNavOpen, setSubNavOpen] = useState(false)
const handleSidebarLinkClick = useCallback(() => { const handleSidebarLinkClick = useCallback(() => {
setSubNavOpen((prev) => !prev) setSubNavOpen((prev) => !prev)
}, [isSubNavOpen]) }, [isSubNavOpen])
return ( if (path.at(0) == "/") {
<> return (
<SidebarLink to={props.item.path} onClick={handleSidebarLinkClick}> <SidebarLink to={path} onClick={handleSidebarLinkClick}>
<div> <div>
{props.item.icon} {icon}
<SidebarLabel>{props.item.title}</SidebarLabel> <SidebarLabel>{title}</SidebarLabel>
</div> </div>
</SidebarLink> </SidebarLink>
</> )
}
return (
<SidebarAnchor target="_blank" href={path} onClick={handleSidebarLinkClick}>
<div>
{icon}
<SidebarLabel>{title}</SidebarLabel>
</div>
</SidebarAnchor>
) )
} }