Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: collapse sidebar by default in main docs + persist state #8608

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions www/apps/api-reference/providers/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const SidebarProvider = ({ children }: SidebarProviderProps) => {
scrollableElement={scrollableElement}
initialItems={config.sidebar}
resetOnCondition={resetOnCondition}
persistState={false}
projectName="api"
>
{children}
</UiSidebarProvider>
Expand Down
1 change: 1 addition & 0 deletions www/apps/book/providers/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const SidebarProvider = ({ children }: SidebarProviderProps) => {
initialItems={config.sidebar}
staticSidebarItems={true}
disableActiveTransition={true}
projectName="docs"
>
{children}
</UiSidebarProvider>
Expand Down
13 changes: 8 additions & 5 deletions www/apps/book/utils/number-sidebar-items.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,27 @@ export default function numberSidebarItems(sidebarItems, numbering = [1]) {
const numberedItems = []
/** @type {import("@/types").SidebarItem | undefined} */
let parentItem
sidebarItems.forEach((item) => {
sidebarItems.forEach((item, index) => {
if (item.type === "separator") {
;(parentItem?.children || numberedItems).push(item)
}

// append current number to the item's title
item.number = `${numbering.join(".")}.`
item.title = `${item.number} ${item.title.trim()}`

if (isTopItems) {
// Add chapter category
numberedItems.push({
type: "category",
title: `Chapter ${padNumber(numbering[0])}`,
title: item.title,
children: [],
loaded: true,
initialOpen: false,
})

parentItem = numberedItems[numberedItems.length - 1]
}
// append current number to the item's title
item.number = `${numbering.join(".")}.`
item.title = `${item.number} ${item.title.trim()}`

if (item.children) {
item.children = numberSidebarItems(item.children, [...numbering, 1])
Expand Down
1 change: 1 addition & 0 deletions www/apps/resources/providers/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SidebarProvider = ({ children }: SidebarProviderProps) => {
initialItems={config.sidebar}
staticSidebarItems={true}
disableActiveTransition={true}
projectName="resources"
>
{children}
</UiSidebarProvider>
Expand Down
1 change: 1 addition & 0 deletions www/apps/ui/src/providers/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const SidebarProvider = ({ children }: SidebarProviderProps) => {
shouldHandlePathChange={true}
scrollableElement={scrollableElement}
disableActiveTransition={true}
projectName="ui"
>
{children}
</UiSidebarProvider>
Expand Down
1 change: 1 addition & 0 deletions www/apps/user-guide/providers/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SidebarProvider = ({ children }: SidebarProviderProps) => {
initialItems={config.sidebar}
staticSidebarItems={true}
disableActiveTransition={true}
projectName="user-guide"
>
{children}
</UiSidebarProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export const SidebarItemCategory = ({
item.initialOpen !== undefined ? item.initialOpen : expandItems
)
const childrenRef = useRef<HTMLUListElement>(null)
const { isChildrenActive } = useSidebar()
const {
isChildrenActive,
updatePersistedCategoryState,
getPersistedCategoryState,
persistState,
} = useSidebar()

useEffect(() => {
if (open && !item.loaded) {
Expand All @@ -44,6 +49,16 @@ export const SidebarItemCategory = ({
}
}, [isChildrenActive])

useEffect(() => {
if (!persistState) {
return
}
const persistedOpen = getPersistedCategoryState(item.title)
if (persistedOpen !== undefined) {
setOpen(persistedOpen)
}
}, [persistState])

const handleOpen = () => {
item.onOpen?.()
}
Expand All @@ -70,6 +85,9 @@ export const SidebarItemCategory = ({
if (!open) {
handleOpen()
}
if (persistState) {
updatePersistedCategoryState(item.title, !open)
}
setOpen((prev) => !prev)
}}
>
Expand Down
62 changes: 62 additions & 0 deletions www/packages/docs-ui/src/providers/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export type SidebarContextType = {
setSidebarTopHeight: React.Dispatch<React.SetStateAction<number>>
resetItems: () => void
isItemLoaded: (path: string) => boolean
updatePersistedCategoryState: (title: string, opened: boolean) => void
getPersistedCategoryState: (title: string) => boolean | undefined
persistState: boolean
} & SidebarStyleOptions

export const SidebarContext = createContext<SidebarContextType | null>(null)
Expand Down Expand Up @@ -191,6 +194,8 @@ export type SidebarProviderProps = {
scrollableElement?: Element | Window
staticSidebarItems?: boolean
resetOnCondition?: () => boolean
projectName: string
persistState?: boolean
} & SidebarStyleOptions

export const SidebarProvider = ({
Expand All @@ -204,7 +209,11 @@ export const SidebarProvider = ({
staticSidebarItems = false,
disableActiveTransition = false,
resetOnCondition,
projectName,
persistState = true,
}: SidebarProviderProps) => {
const categoriesStorageKey = `${projectName}_categories`
const hideSidebarStorageKey = `hide_sidebar`
const [items, dispatch] = useReducer(reducer, {
default: initialItems?.default || [],
mobile: initialItems?.mobile || [],
Expand Down Expand Up @@ -462,6 +471,56 @@ export const SidebarProvider = ({
}
}, [resetOnCondition, resetItems])

useEffect(() => {
if (!isBrowser) {
return
}

const storageValue = localStorage.getItem(hideSidebarStorageKey)

if (storageValue !== null) {
setDesktopSidebarOpen(storageValue === "false")
}
}, [isBrowser])

useEffect(() => {
if (!isBrowser) {
return
}

localStorage.setItem(
hideSidebarStorageKey,
`${desktopSidebarOpen === false}`
)
}, [isBrowser, desktopSidebarOpen])

const updatePersistedCategoryState = (title: string, opened: boolean) => {
const storageData = JSON.parse(
localStorage.getItem(categoriesStorageKey) || "{}"
)
if (!Object.hasOwn(storageData, projectName)) {
storageData[projectName] = {}
}

storageData[projectName] = {
...storageData[projectName],
[title]: opened,
}

localStorage.setItem(categoriesStorageKey, JSON.stringify(storageData))
}

const getPersistedCategoryState = (title: string): boolean | undefined => {
const storageData = JSON.parse(
localStorage.getItem(categoriesStorageKey) || "{}"
)

return !Object.hasOwn(storageData, projectName) ||
!Object.hasOwn(storageData[projectName], title)
? undefined
: storageData[projectName][title]
}

return (
<SidebarContext.Provider
value={{
Expand All @@ -487,6 +546,9 @@ export const SidebarProvider = ({
setSidebarTopHeight,
resetItems,
isItemLoaded,
updatePersistedCategoryState,
getPersistedCategoryState,
persistState,
}}
>
{children}
Expand Down
6 changes: 6 additions & 0 deletions www/packages/types/src/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ export type RawSidebarItem = SidebarItem & {
custom_autogenerate?: string
number?: string
}

export type PersistedSidebarCategoryState = {
[k: string]: {
[k: string]: boolean
}
}
Loading