mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-14 09:37:37 +02:00
feat(v2): add custom props for consumption by swizzled sidebar (#3888)
This commit is contained in:
parent
0b05806593
commit
b11c24b752
6 changed files with 41 additions and 11 deletions
|
@ -361,6 +361,7 @@ describe('transformSidebarItems', () => {
|
||||||
collapsed: false,
|
collapsed: false,
|
||||||
label: 'Subcategory 1',
|
label: 'Subcategory 1',
|
||||||
items: [{type: 'doc', id: 'doc1'}],
|
items: [{type: 'doc', id: 'doc1'}],
|
||||||
|
customProps: {fakeProp: false},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'category',
|
type: 'category',
|
||||||
|
@ -372,7 +373,9 @@ describe('transformSidebarItems', () => {
|
||||||
type: 'category',
|
type: 'category',
|
||||||
collapsed: false,
|
collapsed: false,
|
||||||
label: 'Sub sub category 1',
|
label: 'Sub sub category 1',
|
||||||
items: [{type: 'doc', id: 'doc3'}],
|
items: [
|
||||||
|
{type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -407,6 +410,7 @@ describe('transformSidebarItems', () => {
|
||||||
collapsed: false,
|
collapsed: false,
|
||||||
label: 'MODIFIED LABEL: Subcategory 1',
|
label: 'MODIFIED LABEL: Subcategory 1',
|
||||||
items: [{type: 'doc', id: 'doc1'}],
|
items: [{type: 'doc', id: 'doc1'}],
|
||||||
|
customProps: {fakeProp: false},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: 'category',
|
type: 'category',
|
||||||
|
@ -418,7 +422,9 @@ describe('transformSidebarItems', () => {
|
||||||
type: 'category',
|
type: 'category',
|
||||||
collapsed: false,
|
collapsed: false,
|
||||||
label: 'MODIFIED LABEL: Sub sub category 1',
|
label: 'MODIFIED LABEL: Sub sub category 1',
|
||||||
items: [{type: 'doc', id: 'doc3'}],
|
items: [
|
||||||
|
{type: 'doc', id: 'doc3', customProps: {lorem: 'ipsum'}},
|
||||||
|
],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
|
@ -21,13 +21,17 @@ declare module '@docusaurus/plugin-content-docs-types' {
|
||||||
permalinkToSidebar: PermalinkToSidebar;
|
permalinkToSidebar: PermalinkToSidebar;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PropSidebarItemLink = {
|
type PropsSidebarItemBase = {
|
||||||
|
customProps?: object;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PropSidebarItemLink = PropsSidebarItemBase & {
|
||||||
type: 'link';
|
type: 'link';
|
||||||
href: string;
|
href: string;
|
||||||
label: string;
|
label: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PropSidebarItemCategory = {
|
export type PropSidebarItemCategory = PropsSidebarItemBase & {
|
||||||
type: 'category';
|
type: 'category';
|
||||||
label: string;
|
label: string;
|
||||||
items: PropSidebarItem[];
|
items: PropSidebarItem[];
|
||||||
|
|
|
@ -39,6 +39,7 @@ Available document ids=
|
||||||
type: 'link',
|
type: 'link',
|
||||||
label: sidebar_label || title,
|
label: sidebar_label || title,
|
||||||
href: permalink,
|
href: permalink,
|
||||||
|
customProps: item.customProps,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ import importFresh from 'import-fresh';
|
||||||
import {
|
import {
|
||||||
Sidebars,
|
Sidebars,
|
||||||
SidebarItem,
|
SidebarItem,
|
||||||
|
SidebarItemBase,
|
||||||
SidebarItemLink,
|
SidebarItemLink,
|
||||||
SidebarItemDoc,
|
SidebarItemDoc,
|
||||||
Sidebar,
|
Sidebar,
|
||||||
|
@ -20,7 +21,7 @@ import {
|
||||||
import {mapValues, flatten, difference} from 'lodash';
|
import {mapValues, flatten, difference} from 'lodash';
|
||||||
import {getElementsAround} from '@docusaurus/utils';
|
import {getElementsAround} from '@docusaurus/utils';
|
||||||
|
|
||||||
type SidebarItemCategoryJSON = {
|
type SidebarItemCategoryJSON = SidebarItemBase & {
|
||||||
type: 'category';
|
type: 'category';
|
||||||
label: string;
|
label: string;
|
||||||
items: SidebarItemJSON[];
|
items: SidebarItemJSON[];
|
||||||
|
@ -96,7 +97,7 @@ function assertItem<K extends string>(
|
||||||
function assertIsCategory(
|
function assertIsCategory(
|
||||||
item: unknown,
|
item: unknown,
|
||||||
): asserts item is SidebarItemCategoryJSON {
|
): asserts item is SidebarItemCategoryJSON {
|
||||||
assertItem(item, ['items', 'label', 'collapsed']);
|
assertItem(item, ['items', 'label', 'collapsed', 'customProps']);
|
||||||
if (typeof item.label !== 'string') {
|
if (typeof item.label !== 'string') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Error loading ${JSON.stringify(item)}. "label" must be a string.`,
|
`Error loading ${JSON.stringify(item)}. "label" must be a string.`,
|
||||||
|
@ -116,7 +117,7 @@ function assertIsCategory(
|
||||||
}
|
}
|
||||||
|
|
||||||
function assertIsDoc(item: unknown): asserts item is SidebarItemDoc {
|
function assertIsDoc(item: unknown): asserts item is SidebarItemDoc {
|
||||||
assertItem(item, ['id']);
|
assertItem(item, ['id', 'customProps']);
|
||||||
if (typeof item.id !== 'string') {
|
if (typeof item.id !== 'string') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Error loading ${JSON.stringify(item)}. "id" must be a string.`,
|
`Error loading ${JSON.stringify(item)}. "id" must be a string.`,
|
||||||
|
@ -125,7 +126,7 @@ function assertIsDoc(item: unknown): asserts item is SidebarItemDoc {
|
||||||
}
|
}
|
||||||
|
|
||||||
function assertIsLink(item: unknown): asserts item is SidebarItemLink {
|
function assertIsLink(item: unknown): asserts item is SidebarItemLink {
|
||||||
assertItem(item, ['href', 'label']);
|
assertItem(item, ['href', 'label', 'customProps']);
|
||||||
if (typeof item.href !== 'string') {
|
if (typeof item.href !== 'string') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Error loading ${JSON.stringify(item)}. "href" must be a string.`,
|
`Error loading ${JSON.stringify(item)}. "href" must be a string.`,
|
||||||
|
|
|
@ -75,18 +75,22 @@ export type PluginOptions = MetadataOptions &
|
||||||
includeCurrentVersion: boolean;
|
includeCurrentVersion: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SidebarItemDoc = {
|
export type SidebarItemBase = {
|
||||||
|
customProps?: object;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SidebarItemDoc = SidebarItemBase & {
|
||||||
type: 'doc' | 'ref';
|
type: 'doc' | 'ref';
|
||||||
id: string;
|
id: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SidebarItemLink = {
|
export type SidebarItemLink = SidebarItemBase & {
|
||||||
type: 'link';
|
type: 'link';
|
||||||
href: string;
|
href: string;
|
||||||
label: string;
|
label: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SidebarItemCategory = {
|
export type SidebarItemCategory = SidebarItemBase & {
|
||||||
type: 'category';
|
type: 'category';
|
||||||
label: string;
|
label: string;
|
||||||
items: SidebarItem[];
|
items: SidebarItem[];
|
||||||
|
|
|
@ -288,6 +288,20 @@ module.exports = {
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Custom Props
|
||||||
|
|
||||||
|
If you would like to pass in custom props to a swizzled sidebar item, an optional object called `customProps` can be added to any of the items:
|
||||||
|
|
||||||
|
```js
|
||||||
|
{
|
||||||
|
type: 'doc';
|
||||||
|
id: 'doc1';
|
||||||
|
customProps: {
|
||||||
|
/* props */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
#### Collapsible categories
|
#### Collapsible categories
|
||||||
|
|
||||||
For sites with a sizable amount of content, we support the option to expand/collapse a category to toggle the display of its contents. Categories are collapsible by default. If you want them to be always expanded, set `themeConfig.sidebarCollapsible` to `false`:
|
For sites with a sizable amount of content, we support the option to expand/collapse a category to toggle the display of its contents. Categories are collapsible by default. If you want them to be always expanded, set `themeConfig.sidebarCollapsible` to `false`:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue