Skip to content
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

feat(theme-classic): reintroduce autoscrolling TOC #6666

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ export default function TOCItems({
linkActiveClassName,
minHeadingLevel,
maxHeadingLevel,
autoScrollTOC: themeConfig.autoScrollTOC,
};
}
return undefined;
}, [linkClassName, linkActiveClassName, minHeadingLevel, maxHeadingLevel]);
}, [
linkClassName,
linkActiveClassName,
minHeadingLevel,
maxHeadingLevel,
themeConfig,
]);
useTOCHighlight(tocHighlightConfig);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const DEFAULT_CONFIG = {
minHeadingLevel: 2,
maxHeadingLevel: 3,
},
autoScrollTOC: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep this undocumented for now, probably not useful for most people, as their sites don't work because of broken CSS in the first place...

};

const NavbarItemPosition = Joi.string().equal('left', 'right').default('left');
Expand Down Expand Up @@ -349,6 +350,7 @@ export const ThemeConfigSchema = Joi.object({
autoCollapseSidebarCategories: Joi.bool().default(
DEFAULT_CONFIG.autoCollapseSidebarCategories,
),
autoScrollTOC: Joi.bool().default(DEFAULT_CONFIG.autoScrollTOC),
sidebarCollapsible: Joi.forbidden().messages({
'any.unknown':
'The themeConfig.sidebarCollapsible has been moved to docs plugin options. See: https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-docs',
Expand Down
51 changes: 37 additions & 14 deletions packages/docusaurus-theme-common/src/hooks/useTOCHighlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export type TOCHighlightConfig = {
minHeadingLevel: number;
/** @see {@link TOCHighlightConfig.minHeadingLevel} */
maxHeadingLevel: number;
autoScrollTOC: boolean;
};

/**
Expand All @@ -138,6 +139,8 @@ export function useTOCHighlight(config: TOCHighlightConfig | undefined): void {

const anchorTopOffsetRef = useAnchorTopOffsetRef();

const cancelScroll = useRef<() => void>(() => {});

useEffect(() => {
if (!config) {
// no-op, highlighting is disabled
Expand All @@ -149,19 +152,30 @@ export function useTOCHighlight(config: TOCHighlightConfig | undefined): void {
linkActiveClassName,
minHeadingLevel,
maxHeadingLevel,
autoScrollTOC,
} = config;

function updateLinkActiveClass(link: HTMLAnchorElement, active: boolean) {
if (active) {
if (lastActiveLinkRef.current && lastActiveLinkRef.current !== link) {
lastActiveLinkRef.current?.classList.remove(linkActiveClassName);
}
link.classList.add(linkActiveClassName);
lastActiveLinkRef.current = link;
// link.scrollIntoView({block: 'nearest'});
} else {
link.classList.remove(linkActiveClassName);
// When the page is scrolling (either a user scroll or smooth CSS scroll),
// We need to defer the TOC scroll or the two concurrent scrolls would block
// each other.
function scheduleScroll(link: HTMLAnchorElement) {
if (!autoScrollTOC) {
return () => {};
}
const handle = setTimeout(() => {
const linkRect = link.getBoundingClientRect();
const viewport = window.visualViewport;
// Only scroll if a vertical scroll is sufficient to bring the link into
// view. e.g. if the window is pinch-zoomed, we should not horizontally
// scroll
if (
linkRect.right <= viewport.pageLeft + viewport.width &&
linkRect.left >= viewport.pageLeft
) {
link.scrollIntoView({block: 'nearest', behavior: 'smooth'});
}
}, 200);
return () => clearTimeout(handle);
}

function updateActiveLink() {
Expand All @@ -170,12 +184,21 @@ export function useTOCHighlight(config: TOCHighlightConfig | undefined): void {
const activeAnchor = getActiveAnchor(anchors, {
anchorTopOffset: anchorTopOffsetRef.current,
});
const activeLink = links.find(
(link) => activeAnchor && activeAnchor.id === getLinkAnchorValue(link),
);

links.forEach((link) => {
updateLinkActiveClass(link, link === activeLink);
const isActive = activeAnchor?.id === getLinkAnchorValue(link);
if (isActive) {
if (lastActiveLinkRef.current !== link) {
lastActiveLinkRef.current?.classList.remove(linkActiveClassName);
}
link.classList.add(linkActiveClassName);
lastActiveLinkRef.current = link;
// Cancel the last scheduled TOC scroll, and start a new timeout
cancelScroll.current();
cancelScroll.current = scheduleScroll(link);
} else {
link.classList.remove(linkActiveClassName);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export type ThemeConfig = {
footer?: Footer;
hideableSidebar: boolean;
autoCollapseSidebarCategories: boolean;
autoScrollTOC: boolean;
image?: string;
metadata: Array<{[key: string]: string}>;
sidebarCollapsible: boolean;
Expand Down
2 changes: 2 additions & 0 deletions website/_dogfooding/_docs tests/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ tags: [a, b, c, some tag]
# Docs tests

This Docusaurus docs plugin instance is meant to test fancy edge-cases that regular unit tests don't really cover.

[Test link](./folder%20with%20space/doc%201.md)
Loading