Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe('themeConfig', () => {
{
type: 'docsVersionDropdown',
position: 'left',
dropdownItemsBefore: [],
dropdownItemsAfter: [],
},
{
to: 'docs/next/support',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,16 @@ export default function DocsVersionDropdownNavbarItem({
mobile,
docsPluginId,
dropdownActiveClassDisabled,
dropdownItemsBefore,
dropdownItemsAfter,
...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) => {
const versionLinks = 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 =
Expand All @@ -48,6 +43,21 @@ export default function DocsVersionDropdownNavbarItem({
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;
Expand Down
3 changes: 3 additions & 0 deletions packages/docusaurus-theme-classic/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,13 @@ declare module '@theme/NavbarItem/DefaultNavbarItem' {

declare module '@theme/NavbarItem/DocsVersionDropdownNavbarItem' {
import type {Props as DefaultNavbarItemProps} from '@theme/NavbarItem/DefaultNavbarItem';
import type {NavLinkProps} from '@theme/NavbarItem/DefaultNavbarItem';

export type Props = DefaultNavbarItemProps & {
readonly docsPluginId?: string;
dropdownActiveClassDisabled?: boolean;
dropdownItemsBefore: NavLinkProps[];
dropdownItemsAfter: NavLinkProps[];
};

const DocsVersionDropdownNavbarItem: (props: Props) => JSX.Element;
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-theme-classic/src/validateThemeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const DocsVersionDropdownNavbarItemSchema = Joi.object({
position: NavbarItemPosition,
docsPluginId: Joi.string(),
dropdownActiveClassDisabled: Joi.boolean(),
dropdownItemsBefore: Joi.array().items(DefaultNavbarItemSchema).default([]),
dropdownItemsAfter: Joi.array().items(DefaultNavbarItemSchema).default([]),
});

const DocItemSchema = Joi.object({
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus/src/client/exports/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface Props {
readonly href?: string;
readonly activeClassName?: string;
readonly children?: ReactNode;
readonly isActive?: () => boolean;

// escape hatch in case broken links check is annoying for a specific link
readonly 'data-noBrokenLinkCheck'?: boolean;
Expand All @@ -42,6 +43,7 @@ function Link({
to,
href,
activeClassName,
isActive,
'data-noBrokenLinkCheck': noBrokenLinkCheck,
...props
}: Props): JSX.Element {
Expand Down Expand Up @@ -154,7 +156,7 @@ function Link({
innerRef={handleRef}
to={targetLink || ''}
// avoid "React does not recognize the `activeClassName` prop on a DOM element"
{...(isNavLink && {activeClassName})}
{...(isNavLink && {isActive, activeClassName})}
/>
);
}
Expand Down
8 changes: 7 additions & 1 deletion website/docs/theme-classic.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,13 @@ module.exports = {
{
type: 'docsVersionDropdown',
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,
},
],
},
Expand Down
16 changes: 9 additions & 7 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,6 @@ module.exports = {
srcDark: 'img/docusaurus_keytar.svg',
},
items: [
{
type: 'docsVersionDropdown',
position: 'left',
dropdownActiveClassDisabled: true,
},
{
type: 'doc',
position: 'left',
Expand All @@ -287,10 +282,17 @@ module.exports = {
position: 'left',
activeBaseRegex: `/community/`,
},
// right
{
to: '/versions',
label: 'All versions',
type: 'docsVersionDropdown',
position: 'right',
dropdownActiveClassDisabled: true,
dropdownItemsAfter: [
{
to: '/versions',
label: 'All versions',
},
],
},
{
href: 'https://github.com/facebook/docusaurus',
Expand Down