Skip to content

Commit b4fbf6d

Browse files
committedApr 12, 2024·
feat: highlight selected navbar item
1 parent ec037eb commit b4fbf6d

File tree

2 files changed

+80
-41
lines changed

2 files changed

+80
-41
lines changed
 

‎app/src/components/NavigationBar.tsx

+58-40
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,30 @@ function DropdownMenu({
2727
);
2828
}
2929

30+
function MenuLink({
31+
path,
32+
text,
33+
side = false,
34+
children,
35+
}: {
36+
path: string;
37+
text: string;
38+
side?: boolean;
39+
children?: any;
40+
}) {
41+
return (
42+
<Link
43+
className={`${side ? "" : styles.menuItem} ${
44+
useRouter().asPath.startsWith(path) ? styles.selected : ""
45+
}`}
46+
href={path}
47+
>
48+
{text}
49+
{children}
50+
</Link>
51+
);
52+
}
53+
3054
function SideMenu({
3155
headToggle: toggle,
3256
children,
@@ -98,31 +122,26 @@ export default function NavigationBar(props: {
98122
const translations = useTranslationTable();
99123

100124
const router = useRouter();
125+
101126
const entries = [
102-
<Link
127+
<MenuLink
103128
key={0}
104-
className={styles.sidemenuItem}
105-
href="/association"
106-
onClick={toggleMenu}
107-
>
108-
{capitalize(translations["association"])}
109-
</Link>,
110-
<Link
129+
side={true}
130+
path="/association"
131+
text={capitalize(translations["association"])}
132+
/>,
133+
<MenuLink
111134
key={1}
112-
className={styles.sidemenuItem}
113-
href="/commissions"
114-
onClick={toggleMenu}
115-
>
116-
{capitalize(translations["commissions"])}
117-
</Link>,
118-
<Link
135+
side={true}
136+
path="/commissions"
137+
text={capitalize(translations["commissions"])}
138+
/>,
139+
<MenuLink
119140
key={2}
120-
className={styles.sidemenuItem}
121-
href="/news"
122-
onClick={toggleMenu}
123-
>
124-
{capitalize(translations["news"])}
125-
</Link>,
141+
side={true}
142+
path="/news"
143+
text={capitalize(translations["news"])}
144+
/>,
126145
];
127146

128147
return (
@@ -132,30 +151,33 @@ export default function NavigationBar(props: {
132151
</Link>
133152

134153
<div className={styles.navigationMenu}>
135-
<Link className={styles.menuItem} href="/association">
136-
{capitalize(translations["association"])}
137-
</Link>
154+
<MenuLink
155+
path="/association"
156+
text={capitalize(translations["association"])}
157+
/>
138158

139159
{props.commissions ? (
140160
<DropdownMenu
141161
head={
142-
<Link className={styles.menuItem} href="/commissions">
143-
{capitalize(translations["commissions"])}
144-
<i className={styles.arrow} />
145-
</Link>
162+
<>
163+
<MenuLink
164+
path="/commissions"
165+
text={capitalize(translations["commissions"])}
166+
>
167+
<i className={styles.arrow} />
168+
</MenuLink>
169+
</>
146170
}
147171
>
148172
{props.commissions ? (
149173
props.commissions.map((c) => {
150174
if (c.name && c.slug) {
151175
return (
152-
<Link
176+
<MenuLink
153177
key={c.slug}
154-
href={`/commissions/${c.slug}`}
155-
className={styles.menuItem}
156-
>
157-
{c.name}
158-
</Link>
178+
path={`/commissions/${c.slug}`}
179+
text={c.name}
180+
/>
159181
);
160182
} else {
161183
console.error(
@@ -171,14 +193,10 @@ export default function NavigationBar(props: {
171193
)}
172194
</DropdownMenu>
173195
) : (
174-
<Link className={styles.menuItem} href="/commissions">
175-
{capitalize(translations["commissions"])}
176-
</Link>
196+
<></>
177197
)}
178198

179-
<Link className={styles.menuItem} href="/news">
180-
{capitalize(translations["news"])}
181-
</Link>
199+
<MenuLink path="/news" text={capitalize(translations["news"])} />
182200

183201
{props.langs ? (
184202
<DropdownMenu

‎app/src/styles/NavigationBar.module.scss

+22-1
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,34 @@
6464
cursor: pointer;
6565

6666
&:hover {
67-
color: lightgrey;
67+
color: variables.$detail-color;
6868

6969
transition-duration: 250ms;
7070
transition-timing-function: ease-out;
71+
72+
.arrow {
73+
border-color: variables.$detail-color;
74+
}
7175
}
7276

7377
@media (max-width: 800px) {
7478
display: none;
7579
}
7680
}
7781

82+
.selected {
83+
background-color: variables.$dark-text-color;
84+
padding: 0.5rem;
85+
border-radius: 2rem;
86+
87+
&:hover {
88+
color: variables.$text-color;
89+
.arrow {
90+
border-color: variables.$text-color;
91+
}
92+
}
93+
}
94+
7895
.dropdownHead {
7996
display: flex;
8097
justify-content: center;
@@ -93,6 +110,10 @@
93110
margin-left: 0.5em;
94111
margin-bottom: 0.2em;
95112
transform: rotate(45deg);
113+
114+
@media (max-width: 800px) {
115+
display: none !important;
116+
}
96117
}
97118

98119
.content {

0 commit comments

Comments
 (0)
Please sign in to comment.