Skip to content

Commit

Permalink
refactor(docs,theme): split DocItem comp, useDoc hook (#7644)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
  • Loading branch information
slorber and Josh-Cena committed Jun 22, 2022
1 parent 2316900 commit fd87afd
Show file tree
Hide file tree
Showing 17 changed files with 357 additions and 144 deletions.
Expand Up @@ -8,7 +8,8 @@
/// <reference types="@docusaurus/module-type-aliases" />

declare module '@docusaurus/plugin-content-docs' {
import type {MDXOptions} from '@docusaurus/mdx-loader';
import type {MDXOptions, LoadedMDXContent} from '@docusaurus/mdx-loader';

import type {
ContentPaths,
FrontMatterTag,
Expand Down Expand Up @@ -491,6 +492,12 @@ declare module '@docusaurus/plugin-content-docs' {
[docId: string]: PropVersionDoc;
};

export type PropDocContent = LoadedMDXContent<
DocFrontMatter,
DocMetadata,
Assets
>;

export type PropVersionMetadata = Pick<
VersionMetadata,
'label' | 'banner' | 'badge' | 'className' | 'isLast'
Expand Down Expand Up @@ -549,13 +556,7 @@ declare module '@docusaurus/plugin-content-docs' {
}

declare module '@theme/DocItem' {
import type {LoadedMDXContent} from '@docusaurus/mdx-loader';
import type {
PropVersionMetadata,
Assets,
DocMetadata,
DocFrontMatter,
} from '@docusaurus/plugin-content-docs';
import type {PropDocContent} from '@docusaurus/plugin-content-docs';

export type DocumentRoute = {
readonly component: () => JSX.Element;
Expand All @@ -566,8 +567,7 @@ declare module '@theme/DocItem' {

export interface Props {
readonly route: DocumentRoute;
readonly versionMetadata: PropVersionMetadata;
readonly content: LoadedMDXContent<DocFrontMatter, DocMetadata, Assets>;
readonly content: PropDocContent;
}

export default function DocItem(props: Props): JSX.Element;
Expand Down
36 changes: 33 additions & 3 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Expand Up @@ -261,10 +261,40 @@ declare module '@theme/DocCardList' {
export default function DocCardList(props: Props): JSX.Element;
}

declare module '@theme/DocItemFooter' {
import type {Props} from '@theme/DocItem';
declare module '@theme/DocItem/Layout' {
export interface Props {
readonly children: JSX.Element;
}

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

declare module '@theme/DocItem/Metadata' {
export default function DocItemMetadata(): JSX.Element;
}

declare module '@theme/DocItem/Content' {
export interface Props {
readonly children: JSX.Element;
}

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

declare module '@theme/DocItem/TOC/Mobile' {
export default function DocItemTOCMobile(): JSX.Element;
}

declare module '@theme/DocItem/TOC/Desktop' {
export default function DocItemTOCDesktop(): JSX.Element;
}

declare module '@theme/DocItem/Paginator' {
export default function DocItemPaginator(): JSX.Element;
}

export default function DocItemFooter(props: Props): JSX.Element;
declare module '@theme/DocItem/Footer' {
export default function DocItemFooter(): JSX.Element;
}

declare module '@theme/DocPage/Layout' {
Expand Down
@@ -0,0 +1,47 @@
/**
* 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 {ThemeClassNames, useDoc} from '@docusaurus/theme-common';
import Heading from '@theme/Heading';
import MDXContent from '@theme/MDXContent';
import type {Props} from '@theme/DocItem/Content';

/**
Title can be declared inside md content or declared through
front matter and added manually. To make both cases consistent,
the added title is added under the same div.markdown block
See https://github.com/facebook/docusaurus/pull/4882#issuecomment-853021120
We render a "synthetic title" if:
- user doesn't ask to hide it with front matter
- the markdown content does not already contain a top-level h1 heading
*/
function useSyntheticTitle(): string | null {
const {metadata, frontMatter, contentTitle} = useDoc();
const shouldRender =
!frontMatter.hide_title && typeof contentTitle === 'undefined';
if (!shouldRender) {
return null;
}
return metadata.title;
}

export default function DocItemContent({children}: Props): JSX.Element {
const syntheticTitle = useSyntheticTitle();
return (
<div className={clsx(ThemeClassNames.docs.docMarkdown, 'markdown')}>
{syntheticTitle && (
<header>
<Heading as="h1">{syntheticTitle}</Heading>
</header>
)}
<MDXContent>{children}</MDXContent>
</div>
);
}
Expand Up @@ -7,13 +7,16 @@

import React from 'react';
import clsx from 'clsx';
import {ThemeClassNames} from '@docusaurus/theme-common';
import {
ThemeClassNames,
useDoc,
type DocContextValue,
} from '@docusaurus/theme-common';
import LastUpdated from '@theme/LastUpdated';
import EditThisPage from '@theme/EditThisPage';
import TagsListInline, {
type Props as TagsListInlineProps,
} from '@theme/TagsListInline';
import type {Props} from '@theme/DocItem';

import styles from './styles.module.css';

Expand All @@ -32,7 +35,7 @@ function TagsRow(props: TagsListInlineProps) {
}

type EditMetaRowProps = Pick<
Props['content']['metadata'],
DocContextValue['metadata'],
'editUrl' | 'lastUpdatedAt' | 'lastUpdatedBy' | 'formattedLastUpdatedAt'
>;
function EditMetaRow({
Expand All @@ -58,9 +61,8 @@ function EditMetaRow({
);
}

export default function DocItemFooter(props: Props): JSX.Element | null {
const {content: DocContent} = props;
const {metadata} = DocContent;
export default function DocItemFooter(): JSX.Element | null {
const {metadata} = useDoc();
const {editUrl, lastUpdatedAt, formattedLastUpdatedAt, lastUpdatedBy, tags} =
metadata;

Expand Down
@@ -0,0 +1,67 @@
/**
* 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 {useWindowSize, useDoc} from '@docusaurus/theme-common';
import DocItemPaginator from '@theme/DocItem/Paginator';
import DocVersionBanner from '@theme/DocVersionBanner';
import DocVersionBadge from '@theme/DocVersionBadge';
import DocItemFooter from '@theme/DocItem/Footer';
import DocItemTOCMobile from '@theme/DocItem/TOC/Mobile';
import DocItemTOCDesktop from '@theme/DocItem/TOC/Desktop';
import DocItemContent from '@theme/DocItem/Content';
import DocBreadcrumbs from '@theme/DocBreadcrumbs';
import type {Props} from '@theme/DocItem/Layout';

import styles from './styles.module.css';

/**
* Decide if the toc should be rendered, on mobile or desktop viewports
*/
function useDocTOC() {
const {frontMatter, toc} = useDoc();
const windowSize = useWindowSize();

const hidden = frontMatter.hide_table_of_contents;
const canRender = !hidden && toc.length > 0;

const mobile = canRender ? <DocItemTOCMobile /> : undefined;

const desktop =
canRender && (windowSize === 'desktop' || windowSize === 'ssr') ? (
<DocItemTOCDesktop />
) : undefined;

return {
hidden,
mobile,
desktop,
};
}

export default function DocItemLayout({children}: Props): JSX.Element {
const docTOC = useDocTOC();
return (
<div className="row">
<div className={clsx('col', !docTOC.hidden && styles.docItemCol)}>
<DocVersionBanner />
<div className={styles.docItemContainer}>
<article>
<DocBreadcrumbs />
<DocVersionBadge />
{docTOC.mobile}
<DocItemContent>{children}</DocItemContent>
<DocItemFooter />
</article>
<DocItemPaginator />
</div>
</div>
{docTOC.desktop && <div className="col col--3">{docTOC.desktop}</div>}
</div>
);
}
@@ -0,0 +1,17 @@
/**
* 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.
*/

.docItemContainer header + *,
.docItemContainer article > *:first-child {
margin-top: 0;
}

@media (min-width: 997px) {
.docItemCol {
max-width: 75% !important;
}
}
@@ -0,0 +1,21 @@
/**
* 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 {PageMetadata, useDoc} from '@docusaurus/theme-common';

export default function DocItemMetadata(): JSX.Element {
const {metadata, frontMatter, assets} = useDoc();
return (
<PageMetadata
title={metadata.title}
description={metadata.description}
keywords={frontMatter.keywords}
image={assets.image ?? frontMatter.image}
/>
);
}
@@ -0,0 +1,19 @@
/**
* 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 {useDoc} from '@docusaurus/theme-common';
import DocPaginator from '@theme/DocPaginator';

/**
* This extra component is needed, because <DocPaginator> should remain generic.
* DocPaginator is used in non-docs contexts too: generated-index pages...
*/
export default function DocItemPaginator(): JSX.Element {
const {metadata} = useDoc();
return <DocPaginator previous={metadata.previous} next={metadata.next} />;
}
@@ -0,0 +1,23 @@
/**
* 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 {ThemeClassNames, useDoc} from '@docusaurus/theme-common';

import TOC from '@theme/TOC';

export default function DocItemTOCDesktop(): JSX.Element {
const {toc, frontMatter} = useDoc();
return (
<TOC
toc={toc}
minHeadingLevel={frontMatter.toc_min_heading_level}
maxHeadingLevel={frontMatter.toc_max_heading_level}
className={ThemeClassNames.docs.docTocDesktop}
/>
);
}
@@ -0,0 +1,25 @@
/**
* 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 {ThemeClassNames, useDoc} from '@docusaurus/theme-common';
import TOCCollapsible from '@theme/TOCCollapsible';

import styles from './styles.module.css';

export default function DocItemTOCMobile(): JSX.Element {
const {toc, frontMatter} = useDoc();
return (
<TOCCollapsible
toc={toc}
minHeadingLevel={frontMatter.toc_min_heading_level}
maxHeadingLevel={frontMatter.toc_max_heading_level}
className={clsx(ThemeClassNames.docs.docTocMobile, styles.tocMobile)}
/>
);
}
Expand Up @@ -5,16 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

.docItemContainer header + *,
.docItemContainer article > *:first-child {
margin-top: 0;
}

@media (min-width: 997px) {
.docItemCol {
max-width: 75% !important;
}

/* Prevent hydration FOUC, as the mobile TOC needs to be server-rendered */
.tocMobile {
display: none;
Expand Down

0 comments on commit fd87afd

Please sign in to comment.