-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add version dropdown to navbar to make earlier versions more accessib…
…le (#131) There is a bug in the dropdown component, which I'm fixing in facebook/docusaurus#3472 Meanwhile, I swizzled the buggy component and applied my fix.
- Loading branch information
1 parent
b3ffc7d
commit 35afc1b
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import React from 'react'; | ||
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem'; | ||
import { | ||
useVersions, | ||
useLatestVersion, | ||
useActiveDocContext, | ||
} from '@theme/hooks/useDocs'; | ||
import type { Props } from '@theme/NavbarItem/DocsVersionDropdownNavbarItem'; | ||
|
||
const getVersionMainDoc = (version) => | ||
version.docs.find((doc) => doc.id === version.mainDocId); | ||
|
||
export default function DocsVersionDropdownNavbarItem({ | ||
mobile, | ||
docsPluginId, | ||
...props | ||
}: Props): JSX.Element { | ||
const activeDocContext = useActiveDocContext(docsPluginId); | ||
const versions = useVersions(docsPluginId); | ||
const latestVersion = useLatestVersion(docsPluginId); | ||
|
||
function getItems() { | ||
// 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 (versions.length <= 1) { | ||
return undefined; | ||
} | ||
|
||
return versions.map((version) => { | ||
// We try to link to the same doc, in another version | ||
// When not possible, fallback to the "main doc" of the version | ||
const versionDoc = | ||
activeDocContext?.alternateDocVersions[version.name] || | ||
getVersionMainDoc(version); | ||
return { | ||
isNavLink: true, | ||
label: version.label, | ||
to: versionDoc.path, | ||
isActive: () => version === activeDocContext?.activeVersion, | ||
}; | ||
}); | ||
} | ||
|
||
const dropdownVersion = activeDocContext.activeVersion ?? latestVersion; | ||
|
||
// Mobile is handled a bit differently | ||
const dropdownLabel = mobile ? 'Versions' : dropdownVersion.label; | ||
const dropdownTo = mobile | ||
? undefined | ||
: getVersionMainDoc(dropdownVersion).path; | ||
|
||
return ( | ||
<DefaultNavbarItem | ||
{...props} | ||
mobile={mobile} | ||
label={dropdownLabel} | ||
to={dropdownTo} | ||
items={getItems()} | ||
/> | ||
); | ||
} |