Skip to content

Commit e434442

Browse files
authored
Support site section groups (#3664)
1 parent 9344431 commit e434442

File tree

8 files changed

+316
-122
lines changed

8 files changed

+316
-122
lines changed

.changeset/fluffy-clouds-tap.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+
Support nested site section groups

bun.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@
303303
"react-dom": "^19.0.0",
304304
},
305305
"catalog": {
306-
"@gitbook/api": "^0.141.0",
306+
"@gitbook/api": "^0.142.0",
307307
"bidc": "^0.0.2",
308308
},
309309
"packages": {
@@ -675,7 +675,7 @@
675675

676676
"@fortawesome/fontawesome-svg-core": ["@fortawesome/fontawesome-svg-core@6.6.0", "", { "dependencies": { "@fortawesome/fontawesome-common-types": "6.6.0" } }, "sha512-KHwPkCk6oRT4HADE7smhfsKudt9N/9lm6EJ5BVg0tD1yPA5hht837fB87F8pn15D8JfTqQOjhKTktwmLMiD7Kg=="],
677677

678-
"@gitbook/api": ["@gitbook/api@0.141.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-KViAAUUStITbNGqDydUjZhxGdG4lJrS3ll8eQI5Tvs0j+Wvb4x1lxGvnm/LgtrhW+FoEgrFuRKZWVga1bI1Qmg=="],
678+
"@gitbook/api": ["@gitbook/api@0.142.0", "", { "dependencies": { "event-iterator": "^2.0.0", "eventsource-parser": "^3.0.0" } }, "sha512-Lq1IbepAykHNG8y0fBvC7hQj3i/f1XATX58wLYXWCL3W1x6Z9f6Rs5K2qCOONswJh3l2NrX3ujrbxx3D8goRdw=="],
679679

680680
"@gitbook/browser-types": ["@gitbook/browser-types@workspace:packages/browser-types"],
681681

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"workspaces": {
3535
"packages": ["packages/*"],
3636
"catalog": {
37-
"@gitbook/api": "^0.141.0",
37+
"@gitbook/api": "^0.142.0",
3838
"bidc": "^0.0.2"
3939
}
4040
},

packages/gitbook/src/components/SiteSections/SiteSectionList.tsx

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { motion } from 'framer-motion';
55
import React from 'react';
66

77
import { type ClassValue, tcls } from '@/lib/tailwind';
8+
import { findSectionInGroup } from '@/lib/utils';
89
import { useToggleAnimation } from '../hooks';
910
import { Link } from '../primitives';
1011
import { ScrollContainer } from '../primitives/ScrollContainer';
@@ -15,7 +16,7 @@ import type {
1516
ClientSiteSections,
1617
} from './encodeClientSiteSections';
1718

18-
const MAX_ITEMS = 5; // If there are more sections than this, they'll be shown below the fold in a scrollview.
19+
const MAX_ITEMS = 6; // If there are more sections than this, they'll be shown below the fold in a scrollview.
1920

2021
/**
2122
* A list of items representing site sections for multi-section sites
@@ -72,8 +73,9 @@ export function SiteSectionListItem(props: {
7273
section: ClientSiteSection;
7374
isActive: boolean;
7475
className?: string;
76+
style?: React.CSSProperties;
7577
}) {
76-
const { section, isActive, className, ...otherProps } = props;
78+
const { section, isActive, className, style, ...otherProps } = props;
7779

7880
return (
7981
<Link
@@ -101,6 +103,7 @@ export function SiteSectionListItem(props: {
101103
: null,
102104
className
103105
)}
106+
style={style}
104107
{...otherProps}
105108
>
106109
<div
@@ -127,11 +130,12 @@ export function SiteSectionListItem(props: {
127130
export function SiteSectionGroupItem(props: {
128131
group: ClientSiteSectionGroup;
129132
currentSection: ClientSiteSection;
133+
level?: number;
130134
}) {
131-
const { group, currentSection } = props;
135+
const { group, currentSection, level = 0 } = props;
132136

133-
const hasDescendants = group.sections.length > 0;
134-
const isActiveGroup = group.sections.some((section) => section.id === currentSection.id);
137+
const hasDescendants = group.children.length > 0;
138+
const isActiveGroup = Boolean(findSectionInGroup(group, currentSection.id));
135139
const shouldOpen = hasDescendants && isActiveGroup;
136140
const [isOpen, setIsOpen] = React.useState(shouldOpen);
137141

@@ -161,7 +165,7 @@ export function SiteSectionGroupItem(props: {
161165
className={tcls(
162166
'flex size-8 shrink-0 items-center justify-center rounded-md straight-corners:rounded-none bg-tint-subtle text-lg text-tint leading-none shadow-tint shadow-xs ring-1 ring-tint-subtle transition-transform group-hover/section-link:scale-110 group-hover/section-link:ring-tint-hover group-active/section-link:scale-90 group-active/section-link:shadow-none contrast-more:text-tint-strong dark:shadow-none',
163167
isActiveGroup
164-
? 'bg-primary tint:bg-primary-solid text-primary tint:text-contrast-primary-solid shadow-md shadow-primary ring-primary group-hover/section-link:ring-primary-hover, contrast-more:text-primary-strong contrast-more:ring-2 contrast-more:ring-primary'
168+
? 'bg-primary text-primary shadow-md shadow-primary ring-primary group-hover/section-link:ring-primary-hover, contrast-more:text-primary-strong contrast-more:ring-2 contrast-more:ring-primary'
165169
: null
166170
)}
167171
>
@@ -216,14 +220,26 @@ export function SiteSectionGroupItem(props: {
216220
</button>
217221
{hasDescendants ? (
218222
<Descendants isVisible={isOpen}>
219-
{group.sections.map((section) => (
220-
<SiteSectionListItem
221-
section={section}
222-
isActive={section.id === currentSection.id}
223-
key={section.id}
224-
className="pl-5"
225-
/>
226-
))}
223+
{group.children.map((child) => {
224+
if (child.object === 'site-section') {
225+
return (
226+
<SiteSectionListItem
227+
section={child}
228+
isActive={child.id === currentSection.id}
229+
key={child.id}
230+
/>
231+
);
232+
}
233+
234+
return (
235+
<SiteSectionGroupItem
236+
group={child}
237+
currentSection={currentSection}
238+
key={child.id}
239+
level={level + 1}
240+
/>
241+
);
242+
})}
227243
</Descendants>
228244
) : null}
229245
</>
@@ -239,7 +255,7 @@ function Descendants(props: {
239255
return (
240256
<motion.div
241257
ref={scope}
242-
className={isVisible ? undefined : '[&_ul>li]:opacity-1'}
258+
className={isVisible ? 'pl-3' : 'pl-3 [&_ul>li]:opacity-1'}
243259
initial={isVisible ? show : hide}
244260
>
245261
{children}

0 commit comments

Comments
 (0)