Skip to content

Commit f7fa8c6

Browse files
committed
Foramtting
1 parent 26f387f commit f7fa8c6

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

packages/gitbook/src/components/TableOfContents/TOCScroller.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ export function useScrollToActiveTOCItem(tocItem: {
3737

3838
const hash = useHash();
3939
const scrollContainerRef = React.useContext(TOCScrollContainerContext);
40+
const isScrolled = React.useRef(false);
4041

41-
React.useLayoutEffect(() => {
42-
if (!isActive) { return; }
43-
42+
const isOutOfView = React.useCallback(() => {
4443
if (linkRef.current && scrollContainerRef?.current) {
4544
const tocItem = linkRef.current;
4645
const tocContainer = scrollContainerRef.current;
@@ -49,17 +48,31 @@ export function useScrollToActiveTOCItem(tocItem: {
4948
const containerTop = tocContainer.scrollTop;
5049
const containerBottom = containerTop + tocContainer.clientHeight;
5150

52-
// Only scroll if the TOC item is outside the viewable area of the container
53-
if (
51+
return (
5452
tocItemTop < containerTop + TOC_ITEM_OFFSET ||
5553
tocItemTop > containerBottom - TOC_ITEM_OFFSET
56-
) {
57-
tocItem.scrollIntoView({
58-
behavior: 'instant', // using instant as smooth can interrupt or get interrupted by other `scrollIntoView` changes
59-
block: 'center',
60-
});
61-
}
54+
);
55+
}
56+
return false;
57+
}, [linkRef, scrollContainerRef]);
58+
59+
React.useLayoutEffect(() => {
60+
if (!isActive) {
61+
isScrolled.current = false;
62+
return;
63+
}
64+
65+
if (!isOutOfView() || isScrolled.current) {
66+
return;
6267
}
68+
69+
const tocItem = linkRef.current;
70+
console.log({ isActive, scrollToView: tocItem?.textContent });
71+
tocItem?.scrollIntoView({
72+
behavior: 'instant', // using instant as smooth can interrupt or get interrupted by other `scrollIntoView` changes
73+
block: 'center',
74+
});
75+
isScrolled.current = true;
6376
// We've included `hash` from `useHash` hook as a dependency so we trigger the effect in response to changes to the url hash
64-
}, [isActive, hash, linkRef, scrollContainerRef]);
77+
}, [hash, isActive, isOutOfView, linkRef]);
6578
}

packages/gitbook/src/components/TableOfContents/ToggleableLinkItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export function ToggleableLinkItem(props: {
4949
const [scope, animate] = useAnimate();
5050
const [isVisible, setIsVisible] = React.useState(hasActiveDescendant);
5151
const isMounted = useIsMounted();
52+
5253
// Update the visibility of the children, if we are navigating to a descendant.
5354
React.useEffect(() => {
5455
if (!hasDescendants) {
@@ -64,7 +65,6 @@ export function ToggleableLinkItem(props: {
6465
if (!isMounted || !hasDescendants) {
6566
return;
6667
}
67-
6868
try {
6969
animate(scope.current, isVisible ? show : hide, {
7070
duration: 0.1,
@@ -89,7 +89,7 @@ export function ToggleableLinkItem(props: {
8989
}, [isVisible, isMounted, hasDescendants, animate, scope]);
9090

9191
const linkRef = React.createRef<HTMLAnchorElement>();
92-
useScrollToActiveTOCItem({ linkRef, isActive: isMounted && isActive });
92+
useScrollToActiveTOCItem({ linkRef, isActive });
9393

9494
return (
9595
<div>

0 commit comments

Comments
 (0)