Last active
February 1, 2024 11:48
-
-
Save dninomiya/4acb52fa14c107c3e4a73f2b353ec375 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use client'; | |
import { cn } from '@/app/lib/utils'; | |
import Link from 'next/link'; | |
import { usePathname } from 'next/navigation'; | |
import { ComponentProps, forwardRef } from 'react'; | |
const ActiveLink = forwardRef< | |
HTMLAnchorElement, | |
ComponentProps<typeof Link> & { | |
activeClass: string; | |
} | |
>(({ href, className, children, activeClass, ...props }, ref) => { | |
const pathname = usePathname(); | |
const active = pathname === href; | |
return ( | |
<Link | |
href={href} | |
data-active={active} | |
className={cn('group', className, active && activeClass)} | |
ref={ref} | |
{...props} | |
> | |
{children} | |
</Link> | |
); | |
}); | |
ActiveLink.displayName = 'ActiveLink'; | |
export default ActiveLink; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment