-
Couldn't load subscription status.
- Fork 4k
Add ScrollContainer component, use for section tabs #3661
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
zenoachtig
merged 9 commits into
main
from
zeno/rnd-8018-its-not-obvious-that-you-can-scroll-through-site-sections-on
Sep 17, 2025
+240
−33
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
66ce3e3
Refactor site section tabs
zenoachtig bb29763
Simplify
zenoachtig 19ec10e
Merge branch 'main' into refactor-section-tabs
zenoachtig 76862ab
Add dependency array to useEffect
zenoachtig b624da5
Add ScrollContainer component, use for section tabs
zenoachtig 1f47e18
Merge branch 'main' into zeno/rnd-8018-its-not-obvious-that-you-can-s…
zenoachtig 0a6d346
Merge branch 'main' into zeno/rnd-8018-its-not-obvious-that-you-can-s…
zenoachtig 528674d
Review & add to site sections list
zenoachtig e49fe8d
Update Header.tsx
zenoachtig 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 | ||
| --- | ||
|
|
||
| Add scrollcontainer component |
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
177 changes: 177 additions & 0 deletions
177
packages/gitbook/src/components/primitives/ScrollContainer.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,177 @@ | ||
| 'use client'; | ||
|
|
||
| import { tString, useLanguage } from '@/intl/client'; | ||
| import { tcls } from '@/lib/tailwind'; | ||
| import * as React from 'react'; | ||
| import { Button } from './Button'; | ||
|
|
||
| /** | ||
| * A container that encapsulates a scrollable area with usability features. | ||
| * - Faded edges when there is more content than the container can display. | ||
| * - Buttons to advance the scroll position. | ||
| * - Auto-scroll to the active item when it's initially active. | ||
| */ | ||
| export type ScrollContainerProps = { | ||
| children: React.ReactNode; | ||
| className?: string; | ||
|
|
||
| /** The direction of the scroll container. */ | ||
| orientation: 'horizontal' | 'vertical'; | ||
|
|
||
| /** The ID of the active item to scroll to. */ | ||
| activeId?: string; | ||
| } & React.HTMLAttributes<HTMLDivElement>; | ||
|
|
||
| export function ScrollContainer(props: ScrollContainerProps) { | ||
| const { children, className, orientation, activeId, ...rest } = props; | ||
|
|
||
| const containerRef = React.useRef<HTMLDivElement>(null); | ||
|
|
||
| const [scrollPosition, setScrollPosition] = React.useState(0); | ||
| const [scrollSize, setScrollSize] = React.useState(0); | ||
|
|
||
| const language = useLanguage(); | ||
|
|
||
| React.useEffect(() => { | ||
| const container = containerRef.current; | ||
| if (!container) { | ||
| return; | ||
| } | ||
|
|
||
| // Update scroll position on scroll using requestAnimationFrame | ||
| const scrollListener: EventListener = () => { | ||
| requestAnimationFrame(() => { | ||
| setScrollPosition( | ||
| orientation === 'horizontal' ? container.scrollLeft : container.scrollTop | ||
| ); | ||
| }); | ||
| }; | ||
| container.addEventListener('scroll', scrollListener); | ||
|
|
||
| // Update max scroll position using resize observer | ||
| const resizeObserver = new ResizeObserver((entries) => { | ||
| const containerEntry = entries.find((i) => i.target === containerRef.current); | ||
| if (containerEntry) { | ||
| setScrollSize( | ||
| orientation === 'horizontal' | ||
| ? containerEntry.target.scrollWidth - containerEntry.target.clientWidth - 1 | ||
| : containerEntry.target.scrollHeight - | ||
| containerEntry.target.clientHeight - | ||
| 1 | ||
| ); | ||
| } | ||
| }); | ||
| resizeObserver.observe(container); | ||
|
|
||
| return () => { | ||
| container.removeEventListener('scroll', scrollListener); | ||
| resizeObserver.disconnect(); | ||
| }; | ||
| }, [orientation]); | ||
|
|
||
| // Scroll to the active item | ||
| React.useEffect(() => { | ||
| const container = containerRef.current; | ||
| if (!container || !activeId) { | ||
| return; | ||
| } | ||
| const activeItem = container.querySelector(`#${CSS.escape(activeId)}`); | ||
| if (activeItem) { | ||
| activeItem.scrollIntoView({ | ||
| inline: 'center', | ||
| block: 'center', | ||
| }); | ||
| } | ||
| }, [activeId]); | ||
|
|
||
| const scrollFurther = () => { | ||
| const container = containerRef.current; | ||
| if (!container) { | ||
| return; | ||
| } | ||
| container.scrollTo({ | ||
| top: orientation === 'vertical' ? scrollPosition + container.clientHeight : undefined, | ||
| left: orientation === 'horizontal' ? scrollPosition + container.clientWidth : undefined, | ||
| behavior: 'smooth', | ||
| }); | ||
| }; | ||
|
|
||
| const scrollBack = () => { | ||
| const container = containerRef.current; | ||
| if (!container) { | ||
| return; | ||
| } | ||
| container.scrollTo({ | ||
| top: orientation === 'vertical' ? scrollPosition - container.clientHeight : undefined, | ||
| left: orientation === 'horizontal' ? scrollPosition - container.clientWidth : undefined, | ||
| behavior: 'smooth', | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <div | ||
| className={tcls('group/scroll-container relative flex overflow-hidden', className)} | ||
| {...rest} | ||
| > | ||
| {/* Scrollable content */} | ||
| <div | ||
| className={tcls( | ||
| 'flex shrink grow', | ||
| orientation === 'horizontal' ? 'no-scrollbar' : 'hide-scrollbar', | ||
| orientation === 'horizontal' ? 'overflow-x-scroll' : 'overflow-y-auto', | ||
| scrollPosition > 0 | ||
| ? orientation === 'horizontal' | ||
| ? 'mask-l-from-[calc(100%-2rem)]' | ||
| : 'mask-t-from-[calc(100%-2rem)]' | ||
| : '', | ||
| scrollPosition < scrollSize | ||
| ? orientation === 'horizontal' | ||
| ? 'mask-r-from-[calc(100%-2rem)]' | ||
| : 'mask-b-from-[calc(100%-2rem)]' | ||
| : '' | ||
| )} | ||
| ref={containerRef} | ||
| > | ||
| {children} | ||
| </div> | ||
|
|
||
| {/* Scroll buttons back & forward */} | ||
| <Button | ||
| icon={orientation === 'horizontal' ? 'chevron-left' : 'chevron-up'} | ||
| iconOnly | ||
| size="xsmall" | ||
| variant="secondary" | ||
| tabIndex={-1} | ||
| className={tcls( | ||
| orientation === 'horizontal' | ||
| ? '-translate-y-1/2! top-1/2 left-0 ml-2' | ||
| : '-translate-x-1/2! top-0 left-1/2 mt-2', | ||
| 'absolute not-pointer-none:block hidden scale-0 opacity-0 transition-[scale,opacity]', | ||
| scrollPosition > 0 | ||
| ? 'not-pointer-none:group-hover/scroll-container:scale-100 not-pointer-none:group-hover/scroll-container:opacity-11' | ||
| : 'pointer-events-none' | ||
| )} | ||
| onClick={scrollBack} | ||
| label={tString(language, 'scroll_back')} | ||
| /> | ||
| <Button | ||
| icon={orientation === 'horizontal' ? 'chevron-right' : 'chevron-down'} | ||
| iconOnly | ||
| size="xsmall" | ||
| variant="secondary" | ||
| tabIndex={-1} | ||
| className={tcls( | ||
| orientation === 'horizontal' | ||
| ? '-translate-y-1/2! top-1/2 right-0 mr-2' | ||
| : '-translate-x-1/2! bottom-0 left-1/2 mb-2', | ||
| 'absolute not-pointer-none:block hidden scale-0 transition-[scale,opacity]', | ||
| scrollPosition < scrollSize | ||
| ? 'not-pointer-none:group-hover/scroll-container:scale-100 not-pointer-none:group-hover/scroll-container:opacity-11' | ||
zenoachtig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| : 'pointer-events-none' | ||
| )} | ||
| onClick={scrollFurther} | ||
| label={tString(language, 'scroll_further')} | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
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
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
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
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
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
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.