refactor(theme-classic): move all sidebar-related config under themeConfig.docs.sidebar (#7277)

This commit is contained in:
Joshua Chen 2022-05-04 18:08:19 +08:00 committed by GitHub
parent 881430078e
commit 785fed723f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 79 additions and 31 deletions

View file

@ -133,7 +133,6 @@ exports[`translateThemeConfig returns translated themeConfig 1`] = `
], ],
"style": "light", "style": "light",
}, },
"hideableSidebar": true,
"navbar": { "navbar": {
"hideOnScroll": false, "hideOnScroll": false,
"items": [ "items": [

View file

@ -16,7 +16,6 @@ const ThemeConfigSample: ThemeConfig = {
docs: { docs: {
versionPersistence: 'none', versionPersistence: 'none',
}, },
hideableSidebar: true,
navbar: { navbar: {
title: 'navbar title', title: 'navbar title',
style: 'dark', style: 'dark',

View file

@ -37,6 +37,13 @@ describe('themeConfig', () => {
defaultLanguage: 'javascript', defaultLanguage: 'javascript',
additionalLanguages: ['kotlin', 'java'], additionalLanguages: ['kotlin', 'java'],
}, },
docs: {
versionPersistence: 'localStorage',
sidebar: {
hideable: true,
autoCollapseCategories: false,
},
},
announcementBar: { announcementBar: {
id: 'supports', id: 'supports',
content: 'pls support', content: 'pls support',
@ -101,6 +108,19 @@ describe('themeConfig', () => {
}); });
}); });
it('rejects outdated sidebar options', () => {
expect(() =>
testValidateThemeConfig({hideableSidebar: true}),
).toThrowErrorMatchingInlineSnapshot(
`"themeConfig.hideableSidebar has been moved to themeConfig.docs.sidebar.hideable."`,
);
expect(() =>
testValidateThemeConfig({autoCollapseSidebarCategories: true}),
).toThrowErrorMatchingInlineSnapshot(
`"themeConfig.autoCollapseSidebarCategories has been moved to themeConfig.docs.sidebar.autoCollapseCategories."`,
);
});
it('allows possible types of navbar items', () => { it('allows possible types of navbar items', () => {
const config = { const config = {
navbar: { navbar: {

View file

@ -18,7 +18,9 @@ import styles from './styles.module.css';
function DocSidebarDesktop({path, sidebar, onCollapse, isHidden}: Props) { function DocSidebarDesktop({path, sidebar, onCollapse, isHidden}: Props) {
const { const {
navbar: {hideOnScroll}, navbar: {hideOnScroll},
hideableSidebar, docs: {
sidebar: {hideable},
},
} = useThemeConfig(); } = useThemeConfig();
return ( return (
@ -30,7 +32,7 @@ function DocSidebarDesktop({path, sidebar, onCollapse, isHidden}: Props) {
)}> )}>
{hideOnScroll && <Logo tabIndex={-1} className={styles.sidebarLogo} />} {hideOnScroll && <Logo tabIndex={-1} className={styles.sidebarLogo} />}
<Content path={path} sidebar={sidebar} /> <Content path={path} sidebar={sidebar} />
{hideableSidebar && <CollapseButton onClick={onCollapse} />} {hideable && <CollapseButton onClick={onCollapse} />}
</div> </div>
); );
} }

View file

@ -127,23 +127,21 @@ export default function DocSidebarItemCategory({
setExpandedItem(toCollapsed ? null : index); setExpandedItem(toCollapsed ? null : index);
setCollapsed(toCollapsed); setCollapsed(toCollapsed);
} }
const {autoCollapseSidebarCategories} = useThemeConfig(); const {
docs: {
sidebar: {autoCollapseCategories},
},
} = useThemeConfig();
useEffect(() => { useEffect(() => {
if ( if (
collapsible && collapsible &&
expandedItem && expandedItem &&
expandedItem !== index && expandedItem !== index &&
autoCollapseSidebarCategories autoCollapseCategories
) { ) {
setCollapsed(true); setCollapsed(true);
} }
}, [ }, [collapsible, expandedItem, index, setCollapsed, autoCollapseCategories]);
collapsible,
expandedItem,
index,
setCollapsed,
autoCollapseSidebarCategories,
]);
return ( return (
<li <li

View file

@ -14,11 +14,21 @@ import type {
const DEFAULT_DOCS_CONFIG = { const DEFAULT_DOCS_CONFIG = {
versionPersistence: 'localStorage', versionPersistence: 'localStorage',
sidebar: {
hideable: false,
autoCollapseCategories: false,
},
}; };
const DocsSchema = Joi.object({ const DocsSchema = Joi.object({
versionPersistence: Joi.string() versionPersistence: Joi.string()
.equal('localStorage', 'none') .equal('localStorage', 'none')
.default(DEFAULT_DOCS_CONFIG.versionPersistence), .default(DEFAULT_DOCS_CONFIG.versionPersistence),
sidebar: Joi.object({
hideable: Joi.bool().default(DEFAULT_DOCS_CONFIG.sidebar.hideable),
autoCollapseCategories: Joi.bool().default(
DEFAULT_DOCS_CONFIG.sidebar.autoCollapseCategories,
),
}).default(DEFAULT_DOCS_CONFIG.sidebar),
}).default(DEFAULT_DOCS_CONFIG); }).default(DEFAULT_DOCS_CONFIG);
const DEFAULT_COLOR_MODE_CONFIG = { const DEFAULT_COLOR_MODE_CONFIG = {
@ -39,8 +49,6 @@ export const DEFAULT_CONFIG = {
hideOnScroll: false, hideOnScroll: false,
items: [], items: [],
}, },
hideableSidebar: false,
autoCollapseSidebarCategories: false,
tableOfContents: { tableOfContents: {
minHeadingLevel: 2, minHeadingLevel: 2,
maxHeadingLevel: 3, maxHeadingLevel: 3,
@ -381,10 +389,14 @@ export const ThemeConfigSchema = Joi.object({
}) })
.default(DEFAULT_CONFIG.prism) .default(DEFAULT_CONFIG.prism)
.unknown(), .unknown(),
hideableSidebar: Joi.bool().default(DEFAULT_CONFIG.hideableSidebar), hideableSidebar: Joi.forbidden().messages({
autoCollapseSidebarCategories: Joi.bool().default( 'any.unknown':
DEFAULT_CONFIG.autoCollapseSidebarCategories, 'themeConfig.hideableSidebar has been moved to themeConfig.docs.sidebar.hideable.',
), }),
autoCollapseSidebarCategories: Joi.forbidden().messages({
'any.unknown':
'themeConfig.autoCollapseSidebarCategories has been moved to themeConfig.docs.sidebar.autoCollapseCategories.',
}),
sidebarCollapsible: Joi.forbidden().messages({ sidebarCollapsible: Joi.forbidden().messages({
'any.unknown': 'any.unknown':
'The themeConfig.sidebarCollapsible has been moved to docs plugin options. See: https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-docs', 'The themeConfig.sidebarCollapsible has been moved to docs plugin options. See: https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-docs',

View file

@ -104,6 +104,10 @@ export type TableOfContents = {
export type ThemeConfig = { export type ThemeConfig = {
docs: { docs: {
versionPersistence: DocsVersionPersistence; versionPersistence: DocsVersionPersistence;
sidebar: {
hideable: boolean;
autoCollapseCategories: boolean;
};
}; };
// TODO we should complete this theme config type over time // TODO we should complete this theme config type over time
@ -116,11 +120,8 @@ export type ThemeConfig = {
announcementBar?: AnnouncementBarConfig; announcementBar?: AnnouncementBarConfig;
prism: PrismConfig; prism: PrismConfig;
footer?: Footer; footer?: Footer;
hideableSidebar: boolean;
autoCollapseSidebarCategories: boolean;
image?: string; image?: string;
metadata: Array<{[key: string]: string}>; metadata: Array<{[key: string]: string}>;
sidebarCollapsible: boolean;
tableOfContents: TableOfContents; tableOfContents: TableOfContents;
}; };

View file

@ -286,8 +286,12 @@ Example:
```js title="docusaurus.config.js" ```js title="docusaurus.config.js"
module.exports = { module.exports = {
themeConfig: { themeConfig: {
hideableSidebar: false, docs: {
autoCollapseSidebarCategories: false, sidebar: {
hideable: false,
autoCollapseCategories: false,
},
},
colorMode: { colorMode: {
defaultMode: 'light', defaultMode: 'light',
disableSwitch: false, disableSwitch: false,

View file

@ -121,13 +121,17 @@ type SidebarsFile = {
### Hideable sidebar {#hideable-sidebar} ### Hideable sidebar {#hideable-sidebar}
By enabling the `themeConfig.hideableSidebar` option, you can make the entire sidebar hideable, allowing users to better focus on the content. This is especially useful when content is consumed on medium-sized screens (e.g. tablets). By enabling the `themeConfig.docs.sidebar.hideable` option, you can make the entire sidebar hideable, allowing users to better focus on the content. This is especially useful when content is consumed on medium-sized screens (e.g. tablets).
```js title="docusaurus.config.js" ```js title="docusaurus.config.js"
module.exports = { module.exports = {
themeConfig: { themeConfig: {
// highlight-start // highlight-start
hideableSidebar: true, docs: {
sidebar: {
hideable: true,
},
},
// highlight-end // highlight-end
}, },
}; };
@ -135,13 +139,18 @@ module.exports = {
### Auto-collapse sidebar categories {#auto-collapse-sidebar-categories} ### Auto-collapse sidebar categories {#auto-collapse-sidebar-categories}
The `themeConfig.autoCollapseSidebarCategories` option would collapse all sibling categories when expanding one category. This saves the user from having too many categories open and helps them focus on the selected section. The `themeConfig.docs.sidebar.autoCollapseCategories` option would collapse all sibling categories when expanding one category. This saves the user from having too many categories open and helps them focus on the selected section.
```js title="docusaurus.config.js" ```js title="docusaurus.config.js"
module.exports = { module.exports = {
themeConfig: { themeConfig: {
// highlight-next-line // highlight-start
autoCollapseSidebarCategories: true, docs: {
sidebar: {
autoCollapseCategories: true,
},
},
// highlight-end
}, },
}; };
``` ```

View file

@ -357,8 +357,12 @@ const config = {
liveCodeBlock: { liveCodeBlock: {
playgroundPosition: 'bottom', playgroundPosition: 'bottom',
}, },
hideableSidebar: true, docs: {
autoCollapseSidebarCategories: true, sidebar: {
hideable: true,
autoCollapseCategories: true,
},
},
colorMode: { colorMode: {
defaultMode: 'light', defaultMode: 'light',
disableSwitch: false, disableSwitch: false,