From 5eba8786570f987cc5d2c12c5d04cd84bcc46720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Sun, 13 Apr 2025 10:02:36 +0200 Subject: [PATCH] wip --- .../PageAside/ScrollSectionsList.tsx | 24 ++-- .../gitbook/src/components/hooks/useHash.ts | 38 +++--- .../components/hooks/usePageActiveSections.ts | 128 ++++++++++++++++++ .../src/components/hooks/useScrollListener.ts | 2 +- .../src/components/hooks/useScrollPage.ts | 34 ++--- 5 files changed, 181 insertions(+), 45 deletions(-) create mode 100644 packages/gitbook/src/components/hooks/usePageActiveSections.ts diff --git a/packages/gitbook/src/components/PageAside/ScrollSectionsList.tsx b/packages/gitbook/src/components/PageAside/ScrollSectionsList.tsx index a2adf315e..82688786e 100644 --- a/packages/gitbook/src/components/PageAside/ScrollSectionsList.tsx +++ b/packages/gitbook/src/components/PageAside/ScrollSectionsList.tsx @@ -1,18 +1,19 @@ 'use client'; import { motion } from 'framer-motion'; -import React from 'react'; +import React, { useRef } from 'react'; -import { useScrollActiveId } from '@/components/hooks'; +// import { useScrollActiveId } from '@/components/hooks'; import type { DocumentSection } from '@/lib/document-sections'; import { tcls } from '@/lib/tailwind'; -import { HEADER_HEIGHT_DESKTOP } from '../layout'; +import { usePageActiveSections } from '../hooks/usePageActiveSections'; +// import { HEADER_HEIGHT_DESKTOP } from '../layout'; import { AsideSectionHighlight } from './AsideSectionHighlight'; /** * The threshold at which we consider a section as intersecting the viewport. */ -const SECTION_INTERSECTING_THRESHOLD = 0.9; +// const SECTION_INTERSECTING_THRESHOLD = 0.9; const springCurve = { type: 'spring', @@ -30,10 +31,13 @@ export function ScrollSectionsList(props: { sections: DocumentSection[] }) { }); }, [sections]); - const activeId = useScrollActiveId(ids, { - rootMargin: `-${HEADER_HEIGHT_DESKTOP}px 0px -40% 0px`, - threshold: SECTION_INTERSECTING_THRESHOLD, - }); + const windowRef = useRef(typeof window === 'undefined' ? null : window); + const activeIds = usePageActiveSections(ids, windowRef); + + // const activeId = useScrollActiveId(ids, { + // rootMargin: `-${HEADER_HEIGHT_DESKTOP}px 0px -40% 0px`, + // threshold: SECTION_INTERSECTING_THRESHOLD, + // }); return (