website: update swizzled items

This commit is contained in:
Yangshun
2021-10-19 02:44:22 +08:00
parent 77e5cd2f42
commit 69674e5367
4 changed files with 65 additions and 61 deletions

View File

@ -6,50 +6,25 @@
*/
import React from 'react';
import clsx from 'clsx';
import useTOCHighlight from '@theme/hooks/useTOCHighlight';
import styles from './styles.module.css';
import TOCItems from '@theme/TOCItems';
import styles from './styles.module.css'; // Using a custom className
import SidebarAd from '../../components/SidebarAd';
// This prevents TOC highlighting to highlight TOCInline/TOCCollapsible by mistake
const LINK_CLASS_NAME = 'table-of-contents__link';
const ACTIVE_LINK_CLASS_NAME = 'table-of-contents__link--active';
const TOP_OFFSET = 100;
/* eslint-disable jsx-a11y/control-has-associated-label */
export function TOCHeadings({toc, isChild}) {
if (!toc.length) {
return null;
}
const LINK_CLASS_NAME = 'table-of-contents__link toc-highlight';
const LINK_ACTIVE_CLASS_NAME = 'table-of-contents__link--active';
function TOC({className, ...props}) {
return (
<ul
className={
isChild ? '' : 'table-of-contents table-of-contents__left-border'
}>
{toc.map((heading) => (
<li key={heading.id}>
<a
href={`#${heading.id}`}
className={LINK_CLASS_NAME} // Developer provided the HTML, so assume it's safe.
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: heading.value,
}}
/>
<TOCHeadings isChild toc={heading.children} />
</li>
))}
</ul>
);
}
function TOC({toc}) {
useTOCHighlight(LINK_CLASS_NAME, ACTIVE_LINK_CLASS_NAME, TOP_OFFSET);
return (
<div className={clsx(styles.tableOfContents, 'thin-scrollbar')}>
<div className={clsx(styles.tableOfContents, 'thin-scrollbar', className)}>
<div className="margin--md">
<SidebarAd />
</div>
<TOCHeadings toc={toc} />
<TOCItems
{...props}
linkClassName={LINK_CLASS_NAME}
linkActiveClassName={LINK_ACTIVE_CLASS_NAME}
/>
</div>
);
}