Skip to content

Fix page group not expanded by default #3358

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

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
5 changes: 5 additions & 0 deletions .changeset/real-trains-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook": patch
---

Fix page group not expanded by default
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPagePath, hasPageVisibleDescendant } from '@/lib/pages';
import { getPagePaths, hasPageVisibleDescendant } from '@/lib/pages';
import { tcls } from '@/lib/tailwind';
import {
type RevisionPage,
Expand Down Expand Up @@ -27,7 +27,7 @@ export async function PageDocumentItem(props: {
<li className="flex flex-col">
<ToggleableLinkItem
href={href}
pathname={getPagePath(rootPages, page)}
pathnames={getPagePaths(rootPages, page)}
insights={{
type: 'link_click',
link: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import { useScrollToActiveTOCItem } from './TOCScroller';
export function ToggleableLinkItem(
props: {
href: string;
pathname: string;
pathnames: string[];
children: React.ReactNode;
descendants: React.ReactNode;
} & LinkInsightsProps
) {
const { href, children, descendants, pathname, insights } = props;
const { href, children, descendants, pathnames, insights } = props;

const currentPagePath = useCurrentPagePath();
const isActive = currentPagePath === pathname;
const isActive = pathnames.some((pathname) => pathname === currentPagePath);

if (!descendants) {
return (
Expand All @@ -37,7 +37,9 @@ export function ToggleableLinkItem(
return (
<DescendantsRenderer
descendants={descendants}
defaultIsOpen={isActive || currentPagePath.startsWith(`${pathname}/`)}
defaultIsOpen={
isActive || pathnames.some((pathname) => currentPagePath.startsWith(`${pathname}/`))
}
>
{({ descendants, toggler }) => (
<>
Expand Down
14 changes: 14 additions & 0 deletions packages/gitbook/src/lib/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ export function getPagePath(
return page.path;
}

/**
* Get all possible paths for a page.
*/
export function getPagePaths(
rootPages: Revision['pages'],
page: RevisionPageDocument | RevisionPageGroup
): string[] {
const path = getPagePath(rootPages, page);
if (path === page.path) {
return [path];
}
return [path, page.path];
}

/**
* Test if a page has at least one descendant.
*/
Expand Down