Skip to content

Add highlight to navbar showing current tab selected #251

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

Merged
merged 3 commits into from
Nov 16, 2023
Merged
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
30 changes: 21 additions & 9 deletions src/components/navbar/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import useMediaQuery from "@mui/material/useMediaQuery";
import { signOut, useSession } from "next-auth/react";
import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/router";
import * as React from "react";
import { MouseEventHandler } from "react";
import $navbar from "./Navbar.module.css";
Expand All @@ -29,6 +30,7 @@ interface NavItemProps {
export default function NavBar() {
const [mobileOpen, setMobileOpen] = React.useState(false);
const desktop = useMediaQuery("(min-width: 992px)");
const router = useRouter();

const { status } = useSession();

Expand Down Expand Up @@ -64,16 +66,26 @@ export default function NavBar() {
</Toolbar>
);

const NavItem = ({ href, icon, text, onClick }: NavItemProps) => (
<ListItem disablePadding className={$navbar.linkItem}>
<Link href={href || ""} className={$navbar.link}>
{icon}
<p className={$navbar.linkTitle} onClick={onClick}>
{text}
</p>
const NavItem = ({ href, icon, text, onClick }: NavItemProps) => {
const currentPath = router.pathname;
const selectedPath = currentPath.includes(text.toLowerCase());

return (
<Link
href={href || ""}
className={`${$navbar.link} ${
selectedPath ? $navbar.linkSelected : ""
}`}
>
<ListItem disablePadding className={$navbar.linkItem}>
{icon}
<p className={$navbar.linkTitle} onClick={onClick}>
{text}
</p>
</ListItem>
</Link>
</ListItem>
);
);
};

const drawer = (
<div className={$navbar.sidebar}>
Expand Down
12 changes: 9 additions & 3 deletions src/components/navbar/Navbar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
background-color: var(--primary-40);
}

.linkItem {
padding: 1.25rem 0 1.25rem calc(45% - 1.5rem * 1.5);
}
.link {
color: var(--on-primary);
text-decoration: none;
display: flex;
}

.linkItem {
padding: 1.25rem 0 1.25rem calc(45% - 1.5rem * 1.5);
}

.linkTitle {
padding-left: 1rem;
}
Expand All @@ -30,9 +31,14 @@
background-color: var(--primary-60);
}

.linkSelected {
background-color: var(--primary-60);
}

.burger {
color: var(--primary-99);
}

@media (min-width: 992px) {
.linkItem {
padding-left: 9%;
Expand Down