mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-04 12:47:14 +02:00
feat(v2): add ability expand all items in doc sidebar (#1876)
* feat(v2): add ability expand all items in doc sidebar * Fix tests * Refactor: use themeConfig * Improve docs * Revert unnecessary changes * Refactor: better consistency * Revert extra change * Refactor: use useDocusaurusContext to get config value
This commit is contained in:
parent
22fb8698c5
commit
ce6a725ff5
4 changed files with 27 additions and 10 deletions
|
@ -5,6 +5,7 @@
|
||||||
- Add sticky footer.
|
- Add sticky footer.
|
||||||
- Remove empty doc sidebar container
|
- Remove empty doc sidebar container
|
||||||
- PostCSS preset env now only polyfills Stage 3 features (previously it was stage 2) like Create React App. Stage 2 CSS is considered relatively unstable and subject to change while Stage 3 features will likely become a standard.
|
- PostCSS preset env now only polyfills Stage 3 features (previously it was stage 2) like Create React App. Stage 2 CSS is considered relatively unstable and subject to change while Stage 3 features will likely become a standard.
|
||||||
|
- Add ability expand all doc items in sidebar (same as `docsSideNavCollapsible` field in v1)
|
||||||
|
|
||||||
## 2.0.0-alpha.30
|
## 2.0.0-alpha.30
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import {MDXProvider} from '@mdx-js/react';
|
import {MDXProvider} from '@mdx-js/react';
|
||||||
|
|
||||||
|
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||||
import renderRoutes from '@docusaurus/renderRoutes';
|
import renderRoutes from '@docusaurus/renderRoutes';
|
||||||
import Layout from '@theme/Layout';
|
import Layout from '@theme/Layout';
|
||||||
import DocSidebar from '@theme/DocSidebar';
|
import DocSidebar from '@theme/DocSidebar';
|
||||||
|
@ -19,6 +20,8 @@ function DocPage(props) {
|
||||||
const {route, docsMetadata, location} = props;
|
const {route, docsMetadata, location} = props;
|
||||||
const {permalinkToSidebar, docsSidebars} = docsMetadata;
|
const {permalinkToSidebar, docsSidebars} = docsMetadata;
|
||||||
const sidebar = permalinkToSidebar[location.pathname.replace(/\/$/, '')];
|
const sidebar = permalinkToSidebar[location.pathname.replace(/\/$/, '')];
|
||||||
|
const {siteConfig: {themeConfig = {}} = {}} = useDocusaurusContext();
|
||||||
|
const {sidebarCollapsible = true} = themeConfig;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout noFooter>
|
<Layout noFooter>
|
||||||
|
@ -29,6 +32,7 @@ function DocPage(props) {
|
||||||
docsSidebars={docsSidebars}
|
docsSidebars={docsSidebars}
|
||||||
location={location}
|
location={location}
|
||||||
sidebar={sidebar}
|
sidebar={sidebar}
|
||||||
|
sidebarCollapsible={sidebarCollapsible}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -14,7 +14,7 @@ import styles from './styles.module.css';
|
||||||
|
|
||||||
const MOBILE_TOGGLE_SIZE = 24;
|
const MOBILE_TOGGLE_SIZE = 24;
|
||||||
|
|
||||||
function DocSidebarItem({item, onItemClick}) {
|
function DocSidebarItem({item, onItemClick, collapsible}) {
|
||||||
const {items, href, label, type} = item;
|
const {items, href, label, type} = item;
|
||||||
const [collapsed, setCollapsed] = useState(item.collapsed);
|
const [collapsed, setCollapsed] = useState(item.collapsed);
|
||||||
const [prevCollapsedProp, setPreviousCollapsedProp] = useState(null);
|
const [prevCollapsedProp, setPreviousCollapsedProp] = useState(null);
|
||||||
|
@ -36,11 +36,12 @@ function DocSidebarItem({item, onItemClick}) {
|
||||||
})}
|
})}
|
||||||
key={label}>
|
key={label}>
|
||||||
<a
|
<a
|
||||||
className={classnames('menu__link', 'menu__link--sublist', {
|
className={classnames('menu__link', {
|
||||||
'menu__link--active': !item.collapsed,
|
'menu__link--sublist': collapsible,
|
||||||
|
'menu__link--active': collapsible && !item.collapsed,
|
||||||
})}
|
})}
|
||||||
href="#!"
|
href="#!"
|
||||||
onClick={() => setCollapsed(!collapsed)}>
|
onClick={collapsible ? () => setCollapsed(!collapsed) : undefined}>
|
||||||
{label}
|
{label}
|
||||||
</a>
|
</a>
|
||||||
<ul className="menu__list">
|
<ul className="menu__list">
|
||||||
|
@ -49,6 +50,7 @@ function DocSidebarItem({item, onItemClick}) {
|
||||||
key={childItem.label}
|
key={childItem.label}
|
||||||
item={childItem}
|
item={childItem}
|
||||||
onItemClick={onItemClick}
|
onItemClick={onItemClick}
|
||||||
|
collapsible={collapsible}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -95,7 +97,12 @@ function mutateSidebarCollapsingState(item, location) {
|
||||||
function DocSidebar(props) {
|
function DocSidebar(props) {
|
||||||
const [showResponsiveSidebar, setShowResponsiveSidebar] = useState(false);
|
const [showResponsiveSidebar, setShowResponsiveSidebar] = useState(false);
|
||||||
|
|
||||||
const {docsSidebars, location, sidebar: currentSidebar} = props;
|
const {
|
||||||
|
docsSidebars,
|
||||||
|
location,
|
||||||
|
sidebar: currentSidebar,
|
||||||
|
sidebarCollapsible,
|
||||||
|
} = props;
|
||||||
|
|
||||||
if (!currentSidebar) {
|
if (!currentSidebar) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -109,9 +116,11 @@ function DocSidebar(props) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sidebarCollapsible) {
|
||||||
sidebarData.forEach(sidebarItem =>
|
sidebarData.forEach(sidebarItem =>
|
||||||
mutateSidebarCollapsingState(sidebarItem, location),
|
mutateSidebarCollapsingState(sidebarItem, location),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.sidebar}>
|
<div className={styles.sidebar}>
|
||||||
|
@ -162,6 +171,7 @@ function DocSidebar(props) {
|
||||||
onItemClick={() => {
|
onItemClick={() => {
|
||||||
setShowResponsiveSidebar(false);
|
setShowResponsiveSidebar(false);
|
||||||
}}
|
}}
|
||||||
|
collapsible={sidebarCollapsible}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
|
@ -161,7 +161,7 @@ Infima uses 7 shades of each color. We recommend using [ColorBox](https://www.co
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `footerIcon`, `copyright`, `ogImage`, `twitterImage`
|
#### `footerIcon`, `copyright`, `ogImage`, `twitterImage`, 'docsSideNavCollapsible'
|
||||||
|
|
||||||
Site meta info such as assets, SEO, copyright info are now handled by themes. To customize them, use the `themeConfig` field in your `docusaurus.config.js`:
|
Site meta info such as assets, SEO, copyright info are now handled by themes. To customize them, use the `themeConfig` field in your `docusaurus.config.js`:
|
||||||
|
|
||||||
|
@ -177,6 +177,8 @@ module.exports = {
|
||||||
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`,
|
copyright: `Copyright © ${new Date().getFullYear()} Facebook, Inc.`,
|
||||||
},
|
},
|
||||||
image: 'img/docusaurus.png',
|
image: 'img/docusaurus.png',
|
||||||
|
// Equivalent to `docsSideNavCollapsible`
|
||||||
|
sidebarCollapsible: false,
|
||||||
...
|
...
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -333,7 +335,7 @@ The following fields are all deprecated, you may remove from your configuration
|
||||||
- `defaultVersionShown` - Versioning is not ported yet. You'd be unable to migration to Docusaurus 2 if you are using versioning. Stay tuned.
|
- `defaultVersionShown` - Versioning is not ported yet. You'd be unable to migration to Docusaurus 2 if you are using versioning. Stay tuned.
|
||||||
- `disableHeaderTitle`
|
- `disableHeaderTitle`
|
||||||
- `disableTitleTagline`
|
- `disableTitleTagline`
|
||||||
- `docsSideNavCollapsible` - This is turned on by default now.
|
- `docsSideNavCollapsible` is available at `themeConfig.sidebarCollapsible`, and this is turned on by default now.
|
||||||
- `facebookAppId`
|
- `facebookAppId`
|
||||||
- `facebookComments`
|
- `facebookComments`
|
||||||
- `facebookPixelId`
|
- `facebookPixelId`
|
||||||
|
|
Loading…
Add table
Reference in a new issue