Skip to content

Commit 185cdb4

Browse files
authored
Fix scrolling to anchor positionning (#3521)
1 parent 2cdba53 commit 185cdb4

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

.changeset/rude-wombats-worry.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 scrolling to anchor positionning

packages/gitbook/src/components/DocumentView/Heading.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function Heading(props: BlockProps<DocumentBlockHeading>) {
2626
'heading',
2727
'flex',
2828
'items-baseline',
29-
'scroll-m-12',
29+
'scroll-mt-(--content-scroll-margin)',
3030
getTextAlignment(block.data.align),
3131
hashLinkButtonWrapperStyles,
3232
style,

packages/gitbook/src/components/SitePage/PageClientLayout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { useScrollPage } from '@/components/hooks';
88
/**
99
* Client component to initialize interactivity for a page.
1010
*/
11-
export function PageClientLayout(props: { withSections?: boolean }) {
11+
export function PageClientLayout() {
1212
// We use this hook in the page layout to ensure the elements for the blocks
1313
// are rendered before we scroll to a hash or to the top of the page
14-
useScrollPage({ scrollMarginTop: props.withSections ? 108 : 64 });
14+
useScrollPage();
1515

1616
useStripFallbackQueryParam();
1717
return null;

packages/gitbook/src/components/SitePage/SitePage.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getPagePath } from '@/lib/pages';
1111
import { isPageIndexable, isSiteIndexable } from '@/lib/seo';
1212

1313
import { getResizedImageURL } from '@/lib/images';
14+
import { tcls } from '@/lib/tailwind';
1415
import { PageContextProvider } from '../PageContext';
1516
import { PageClientLayout } from './PageClientLayout';
1617
import { type PagePathParams, fetchPageData, getPathnameParam } from './fetch';
@@ -70,7 +71,14 @@ export async function SitePage(props: SitePageProps) {
7071
<PageCover as="full" page={page} cover={page.cover} context={context} />
7172
) : null}
7273
{/* We use a flex row reverse to render the aside first because the page is streamed. */}
73-
<div className="flex grow flex-row-reverse justify-end">
74+
<div
75+
className={tcls(
76+
'flex grow flex-row-reverse justify-end',
77+
withSections
78+
? '[--content-scroll-margin:calc(var(--spacing)*27)]'
79+
: '[--content-scroll-margin:calc(var(--spacing)*16)]'
80+
)}
81+
>
7482
<PageAside
7583
page={page}
7684
document={document}
@@ -88,7 +96,7 @@ export async function SitePage(props: SitePageProps) {
8896
/>
8997
</div>
9098
<React.Suspense fallback={null}>
91-
<PageClientLayout withSections={withSections} />
99+
<PageClientLayout />
92100
</React.Suspense>
93101
</PageContextProvider>
94102
);

packages/gitbook/src/components/hooks/useScrollPage.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ import { useHash } from './useHash';
1010
* to the top of the page when navigating between pages (pathname)
1111
* or sections of a page (hash).
1212
*/
13-
export function useScrollPage(props: { scrollMarginTop?: number }) {
13+
export function useScrollPage() {
1414
const hash = useHash();
1515
const pathname = usePathname();
1616
React.useLayoutEffect(() => {
1717
if (hash) {
1818
const element = document.getElementById(hash);
1919
if (element) {
20-
if (props.scrollMarginTop) {
21-
element.style.scrollMarginTop = `${props.scrollMarginTop}px`;
22-
}
2320
element.scrollIntoView({
2421
block: 'start',
2522
behavior: 'smooth',
@@ -36,5 +33,5 @@ export function useScrollPage(props: { scrollMarginTop?: number }) {
3633
}
3734
}
3835
};
39-
}, [hash, pathname, props.scrollMarginTop]);
36+
}, [hash, pathname]);
4037
}

0 commit comments

Comments
 (0)