Files
Anthony Lio 51c07ef6b2 feat(docs): ai layout sidebar tree (#52972)
## Problem

- current sidebar shows only the page's product, so a reader opens the
top bar menus to reach another product
- redesign moves the global nav into the sidebar, so the sidebar needs
levels and a way back up

## Solution

- adds a sidebar that opens one level at a time: the docs sections, then
a section's products, then a product's pages
- adds a header row with a back link to the level above, and a home link
at the top level
- changes a product row to open the product overview and show its pages
at once
- adds icons to the top level sections in the current nav and in the
option-1 nav
- changes the new page tree to show a background on hover only, and text
color on the current page
- adds an overview row to the start section, which only the new layout
shows

#52971 must merge first, because the sidebar uses the `layout-v2:` css
variant

<!--
## Preview links

If relevant, include links to changed pages for easy review access.

Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.

| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->

<!-- ## Additional context

Optionally add any other context or screenshots.

-->

## Review instructions

1. run `pnpm --filter docs exec vitest run
components/Navigation/NavigationMenu`, and make sure that all tests pass
2. read `DocsSidebarNav.test.tsx`, and make sure that the tests cover
the back link, the home link and the product link to its overview
3. the layout mounts the sidebar in the stack top, so check the sidebar
on the preview of that pr

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-11-11 16:31:32 +02:00

225 lines
6.4 KiB
TypeScript

'use client'
import type { ICommonItem, ICommonSection } from '~/components/reference/Reference.types'
import RevVersionDropdown from '~/components/RefVersionDropdown'
import { menuState, useMenuActiveRefId } from '~/hooks/useMenuState'
import { BASE_PATH } from '~/lib/constants'
import { ChevronUp } from 'lucide-react'
import Image from 'next/legacy/image'
import { Accordion } from 'radix-ui'
import { Fragment, memo } from 'react'
import { cn } from 'ui'
import MenuIconPicker from './MenuIconPicker'
import * as NavItems from './NavigationMenu.constants'
import { deepFilterSections } from './NavigationMenu.utils'
import { SidebarGroupLabel } from './SidebarGroupLabel'
const HeaderLink = memo(function HeaderLink(props: any) {
return (
<span
className={[
'text-base text-brand-600 layout-v2:text-foreground ',
!props.title && 'capitalize',
].join(' ')}
>
{props.title ?? props.id}
</span>
)
})
interface FunctionLinkProps {
title: string
name?: string
id: string
icon?: string
basePath: string
slug: string
isParent?: boolean
isSubItem?: boolean
onClick?: () => void
}
const FunctionLink = memo(function FunctionLink({
title,
id,
icon,
basePath,
slug,
isParent = false,
isSubItem = false,
onClick = () => {},
}: FunctionLinkProps) {
const activeAccordionItem = useMenuActiveRefId()
const url = `${BASE_PATH}${basePath}/${slug}`
const active = activeAccordionItem === id
return (
<li className="function-link-item leading-5">
<a
href={url}
aria-current={active ? 'page' : undefined}
/**
* We don't actually want to navigate or re-render anything
* since ref links are all sub-sections on the same page
*/
onClick={(e) => {
e.preventDefault()
menuState.setMenuActiveRefId(id)
history.pushState({}, '', url)
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches
document.getElementById(slug)?.scrollIntoView({
behavior: reduceMotion ? 'auto' : 'smooth',
})
onClick()
}}
className={cn(
'cursor-pointer transition text-sm hover:text-foreground gap-3 relative',
isParent ? 'flex justify-between' : 'leading-3',
active ? 'text-primary layout-v2:text-foreground' : 'text-foreground-lighter'
)}
>
{icon && <Image width={16} height={16} alt={icon} src={`${BASE_PATH}${icon}`} />}
{title}
{active && !isSubItem && (
<div
aria-hidden="true"
className="absolute left-[-13px] top-0 bottom-0 w-px bg-brand-600"
></div>
)}
{isParent && (
<ChevronUp
width={16}
className="data-open-parent:rotate-0 data-closed-parent:rotate-90 transition"
/>
)}
</a>
</li>
)
})
export interface RenderLinkProps {
section: ICommonSection
basePath: string
}
const RenderLink = memo(function RenderLink({ section, basePath }: RenderLinkProps) {
const activeAccordionItem = useMenuActiveRefId()
if (!('items' in section)) {
return (
<FunctionLink
title={section.title}
id={section.id}
slug={section.slug}
basePath={basePath}
isParent={false}
isSubItem
onClick={() => menuState.setMenuMobileOpen(false)}
/>
)
}
let active =
section.id === activeAccordionItem ||
section.items.some((item) => item.id === activeAccordionItem)
return (
<Accordion.Root collapsible type="single" value={active ? section.id : ''}>
<Accordion.Item value={section.id}>
<FunctionLink
title={section.title}
id={section.id}
slug={section.slug}
basePath={basePath}
isParent
isSubItem
/>
<Accordion.Content className="transition data-open:animate-slide-down data-closed:animate-slide-up border-l border-control pl-3 ml-1 data-open:mt-2 grid gap-2.5">
{section.items.map((item) => {
return (
<FunctionLink
key={item.id}
title={item.title}
id={item.id}
slug={item.slug}
basePath={basePath}
isParent={false}
isSubItem={false}
onClick={() => menuState.setMenuMobileOpen(false)}
/>
)
})}
</Accordion.Content>
</Accordion.Item>
</Accordion.Root>
)
})
const SideMenuTitle = ({ title }: { title: string }) => {
return (
<>
<span className="font-mono text-xs uppercase text-foreground font-medium tracking-wider layout-v2:hidden">
{title}
</span>
<SidebarGroupLabel className="hidden mt-1 layout-v2:flex pb-1.5">{title}</SidebarGroupLabel>
</>
)
}
const Divider = () => {
return <div className="h-px w-full bg-control my-3 layout-v2:hidden"></div>
}
interface NavigationMenuRefListItemsProps {
id: string
basePath: string
commonSections: ICommonItem[]
spec?: any
}
const NavigationMenuRefListItems = ({
id,
basePath,
commonSections,
spec,
}: NavigationMenuRefListItemsProps) => {
const menu = NavItems[id]
const specFunctionIds = spec?.functions.map(({ id }) => id)
const filteredSections = spec
? deepFilterSections(commonSections, specFunctionIds)
: commonSections
return (
<div className={'w-full flex flex-col gap-0 sticky top-8'}>
<div className="flex items-center gap-3 my-3 layout-v2:lg:hidden">
<MenuIconPicker icon={menu.icon} width={21} height={21} />
<HeaderLink title={menu.title} url={menu.url} id={id} />
</div>
<ul className="function-link-list flex flex-col gap-2 pb-5">
{filteredSections.map((section) => {
return (
<Fragment key={section.title}>
{section.type === 'category' ? (
<>
<Divider />
<SideMenuTitle title={section.title} />
{section.items.map((item) => (
<RenderLink key={item.id} section={item} basePath={basePath} />
))}
</>
) : (
<RenderLink section={section} basePath={basePath} />
)}
</Fragment>
)
})}
</ul>
</div>
)
}
export default memo(NavigationMenuRefListItems)