Link.tsx 715 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. 'use client';
  2. import { default as NextLink } from 'next/link';
  3. import { usePathname } from 'next/navigation';
  4. import clsx from 'clsx';
  5. export default function Link({
  6. href,
  7. children,
  8. className,
  9. prefetch = false,
  10. exact = false,
  11. onClick = () => {},
  12. ...props
  13. }: {
  14. href: string
  15. children?: React.ReactNode
  16. prefetch?: boolean
  17. className?: string
  18. exact?: boolean
  19. onClick?: (event: React.MouseEvent) => void
  20. }): JSX.Element {
  21. const pathname = usePathname();
  22. return (
  23. <NextLink
  24. href={href}
  25. className={clsx(className, [
  26. (exact ? pathname === href : pathname.startsWith(href)) ? 'active' : '',
  27. ])}
  28. {...props}
  29. >
  30. {children}
  31. </NextLink>
  32. );
  33. }