Skip to content

Commit

Permalink
feat: add NavLink with dynamic className support (#2539)
Browse files Browse the repository at this point in the history
Co-authored-by: Priyankar Pal <[email protected]>
  • Loading branch information
manuelbento19 and priyankarpal authored Jun 21, 2024
1 parent 37b3372 commit 25021ac
Show file tree
Hide file tree
Showing 5 changed files with 2,487 additions and 19 deletions.
23 changes: 23 additions & 0 deletions components/NavLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { LinkProps } from "next/link";
import { ReactNode } from "react";
import { ComponentProps } from "react";

type Props = LinkProps & Omit<ComponentProps<"a">,"className"> & {
className: string | ((active:boolean) => string);
children: ReactNode;
}

export const NavLink: React.FC<Props> = ({children,href,className,...rest}) => {
const activePath = usePathname();

const actualClass = typeof className === 'function' ? className(activePath === href || activePath === href + "/") : className;

return (
<Link href={href} {...rest} className={actualClass}>
{children}
</Link>
);
};
25 changes: 7 additions & 18 deletions components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import React, { Suspense } from "react";
import { NextPage } from "next";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { NavLink } from "./NavLink";

interface NavLink {
name: string;
Expand All @@ -11,7 +11,6 @@ interface NavLink {
}

const Navbar: NextPage = () => {
const activePath = usePathname();
const navLinks: NavLink[] = [
{
name: "Home",
Expand Down Expand Up @@ -55,32 +54,22 @@ const Navbar: NextPage = () => {
{navLinks.map((navLink) => (
<li key={navLink.path}>
{navLink.external ? (
<Link
<NavLink
href={navLink.path}
aria-label="desktop navbar link"
target="_blank"
rel="noopener noreferrer"
className={
activePath === navLink.path ||
activePath === navLink.path + "/"
? "inline-block py-2 px-3 text-center text-primary hover:text-primary rounded-lg"
: "inline-block py-2 px-3 text-center text-white hover:text-primary rounded-lg"
}
className={(active)=> active ? "inline-block py-2 px-3 text-center text-primary hover:text-primary rounded-lg" : "inline-block py-2 px-3 text-center text-white hover:text-primary rounded-lg"}
>
{navLink.name}
</Link>
</NavLink>
) : (
<Link
<NavLink
href={navLink.path}
className={
activePath === navLink.path ||
activePath === navLink.path + "/"
? "inline-block py-2 px-3 text-center text-primary hover:text-primary rounded-lg"
: "inline-block py-2 px-3 text-center text-white hover:text-primary rounded-lg"
}
className={(active)=> active ? "inline-block py-2 px-3 text-center text-primary hover:text-primary rounded-lg" : "inline-block py-2 px-3 text-center text-white hover:text-primary rounded-lg"}
>
{navLink.name}
</Link>
</NavLink>
)}
</li>
))}
Expand Down
Loading

0 comments on commit 25021ac

Please sign in to comment.