Files
supabase/apps/learn/components/side-navigation-item.tsx
Ivan Vasilov 308cd791a2 chore: Prep work for migrating to Tailwind v4 (#45285)
This PR preps the monorepo for a migration to Tailwind v4:
- Bump all Tailwind dependencies and libraries to the latest possible
version, while still compatible with Tailwind 3.
- Cleans up obsolete Tailwind 3 specific options and configs.
- Cleans up unused CSS files and fixes the CSS imports.
- Migrates all `important` uses in `@apply` lines to using the `!`
prefix.
- Move `typography.css` to the `config` package and import it from the
apps.
- Migrated all occurrences of `flex-grow`, `flex-shrink`,
`overflow-clip` and `overflow-ellipsis` since they're deprecated and
will be removed in Tailwind 4.
- Make the default theme object typesafe in the `ui` package.
- Migrate all `bg-opacity`, `border-opacity`, `ring-opacity` and
`divider-opacity` to the new format where they're declared as part of
the property color.
- Bump and unify all imports of `postcss` dependency.
2026-04-28 11:33:53 +02:00

168 lines
5.1 KiB
TypeScript

'use client'
import { ChevronRight, Lock } from 'lucide-react'
import Link, { LinkProps } from 'next/link'
import { usePathname } from 'next/navigation'
import React, { useEffect, useState } from 'react'
import { Badge, cn } from 'ui'
import { useMobileMenu } from '@/hooks/use-mobile-menu'
import { SidebarNavItem } from '@/types/nav'
// We extend:
// 1. LinkProps - for Next.js Link component props (prefetch, etc)
// 2. AnchorHTMLAttributes - for standard HTML anchor props (className, etc)
// We omit href from both since we compute it internally from item.href
interface NavigationItemProps
extends Omit<LinkProps, 'href'>, Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
item: SidebarNavItem
onClick?: (e: React.MouseEvent<HTMLAnchorElement>) => void
level?: number
internalPaths?: string[]
isLoggedIn?: boolean
}
const NavigationItem: React.FC<NavigationItemProps> = ({
item,
onClick,
level = 0,
internalPaths,
isLoggedIn = true,
...props
}) => {
const { setOpen } = useMobileMenu()
const pathname = usePathname()
const [isOpen, setIsOpen] = useState(false)
const pathParts = pathname.split('/')
const slug = pathParts[pathParts.length - 1]
const hasChildren = item.items && item.items.length > 0
// Check if internal content exists for this item and user is logged in
const hasInternal = item.href && internalPaths?.includes(item.href) && isLoggedIn
// Auto-expand if any child is active
useEffect(() => {
if (hasChildren) {
const hasActiveChild = item.items?.some((child) => {
return pathname === child.href
})
if (hasActiveChild) {
setIsOpen(true)
}
}
}, [hasChildren, item.items, pathname])
// Use item.href if available, otherwise build from slug
let href = item.href
if (!href && slug) {
href = `/docs/${slug}`
}
// Determine if this link represents the current page
const isActive = pathname === href
const handleLinkClick = (e: React.MouseEvent<HTMLAnchorElement>) => {
// Close the mobile menu when navigating
setOpen(false)
// Call the onClick prop if it exists
if (onClick) {
onClick(e)
}
}
const handleButtonClick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault()
setIsOpen(!isOpen)
}
const itemClasses = cn(
'flex text-sm rounded-md transition-colors',
level === 0 ? 'px-3 py-2' : 'px-3 py-1.5',
isActive
? 'bg-surface-200 text-foreground'
: hasChildren && isOpen
? 'bg-surface-100 text-foreground'
: 'text-foreground-lighter hover:bg-surface-100 hover:text-foreground'
)
return (
<li>
{hasChildren ? (
<>
<button
onClick={handleButtonClick}
className={cn('w-full flex items-center justify-between gap-2 zans', itemClasses)}
>
<span className="flex items-center gap-2 flex-1 min-w-0">
{item.title}
{item.new && (
<Badge variant="default" className="capitalize shrink-0">
NEW
</Badge>
)}
</span>
<ChevronRight
className={cn(
'w-4 h-4 transition-transform shrink-0',
isOpen && 'rotate-90',
(hasChildren && isOpen) || isActive ? 'text-foreground' : 'text-foreground-lighter'
)}
/>
</button>
{isOpen && (
<ul className="mt-1 ml-3 space-y-1 border-l border-border pl-3">
{item.items?.map((childItem, i) => (
<NavigationItem
item={childItem}
key={`${childItem.href}-${i}`}
level={level + 1}
internalPaths={internalPaths}
isLoggedIn={isLoggedIn}
/>
))}
</ul>
)}
</>
) : (
<>
<Link href={href || '#'} {...props} onClick={handleLinkClick} className={itemClasses}>
<span className="flex items-center gap-2">
<span className="truncate">{item.title}</span>
{item.new && (
<Badge variant="default" className="capitalize shrink-0">
NEW
</Badge>
)}
</span>
</Link>
{hasInternal && (
<Link
href={`/internal${href}`}
onClick={handleLinkClick}
className={cn(
'flex text-sm rounded-md transition-colors mt-1',
level === 0 ? 'px-3 py-2' : 'px-3 py-1.5',
pathname === `/internal${href}`
? 'bg-surface-200 text-foreground'
: 'text-foreground-lighter hover:bg-surface-100 hover:text-foreground'
)}
>
<span className="flex items-center gap-2">
<Lock className="w-3 h-3 text-foreground-muted shrink-0" />
<span className="truncate">{item.title} (Internal)</span>
</span>
</Link>
)}
</>
)}
</li>
)
}
NavigationItem.displayName = 'NavigationItem'
export default NavigationItem