Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat(components:navbar): highlight active links
Browse files Browse the repository at this point in the history
  ## what
  - highlight active links

  ## how
  - use css classname from `globals.css`
    - nav-link
      - highlight link on hover
    - active-nav-link
      - highlight link when link href is the same as the url pathname

  ## why

  ## where
  - ./src/components/navbar.tsx

  ## usage
  • Loading branch information
Clumsy-Coder committed Jan 2, 2024
1 parent b39fca5 commit 1c21c75
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/components/navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"use client";

import Link from "next/link";
import { usePathname } from "next/navigation";

import { cn } from "@/lib/utils";
import { ModeToggle } from "@/components/darkmode-toggle";
Expand All @@ -9,6 +12,8 @@ const links = [
];

const Navbar = ({ className, ...props }: React.HTMLAttributes<HTMLElement>) => {
const pathname = usePathname();

return (
<header className="flex flex-col justify-between md:flex border-b w-full sticky top-0 z-50 bg-background">
<nav
Expand All @@ -18,15 +23,20 @@ const Navbar = ({ className, ...props }: React.HTMLAttributes<HTMLElement>) => {
)}
{...props}
>
{links.map((link) => (
<Link
key={link.label}
href={link.href}
className="text-lg font-medium rounded-lg px-2 py-2 hover:bg-zinc-800 hover:underline"
>
{link.label}
</Link>
))}
<div className="flex items-center gap-2">
{links.map((link) => (
<Link
key={link.label}
href={link.href}
className={cn(
"nav-link",
pathname === link.href && "active-nav-link",
)}
>
{link.label}
</Link>
))}
</div>
<ModeToggle />
</nav>
</header>
Expand Down

0 comments on commit 1c21c75

Please sign in to comment.