Skip to content

Commit 8c0a53a

Browse files
authored
Fix page group not expanded by default (#3358)
1 parent 2dfd5aa commit 8c0a53a

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

.changeset/real-trains-perform.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Fix page group not expanded by default

packages/gitbook/src/components/TableOfContents/PageDocumentItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getPagePath, hasPageVisibleDescendant } from '@/lib/pages';
1+
import { getPagePaths, hasPageVisibleDescendant } from '@/lib/pages';
22
import { tcls } from '@/lib/tailwind';
33
import {
44
type RevisionPage,
@@ -27,7 +27,7 @@ export async function PageDocumentItem(props: {
2727
<li className="flex flex-col">
2828
<ToggleableLinkItem
2929
href={href}
30-
pathname={getPagePath(rootPages, page)}
30+
pathnames={getPagePaths(rootPages, page)}
3131
insights={{
3232
type: 'link_click',
3333
link: {

packages/gitbook/src/components/TableOfContents/ToggleableLinkItem.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ import { useScrollToActiveTOCItem } from './TOCScroller';
1616
export function ToggleableLinkItem(
1717
props: {
1818
href: string;
19-
pathname: string;
19+
pathnames: string[];
2020
children: React.ReactNode;
2121
descendants: React.ReactNode;
2222
} & LinkInsightsProps
2323
) {
24-
const { href, children, descendants, pathname, insights } = props;
24+
const { href, children, descendants, pathnames, insights } = props;
2525

2626
const currentPagePath = useCurrentPagePath();
27-
const isActive = currentPagePath === pathname;
27+
const isActive = pathnames.some((pathname) => pathname === currentPagePath);
2828

2929
if (!descendants) {
3030
return (
@@ -37,7 +37,9 @@ export function ToggleableLinkItem(
3737
return (
3838
<DescendantsRenderer
3939
descendants={descendants}
40-
defaultIsOpen={isActive || currentPagePath.startsWith(`${pathname}/`)}
40+
defaultIsOpen={
41+
isActive || pathnames.some((pathname) => currentPagePath.startsWith(`${pathname}/`))
42+
}
4143
>
4244
{({ descendants, toggler }) => (
4345
<>

packages/gitbook/src/lib/pages.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ export function getPagePath(
120120
return page.path;
121121
}
122122

123+
/**
124+
* Get all possible paths for a page.
125+
*/
126+
export function getPagePaths(
127+
rootPages: Revision['pages'],
128+
page: RevisionPageDocument | RevisionPageGroup
129+
): string[] {
130+
const path = getPagePath(rootPages, page);
131+
if (path === page.path) {
132+
return [path];
133+
}
134+
return [path, page.path];
135+
}
136+
123137
/**
124138
* Test if a page has at least one descendant.
125139
*/

0 commit comments

Comments
 (0)