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

fix(theme-classic): consistently apply the right active class name for all navbar items #7430

Merged
merged 3 commits into from
May 25, 2022
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
7 changes: 0 additions & 7 deletions packages/docusaurus-theme-classic/src/getSwizzleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,6 @@ export default function getSwizzleConfig(): SwizzleConfig {
description:
'The Navbar item components mapping. Can be ejected to add custom navbar item types. See https://github.com/facebook/docusaurus/issues/7227.',
},
// TODO should probably not even appear here
'NavbarItem/utils': {
actions: {
eject: 'forbidden',
wrap: 'forbidden',
},
},
NotFound: {
actions: {
eject: 'safe',
Expand Down
14 changes: 2 additions & 12 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,8 @@ declare module '@theme/NavbarItem/NavbarNavLink' {
readonly exact?: boolean;
readonly label?: ReactNode;
readonly html?: string;
readonly prependBaseUrlToHref?: string;
readonly prependBaseUrlToHref?: boolean;
readonly isDropdownLink?: boolean;
}

export default function NavbarNavLink(props: Props): JSX.Element;
Expand Down Expand Up @@ -980,17 +981,6 @@ declare module '@theme/NavbarItem' {
export default function NavbarItem(props: Props): JSX.Element;
}

declare module '@theme/NavbarItem/utils' {
/**
* On desktop and mobile, we would apply different class names for dropdown
* items.
* @see https://github.com/facebook/docusaurus/pull/5431
*/
export function getInfimaActiveClassName(
mobile?: boolean,
): `${'menu' | 'navbar'}__link--active`;
}

declare module '@theme/PaginatorNavLink' {
import type {ReactNode} from 'react';
import type {PropNavigationLink} from '@docusaurus/plugin-content-docs';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import React from 'react';
import clsx from 'clsx';
import NavbarNavLink from '@theme/NavbarItem/NavbarNavLink';
import {getInfimaActiveClassName} from '@theme/NavbarItem/utils';
import type {
DesktopOrMobileNavBarItemProps,
Props,
Expand All @@ -25,6 +24,7 @@ function DefaultNavbarItemDesktop({
isDropdownItem ? 'dropdown__link' : 'navbar__item navbar__link',
className,
)}
isDropdownLink={isDropdownItem}
{...props}
/>
);
Expand Down Expand Up @@ -58,7 +58,8 @@ export default function DefaultNavbarItem({
<Comp
{...props}
activeClassName={
props.activeClassName ?? getInfimaActiveClassName(mobile)
props.activeClassName ??
(mobile ? 'menu__link--active' : 'navbar__link--active')
}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
*/

import React from 'react';
import clsx from 'clsx';
import {useActiveDocContext} from '@docusaurus/plugin-content-docs/client';
import {useLayoutDoc} from '@docusaurus/theme-common';
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
import {getInfimaActiveClassName} from '@theme/NavbarItem/utils';
import type {Props} from '@theme/NavbarItem/DocNavbarItem';

export default function DocNavbarItem({
Expand All @@ -27,20 +25,14 @@ export default function DocNavbarItem({
return null;
}

const activeDocInfimaClassName = getInfimaActiveClassName(props.mobile);

return (
<DefaultNavbarItem
exact
{...props}
className={clsx(props.className, {
[activeDocInfimaClassName]:
// Do not make the item active if the active doc doesn't have sidebar.
// If `activeDoc === doc` react-router will make it active anyways,
// regardless of the existence of a sidebar
activeDoc?.sidebar && activeDoc.sidebar === doc.sidebar,
})}
activeClassName={activeDocInfimaClassName}
isActive={() =>
activeDoc?.path === doc.path ||
(!!activeDoc?.sidebar && activeDoc.sidebar === doc.sidebar)
}
label={staticLabel ?? doc.id}
to={doc.path}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
*/

import React from 'react';
import clsx from 'clsx';
import {useActiveDocContext} from '@docusaurus/plugin-content-docs/client';
import {useLayoutDocsSidebar} from '@docusaurus/theme-common';
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
import {getInfimaActiveClassName} from '@theme/NavbarItem/utils';
import type {Props} from '@theme/NavbarItem/DocSidebarNavbarItem';

export default function DocSidebarNavbarItem({
Expand All @@ -26,16 +24,11 @@ export default function DocSidebarNavbarItem({
`DocSidebarNavbarItem: Sidebar with ID "${sidebarId}" doesn't have anything to be linked to.`,
);
}
const activeDocInfimaClassName = getInfimaActiveClassName(props.mobile);

return (
<DefaultNavbarItem
exact
{...props}
className={clsx(props.className, {
[activeDocInfimaClassName]: activeDoc?.sidebar === sidebarId,
})}
activeClassName={activeDocInfimaClassName}
isActive={() => activeDoc?.sidebar === sidebarId}
label={label ?? sidebarLink.label}
to={sidebarLink.path}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export default function DocsVersionDropdownNavbarItem({
activeDocContext.alternateDocVersions[version.name] ??
getVersionMainDoc(version);
return {
isNavLink: true,
label: version.label,
to: versionDoc.path,
isActive: () => version === activeDocContext.activeVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import type {
Props,
} from '@theme/NavbarItem/DropdownNavbarItem';

const dropdownLinkActiveClass = 'dropdown__link--active';

function isItemActive(
item: LinkLikeNavbarItemProps,
localPathname: string,
Expand Down Expand Up @@ -50,6 +48,7 @@ function DropdownNavbarItemDesktop({
items,
position,
className,
onClick,
...props
}: DesktopOrMobileNavBarItemProps) {
const dropdownRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -113,12 +112,12 @@ function DropdownNavbarItemDesktop({
? nextNavbarItem
: // Next item is another dropdown; focus on the inner
// anchor element instead so there's outline
nextNavbarItem.querySelector('a');
(targetItem as HTMLElement).focus();
nextNavbarItem.querySelector('a')!;
targetItem.focus();
}
}
}}
activeClassName={dropdownLinkActiveClass}
activeClassName="dropdown__link--active"
{...childItemProps}
key={i}
/>
Expand All @@ -132,6 +131,7 @@ function DropdownNavbarItemMobile({
items,
className,
position, // Need to destructure position from props so that it doesn't get passed on.
onClick,
...props
}: DesktopOrMobileNavBarItemProps) {
const localPathname = useLocalPathname();
Expand Down Expand Up @@ -171,7 +171,7 @@ function DropdownNavbarItemMobile({
<NavbarItem
mobile
isDropdownItem
onClick={props.onClick}
onClick={onClick}
activeClassName="menu__link--active"
{...childItemProps}
key={i}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ export default function LocaleDropdownNavbarItem({
fullyQualified: false,
})}`;
return {
isNavLink: true,
label: localeConfigs[locale]!.label,
to,
target: '_self',
autoAddBaseUrl: false,
className: locale === currentLocale ? 'dropdown__link--active' : '',
className:
// eslint-disable-next-line no-nested-ternary
locale === currentLocale
? // Similar idea as DefaultNavbarItem: select the right Infima active
// class name. This cannot be substituted with isActive, because the
// target URLs contain `pathname://` and therefore are not NavLinks!
mobile
? 'menu__link--active'
: 'dropdown__link--active'
: '',
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ import {isRegexpStringMatch} from '@docusaurus/theme-common';
import IconExternalLink from '@theme/IconExternalLink';
import type {Props} from '@theme/NavbarItem/NavbarNavLink';

const dropdownLinkActiveClass = 'dropdown__link--active';

export default function NavbarNavLink({
activeBasePath,
activeBaseRegex,
to,
href,
label,
html,
activeClassName = '',
isDropdownLink,
prependBaseUrlToHref,
...props
}: Props): JSX.Element {
Expand All @@ -32,7 +30,6 @@ export default function NavbarNavLink({
const activeBaseUrl = useBaseUrl(activeBasePath);
const normalizedHref = useBaseUrl(href, {forcePrependBaseUrl: true});
const isExternalLink = label && href && !isInternalUrl(href);
const isDropdownLink = activeClassName === dropdownLinkActiveClass;

// Link content is set through html XOR label
const linkContentProps = html
Expand Down Expand Up @@ -64,9 +61,6 @@ export default function NavbarNavLink({
<Link
to={toUrl}
isNavLink
activeClassName={
!props.className?.includes(activeClassName) ? activeClassName : ''
}
{...((activeBasePath || activeBaseRegex) && {
isActive: (_match, location) =>
activeBaseRegex
Expand Down
13 changes: 0 additions & 13 deletions packages/docusaurus-theme-classic/src/theme/NavbarItem/utils.ts

This file was deleted.