Skip to content

Commit a01ac89

Browse files
committed
replace custom hook to set tw classes with nav variants
1 parent b047647 commit a01ac89

File tree

6 files changed

+37
-154
lines changed

6 files changed

+37
-154
lines changed

src/components/Nav/Menu/ItemContent.tsx

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,20 @@
1-
import { useRouter } from "next/router"
2-
3-
import { cn } from "@/lib/utils/cn"
4-
import { cleanPath } from "@/lib/utils/url"
5-
61
import type { Level, NavItem } from "../types"
72

8-
import { useNavMenuColorsTw } from "@/hooks/useNavMenuColorsTw"
3+
import { navMenuVariants } from "./MenuContent"
94

105
type ItemProps = {
116
item: NavItem
127
lvl: Level
138
}
149

1510
const ItemContent = ({ item, lvl }: ItemProps) => {
16-
const { label, description, ...action } = item
17-
const { asPath } = useRouter()
18-
const menuColors = useNavMenuColorsTw()
19-
20-
const isLink = "href" in action
21-
const isActivePage = isLink && cleanPath(asPath) === action.href
11+
const { label, description } = item
12+
const { linkSubtext } = navMenuVariants({ level: lvl })
2213

2314
return (
2415
<div className="relative me-auto text-start">
25-
<p className={cn("font-bold text-body", menuColors.body)}>{label}</p>
26-
<p
27-
className={cn(
28-
"text-sm",
29-
isActivePage ? menuColors.body : menuColors.lvl[lvl].subtext
30-
)}
31-
>
32-
{description}
33-
</p>
16+
<p className="font-bold">{label}</p>
17+
<p className={linkSubtext()}>{description}</p>
3418
</div>
3519
)
3620
}

src/components/Nav/Menu/MenuContent.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const navMenuVariants = tv({
1313
slots: {
1414
base: "text-body",
1515
item: "has-[button[data-state=open]]:rounded-s-md has-[button[data-state=open]]:rounded-e-none has-[button[data-state=open]]:-me-2 has-[button[data-state=open]]:pe-2",
16-
link: "w-full relative py-4 hover:text-menu-active [&:hover_p]:text-menu-active focus-visible:text-menu-active [&:focus-visible_p]:text-menu-active hover:outline-0 rounded-md hover:shadow-none focus-visible:outline-0 focus-visible:rounded-md focus-visible:shadow-none",
16+
link: "group w-full relative py-4 hover:text-menu-active [&:hover_p]:text-menu-active focus-visible:text-menu-active [&:focus-visible_p]:text-menu-active hover:outline-0 rounded-md hover:shadow-none focus-visible:outline-0 focus-visible:rounded-md focus-visible:shadow-none",
17+
linkSubtext: "text-sm",
1718
submenu: "grid h-full w-full grid-cols-1",
1819
},
1920
variants: {
@@ -22,21 +23,25 @@ export const navMenuVariants = tv({
2223
submenu: "grid-cols-3 bg-menu-1-background",
2324
item: "has-[button[data-state=open]]:bg-menu-1-active-background",
2425
link: "data-[active=true]:bg-menu-1-active-background hover:bg-menu-1-active-background focus-visible:bg-menu-1-active-background",
26+
linkSubtext: "group-[data-active=true]:text-menu-1-subtext",
2527
},
2628
2: {
2729
submenu: "grid-cols-2 bg-menu-2-background",
2830
item: "has-[button[data-state=open]]:bg-menu-2-active-background",
2931
link: "hover:bg-menu-2-active-background focus-visible:bg-menu-2-active-background data-[active=true]:bg-menu-2-active-background",
32+
linkSubtext: "group-[data-active=true]:text-menu-2-subtext",
3033
},
3134
3: {
3235
submenu: "grid-cols-1 bg-menu-3-background",
3336
item: "has-[button[data-state=open]]:bg-menu-3-active-background",
3437
link: "data-[active=true]:bg-menu-3-active-background hover:bg-menu-3-active-background",
38+
linkSubtext: "group-[data-active=true]:text-menu-3-subtext",
3539
},
3640
4: {
3741
submenu: "grid-cols-1 bg-menu-4-background",
3842
item: "has-[button[data-state=open]]:bg-menu-4-active-background",
3943
link: "data-[active=true]:bg-menu-4-active-background hover:bg-menu-4-active-background",
44+
linkSubtext: "group-[data-active=true]:text-menu-4-subtext",
4045
},
4146
},
4247
},

src/components/Nav/Mobile/LvlAccordion.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,34 @@ import {
1818
AccordionTrigger,
1919
} from "./MenuAccordion"
2020

21-
import { useNavMenuColorsTw } from "@/hooks/useNavMenuColorsTw"
22-
2321
type LvlAccordionProps = {
2422
lvl: Level
2523
items: NavItem[]
2624
activeSection: NavSectionKey
2725
onToggle: () => void
2826
}
2927

28+
const subtextColorPerLevel = {
29+
1: "text-menu-1-subtext",
30+
2: "text-menu-2-subtext",
31+
3: "text-menu-3-subtext",
32+
4: "text-menu-4-subtext",
33+
}
34+
35+
const backgroundColorPerLevel = {
36+
1: "bg-menu-1-background",
37+
2: "bg-menu-2-background",
38+
3: "bg-menu-3-background",
39+
4: "bg-menu-4-background",
40+
}
41+
3042
const LvlAccordion = ({
3143
lvl,
3244
items,
3345
activeSection,
3446
onToggle,
3547
}: LvlAccordionProps) => {
3648
const { asPath, locale } = useRouter()
37-
const menuColors = useNavMenuColorsTw()
3849
const [value, setValue] = useState("")
3950

4051
return (
@@ -84,7 +95,9 @@ const LvlAccordion = ({
8495
<p
8596
className={cn(
8697
"text-md font-bold",
87-
isActivePage ? menuColors.active : menuColors.body
98+
isActivePage
99+
? "text-primary-low-contrast"
100+
: "text-body"
88101
)}
89102
>
90103
{label}
@@ -93,8 +106,8 @@ const LvlAccordion = ({
93106
className={cn(
94107
"text-sm font-normal",
95108
isActivePage
96-
? menuColors.active
97-
: menuColors.lvl[lvl].subtext
109+
? "text-primary-high-contrast"
110+
: subtextColorPerLevel[lvl]
98111
)}
99112
>
100113
{description}
@@ -114,7 +127,7 @@ const LvlAccordion = ({
114127
>
115128
<AccordionTrigger
116129
heading={`h${lvl + 1}` as "h2" | "h3" | "h4" | "h5"}
117-
className={cn(menuColors.body, nestedAccordionSpacingMap[lvl])}
130+
className={cn("text-body", nestedAccordionSpacingMap[lvl])}
118131
onClick={() => {
119132
trackCustomEvent({
120133
eventCategory: "Mobile navigation menu",
@@ -127,18 +140,13 @@ const LvlAccordion = ({
127140
>
128141
<ExpandIcon isOpen={isExpanded} />
129142
<div>
130-
<p
131-
className={cn(
132-
"flex-1 text-md font-bold leading-tight",
133-
menuColors.body
134-
)}
135-
>
143+
<p className="flex-1 text-md font-bold leading-tight text-body">
136144
{label}
137145
</p>
138146
<p
139147
className={cn(
140148
"text-sm font-normal leading-tight",
141-
menuColors.lvl[lvl].subtext
149+
subtextColorPerLevel[lvl]
142150
)}
143151
>
144152
{description}
@@ -147,7 +155,7 @@ const LvlAccordion = ({
147155
</AccordionTrigger>
148156

149157
<AccordionContent
150-
className={cn("mt-0 p-0", menuColors.lvl[lvl + 1].background)}
158+
className={cn("mt-0 p-0", backgroundColorPerLevel[lvl])}
151159
>
152160
<LvlAccordion
153161
lvl={(lvl + 1) as Level}

src/components/Nav/Mobile/MenuBody.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@ import {
1717
AccordionTrigger,
1818
} from "./MenuAccordion"
1919

20-
import { useNavMenuColorsTw } from "@/hooks/useNavMenuColorsTw"
21-
2220
type MenuBodyProps = {
2321
onToggle: () => void
2422
linkSections: NavSections
2523
}
2624

2725
const MenuBody = ({ linkSections, onToggle }: MenuBodyProps) => {
2826
const { locale } = useRouter()
29-
const menuColors = useNavMenuColorsTw()
3027
const [value, setValue] = useState("")
3128

3229
return (
@@ -48,7 +45,7 @@ const MenuBody = ({ linkSections, onToggle }: MenuBodyProps) => {
4845
className="border-b border-body-light first:border-t"
4946
>
5047
<AccordionTrigger
51-
className={cn(menuColors.body)}
48+
className="text-body"
5249
onClick={() => {
5350
trackCustomEvent({
5451
eventCategory: "Mobile navigation menu",
@@ -67,9 +64,8 @@ const MenuBody = ({ linkSections, onToggle }: MenuBodyProps) => {
6764

6865
<AccordionContent
6966
className={cn(
70-
"mt-0 p-0",
71-
isExpanded && "border-t border-disabled",
72-
menuColors.lvl[2].background
67+
"mt-0 bg-menu-2-background p-0",
68+
isExpanded && "border-t border-disabled"
7369
)}
7470
>
7571
<LvlAccordion

src/hooks/useNavMenuColors.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/hooks/useNavMenuColorsTw.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)