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

fix(v2): useTOC hooks should not be called in each nested children #1959

Merged
merged 3 commits into from
Nov 11, 2019
Merged
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
23 changes: 13 additions & 10 deletions packages/docusaurus-theme-classic/src/theme/DocItem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ const LINK_CLASS_NAME = 'contents__link';
const ACTIVE_LINK_CLASS_NAME = 'contents__link--active';
const TOP_OFFSET = 100;

function Headings({headings, isChild}) {
function DocTOC({headings}) {
useTOCHighlight(LINK_CLASS_NAME, ACTIVE_LINK_CLASS_NAME, TOP_OFFSET);
return (
<div className="col col--3">
<div className={styles.tableOfContents}>
<Headings headings={headings} />
</div>
</div>
);
}

function Headings({headings, isChild}) {
if (!headings.length) return null;
return (
<ul className={isChild ? '' : 'contents contents__left-border'}>
Expand Down Expand Up @@ -55,7 +64,7 @@ function DocItem(props) {
const metaImageUrl = siteUrl + useBaseUrl(metaImage);

return (
<div>
<>
<Head>
{title && <title>{title}</title>}
{description && <meta name="description" content={description} />}
Expand Down Expand Up @@ -155,17 +164,11 @@ function DocItem(props) {
</div>
</div>
</div>
{DocContent.rightToc && (
<div className="col col--3">
<div className={styles.tableOfContents}>
<Headings headings={DocContent.rightToc} />
</div>
</div>
)}
{DocContent.rightToc && <DocTOC headings={DocContent.rightToc} />}
</div>
</div>
</div>
</div>
</>
);
}

Expand Down