From f609acab4f4ea2bf001432e216c3ddb3f362abdf Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 26 May 2022 01:01:29 +0800 Subject: [PATCH] fix(theme-classic): consistently apply the right active class name for all navbar items (#7430) --- .../src/getSwizzleConfig.ts | 7 ------- .../src/theme-classic.d.ts | 14 ++------------ .../src/theme/NavbarItem/DefaultNavbarItem.tsx | 5 +++-- .../src/theme/NavbarItem/DocNavbarItem.tsx | 16 ++++------------ .../theme/NavbarItem/DocSidebarNavbarItem.tsx | 9 +-------- .../NavbarItem/DocsVersionDropdownNavbarItem.tsx | 1 - .../src/theme/NavbarItem/DropdownNavbarItem.tsx | 12 ++++++------ .../LocaleDropdownNavbarItem/index.tsx | 12 ++++++++++-- .../src/theme/NavbarItem/NavbarNavLink.tsx | 8 +------- .../src/theme/NavbarItem/utils.ts | 13 ------------- 10 files changed, 27 insertions(+), 70 deletions(-) delete mode 100644 packages/docusaurus-theme-classic/src/theme/NavbarItem/utils.ts diff --git a/packages/docusaurus-theme-classic/src/getSwizzleConfig.ts b/packages/docusaurus-theme-classic/src/getSwizzleConfig.ts index 0c34618bdc32..a0549256e98f 100644 --- a/packages/docusaurus-theme-classic/src/getSwizzleConfig.ts +++ b/packages/docusaurus-theme-classic/src/getSwizzleConfig.ts @@ -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', diff --git a/packages/docusaurus-theme-classic/src/theme-classic.d.ts b/packages/docusaurus-theme-classic/src/theme-classic.d.ts index 82ebfdf45d1b..1986158d8e22 100644 --- a/packages/docusaurus-theme-classic/src/theme-classic.d.ts +++ b/packages/docusaurus-theme-classic/src/theme-classic.d.ts @@ -809,7 +809,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; @@ -982,17 +983,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'; diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx index 7036b2b6c12e..948d6507d7bc 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DefaultNavbarItem.tsx @@ -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, @@ -25,6 +24,7 @@ function DefaultNavbarItemDesktop({ isDropdownItem ? 'dropdown__link' : 'navbar__item navbar__link', className, )} + isDropdownLink={isDropdownItem} {...props} /> ); @@ -58,7 +58,8 @@ export default function DefaultNavbarItem({ ); diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx index 797ef1ec2d50..76df1591ba90 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx @@ -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({ @@ -27,20 +25,14 @@ export default function DocNavbarItem({ return null; } - const activeDocInfimaClassName = getInfimaActiveClassName(props.mobile); - return ( + activeDoc?.path === doc.path || + (!!activeDoc?.sidebar && activeDoc.sidebar === doc.sidebar) + } label={staticLabel ?? doc.id} to={doc.path} /> diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocSidebarNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocSidebarNavbarItem.tsx index 874605bcf1a1..08771f8aa6c7 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocSidebarNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocSidebarNavbarItem.tsx @@ -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({ @@ -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 ( activeDoc?.sidebar === sidebarId} label={label ?? sidebarLink.label} to={sidebarLink.path} /> diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx index 47903ce0aa35..8893306f7abd 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DocsVersionDropdownNavbarItem.tsx @@ -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, diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DropdownNavbarItem.tsx b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DropdownNavbarItem.tsx index 45c61dac60e7..3678d9d0bb03 100644 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/DropdownNavbarItem.tsx +++ b/packages/docusaurus-theme-classic/src/theme/NavbarItem/DropdownNavbarItem.tsx @@ -21,8 +21,6 @@ import type { Props, } from '@theme/NavbarItem/DropdownNavbarItem'; -const dropdownLinkActiveClass = 'dropdown__link--active'; - function isItemActive( item: LinkLikeNavbarItemProps, localPathname: string, @@ -50,6 +48,7 @@ function DropdownNavbarItemDesktop({ items, position, className, + onClick, ...props }: DesktopOrMobileNavBarItemProps) { const dropdownRef = useRef(null); @@ -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} /> @@ -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(); @@ -171,7 +171,7 @@ function DropdownNavbarItemMobile({ activeBaseRegex diff --git a/packages/docusaurus-theme-classic/src/theme/NavbarItem/utils.ts b/packages/docusaurus-theme-classic/src/theme/NavbarItem/utils.ts deleted file mode 100644 index a98db455f25e..000000000000 --- a/packages/docusaurus-theme-classic/src/theme/NavbarItem/utils.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * 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. - */ - -/* eslint-disable import/no-named-export */ - -export const getInfimaActiveClassName = ( - mobile?: boolean, -): `${'menu' | 'navbar'}__link--active` => - mobile ? 'menu__link--active' : 'navbar__link--active';