feat(v2): version dropdown before/after items + move site "All Versions" link (#3548)

* add dropdownItemsAfter option + reorg a bit docusaurus site navbar

* fix isActive  react warning

* Update theme-classic.md

Co-authored-by: Alexey Pyltsyn <lex61rus@gmail.com>
This commit is contained in:
Sébastien Lorber 2020-10-07 18:58:00 +02:00 committed by GitHub
parent 5b79f2ebc2
commit 649882d867
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 17 deletions

View file

@ -57,6 +57,8 @@ describe('themeConfig', () => {
{ {
type: 'docsVersionDropdown', type: 'docsVersionDropdown',
position: 'left', position: 'left',
dropdownItemsBefore: [],
dropdownItemsAfter: [],
}, },
{ {
to: 'docs/next/support', to: 'docs/next/support',

View file

@ -21,6 +21,8 @@ export default function DocsVersionDropdownNavbarItem({
mobile, mobile,
docsPluginId, docsPluginId,
dropdownActiveClassDisabled, dropdownActiveClassDisabled,
dropdownItemsBefore,
dropdownItemsAfter,
...props ...props
}: Props): JSX.Element { }: Props): JSX.Element {
const activeDocContext = useActiveDocContext(docsPluginId); const activeDocContext = useActiveDocContext(docsPluginId);
@ -28,14 +30,7 @@ export default function DocsVersionDropdownNavbarItem({
const latestVersion = useLatestVersion(docsPluginId); const latestVersion = useLatestVersion(docsPluginId);
function getItems() { function getItems() {
// We don't want to render a version dropdown with 0 or 1 item const versionLinks = versions.map((version) => {
// If we build the site with a single docs version (onlyIncludeVersions: ['1.0.0'])
// We'd rather render a buttonb instead of a dropdown
if (versions.length <= 1) {
return undefined;
}
return versions.map((version) => {
// We try to link to the same doc, in another version // We try to link to the same doc, in another version
// When not possible, fallback to the "main doc" of the version // When not possible, fallback to the "main doc" of the version
const versionDoc = const versionDoc =
@ -48,6 +43,21 @@ export default function DocsVersionDropdownNavbarItem({
isActive: () => version === activeDocContext?.activeVersion, isActive: () => version === activeDocContext?.activeVersion,
}; };
}); });
const items = [
...dropdownItemsBefore,
...versionLinks,
...dropdownItemsAfter,
];
// We don't want to render a version dropdown with 0 or 1 item
// If we build the site with a single docs version (onlyIncludeVersions: ['1.0.0'])
// We'd rather render a buttonb instead of a dropdown
if (items.length <= 1) {
return undefined;
}
return items;
} }
const dropdownVersion = activeDocContext.activeVersion ?? latestVersion; const dropdownVersion = activeDocContext.activeVersion ?? latestVersion;

View file

@ -308,10 +308,13 @@ declare module '@theme/NavbarItem/DefaultNavbarItem' {
declare module '@theme/NavbarItem/DocsVersionDropdownNavbarItem' { declare module '@theme/NavbarItem/DocsVersionDropdownNavbarItem' {
import type {Props as DefaultNavbarItemProps} from '@theme/NavbarItem/DefaultNavbarItem'; import type {Props as DefaultNavbarItemProps} from '@theme/NavbarItem/DefaultNavbarItem';
import type {NavLinkProps} from '@theme/NavbarItem/DefaultNavbarItem';
export type Props = DefaultNavbarItemProps & { export type Props = DefaultNavbarItemProps & {
readonly docsPluginId?: string; readonly docsPluginId?: string;
dropdownActiveClassDisabled?: boolean; dropdownActiveClassDisabled?: boolean;
dropdownItemsBefore: NavLinkProps[];
dropdownItemsAfter: NavLinkProps[];
}; };
const DocsVersionDropdownNavbarItem: (props: Props) => JSX.Element; const DocsVersionDropdownNavbarItem: (props: Props) => JSX.Element;

View file

@ -69,6 +69,8 @@ const DocsVersionDropdownNavbarItemSchema = Joi.object({
position: NavbarItemPosition, position: NavbarItemPosition,
docsPluginId: Joi.string(), docsPluginId: Joi.string(),
dropdownActiveClassDisabled: Joi.boolean(), dropdownActiveClassDisabled: Joi.boolean(),
dropdownItemsBefore: Joi.array().items(DefaultNavbarItemSchema).default([]),
dropdownItemsAfter: Joi.array().items(DefaultNavbarItemSchema).default([]),
}); });
const DocItemSchema = Joi.object({ const DocItemSchema = Joi.object({

View file

@ -25,6 +25,7 @@ interface Props {
readonly href?: string; readonly href?: string;
readonly activeClassName?: string; readonly activeClassName?: string;
readonly children?: ReactNode; readonly children?: ReactNode;
readonly isActive?: () => boolean;
// escape hatch in case broken links check is annoying for a specific link // escape hatch in case broken links check is annoying for a specific link
readonly 'data-noBrokenLinkCheck'?: boolean; readonly 'data-noBrokenLinkCheck'?: boolean;
@ -42,6 +43,7 @@ function Link({
to, to,
href, href,
activeClassName, activeClassName,
isActive,
'data-noBrokenLinkCheck': noBrokenLinkCheck, 'data-noBrokenLinkCheck': noBrokenLinkCheck,
...props ...props
}: Props): JSX.Element { }: Props): JSX.Element {
@ -154,7 +156,7 @@ function Link({
innerRef={handleRef} innerRef={handleRef}
to={targetLink || ''} to={targetLink || ''}
// avoid "React does not recognize the `activeClassName` prop on a DOM element" // avoid "React does not recognize the `activeClassName` prop on a DOM element"
{...(isNavLink && {activeClassName})} {...(isNavLink && {isActive, activeClassName})}
/> />
); );
} }

View file

@ -296,7 +296,13 @@ module.exports = {
{ {
type: 'docsVersionDropdown', type: 'docsVersionDropdown',
position: 'left', position: 'left',
// dropdownActiveClassDisabled: true, //
// Add additional dropdown items at the beginning/end of the dropdown.
dropdownItemsBefore: [],
dropdownItemsAfter: [{to: '/versions', label: 'All versions'}],
// Do not add the link active class when browsing docs.
dropdownActiveClassDisabled: true,
}, },
], ],
}, },

View file

@ -262,11 +262,6 @@ module.exports = {
srcDark: 'img/docusaurus_keytar.svg', srcDark: 'img/docusaurus_keytar.svg',
}, },
items: [ items: [
{
type: 'docsVersionDropdown',
position: 'left',
dropdownActiveClassDisabled: true,
},
{ {
type: 'doc', type: 'doc',
position: 'left', position: 'left',
@ -287,10 +282,17 @@ module.exports = {
position: 'left', position: 'left',
activeBaseRegex: `/community/`, activeBaseRegex: `/community/`,
}, },
// right
{ {
to: '/versions', type: 'docsVersionDropdown',
label: 'All versions',
position: 'right', position: 'right',
dropdownActiveClassDisabled: true,
dropdownItemsAfter: [
{
to: '/versions',
label: 'All versions',
},
],
}, },
{ {
href: 'https://github.com/facebook/docusaurus', href: 'https://github.com/facebook/docusaurus',