Skip to content

Commit

Permalink
refactor: split and cleanup theme/DocPage (#7006)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Mar 25, 2022
1 parent 2964e6f commit 1b974e8
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,40 @@ declare module '@theme/DocPage' {
export default function DocPage(props: Props): JSX.Element;
}

declare module '@theme/DocPage/Layout' {
import type {ReactNode} from 'react';

export interface Props {
children: ReactNode;
}

export default function DocPageLayout(props: Props): JSX.Element;
}

declare module '@theme/DocPage/Layout/Aside' {
import type {Dispatch, SetStateAction} from 'react';
import type {PropSidebar} from '@docusaurus/plugin-content-docs';

export interface Props {
sidebar: PropSidebar;
hiddenSidebarContainer: boolean;
setHiddenSidebarContainer: Dispatch<SetStateAction<boolean>>;
}

export default function DocPageLayoutAside(props: Props): JSX.Element;
}

declare module '@theme/DocPage/Layout/Main' {
import type {ReactNode} from 'react';

export interface Props {
hiddenSidebarContainer: boolean;
children: ReactNode;
}

export default function DocPageLayoutMain(props: Props): JSX.Element;
}

// TODO until TS supports exports field... hope it's in 4.6
declare module '@docusaurus/plugin-content-docs/client' {
export type ActivePlugin = {
Expand Down
100 changes: 100 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/DocPage/Layout/Aside.tsx
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>
);
}
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>
);
}
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>
);
}
Loading

0 comments on commit 1b974e8

Please sign in to comment.