import { Dialog, Transition } from '@headlessui/react';
import { Menu, MenuItem, MenuSection } from '@nx/nx-dev/models-menu';
import cx from 'classnames';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { createRef, Fragment, useCallback, useEffect, useState } from 'react';
export interface SidebarProps {
menu: Menu;
}
export interface FloatingSidebarProps {
menu: Menu;
navIsOpen: boolean;
}
export function Sidebar({ menu }: SidebarProps): JSX.Element {
return (
);
}
function SidebarSection({ section }: { section: MenuSection }): JSX.Element {
const router = useRouter();
const itemList = section.itemList.map((i) => ({
...i,
ref: createRef(),
}));
const currentItem = itemList.find((s) => router.asPath.includes(s.path));
useEffect(() => {
requestAnimationFrame(() => {
setTimeout(() => {
if (currentItem && currentItem.ref.current)
currentItem.ref.current.scrollIntoView({ behavior: 'smooth' });
}, 0);
});
}, [currentItem]);
return (
<>
{section.hideSectionHeader ? null : (
{section.name}
)}
-
{itemList
.filter((i) => !!i.children?.length)
.map((item, index) => (
))}
>
);
}
function SidebarSectionItems({ item }: { item: MenuItem }): JSX.Element {
const router = useRouter();
const [collapsed, setCollapsed] = useState(!item.disableCollapsible);
const handleCollapseToggle = useCallback(() => {
if (!item.disableCollapsible) {
setCollapsed(!collapsed);
}
}, [collapsed, setCollapsed, item]);
function withoutAnchors(linkText: string): string {
return linkText?.includes('#')
? linkText.substring(0, linkText.indexOf('#'))
: linkText;
}
return (
<>
{item.disableCollapsible ? (
{item.name}
) : (
<>
{item.name}
>
)}
{(item.children as MenuItem[]).map((subItem, index) => {
const isActiveLink = subItem.path === withoutAnchors(router.asPath);
if (isActiveLink && collapsed) {
handleCollapseToggle();
}
return (
-
{subItem.children.length ? (
) : (
{subItem.name}
)}
);
})}
>
);
}
function CollapsibleIcon({
isCollapsed,
}: {
isCollapsed: boolean;
}): JSX.Element {
return (
);
}
export function SidebarMobile({
menu,
navIsOpen,
}: FloatingSidebarProps): JSX.Element {
const router = useRouter();
const isNxCloud: boolean = router.asPath.startsWith('/nx-cloud');
const isPackages: boolean = router.asPath.startsWith('/packages');
const isPlugins: boolean = router.asPath.startsWith('/plugins');
const isRecipes: boolean = router.asPath.startsWith('/recipes');
const isNx: boolean = !isNxCloud && !isPackages && !isPlugins && !isRecipes;
const sections = [
{ name: 'Home', href: '/', current: false },
{ name: 'Nx', href: '/getting-started/intro', current: isNx },
{
name: 'Nx Cloud',
href: '/nx-cloud/intro/what-is-nx-cloud',
current: isNxCloud,
},
{
name: 'Packages',
href: '/packages',
current: isPackages,
},
{
name: 'Plugins',
href: '/plugins/intro/getting-started',
current: isPlugins,
},
{
name: 'Recipes',
href: '/recipes',
current: isRecipes,
},
];
return (
);
}