navLink.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use client'
  2. import { useSelectedLayoutSegment } from 'next/navigation'
  3. import classNames from 'classnames'
  4. import Link from 'next/link'
  5. export type NavIcon = React.ComponentType<
  6. React.PropsWithoutRef<React.ComponentProps<'svg'>> & {
  7. title?: string | undefined
  8. titleId?: string | undefined
  9. }
  10. >
  11. export type NavLinkProps = {
  12. name: string
  13. href: string
  14. iconMap: {
  15. selected: NavIcon
  16. normal: NavIcon
  17. }
  18. }
  19. export default function NavLink({
  20. name,
  21. href,
  22. iconMap,
  23. }: NavLinkProps) {
  24. const segment = useSelectedLayoutSegment()
  25. const isActive = href.toLowerCase().split('/')?.pop() === segment?.toLowerCase()
  26. const NavIcon = isActive ? iconMap.selected : iconMap.normal
  27. return (
  28. <Link
  29. key={name}
  30. href={href}
  31. className={classNames(
  32. isActive ? 'bg-primary-50 text-primary-600 font-semibold' : 'text-gray-700 hover:bg-gray-100 hover:text-gray-700',
  33. 'group flex items-center rounded-md px-2 py-2 text-sm font-normal',
  34. )}
  35. >
  36. <NavIcon
  37. className={classNames(
  38. 'mr-2 h-4 w-4 flex-shrink-0',
  39. isActive ? 'text-primary-600' : 'text-gray-700',
  40. )}
  41. aria-hidden="true"
  42. />
  43. {name}
  44. </Link>
  45. )
  46. }