docs: support multiple sidebars in a project (#11768)

* changed to new sidebar across projects except resources

* finalize multi sidebar support

* clean up

* remove redundant property

* small changes

* fixes

* generate

* fix error

* fix initial open
This commit is contained in:
Shahed Nasser
2025-03-07 15:47:38 +02:00
committed by GitHub
parent 2a0bd86204
commit 5deb8eaf50
108 changed files with 37634 additions and 36749 deletions
@@ -2,7 +2,7 @@
import { ExpandedDocument, SecuritySchemeObject } from "@/types/openapi"
import { ReactNode, createContext, useContext, useEffect, useMemo } from "react"
import { SidebarItem, SidebarItemSections } from "types"
import { Sidebar } from "types"
import getSectionId from "../utils/get-section-id"
import getTagChildSidebarItems from "../utils/get-tag-child-sidebar-items"
import { useRouter } from "next/navigation"
@@ -22,7 +22,8 @@ type BaseSpecsProviderProps = {
const BaseSpecsProvider = ({ children, baseSpecs }: BaseSpecsProviderProps) => {
const router = useRouter()
const { activePath, addItems, setActivePath, resetItems } = useSidebar()
const { activePath, addItems, setActivePath, resetItems, shownSidebar } =
useSidebar()
const getSecuritySchema = (
securityName: string
@@ -48,7 +49,7 @@ const BaseSpecsProvider = ({ children, baseSpecs }: BaseSpecsProviderProps) => {
return []
}
const itemsToAdd: SidebarItem[] = [
const itemsToAdd: Sidebar.SidebarItem[] = [
{
type: "separator",
},
@@ -67,6 +68,7 @@ const BaseSpecsProvider = ({ children, baseSpecs }: BaseSpecsProviderProps) => {
children: childItems,
loaded: childItems.length > 0,
showLoadingIfEmpty: true,
initialOpen: false,
onOpen: () => {
if (location.hash !== tagPathName) {
router.push(`#${tagPathName}`, {
@@ -84,14 +86,14 @@ const BaseSpecsProvider = ({ children, baseSpecs }: BaseSpecsProviderProps) => {
}, [baseSpecs])
useEffect(() => {
if (!itemsToAdd.length) {
if (!itemsToAdd.length || !shownSidebar) {
return
}
addItems(itemsToAdd, {
section: SidebarItemSections.DEFAULT,
sidebar_id: shownSidebar.sidebar_id,
})
}, [itemsToAdd])
}, [itemsToAdd, shownSidebar?.sidebar_id])
useEffect(() => {
return () => {
@@ -3,7 +3,7 @@
import { createContext, useEffect } from "react"
import { useSidebar } from "docs-ui"
import { useArea } from "./area"
import { SidebarItemLink } from "types"
import { Sidebar } from "types"
const PageTitleContext = createContext(null)
@@ -27,7 +27,7 @@ const PageTitleProvider = ({ children }: PageTitleProviderProps) => {
// find the child that matches the active path
const item = activeItem?.children?.find(
(i) => i.type === "link" && i.path === activePath
) as SidebarItemLink
) as Sidebar.SidebarItemLink
if (item) {
document.title = `${item.title} - ${titleSuffix}`
}
+5 -3
View File
@@ -20,10 +20,12 @@ const SidebarProvider = ({ children }: SidebarProviderProps) => {
isLoading={isLoading}
setIsLoading={setIsLoading}
shouldHandleHashChange={true}
shouldHandlePathChange={false}
scrollableElement={scrollableElement}
initialItems={config.sidebar}
persistState={false}
projectName="api"
sidebars={config.sidebars}
persistCategoryState={false}
disableActiveTransition={false}
isSidebarStatic={false}
>
{children}
</UiSidebarProvider>