Skip to content

Commit

Permalink
feat(v2): redesign mobile UX: inline TOC + doc sidebar in main menu (#…
Browse files Browse the repository at this point in the history
…4273)

* feat(v2): mobile TOC

* Bug fixes and various improvements

* Redesign

* extract TOCCollapsible component

* TS improvements

* Assign sidebar name directly to the doc route => no need for either permalinkToSidebar or GlobalData

* revert changes to useWindowSize, fix FOUC issues

* extract DocSidebarDesktop component

* remove now useless menu infima classes

* TOCHeadings => rename + remove unused onClick prop

* Extract DocSidebarItem

* minor renaming

* replace GlobalData usage by a React teleport system to render in the navbar mobile sidebar menu directly from the DocPage component

* useWindowSize => simulate SSR size in dev to make FOUC issues more obvious

* fix remaining sidebar layout shift

* update docs snapshots

* remove unused code translations

* remove unused code translations

* fix minor update-code-translations bug

* Add more build-size paths to watch

* Restyle back button

* Add  missing`menu` class

* extract useShallowMemoizedObject

* fix routes tests + better routes formatting

* use Translate api for labels

* use Translate api for labels

* Update translations

* Improve dark mode support for back button

* Merge branch 'master' into lex111/inline-color-code

# Conflicts:
#	packages/core/dist/css/default-dark/default-dark-rtl.min.css
#	packages/core/dist/css/default-dark/default-dark.min.css
#	packages/core/dist/css/default/default-rtl.min.css
#	packages/core/dist/css/default/default.min.css

* replace useCollapse by new useCollapsible

* Cleanup and use clean-btn for TOCCollapsible button

* Make TOC links clickable over full width

* Cleanup

* fix uncollapsible sidebar that can be collapsed + create <Collapsible> component

* dependency array typo

* rollback sidebars community commit typo

Co-authored-by: slorber <lorber.sebastien@gmail.com>
  • Loading branch information
lex111 and slorber authored Jul 9, 2021
1 parent f03479f commit 9536ef9
Show file tree
Hide file tree
Showing 58 changed files with 1,004 additions and 631 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/v2-build-size-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
build-script: 'build:v2:en'
pattern: '{website/build/assets/js/main*js,website/build/assets/css/styles*css,website/.docusaurus/globalData.json,website/build/index.html,website/build/blog/**/introducing-docusaurus/*,website/build/docs/index.html,website/build/docs/introduction/index.html,website/build/docs-tests/index.html,website/build/docs-tests/standalone/index.html}'
pattern: '{website/build/assets/js/main*js,website/build/assets/css/styles*css,website/.docusaurus/globalData.json,website/build/index.html,website/build/blog/index.html,website/build/blog/**/introducing-docusaurus/*,website/build/docs/index.html,website/build/docs/installation/index.html,website/build/docs-tests/index.html,website/build/docs-tests/standalone/index.html}'
strip-hash: '\.([^;]\w{7})\.'
minimum-change-threshold: 30
compression: 'none'
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,6 @@ Object {
]
}
]
},
\\"permalinkToSidebar\\": {
\\"/docs/foo/bar\\": \\"docs\\",
\\"/docs/foo/bazSlug.html\\": \\"docs\\",
\\"/docs/\\": \\"docs\\"
}
}",
}
Expand Down Expand Up @@ -575,6 +570,7 @@ Array [
"content": "@site/docs/hello.md",
},
"path": "/docs/",
"sidebar": "docs",
},
Object {
"component": "@theme/DocItem",
Expand All @@ -591,6 +587,7 @@ Array [
"content": "@site/docs/foo/bar.md",
},
"path": "/docs/foo/bar",
"sidebar": "docs",
},
Object {
"component": "@theme/DocItem",
Expand All @@ -599,6 +596,7 @@ Array [
"content": "@site/docs/foo/baz.md",
},
"path": "/docs/foo/bazSlug.html",
"sidebar": "docs",
},
Object {
"component": "@theme/DocItem",
Expand Down Expand Up @@ -883,9 +881,6 @@ Object {
\\"href\\": \\"/community/team\\"
}
]
},
\\"permalinkToSidebar\\": {
\\"/community/team\\": \\"version-1.0.0/community\\"
}
}",
"version-current-metadata-prop-751.json": "{
Expand All @@ -902,9 +897,6 @@ Object {
\\"href\\": \\"/community/next/team\\"
}
]
},
\\"permalinkToSidebar\\": {
\\"/community/next/team\\": \\"community\\"
}
}",
}
Expand Down Expand Up @@ -968,6 +960,7 @@ Array [
"content": "@site/i18n/en/docusaurus-plugin-content-docs-community/current/team.md",
},
"path": "/community/next/team",
"sidebar": "community",
},
],
},
Expand All @@ -987,6 +980,7 @@ Array [
"content": "@site/community_versioned_docs/version-1.0.0/team.md",
},
"path": "/community/team",
"sidebar": "version-1.0.0/community",
},
],
},
Expand Down Expand Up @@ -1439,11 +1433,6 @@ Object {
]
}
]
},
\\"permalinkToSidebar\\": {
\\"/docs/1.0.0/foo/barSlug\\": \\"version-1.0.0/docs\\",
\\"/docs/1.0.0/foo/baz\\": \\"version-1.0.0/docs\\",
\\"/docs/1.0.0/\\": \\"version-1.0.0/docs\\"
}
}",
"version-1-0-1-metadata-prop-e87.json": "{
Expand Down Expand Up @@ -1479,10 +1468,6 @@ Object {
]
}
]
},
\\"permalinkToSidebar\\": {
\\"/docs/foo/bar\\": \\"version-1.0.1/docs\\",
\\"/docs/\\": \\"version-1.0.1/docs\\"
}
}",
"version-current-metadata-prop-751.json": "{
Expand Down Expand Up @@ -1518,10 +1503,6 @@ Object {
]
}
]
},
\\"permalinkToSidebar\\": {
\\"/docs/next/foo/barSlug\\": \\"docs\\",
\\"/docs/next/\\": \\"docs\\"
}
}",
"version-with-slugs-metadata-prop-2bf.json": "{
Expand All @@ -1545,9 +1526,6 @@ Object {
]
}
]
},
\\"permalinkToSidebar\\": {
\\"/docs/withSlugs/rootAbsoluteSlug\\": \\"version-1.0.1/docs\\"
}
}",
}
Expand Down Expand Up @@ -1714,6 +1692,7 @@ Array [
"content": "@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md",
},
"path": "/docs/1.0.0/",
"sidebar": "version-1.0.0/docs",
},
Object {
"component": "@theme/DocItem",
Expand All @@ -1722,6 +1701,7 @@ Array [
"content": "@site/versioned_docs/version-1.0.0/foo/bar.md",
},
"path": "/docs/1.0.0/foo/barSlug",
"sidebar": "version-1.0.0/docs",
},
Object {
"component": "@theme/DocItem",
Expand All @@ -1730,6 +1710,7 @@ Array [
"content": "@site/versioned_docs/version-1.0.0/foo/baz.md",
},
"path": "/docs/1.0.0/foo/baz",
"sidebar": "version-1.0.0/docs",
},
],
},
Expand All @@ -1749,6 +1730,7 @@ Array [
"content": "@site/docs/hello.md",
},
"path": "/docs/next/",
"sidebar": "docs",
},
Object {
"component": "@theme/DocItem",
Expand All @@ -1765,6 +1747,7 @@ Array [
"content": "@site/docs/foo/bar.md",
},
"path": "/docs/next/foo/barSlug",
"sidebar": "docs",
},
Object {
"component": "@theme/DocItem",
Expand Down Expand Up @@ -1824,6 +1807,7 @@ Array [
"content": "@site/versioned_docs/version-withSlugs/rootAbsoluteSlug.md",
},
"path": "/docs/withSlugs/rootAbsoluteSlug",
"sidebar": "version-1.0.1/docs",
},
Object {
"component": "@theme/DocItem",
Expand Down Expand Up @@ -1883,6 +1867,7 @@ Array [
"content": "@site/versioned_docs/version-1.0.1/hello.md",
},
"path": "/docs/",
"sidebar": "version-1.0.1/docs",
},
Object {
"component": "@theme/DocItem",
Expand All @@ -1891,6 +1876,7 @@ Array [
"content": "@site/versioned_docs/version-1.0.1/foo/bar.md",
},
"path": "/docs/foo/bar",
"sidebar": "version-1.0.1/docs",
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ Object {
],
"isLast": true,
"mainDocId": "",
"permalinkToSidebar": Object {},
"routePriority": undefined,
"sidebarFilePath": "any",
"sidebars": Object {
Expand Down Expand Up @@ -292,7 +291,6 @@ Object {
],
"isLast": true,
"mainDocId": "",
"permalinkToSidebar": Object {},
"routePriority": undefined,
"sidebarFilePath": "any",
"sidebars": Object {
Expand Down Expand Up @@ -433,7 +431,6 @@ Object {
],
"isLast": true,
"mainDocId": "",
"permalinkToSidebar": Object {},
"routePriority": undefined,
"sidebarFilePath": "any",
"sidebars": Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ Entries created:
`version-${kebabCase(version.versionName)}-metadata-prop`,
);
expect(versionMetadataProp.docsSidebars).toEqual(toSidebarsProp(version));
expect(versionMetadataProp.permalinkToSidebar).toEqual(
version.permalinkToSidebar,
);
},

expectSnapshot: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ function createSampleVersion(
versionLabel: `${version.versionName} label`,
versionPath: '/docs/',
mainDocId: '',
permalinkToSidebar: {},
routePriority: undefined,
sidebarFilePath: 'any',
isLast: true,
Expand Down
34 changes: 16 additions & 18 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
DocFile,
DocsMarkdownOption,
} from './types';
import {PermalinkToSidebar} from '@docusaurus/plugin-content-docs-types';
import {RuleSetRule} from 'webpack';
import {cliDocsVersionCommand} from './cli';
import {VERSIONS_JSON_FILE} from './constants';
Expand Down Expand Up @@ -224,14 +223,6 @@ export default function pluginContentDocs(
// sort to ensure consistent output for tests
docs.sort((a, b) => a.id.localeCompare(b.id));

// TODO really useful? replace with global state logic?
const permalinkToSidebar: PermalinkToSidebar = {};
Object.values(docs).forEach((doc) => {
if (doc.sidebar) {
permalinkToSidebar[doc.permalink] = doc.sidebar;
}
});

// The "main doc" is the "version entry point"
// We browse this doc by clicking on a version:
// - the "home" doc (at '/docs/')
Expand All @@ -256,7 +247,6 @@ export default function pluginContentDocs(
...versionMetadata,
mainDocId: getMainDoc().unversionedId,
sidebars,
permalinkToSidebar,
docs: docs.map(addNavData),
};
}
Expand Down Expand Up @@ -300,30 +290,36 @@ export default function pluginContentDocs(
JSON.stringify(metadataItem, null, 2),
);

return {
const docRoute: RouteConfig = {
path: metadataItem.permalink,
component: docItemComponent,
exact: true,
modules: {
content: metadataItem.source,
},
// Because the parent (DocPage) comp need to access it easily
// This permits to render the sidebar once without unmount/remount when navigating (and preserve sidebar state)
...(metadataItem.sidebar && {
sidebar: metadataItem.sidebar,
}),
};

return docRoute;
}),
);

return routes.sort((a, b) => a.path.localeCompare(b.path));
};

async function doCreateVersionRoutes(loadedVersion: LoadedVersion) {
async function doCreateVersionRoutes(
loadedVersion: LoadedVersion,
): Promise<void> {
const versionMetadata = toVersionMetadataProp(pluginId, loadedVersion);
const versionMetadataPropPath = await createData(
`${docuHash(
`version-${loadedVersion.versionName}-metadata-prop`,
)}.json`,
JSON.stringify(
toVersionMetadataProp(pluginId, loadedVersion),
null,
2,
),
JSON.stringify(versionMetadata, null, 2),
);

addRoute({
Expand All @@ -341,7 +337,9 @@ export default function pluginContentDocs(
});
}

async function createVersionRoutes(loadedVersion: LoadedVersion) {
async function createVersionRoutes(
loadedVersion: LoadedVersion,
): Promise<void> {
try {
return await doCreateVersionRoutes(loadedVersion);
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@
declare module '@docusaurus/plugin-content-docs-types' {
import type {VersionBanner} from './types';

export type PermalinkToSidebar = {
[permalink: string]: string;
};

export type PropVersionMetadata = {
pluginId: string;
version: string;
label: string;
banner: VersionBanner;
isLast: boolean;
docsSidebars: PropSidebars;
permalinkToSidebar: PermalinkToSidebar;
};

type PropsSidebarItemBase = {
Expand Down Expand Up @@ -60,6 +55,7 @@ declare module '@theme/DocItem' {
readonly component: () => JSX.Element;
readonly exact: boolean;
readonly path: string;
readonly sidebar?: string;
};

export type FrontMatter = {
Expand Down
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,5 @@ export function toVersionMetadataProp(
banner: loadedVersion.versionBanner,
isLast: loadedVersion.isLast,
docsSidebars: toSidebarsProp(loadedVersion),
permalinkToSidebar: loadedVersion.permalinkToSidebar,
};
}
1 change: 0 additions & 1 deletion packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ export type LoadedVersion = VersionMetadata & {
mainDocId: string;
docs: DocMetadata[];
sidebars: Sidebars;
permalinkToSidebar: Record<string, string>;
};

export type LoadedContent = {
Expand Down
Loading

0 comments on commit 9536ef9

Please sign in to comment.