|
| 1 | +import React, {type ReactNode} from 'react'; |
| 2 | +import clsx from 'clsx'; |
| 3 | +import {useWindowSize} from '@docusaurus/theme-common'; |
| 4 | +import {useDoc} from '@docusaurus/plugin-content-docs/client'; |
| 5 | +import DocItemPaginator from '@theme/DocItem/Paginator'; |
| 6 | +import DocVersionBanner from '@theme/DocVersionBanner'; |
| 7 | +import DocVersionBadge from '@theme/DocVersionBadge'; |
| 8 | +import DocItemFooter from '@theme/DocItem/Footer'; |
| 9 | +import DocItemTOCMobile from '@theme/DocItem/TOC/Mobile'; |
| 10 | +import DocItemTOCDesktop from '@theme/DocItem/TOC/Desktop'; |
| 11 | +import DocItemContent from '@theme/DocItem/Content'; |
| 12 | +import DocBreadcrumbs from '@theme/DocBreadcrumbs'; |
| 13 | +import ContentVisibility from '@theme/ContentVisibility'; |
| 14 | +import type {Props} from '@theme/DocItem/Layout'; |
| 15 | +import PresentationToggle from '@site/src/components/PresentationMode/PresentationToggle'; |
| 16 | + |
| 17 | +import styles from './styles.module.css'; |
| 18 | + |
| 19 | +interface CustomFrontMatter { |
| 20 | + presentation?: boolean; |
| 21 | + [key: string]: any; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Decide if the toc should be rendered, on mobile or desktop viewports |
| 26 | + */ |
| 27 | +function useDocTOC() { |
| 28 | + const {frontMatter, toc} = useDoc(); |
| 29 | + const windowSize = useWindowSize(); |
| 30 | + |
| 31 | + const hidden = frontMatter.hide_table_of_contents; |
| 32 | + const canRender = !hidden && toc.length > 0; |
| 33 | + |
| 34 | + const mobile = canRender ? <DocItemTOCMobile /> : undefined; |
| 35 | + |
| 36 | + const desktop = |
| 37 | + canRender && (windowSize === 'desktop' || windowSize === 'ssr') ? ( |
| 38 | + <DocItemTOCDesktop /> |
| 39 | + ) : undefined; |
| 40 | + |
| 41 | + return { |
| 42 | + hidden, |
| 43 | + mobile, |
| 44 | + desktop, |
| 45 | + }; |
| 46 | +} |
| 47 | + |
| 48 | +export default function DocItemLayout({children}: Props): ReactNode { |
| 49 | + const docTOC = useDocTOC(); |
| 50 | + const {metadata, frontMatter} = useDoc(); |
| 51 | + |
| 52 | + // Get lesson path for presentation lookup |
| 53 | + const lessonPath = metadata.source?.replace('@site/docs/', '') || ''; |
| 54 | + const customFrontMatter = frontMatter as CustomFrontMatter; |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className="row"> |
| 58 | + <div className={clsx('col', !docTOC.hidden && styles.docItemCol)}> |
| 59 | + <ContentVisibility metadata={metadata} /> |
| 60 | + <DocVersionBanner /> |
| 61 | + <div className={styles.docItemContainer}> |
| 62 | + <article> |
| 63 | + <div className={styles.docHeader}> |
| 64 | + <DocBreadcrumbs /> |
| 65 | + {customFrontMatter.presentation !== false && ( |
| 66 | + <div className={styles.presentationToggleWrapper}> |
| 67 | + <PresentationToggle lessonPath={lessonPath} /> |
| 68 | + </div> |
| 69 | + )} |
| 70 | + </div> |
| 71 | + <DocVersionBadge /> |
| 72 | + {docTOC.mobile} |
| 73 | + <DocItemContent>{children}</DocItemContent> |
| 74 | + <DocItemFooter /> |
| 75 | + </article> |
| 76 | + <DocItemPaginator /> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + {docTOC.desktop && <div className="col col--3">{docTOC.desktop}</div>} |
| 80 | + </div> |
| 81 | + ); |
| 82 | +} |
0 commit comments