Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(docs,theme): split DocItem comp, useDoc hook #7644

Merged
merged 6 commits into from Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we'll want to extract this hook later?

Not 100% satisfied with it atm

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this—seems like over-abstraction that hides its semantics.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you suggest?

I'd like the layout component to be easy to edit visually, without the TOC technical details noise.

Maybe it's fine to keep this weird hook, but just keep it there so that users can understand it more easily? 🤷‍♂️

I don't like it either but I don't have much better to suggest without polluting the component

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when I'm swizzling, I'd personally like to gain some understanding of what the hook is doing. This is especially important for non-TS users. This hook is congregating too many unrelated things. It's fine if it's in the same file and easily viewable, but extracting it to theme-common makes it much harder to work with.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree 👍

let's ship this as is and see how to improve later

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