Skip to content

Commit dcd0355

Browse files
committed
Update TOC to use normal scrollcontainer
1 parent 9281b5e commit dcd0355

File tree

9 files changed

+28
-144
lines changed

9 files changed

+28
-144
lines changed

packages/gitbook/src/components/AIChat/AIChat.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function AIChat() {
8383
data-testid="ai-chat"
8484
withScrim={true}
8585
className={tcls(
86-
'ai-chat mx-auto not-hydrated:hidden w-96 max-w-full pl-8 transition-[width] duration-300 ease-quint lg:max-xl:w-80'
86+
'ai-chat mx-auto ml-8 not-hydrated:hidden w-96 transition-[width] duration-300 ease-quint lg:max-xl:w-80'
8787
)}
8888
>
8989
<EmbeddableFrame className="relative shrink-0 border-tint-subtle border-l to-tint-base">
@@ -226,7 +226,7 @@ export function AIChatBody(props: {
226226
contentClassName="p-4 gutter-stable flex flex-col gap-4"
227227
orientation="vertical"
228228
fadeEdges={['leading']}
229-
active={`message-group-${chat.messages.filter((message) => message.role === 'user').length - 1}`}
229+
active={`#message-group-${chat.messages.filter((message) => message.role === 'user').length - 1}`}
230230
>
231231
{isEmpty ? (
232232
<div className="flex grow flex-col">

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function SiteSectionList(props: { sections: ClientSiteSections; className
4040
orientation="vertical"
4141
style={{ maxHeight: `${MAX_ITEMS * 3 + 2}rem` }}
4242
className="pb-4"
43-
active={currentSection.id}
43+
active={`#${currentSection.id}`}
4444
>
4545
<div className="flex w-full flex-col px-2">
4646
{sectionsAndGroups.map((item) => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function SiteSectionTabs(props: {
7676
? 'md:-mr-8 -mr-4 sm:-mr-6'
7777
: 'after:contents[] after:absolute after:inset-y-2 after:right-0 after:border-transparent after:border-r after:transition-colors'
7878
)}
79-
active={currentSection.id}
79+
active={`#${currentSection.id}`}
8080
trailingEdgeScrollClassName={children ? 'after:border-tint' : ''}
8181
>
8282
<NavigationMenu.List

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

Lines changed: 0 additions & 91 deletions
This file was deleted.

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { SiteInsightsTrademarkPlacement } from '@gitbook/api';
33
import type React from 'react';
44

55
import { tcls } from '@/lib/tailwind';
6+
import { ScrollContainer } from '../primitives/ScrollContainer';
67
import { SideSheet } from '../primitives/SideSheet';
78
import { PagesList } from './PagesList';
8-
import { TOCScrollContainer } from './TOCScroller';
99
import { TableOfContentsScript } from './TableOfContentsScript';
1010
import { Trademark } from './Trademark';
1111
import { encodeClientTableOfContents } from './encodeClientTableOfContents';
@@ -74,7 +74,7 @@ export async function TableOfContents(props: {
7474
'lg:page-no-toc:[html[style*="--outline-top-offset"]_&]:top-(--outline-top-offset)!',
7575
'lg:page-no-toc:[html[style*="--outline-height"]_&]:top-(--outline-height)!',
7676

77-
'py-6',
77+
'pt-6 pb-4',
7878
'lg:sidebar-filled:pr-6',
7979
'lg:page-no-toc:pr-0',
8080
'max-lg:pl-8',
@@ -106,21 +106,24 @@ export async function TableOfContents(props: {
106106
)}
107107
>
108108
{innerHeader ? innerHeader : null}
109-
<TOCScrollContainer // The scrollview inside the sidebar
110-
className="hide-scrollbar gutter-stable flex grow flex-col overflow-y-auto p-2"
109+
<ScrollContainer
110+
fadeEdges={['trailing']}
111+
orientation="vertical"
112+
contentClassName="flex grow flex-col p-2 gutter-stable"
113+
active="[data-active=true]"
111114
>
112115
<PagesList
113116
pages={pages}
114117
isRoot={true}
115-
style="mb-5 page-no-toc:hidden grow border-tint-subtle sidebar-list-line:border-l"
118+
style="page-no-toc:hidden grow border-tint-subtle sidebar-list-line:border-l"
116119
/>
117-
{customization.trademark.enabled ? (
118-
<Trademark
119-
context={context}
120-
placement={SiteInsightsTrademarkPlacement.Sidebar}
121-
/>
122-
) : null}
123-
</TOCScrollContainer>
120+
</ScrollContainer>
121+
{customization.trademark.enabled ? (
122+
<Trademark
123+
context={context}
124+
placement={SiteInsightsTrademarkPlacement.Sidebar}
125+
/>
126+
) : null}
124127
</div>
125128
</SideSheet>
126129
<TableOfContentsScript />

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { tcls } from '@/lib/tailwind';
88

99
import { useCurrentPagePath } from '../hooks';
1010
import { Link, type LinkInsightsProps, type LinkProps } from '../primitives';
11-
import { useScrollToActiveTOCItem } from './TOCScroller';
1211

1312
/**
1413
* Client component for a page document to toggle its children and be marked as active.
@@ -81,8 +80,6 @@ function LinkItem(
8180
}
8281
) {
8382
const { isActive, href, insights, children, onActiveClick } = props;
84-
const anchorRef = useRef<HTMLAnchorElement>(null);
85-
useScrollToActiveTOCItem({ anchorRef, isActive });
8683

8784
const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
8885
if (isActive && onActiveClick) {
@@ -93,7 +90,7 @@ function LinkItem(
9390

9491
return (
9592
<Link
96-
ref={anchorRef}
93+
data-active={isActive}
9794
href={href}
9895
insights={insights}
9996
aria-current={isActive ? 'page' : undefined}

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

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,7 @@ export function Trademark(props: {
1717
}) {
1818
const { className, ...rest } = props;
1919
return (
20-
<div
21-
className={tcls(
22-
'sticky',
23-
'z-2',
24-
25-
'mt-auto',
26-
'inset-x-0',
27-
'-bottom-2',
28-
'-mb-2',
29-
'sidebar-filled:pb-2',
30-
'page-no-toc:pb-0',
31-
32-
'pointer-events-none',
33-
34-
'bg-tint-base',
35-
'sidebar-filled:bg-tint-subtle',
36-
'theme-muted:bg-tint-subtle',
37-
'[html.sidebar-filled.theme-muted_&]:bg-tint-base',
38-
'[html.sidebar-filled.theme-bold.tint_&]:bg-tint-base',
39-
'lg:page-no-toc:bg-transparent',
40-
41-
'rounded-lg',
42-
'straight-corners:rounded-none',
43-
'circular-corners:rounded-2xl',
44-
className
45-
)}
46-
>
20+
<div className={tcls('mt-auto pr-4 pb-2 pl-2', className)}>
4721
<TrademarkLink {...rest} />
4822
</div>
4923
);
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export { TableOfContents } from './TableOfContents';
22
export { PagesList } from './PagesList';
3-
export { TOCScrollContainer } from './TOCScroller';
43
export { Trademark } from './Trademark';

packages/gitbook/src/components/primitives/ScrollContainer.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ export function ScrollContainer(props: ScrollContainerProps) {
102102
return;
103103
}
104104
const activeItem =
105-
typeof active === 'string' ? document.getElementById(active) : active.current;
105+
typeof active === 'string'
106+
? containerRef.current?.querySelector(active)
107+
: active.current;
106108
if (!activeItem || !container.contains(activeItem)) {
107109
return;
108110
}
@@ -180,7 +182,7 @@ export function ScrollContainer(props: ScrollContainerProps) {
180182
orientation === 'horizontal'
181183
? '-translate-y-1/2! top-1/2 left-0 ml-2'
182184
: '-translate-x-1/2! top-0 left-1/2 mt-2',
183-
'absolute not-pointer-none:block hidden scale-0 opacity-0 transition-[scale,opacity]',
185+
'absolute z-10 not-pointer-none:block hidden scale-0 opacity-0 transition-[scale,opacity]',
184186
scrollPosition > 0
185187
? 'not-pointer-none:group-hover/scroll-container:scale-100 not-pointer-none:group-hover/scroll-container:opacity-11'
186188
: 'pointer-events-none'
@@ -199,7 +201,7 @@ export function ScrollContainer(props: ScrollContainerProps) {
199201
orientation === 'horizontal'
200202
? '-translate-y-1/2! top-1/2 right-0 mr-2'
201203
: '-translate-x-1/2! bottom-0 left-1/2 mb-2',
202-
'absolute not-pointer-none:block hidden scale-0 transition-[scale,opacity]',
204+
'absolute z-10 not-pointer-none:block hidden scale-0 transition-[scale,opacity]',
203205
scrollPosition < scrollSize
204206
? 'not-pointer-none:group-hover/scroll-container:scale-100 not-pointer-none:group-hover/scroll-container:opacity-11'
205207
: 'pointer-events-none'
@@ -214,7 +216,7 @@ export function ScrollContainer(props: ScrollContainerProps) {
214216
/**
215217
* Scroll to an element in a container.
216218
*/
217-
function scrollToElementInContainer(element: HTMLElement, container: HTMLElement) {
219+
function scrollToElementInContainer(element: Element, container: HTMLElement) {
218220
const containerRect = container.getBoundingClientRect();
219221
const rect = element.getBoundingClientRect();
220222

@@ -229,6 +231,6 @@ function scrollToElementInContainer(element: HTMLElement, container: HTMLElement
229231
(rect.left - containerRect.left) -
230232
container.clientWidth / 2 +
231233
rect.width / 2,
232-
behavior: 'smooth',
234+
behavior: 'auto',
233235
});
234236
}

0 commit comments

Comments
 (0)