-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(theme-classic): refactor TOC-related theme components (#7270)
* extract TOCItemTree component * refactor TOCCollapsible
- Loading branch information
Showing
7 changed files
with
143 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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 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
35 changes: 35 additions & 0 deletions
35
packages/docusaurus-theme-classic/src/theme/TOCCollapsible/CollapseButton.tsx
This file contains 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,35 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
import Translate from '@docusaurus/Translate'; | ||
import styles from './CollapseButton.module.css'; | ||
import type {Props} from '@theme/TOCCollapsible/CollapseButton'; | ||
|
||
export default function TOCCollapsibleCollapseButton({ | ||
collapsed, | ||
...props | ||
}: Props): JSX.Element { | ||
return ( | ||
<button | ||
type="button" | ||
{...props} | ||
className={clsx( | ||
'clean-btn', | ||
styles.tocCollapsibleButton, | ||
!collapsed && styles.tocCollapsibleButtonExpanded, | ||
props.className, | ||
)}> | ||
<Translate | ||
id="theme.TOCCollapsible.toggleButtonLabel" | ||
description="The label used by the button on the collapsible TOC component"> | ||
On this page | ||
</Translate> | ||
</button> | ||
); | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/docusaurus-theme-classic/src/theme/TOCCollapsible/index.module.css
This file contains 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,31 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
.tocCollapsible { | ||
background-color: var(--ifm-menu-color-background-active); | ||
border-radius: var(--ifm-global-radius); | ||
margin: 1rem 0; | ||
} | ||
|
||
.tocCollapsibleContent > ul { | ||
border-left: none; | ||
border-top: 1px solid var(--ifm-color-emphasis-300); | ||
padding: 0.2rem 0; | ||
font-size: 15px; | ||
} | ||
|
||
.tocCollapsibleContent ul li { | ||
margin: 0.4rem 0.8rem; | ||
} | ||
|
||
.tocCollapsibleContent a { | ||
display: block; | ||
} | ||
|
||
.tocCollapsibleExpanded { | ||
transform: none; | ||
} |
This file contains 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
46 changes: 46 additions & 0 deletions
46
packages/docusaurus-theme-classic/src/theme/TOCItems/Tree.tsx
This file contains 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,46 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import type {Props} from '@theme/TOCItems/Tree'; | ||
|
||
// Recursive component rendering the toc tree | ||
/* eslint-disable jsx-a11y/control-has-associated-label */ | ||
function TOCItemTree({ | ||
toc, | ||
className, | ||
linkClassName, | ||
isChild, | ||
}: Props): JSX.Element | null { | ||
if (!toc.length) { | ||
return null; | ||
} | ||
return ( | ||
<ul className={isChild ? undefined : className}> | ||
{toc.map((heading) => ( | ||
<li key={heading.id}> | ||
<a | ||
href={`#${heading.id}`} | ||
className={linkClassName ?? undefined} | ||
// Developer provided the HTML, so assume it's safe. | ||
// eslint-disable-next-line react/no-danger | ||
dangerouslySetInnerHTML={{__html: heading.value}} | ||
/> | ||
<TOCItemTree | ||
isChild | ||
toc={heading.children} | ||
className={className} | ||
linkClassName={linkClassName} | ||
/> | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
} | ||
|
||
// Memo only the tree root is enough | ||
export default React.memo(TOCItemTree); |
This file contains 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