-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split and cleanup theme/DocPage (#7006)
- Loading branch information
Showing
10 changed files
with
299 additions
and
163 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
100 changes: 100 additions & 0 deletions
100
packages/docusaurus-theme-classic/src/theme/DocPage/Layout/Aside.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,100 @@ | ||
/** | ||
* 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, {type ReactNode, useState, useCallback} from 'react'; | ||
import DocSidebar from '@theme/DocSidebar'; | ||
import IconArrow from '@theme/IconArrow'; | ||
import {translate} from '@docusaurus/Translate'; | ||
import {useLocation} from '@docusaurus/router'; | ||
import type {Props} from '@theme/DocPage/Layout/Aside'; | ||
|
||
import clsx from 'clsx'; | ||
import styles from './styles.module.css'; | ||
|
||
import {ThemeClassNames, useDocsSidebar} from '@docusaurus/theme-common'; | ||
|
||
function SidebarExpandButton({toggleSidebar}: {toggleSidebar: () => void}) { | ||
return ( | ||
<div | ||
className={styles.collapsedDocSidebar} | ||
title={translate({ | ||
id: 'theme.docs.sidebar.expandButtonTitle', | ||
message: 'Expand sidebar', | ||
description: | ||
'The ARIA label and title attribute for expand button of doc sidebar', | ||
})} | ||
aria-label={translate({ | ||
id: 'theme.docs.sidebar.expandButtonAriaLabel', | ||
message: 'Expand sidebar', | ||
description: | ||
'The ARIA label and title attribute for expand button of doc sidebar', | ||
})} | ||
tabIndex={0} | ||
role="button" | ||
onKeyDown={toggleSidebar} | ||
onClick={toggleSidebar}> | ||
<IconArrow className={styles.expandSidebarButtonIcon} /> | ||
</div> | ||
); | ||
} | ||
|
||
// Reset sidebar state when sidebar changes | ||
// Use React key to unmount/remount the children | ||
// See https://github.com/facebook/docusaurus/issues/3414 | ||
function ResetOnSidebarChange({children}: {children: ReactNode}) { | ||
const sidebar = useDocsSidebar(); | ||
return ( | ||
<React.Fragment key={sidebar?.name ?? 'noSidebar'}> | ||
{children} | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
export default function DocPageLayoutAside({ | ||
sidebar, | ||
hiddenSidebarContainer, | ||
setHiddenSidebarContainer, | ||
}: Props): JSX.Element { | ||
const {pathname} = useLocation(); | ||
|
||
const [hiddenSidebar, setHiddenSidebar] = useState(false); | ||
const toggleSidebar = useCallback(() => { | ||
if (hiddenSidebar) { | ||
setHiddenSidebar(false); | ||
} | ||
setHiddenSidebarContainer((value) => !value); | ||
}, [setHiddenSidebarContainer, hiddenSidebar]); | ||
|
||
return ( | ||
<aside | ||
className={clsx( | ||
ThemeClassNames.docs.docSidebarContainer, | ||
styles.docSidebarContainer, | ||
hiddenSidebarContainer && styles.docSidebarContainerHidden, | ||
)} | ||
onTransitionEnd={(e) => { | ||
if (!e.currentTarget.classList.contains(styles.docSidebarContainer!)) { | ||
return; | ||
} | ||
|
||
if (hiddenSidebarContainer) { | ||
setHiddenSidebar(true); | ||
} | ||
}}> | ||
<ResetOnSidebarChange> | ||
<DocSidebar | ||
sidebar={sidebar} | ||
path={pathname} | ||
onCollapse={toggleSidebar} | ||
isHidden={hiddenSidebar} | ||
/> | ||
</ResetOnSidebarChange> | ||
|
||
{hiddenSidebar && <SidebarExpandButton toggleSidebar={toggleSidebar} />} | ||
</aside> | ||
); | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/docusaurus-theme-classic/src/theme/DocPage/Layout/Main.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,37 @@ | ||
/** | ||
* 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 {useDocsSidebar} from '@docusaurus/theme-common'; | ||
|
||
import clsx from 'clsx'; | ||
import styles from './styles.module.css'; | ||
import type {Props} from '@theme/DocPage/Layout/Main'; | ||
|
||
export default function DocPageLayoutMain({ | ||
hiddenSidebarContainer, | ||
children, | ||
}: Props): JSX.Element { | ||
const sidebar = useDocsSidebar(); | ||
return ( | ||
<main | ||
className={clsx( | ||
styles.docMainContainer, | ||
(hiddenSidebarContainer || !sidebar) && styles.docMainContainerEnhanced, | ||
)}> | ||
<div | ||
className={clsx( | ||
'container padding-top--md padding-bottom--lg', | ||
styles.docItemWrapper, | ||
hiddenSidebarContainer && styles.docItemWrapperEnhanced, | ||
)}> | ||
{children} | ||
</div> | ||
</main> | ||
); | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/docusaurus-theme-classic/src/theme/DocPage/Layout/index.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,39 @@ | ||
/** | ||
* 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, {useState} from 'react'; | ||
import Layout from '@theme/Layout'; | ||
import BackToTopButton from '@theme/BackToTopButton'; | ||
import type {Props} from '@theme/DocPage/Layout'; | ||
import DocPageLayoutAside from '@theme/DocPage/Layout/Aside'; | ||
import DocPageLayoutMain from '@theme/DocPage/Layout/Main'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
import {useDocsSidebar} from '@docusaurus/theme-common'; | ||
|
||
export default function DocPageLayout({children}: Props): JSX.Element { | ||
const sidebar = useDocsSidebar(); | ||
const [hiddenSidebarContainer, setHiddenSidebarContainer] = useState(false); | ||
return ( | ||
<Layout> | ||
<BackToTopButton /> | ||
<div className={styles.docPage}> | ||
{sidebar && ( | ||
<DocPageLayoutAside | ||
sidebar={sidebar.items} | ||
hiddenSidebarContainer={hiddenSidebarContainer} | ||
setHiddenSidebarContainer={setHiddenSidebarContainer} | ||
/> | ||
)} | ||
<DocPageLayoutMain hiddenSidebarContainer={hiddenSidebarContainer}> | ||
{children} | ||
</DocPageLayoutMain> | ||
</div> | ||
</Layout> | ||
); | ||
} |
File renamed without changes.
Oops, something went wrong.