manifold/web/components/nav/sidebar-item.tsx

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-09-17 23:25:53 +00:00
import React from 'react'
import clsx from 'clsx'
import Link from 'next/link'
import { trackCallback } from 'web/lib/service/analytics'
export type Item = {
name: string
trackingEventName?: string
2022-09-20 17:03:34 +00:00
href: string
2022-09-17 23:25:53 +00:00
key?: string
icon?: React.ComponentType<{ className?: string }>
}
2022-09-20 17:03:34 +00:00
export function SidebarItem(props: { item: Item; currentPage: string }) {
const { item, currentPage } = props
2022-09-17 23:25:53 +00:00
const isCurrentPage =
item.href != null ? item.href === currentPage : item.key === currentPage
const sidebarItem = (
<a
2022-09-20 17:03:34 +00:00
onClick={() => {
trackCallback('sidebar: ' + item.name)
}}
2022-09-17 23:25:53 +00:00
className={clsx(
isCurrentPage
? 'bg-gray-200 text-gray-900'
: 'text-gray-600 hover:bg-gray-100',
'group flex items-center rounded-md px-3 py-2 text-sm font-medium'
)}
aria-current={item.href == currentPage ? 'page' : undefined}
>
{item.icon && (
<item.icon
className={clsx(
isCurrentPage
? 'text-gray-500'
: 'text-gray-400 group-hover:text-gray-500',
'-ml-1 mr-3 h-6 w-6 flex-shrink-0'
)}
aria-hidden="true"
/>
)}
<span className="truncate">{item.name}</span>
</a>
)
2022-09-20 17:03:34 +00:00
return <Link href={item.href}>{sidebarItem}</Link>
2022-09-17 23:25:53 +00:00
}