|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import React, {type ReactNode} from 'react'; |
| 9 | +import clsx from 'clsx'; |
| 10 | +import {useWindowSize} from '@docusaurus/theme-common'; |
| 11 | +import {useDoc} from '@docusaurus/plugin-content-docs/client'; |
| 12 | +import CopyPageButton from 'docusaurus-plugin-copy-page-button/react'; |
| 13 | +import DocItemPaginator from '@theme/DocItem/Paginator'; |
| 14 | +import DocVersionBanner from '@theme/DocVersionBanner'; |
| 15 | +import DocVersionBadge from '@theme/DocVersionBadge'; |
| 16 | +import DocItemFooter from '@theme/DocItem/Footer'; |
| 17 | +import DocItemTOCMobile from '@theme/DocItem/TOC/Mobile'; |
| 18 | +import DocItemTOCDesktop from '@theme/DocItem/TOC/Desktop'; |
| 19 | +import DocItemContent from '@theme/DocItem/Content'; |
| 20 | +import DocBreadcrumbs from '@theme/DocBreadcrumbs'; |
| 21 | +import ContentVisibility from '@theme/ContentVisibility'; |
| 22 | +import type {Props} from '@theme/DocItem/Layout'; |
| 23 | + |
| 24 | +import styles from './styles.module.css'; |
| 25 | + |
| 26 | +function DocItemCopyPageButton({className}: {className?: string}) { |
| 27 | + return ( |
| 28 | + <div className={clsx(styles.copyPageAction, className)}> |
| 29 | + <CopyPageButton |
| 30 | + customStyles={{ |
| 31 | + container: {className: styles.copyPageButtonContainer}, |
| 32 | + button: { |
| 33 | + className: styles.copyPageButton, |
| 34 | + style: {marginBottom: 0}, |
| 35 | + }, |
| 36 | + dropdown: {className: styles.copyPageDropdown}, |
| 37 | + }} |
| 38 | + /> |
| 39 | + </div> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Decide if the toc should be rendered, on mobile or desktop viewports |
| 45 | + */ |
| 46 | +function useDocTOC() { |
| 47 | + const {frontMatter, toc} = useDoc(); |
| 48 | + const windowSize = useWindowSize(); |
| 49 | + |
| 50 | + const hidden = frontMatter.hide_table_of_contents; |
| 51 | + const canRender = !hidden && toc.length > 0; |
| 52 | + |
| 53 | + const mobile = canRender ? <DocItemTOCMobile /> : undefined; |
| 54 | + |
| 55 | + const desktop = |
| 56 | + canRender && (windowSize === 'desktop' || windowSize === 'ssr') ? ( |
| 57 | + <DocItemTOCDesktop /> |
| 58 | + ) : undefined; |
| 59 | + |
| 60 | + return { |
| 61 | + canRender, |
| 62 | + hidden, |
| 63 | + mobile, |
| 64 | + desktop, |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +export default function DocItemLayout({children}: Props): ReactNode { |
| 69 | + const docTOC = useDocTOC(); |
| 70 | + const {metadata} = useDoc(); |
| 71 | + return ( |
| 72 | + <div className="row"> |
| 73 | + <div className={clsx('col', !docTOC.hidden && styles.docItemCol)}> |
| 74 | + <ContentVisibility metadata={metadata} /> |
| 75 | + <DocVersionBanner /> |
| 76 | + <div className={styles.docItemContainer}> |
| 77 | + <article> |
| 78 | + <DocBreadcrumbs /> |
| 79 | + <div className={styles.docHeaderContainer}> |
| 80 | + <DocVersionBadge /> |
| 81 | + <DocItemCopyPageButton |
| 82 | + className={clsx( |
| 83 | + styles.copyPageArticleAction, |
| 84 | + docTOC.canRender && styles.copyPageArticleActionWithToc |
| 85 | + )} |
| 86 | + /> |
| 87 | + </div> |
| 88 | + {docTOC.mobile} |
| 89 | + <DocItemContent>{children}</DocItemContent> |
| 90 | + <DocItemFooter /> |
| 91 | + </article> |
| 92 | + <DocItemPaginator /> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + {docTOC.desktop && ( |
| 96 | + <div className="col col--3"> |
| 97 | + <DocItemCopyPageButton className={styles.copyPageAsideAction} /> |
| 98 | + {docTOC.desktop} |
| 99 | + </div> |
| 100 | + )} |
| 101 | + </div> |
| 102 | + ); |
| 103 | +} |
0 commit comments