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

Fix sidebar customizations #412

Merged
merged 2 commits into from
Mar 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions src/theme/DocPage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/**
* 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, useCallback} from 'react';
import {MDXProvider} from '@mdx-js/react';
import renderRoutes from '@docusaurus/renderRoutes';
import Layout from '@theme/Layout';
import DocSidebar from '@theme/DocSidebar';
import MDXComponents from '@theme/MDXComponents';
import NotFound from '@theme/NotFound';
import IconArrow from '@theme/IconArrow';
import BackToTopButton from '@theme/BackToTopButton';
import {matchPath} from '@docusaurus/router';
import {translate} from '@docusaurus/Translate';
import clsx from 'clsx';
import styles from './styles.module.css';
import {
ThemeClassNames,
docVersionSearchTag,
DocsSidebarProvider,
useDocsSidebar,
DocsVersionProvider,
} from '@docusaurus/theme-common';
import Head from '@docusaurus/Head';

function DocPageContent({
currentDocRoute,
versionMetadata,
children,
sidebarName,
}) {
const sidebar = useDocsSidebar();
const {pluginId, version} = versionMetadata;
const [hiddenSidebarContainer, setHiddenSidebarContainer] = useState(false);
const [hiddenSidebar, setHiddenSidebar] = useState(false);
const toggleSidebar = useCallback(() => {
if (hiddenSidebar) {
setHiddenSidebar(false);
}

setHiddenSidebarContainer((value) => !value);
}, [hiddenSidebar]);
return (
<Layout
wrapperClassName={ThemeClassNames.wrapper.docsPages}
pageClassName={ThemeClassNames.page.docsDocPage}
searchMetadata={{
version,
tag: docVersionSearchTag(pluginId, version),
}}>
<div className={styles.docPage}>
<BackToTopButton />

{sidebar && (
<aside
className={clsx(
ThemeClassNames.docs.docSidebarContainer,
styles.docSidebarContainer,
{
[styles.docSidebarContainerHidden]: hiddenSidebarContainer,
},
)}
onTransitionEnd={(e) => {
if (
!e.currentTarget.classList.contains(styles.docSidebarContainer)
) {
return;
}

if (hiddenSidebarContainer) {
setHiddenSidebar(true);
}
}}>
<DocSidebar
key={
// Reset sidebar state on sidebar changes
// See https://github.com/facebook/docusaurus/issues/3414
sidebarName
}
sidebar={sidebar}
path={currentDocRoute.path}
onCollapse={toggleSidebar}
isHidden={hiddenSidebar}
/>

{hiddenSidebar && (
<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>
)}
</aside>
)}
<main
className={clsx(styles.docMainContainer, {
[styles.docMainContainerEnhanced]:
hiddenSidebarContainer || !sidebar,
})}>
<div
className={clsx(
'container padding-top--md padding-bottom--lg',
styles.docItemWrapper,
{
[styles.docItemWrapperEnhanced]: hiddenSidebarContainer,
},
)}>
<MDXProvider components={MDXComponents}>{children}</MDXProvider>
</div>
</main>
</div>
</Layout>
);
}

export default function DocPage(props) {
const {
route: {routes: docRoutes},
versionMetadata,
location,
} = props;
const currentDocRoute = docRoutes.find((docRoute) =>
matchPath(location.pathname, docRoute),
);

if (!currentDocRoute) {
return <NotFound />;
} // For now, the sidebarName is added as route config: not ideal!

const sidebarName = currentDocRoute.sidebar;
const sidebar = sidebarName
? versionMetadata.docsSidebars[sidebarName]
: null;
return (
<>
<Head>
{/* TODO we should add a core addRoute({htmlClassName}) action */}
<html className={versionMetadata.className} />
</Head>
<DocsVersionProvider version={versionMetadata}>
<DocsSidebarProvider sidebar={sidebar}>
<DocPageContent
currentDocRoute={currentDocRoute}
versionMetadata={versionMetadata}
sidebarName={sidebarName}>
{renderRoutes(docRoutes, {
versionMetadata,
})}
</DocPageContent>
</DocsSidebarProvider>
</DocsVersionProvider>
</>
);
}
86 changes: 86 additions & 0 deletions src/theme/DocPage/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* 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.
*/

:root {
--doc-sidebar-width: 300px;
--doc-sidebar-hidden-width: 30px;
}

:global(.docs-wrapper) {
display: flex;
}

.docPage,
.docMainContainer {
display: flex;
width: 100%;
}

.docSidebarContainer {
display: none;
}

@media (min-width: 997px) {
.docMainContainer {
flex-grow: 1;
max-width: calc(100% - var(--doc-sidebar-width));
}

.docMainContainerEnhanced {
max-width: calc(100% - var(--doc-sidebar-hidden-width));
}

.docSidebarContainer {
display: block;
width: var(--doc-sidebar-width);
margin-top: calc(-1 * var(--ifm-navbar-height));
border-right: 1px solid var(--ifm-toc-border-color);
will-change: width;
transition: width var(--ifm-transition-fast) ease;
clip-path: inset(0);
}

.docSidebarContainerHidden {
width: var(--doc-sidebar-hidden-width);
cursor: pointer;
}

.collapsedDocSidebar {
position: sticky;
top: 0;
height: 100%;
max-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
transition: background-color var(--ifm-transition-fast) ease;
}

.collapsedDocSidebar:hover,
.collapsedDocSidebar:focus {
background-color: var(--ifm-color-emphasis-200);
}

.expandSidebarButtonIcon {
transform: rotate(0);
}

[dir='rtl'] .expandSidebarButtonIcon {
transform: rotate(180deg);
}

[data-theme='dark'] .collapsedDocSidebar:hover,
[data-theme='dark'] .collapsedDocSidebar:focus {
background-color: var(--collapse-button-bg-color-dark);
}

.docItemWrapperEnhanced {
max-width: calc(
var(--ifm-container-width) + var(--doc-sidebar-width)
) !important;
}
}
4 changes: 2 additions & 2 deletions src/theme/DocSidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
MobileSecondaryMenuFiller,
ThemeClassNames,
useScrollPosition,
useWindowSize,
} from '@docusaurus/theme-common';
import {useWindowSize} from '@lilib/hooks';
import Logo from '@theme/Logo';
import IconArrow from '@theme/IconArrow';
import {translate} from '@docusaurus/Translate';
import {DocSidebarItems} from '@theme/DocSidebarItem';
import DocSidebarItems from '@theme/DocSidebarItems';
import styles from './styles.module.css';

function useShowAnnouncementBar() {
Expand Down
9 changes: 5 additions & 4 deletions src/theme/DocSidebar/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@
transform: rotate(180deg);
margin-top: 4px;
}
html[dir='rtl'] .collapseSidebarButtonIcon {

[dir='rtl'] .collapseSidebarButtonIcon {
transform: rotate(0);
}

html[data-theme='dark'] .collapseSidebarButton {
[data-theme='dark'] .collapseSidebarButton {
background-color: var(--collapse-button-bg-color-dark);
}

html[data-theme='dark'] .collapseSidebarButton:hover,
html[data-theme='dark'] .collapseSidebarButton:focus {
[data-theme='dark'] .collapseSidebarButton:hover,
[data-theme='dark'] .collapseSidebarButton:focus {
background-color: var(--ifm-color-emphasis-200);
}
}
Expand Down
Loading