refactor: Extract Sidebar Link to component
This commit is contained in:
@@ -30,16 +30,11 @@ function getURL(node: PageLink) {
|
|||||||
function mapMenuItems(navigationItems: NavigationItem[]) {
|
function mapMenuItems(navigationItems: NavigationItem[]) {
|
||||||
return navigationItems.map(({ item }): MenuItem => {
|
return navigationItems.map(({ item }): MenuItem => {
|
||||||
const { node } = item.pageConnection.edges[0]
|
const { node } = item.pageConnection.edges[0]
|
||||||
const {
|
|
||||||
title,
|
|
||||||
system: { uid },
|
|
||||||
} = node
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title,
|
uid: node.system.uid,
|
||||||
uid,
|
|
||||||
url: getURL(node),
|
url: getURL(node),
|
||||||
linkText: item.link_text,
|
linkText: item.link_text || node.title,
|
||||||
subItems: item.sub_items ? mapMenuItems(item.sub_items) : null,
|
subItems: item.sub_items ? mapMenuItems(item.sub_items) : null,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,38 +1,29 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import { LogOut } from "react-feather"
|
import { LogOut } from "react-feather"
|
||||||
import Link from "next/link"
|
import Link from "../../TempDesignSystem/Link"
|
||||||
|
|
||||||
import styles from "./sidebar.module.css"
|
import styles from "./sidebar.module.css"
|
||||||
import { SidebarProps } from "@/types/requests/myPages/navigation"
|
import { SidebarProps } from "@/types/requests/myPages/navigation"
|
||||||
import { usePathname } from "next/navigation"
|
|
||||||
import { Fragment } from "react"
|
import { Fragment } from "react"
|
||||||
|
|
||||||
export default function Sidebar({ menuItems }: SidebarProps) {
|
export default function Sidebar({ menuItems }: SidebarProps) {
|
||||||
const currentPageSlug = `/${usePathname()
|
|
||||||
.split("/")
|
|
||||||
.filter((v) => v)
|
|
||||||
.at(-1)}`
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<aside className={styles.sidebar}>
|
<aside className={styles.sidebar}>
|
||||||
<nav className={styles.nav}>
|
<nav className={styles.nav}>
|
||||||
{menuItems.map((item) => (
|
{menuItems.map((item) => (
|
||||||
<Fragment key={item.uid}>
|
<Fragment key={item.uid}>
|
||||||
<Link
|
<Link variant={"sidebar"} href={item.url}>
|
||||||
className={`${styles.link} ${currentPageSlug === item.url ? styles.active : ""}`}
|
{item.linkText}
|
||||||
href={item.url}
|
|
||||||
>
|
|
||||||
{item.linkText || item.title}
|
|
||||||
</Link>
|
</Link>
|
||||||
{item.subItems
|
{item.subItems
|
||||||
? item.subItems.map((subItem) => {
|
? item.subItems.map((subItem) => {
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
key={subItem.uid}
|
key={subItem.uid}
|
||||||
className={`${styles.link} ${currentPageSlug === subItem.url ? styles.active : ""}`}
|
|
||||||
href={subItem.url}
|
href={subItem.url}
|
||||||
|
variant={"sidebar"}
|
||||||
>
|
>
|
||||||
{subItem.linkText || subItem.title}
|
{subItem.linkText}
|
||||||
</Link>
|
</Link>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
29
components/TempDesignSystem/Link/index.tsx
Normal file
29
components/TempDesignSystem/Link/index.tsx
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { linkVariants } from "./variants"
|
||||||
|
|
||||||
|
import NextLink from "next/link"
|
||||||
|
|
||||||
|
import type { LinkProps } from "./link"
|
||||||
|
import { usePathname } from "next/navigation"
|
||||||
|
|
||||||
|
export default function Link({
|
||||||
|
className,
|
||||||
|
href,
|
||||||
|
size,
|
||||||
|
variant,
|
||||||
|
...props
|
||||||
|
}: LinkProps) {
|
||||||
|
const currentPageSlug = `/${usePathname()
|
||||||
|
.split("/")
|
||||||
|
.filter((v) => v)
|
||||||
|
.at(-1)}`
|
||||||
|
const isActive = currentPageSlug === href
|
||||||
|
const classNames = linkVariants({
|
||||||
|
active: isActive,
|
||||||
|
className,
|
||||||
|
size,
|
||||||
|
variant,
|
||||||
|
})
|
||||||
|
return <NextLink className={classNames} href={href} {...props} />
|
||||||
|
}
|
||||||
31
components/TempDesignSystem/Link/link.module.css
Normal file
31
components/TempDesignSystem/Link/link.module.css
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
.link {
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.default {
|
||||||
|
font-family: var(--ff-fira-sans);
|
||||||
|
}
|
||||||
|
|
||||||
|
.sidebar {
|
||||||
|
align-items: center;
|
||||||
|
color: var(--some-text-color, #111);
|
||||||
|
display: flex;
|
||||||
|
font-size: 1.6rem;
|
||||||
|
font-weight: 400;
|
||||||
|
gap: 0.6rem;
|
||||||
|
line-height: 1.9rem;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.activeSidebar {
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.activeSidebar::before {
|
||||||
|
bottom: -0.4rem;
|
||||||
|
background-color: var(--some-text-color, #000);
|
||||||
|
content: "";
|
||||||
|
height: 0.2rem;
|
||||||
|
position: absolute;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
9
components/TempDesignSystem/Link/link.ts
Normal file
9
components/TempDesignSystem/Link/link.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { linkVariants } from "./variants"
|
||||||
|
|
||||||
|
import type { VariantProps } from "class-variance-authority"
|
||||||
|
|
||||||
|
export interface LinkProps
|
||||||
|
extends React.AnchorHTMLAttributes<HTMLAnchorElement>,
|
||||||
|
VariantProps<typeof linkVariants> {
|
||||||
|
href: string
|
||||||
|
}
|
||||||
26
components/TempDesignSystem/Link/variants.ts
Normal file
26
components/TempDesignSystem/Link/variants.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { cva } from "class-variance-authority"
|
||||||
|
|
||||||
|
import styles from "./link.module.css"
|
||||||
|
|
||||||
|
export const linkVariants = cva(styles.link, {
|
||||||
|
variants: {
|
||||||
|
active: {
|
||||||
|
true: styles.active,
|
||||||
|
},
|
||||||
|
size: {},
|
||||||
|
variant: {
|
||||||
|
default: styles.default,
|
||||||
|
sidebar: styles.sidebar,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultVariants: {
|
||||||
|
variant: "default",
|
||||||
|
},
|
||||||
|
compoundVariants: [
|
||||||
|
{
|
||||||
|
class: styles.activeSidebar,
|
||||||
|
active: true,
|
||||||
|
variant: "sidebar",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
@@ -11,7 +11,6 @@ export enum PageLinkEnum {
|
|||||||
|
|
||||||
export type MenuItem = {
|
export type MenuItem = {
|
||||||
uid: string
|
uid: string
|
||||||
title: string
|
|
||||||
linkText: string
|
linkText: string
|
||||||
subItems: MenuItem[] | null
|
subItems: MenuItem[] | null
|
||||||
url: string
|
url: string
|
||||||
|
|||||||
Reference in New Issue
Block a user