-
Notifications
You must be signed in to change notification settings - Fork 4k
Automatically scroll to the active page in table of contents #2425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
spastorelli
merged 13 commits into
main
from
steeve/rnd-2147-automatically-scroll-to-the-current-page-in-table-of
Aug 15, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1266276
Add hook and provider
spastorelli f039a84
Add hook to link item and provider to table of content
spastorelli 304e7c3
Add changeset
spastorelli 41a8719
Merge branch 'main' into steeve/rnd-2147-automatically-scroll-to-the-…
spastorelli 377cf34
review: use scrollintoview
spastorelli 1d59778
bun format
spastorelli 90e1e78
remove id from scroll container
spastorelli 077004a
Merge branch 'main' into steeve/rnd-2147-automatically-scroll-to-the-…
spastorelli 71e4afb
revert back to scrollTo
spastorelli 13eadeb
Merge branch 'main' into steeve/rnd-2147-automatically-scroll-to-the-…
spastorelli 1302f6e
review
spastorelli ced8b6d
Fix lint
spastorelli 4f1253f
Merge branch 'main' into steeve/rnd-2147-automatically-scroll-to-the-…
spastorelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 page in table of contents |
This file contains hidden or 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 hidden or 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
60 changes: 60 additions & 0 deletions
60
packages/gitbook/src/components/TableOfContents/useScrollToActiveTOCItem.tsx
This file contains hidden or 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,60 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
const TOCScrollContainerContext = React.createContext<React.RefObject<HTMLDivElement> | null>(null); | ||
|
||
export function TOCScrollContainerProvider( | ||
props: React.PropsWithChildren<{ | ||
className: React.HTMLAttributes<HTMLDivElement>['className']; | ||
}>, | ||
) { | ||
const { className, children } = props; | ||
const scrollContainerRef = React.createRef<HTMLDivElement>(); | ||
|
||
return ( | ||
<TOCScrollContainerContext.Provider value={scrollContainerRef}> | ||
<div ref={scrollContainerRef} className={className}> | ||
{children} | ||
</div> | ||
</TOCScrollContainerContext.Provider> | ||
); | ||
} | ||
|
||
// Offset to scroll the table of contents item by. | ||
const TOC_ITEM_OFFSET = 200; | ||
spastorelli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Scrolls the table of contents container to the page item when it becomes active, | ||
* but only if the item is outside the viewable area of the container. | ||
*/ | ||
export function useScrollToActiveTOCItem(tocItem: { | ||
isActive: boolean; | ||
linkRef: React.RefObject<HTMLAnchorElement>; | ||
}) { | ||
const { isActive, linkRef } = tocItem; | ||
|
||
const scrollContainerRef = React.useContext(TOCScrollContainerContext); | ||
React.useLayoutEffect(() => { | ||
if (isActive && linkRef.current && scrollContainerRef?.current) { | ||
const tocItem = linkRef.current; | ||
const tocContainer = scrollContainerRef.current; | ||
|
||
if (tocContainer) { | ||
const tocItemTop = tocItem.offsetTop; | ||
const containerTop = tocContainer.scrollTop; | ||
const containerBottom = containerTop + tocContainer.clientHeight; | ||
|
||
// Only scroll if the TOC item is outside the viewable area of the container | ||
if ( | ||
tocItemTop < containerTop + TOC_ITEM_OFFSET || | ||
tocItemTop > containerBottom - TOC_ITEM_OFFSET | ||
) { | ||
tocContainer.scrollTo({ | ||
top: tocItemTop - TOC_ITEM_OFFSET, | ||
}); | ||
} | ||
} | ||
} | ||
}, [isActive, linkRef, scrollContainerRef]); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.