mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-24 21:48:02 +02:00
fix(v2): fix FOUC in doc sidebar and various improvements (#2867)
* bug(v2): fix active sidebar item detection logic (https://github.com/facebook/docusaurus/pull/2682#issuecomment-636631225) * fix sidebar category collapsed normalization to make sure we always have a boolean after normalization * fix sidebarCollapsible option
This commit is contained in:
parent
b8de9c6ded
commit
6797af660f
7 changed files with 131 additions and 137 deletions
|
@ -9,30 +9,25 @@ module.exports = {
|
|||
docs: [
|
||||
{
|
||||
type: 'category',
|
||||
collapsed: true,
|
||||
label: 'level 1',
|
||||
items: [
|
||||
'a',
|
||||
{
|
||||
type: 'category',
|
||||
collapsed: true,
|
||||
label: 'level 2',
|
||||
items: [
|
||||
{
|
||||
type: 'category',
|
||||
collapsed: true,
|
||||
label: 'level 3',
|
||||
items: [
|
||||
'c',
|
||||
{
|
||||
type: 'category',
|
||||
collapsed: true,
|
||||
label: 'level 4',
|
||||
items: [
|
||||
'd',
|
||||
{
|
||||
type: 'category',
|
||||
collapsed: true,
|
||||
label: 'deeper more more',
|
||||
items: ['e'],
|
||||
},
|
||||
|
|
|
@ -7,6 +7,7 @@ Object {
|
|||
"collapsed": true,
|
||||
"items": Array [
|
||||
Object {
|
||||
"collapsed": true,
|
||||
"items": Array [
|
||||
Object {
|
||||
"href": "/docs/foo/bar",
|
||||
|
|
|
@ -157,6 +157,7 @@ exports[`loadSidebars sidebars with first level not a category 1`] = `
|
|||
Object {
|
||||
"docs": Array [
|
||||
Object {
|
||||
"collapsed": true,
|
||||
"items": Array [
|
||||
Object {
|
||||
"id": "greeting",
|
||||
|
|
|
@ -7,6 +7,7 @@ Object {
|
|||
"collapsed": true,
|
||||
"items": Array [
|
||||
Object {
|
||||
"collapsed": true,
|
||||
"items": Array [
|
||||
Object {
|
||||
"id": "version-1.0.0/foo/bar",
|
||||
|
|
|
@ -25,6 +25,9 @@ function isCategoryShorthand(
|
|||
return typeof item !== 'string' && !item.type;
|
||||
}
|
||||
|
||||
// categories are collapsed by default, unless user set collapsed = false
|
||||
const defaultCategoryCollapsedValue = true;
|
||||
|
||||
/**
|
||||
* Convert {category1: [item1,item2]} shorthand syntax to long-form syntax
|
||||
*/
|
||||
|
@ -33,7 +36,7 @@ function normalizeCategoryShorthand(
|
|||
): SidebarItemCategoryRaw[] {
|
||||
return Object.entries(sidebar).map(([label, items]) => ({
|
||||
type: 'category',
|
||||
collapsed: true,
|
||||
collapsed: defaultCategoryCollapsedValue,
|
||||
label,
|
||||
items,
|
||||
}));
|
||||
|
@ -118,7 +121,13 @@ function normalizeItem(item: SidebarItemRaw): SidebarItem[] {
|
|||
switch (item.type) {
|
||||
case 'category':
|
||||
assertIsCategory(item);
|
||||
return [{...item, items: flatMap(item.items, normalizeItem)}];
|
||||
return [
|
||||
{
|
||||
collapsed: defaultCategoryCollapsedValue,
|
||||
...item,
|
||||
items: flatMap(item.items, normalizeItem),
|
||||
},
|
||||
];
|
||||
case 'link':
|
||||
assertIsLink(item);
|
||||
return [item];
|
||||
|
|
|
@ -42,7 +42,7 @@ export interface SidebarItemCategory {
|
|||
type: 'category';
|
||||
label: string;
|
||||
items: SidebarItem[];
|
||||
collapsed?: boolean;
|
||||
collapsed: boolean;
|
||||
}
|
||||
|
||||
export interface SidebarItemCategoryRaw {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue