Skip to content

Commit

Permalink
Add nested navigation links (#112)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduard Gert <[email protected]>
Co-authored-by: braginini <[email protected]>
  • Loading branch information
3 people authored Dec 12, 2023
1 parent 464530a commit ddc65c7
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 93 deletions.
8 changes: 5 additions & 3 deletions src/components/Footer.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {forwardRef, Fragment, useEffect, useState} from 'react'
import {forwardRef, Fragment, useState} from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { Transition } from '@headlessui/react'

import { Button } from '@/components/Button'
import {apiNavigation} from '@/components/NavigationAPI'
import {apiNavigation, flattenNavItems} from '@/components/NavigationAPI'
import {docsNavigation} from "@/components/NavigationDocs";

function CheckIcon(props) {
Expand Down Expand Up @@ -126,7 +126,9 @@ function PageLink({ label, page, previous = false }) {

function PageNavigation() {
let router = useRouter()
let allPages = !router.route.startsWith('/ipa') ? docsNavigation.flatMap((group) => group.links) : apiNavigation.flatMap((group) => group.links)
let allPages = !router.route.startsWith('/ipa')
? docsNavigation.flatMap((group) => flattenNavItems(group.links, true))
: apiNavigation.flatMap((group) => flattenNavItems(group.links,true));
let currentPageIndex = allPages.findIndex(
(page) => page.href === router.pathname
)
Expand Down
147 changes: 94 additions & 53 deletions src/components/NavigationAPI.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Link from 'next/link'
import { useRouter } from 'next/router'
import clsx from 'clsx'
import { AnimatePresence, motion, useIsPresent } from 'framer-motion'
import { AnimatePresence, motion } from 'framer-motion'
import { Button } from '@/components/Button'
import { Tag } from '@/components/Tag'
import { remToPx } from '@/lib/remToPx'
import {useIsInsideMobileNavigation} from "@/components/MobileNavigation";
import {useEffect, useState} from "react";
import {NavigationStateProvider, useNavigationState} from "@/components/NavigationState";

export const apiNavigation = [
{
Expand Down Expand Up @@ -46,12 +47,13 @@ export function NavigationAPI({tableOfContents, className}) {
<TopLevelNavItem href="https://github.com/netbirdio/netbird">Github</TopLevelNavItem>
<TopLevelNavItem href="https://join.slack.com/t/netbirdio/shared_invite/zt-vrahf41g-ik1v7fV8du6t0RwxSrJ96A">Support</TopLevelNavItem>
{apiNavigation.map((group, groupIndex) => (
<NavigationStateProvider key={group.title} index={groupIndex}>
<NavigationGroup
key={group.title}
group={group}
tableOfContents={tableOfContents}
className={groupIndex === 0 && 'md:mt-0'}
/>
</NavigationStateProvider>
))}
<li className="sticky bottom-0 z-10 mt-6 min-[416px]:hidden">
<Button href="https://app.netbird.io/" variant="filled" className="w-full">
Expand All @@ -75,75 +77,114 @@ export function TopLevelNavItem({ href, children }) {
)
}

export function NavLink({ href, tag, active, isAnchorLink = false, children }) {
export function NavLink({ href, tag, active, isAnchorLink = false, children, links, isChildren = false }) {
let router = useRouter();

return (
<Link
href={href}
aria-current={active ? 'page' : undefined}
className={clsx(
'flex justify-between gap-2 py-1 pr-3 text-sm transition',
isAnchorLink ? 'pl-7' : 'pl-4',
active
? 'text-zinc-900 dark:text-white'
: 'text-zinc-600 hover:text-zinc-900 dark:text-zinc-400 dark:hover:text-white'
)}
>
<span className="truncate">{children}</span>
{tag && (
<Tag variant="small" color="zinc">
{tag}
</Tag>
)}
</Link>
<div className={"relative"} >
<Link
href={href ? href : "#"}
data-nb-link={active ? 1 : 0}
aria-current={active ? 'page' : undefined}
className={clsx(
'flex justify-between gap-2 py-1 pr-3 text-sm transition',
isAnchorLink ? 'pl-7' : 'pl-4',
active
? 'text-zinc-900 dark:text-white'
: 'text-zinc-600 hover:text-zinc-900 dark:text-zinc-400 dark:hover:text-white',
isChildren ? 'pl-7' : 'pl-4',
)}
>
<span className="truncate">{children}</span>
{tag && (
<Tag variant="small" color="zinc">
{tag}
</Tag>
)}
</Link>

{links &&
<ul role="list">
{links.map((link,index) => (
<motion.li key={index} layout="position" className="relative">
<NavLink href={link.href} active={link.href === router.pathname} isChildren={true}>
{link.title}
</NavLink>
</motion.li>
))}
</ul>
}
</div>

)
}

export function VisibleSectionHighlight({ group, pathname }) {
let height = remToPx(2)
let offset = remToPx(0)
let activePageIndex = group.links.findIndex((link) => link.href === pathname)
let top = offset + activePageIndex * height
export function flattenNavItems(links, onlyLinks = false) {
let output = []
for (let link of links) {
output.push(link)
if (link.links) output.push(...flattenNavItems(link.links, onlyLinks))
}
if(onlyLinks) output = output.filter((link) => link.href)
return output
}

export function VisibleSectionHighlight() {
const router = useRouter();
let height = remToPx(2)
let offset = remToPx(0)
const [activeIndex] = useNavigationState();
const [top, setTop] = useState(0);

return (
<motion.div
layout
initial={{ opacity: 0 }}
animate={{ opacity: 1, transition: { delay: 0.2 } }}
exit={{ opacity: 0 }}
className="absolute inset-x-0 top-0 bg-zinc-800/2.5 will-change-transform dark:bg-white/2.5"
style={{ borderRadius: 8, height, top }}
/>
)
useEffect(() => {
setTop(offset + (activeIndex) * height);
}, [activeIndex, router.pathname]);

return activeIndex >= 0 && (
<motion.div
layout
initial={{ opacity: 0 }}
animate={{ opacity: 1, transition: { delay: 0.2 } }}
exit={{ opacity: 0 }}
className="absolute inset-x-0 top-0 bg-zinc-800/2.5 will-change-transform dark:bg-white/2.5"
style={{ borderRadius: 8, height, top }}
/>
)
}

export function ActivePageMarker({ group, pathname }) {
let itemHeight = remToPx(2)
let offset = remToPx(0.25)
let activePageIndex = group.links.findIndex((link) => link.href === pathname)
let top = offset + activePageIndex * itemHeight
export function ActivePageMarker() {
const router = useRouter();
let itemHeight = remToPx(2)
let offset = remToPx(0.25)
const [activeIndex] = useNavigationState();
const [top, setTop] = useState(0);

return (
<motion.div
layout
className="absolute left-2 h-6 w-px bg-orange-500"
initial={{ opacity: 0 }}
animate={{ opacity: 1, transition: { delay: 0.2 } }}
exit={{ opacity: 0 }}
style={{ top }}
/>
)
useEffect(() => {
setTop(offset + (activeIndex) * itemHeight);
}, [activeIndex, router.pathname]);

return activeIndex >= 0 && (
<motion.div
layout
className="absolute left-2 h-6 w-px bg-orange-500"
initial={{ opacity: 0 }}
animate={{ opacity: 1, transition: { delay: 0.2 } }}
exit={{ opacity: 0 }}
style={{ top }}
/>
)
}

function NavigationGroup({ group, className, tableOfContents }) {
let router = useRouter()

let isActiveGroup =
group.links.findIndex((link) => link.href === router.pathname.replace("ipa", "api")) !== -1

return (
<li className={clsx('relative mt-6', className)}>
<motion.h2
layout="position"
data-nb-link={group.title}
className="text-xs font-semibold text-zinc-900 dark:text-white"
>
{group.title}
Expand Down
Loading

0 comments on commit ddc65c7

Please sign in to comment.