-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
73892f4
commit 042b850
Showing
4 changed files
with
86 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'gitbook': patch | ||
--- | ||
|
||
Automatically scroll to active item in TOC |
70 changes: 70 additions & 0 deletions
70
packages/gitbook/src/components/TableOfContents/TOCScroller.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
import { ClassValue, tcls } from '@/lib/tailwind'; | ||
|
||
const TOCScrollContainerRefContext = React.createContext<React.RefObject<HTMLDivElement> | null>( | ||
null, | ||
); | ||
|
||
function useTOCScrollContainerRefContext() { | ||
const ctx = React.useContext(TOCScrollContainerRefContext); | ||
if (!ctx) { | ||
throw new Error('Context `TOCScrollContainerRefContext` must be used within Provider'); | ||
} | ||
return ctx; | ||
} | ||
|
||
export function TOCScrollContainer(props: { children: React.ReactNode; className?: ClassValue }) { | ||
const { children, className } = props; | ||
const scrollContainerRef = React.createRef<HTMLDivElement>(); | ||
|
||
return ( | ||
<TOCScrollContainerRefContext.Provider value={scrollContainerRef}> | ||
<div ref={scrollContainerRef} className={tcls(className)}> | ||
{children} | ||
</div> | ||
</TOCScrollContainerRefContext.Provider> | ||
); | ||
} | ||
|
||
// Offset to scroll the table of contents item by. | ||
const TOC_ITEM_OFFSET = 200; | ||
|
||
/** | ||
* Scrolls the table of contents container to the page item when it becomes active | ||
*/ | ||
export function useScrollToActiveTOCItem(props: { | ||
isActive: boolean; | ||
linkRef: React.RefObject<HTMLAnchorElement>; | ||
}) { | ||
const { isActive, linkRef } = props; | ||
const scrollContainerRef = useTOCScrollContainerRefContext(); | ||
const isScrolled = React.useRef(false); | ||
React.useLayoutEffect(() => { | ||
if (!isActive) { | ||
isScrolled.current = false; | ||
return; | ||
} | ||
if (isScrolled.current) { | ||
return; | ||
} | ||
const tocItem = linkRef.current; | ||
const tocContainer = scrollContainerRef.current; | ||
if (!tocItem || !tocContainer || !isOutOfView(tocItem, tocContainer)) { | ||
return; | ||
} | ||
tocContainer?.scrollTo({ | ||
top: tocItem.offsetTop - TOC_ITEM_OFFSET, | ||
}); | ||
isScrolled.current = true; | ||
}, [isActive, linkRef, scrollContainerRef]); | ||
} | ||
|
||
function isOutOfView(tocItem: HTMLElement, tocContainer: HTMLElement) { | ||
const tocItemTop = tocItem.offsetTop; | ||
const containerTop = tocContainer.scrollTop; | ||
const containerBottom = containerTop + tocContainer.clientHeight; | ||
return tocItemTop < containerTop || tocItemTop > containerBottom; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters