diff --git a/components/Common/ActiveLocalizedLink/__tests__/index.test.mjs b/components/Common/ActiveLink/__tests__/index.test.mjs similarity index 56% rename from components/Common/ActiveLocalizedLink/__tests__/index.test.mjs rename to components/Common/ActiveLink/__tests__/index.test.mjs index 576f6f6d922c7..2ce5778f60b63 100644 --- a/components/Common/ActiveLocalizedLink/__tests__/index.test.mjs +++ b/components/Common/ActiveLink/__tests__/index.test.mjs @@ -1,17 +1,13 @@ import { render, screen } from '@testing-library/react'; -import ActiveLocalizedLink from '..'; +import ActiveLink from '..'; -describe('ActiveLocalizedLink', () => { +describe('ActiveLink', () => { it('renders as localized link', () => { render( - + Link - + ); expect(screen.findByText('Link')).resolves.toHaveAttribute( @@ -22,13 +18,9 @@ describe('ActiveLocalizedLink', () => { it('ignores active class when href not matches location.href', () => { render( - + Link - + ); expect(screen.findByText('Link')).resolves.toHaveAttribute('class', 'link'); @@ -36,13 +28,9 @@ describe('ActiveLocalizedLink', () => { it('sets active class when href matches location.href', () => { render( - + Link - + ); expect(screen.findByText('Link')).resolves.toHaveAttribute( diff --git a/components/Common/ActiveLocalizedLink/index.tsx b/components/Common/ActiveLink/index.tsx similarity index 62% rename from components/Common/ActiveLocalizedLink/index.tsx rename to components/Common/ActiveLink/index.tsx index ff6254e6b05dc..d935f9f651ba0 100644 --- a/components/Common/ActiveLocalizedLink/index.tsx +++ b/components/Common/ActiveLink/index.tsx @@ -7,22 +7,24 @@ import Link from '@/components/Link'; import { usePathname } from '@/navigation.mjs'; type ActiveLocalizedLinkProps = ComponentProps & { - activeClassName: string; + activeClassName?: string; + allowSubPath?: boolean; }; -const ActiveLocalizedLink: FC = ({ +const ActiveLink: FC = ({ children, - activeClassName, + activeClassName = 'active', + allowSubPath = false, className, href = '', ...props }) => { const pathname = usePathname(); - const linkURL = new URL(href.toString(), location.href); - const finalClassName = classNames(className, { - [activeClassName]: linkURL.pathname === pathname, + [activeClassName]: allowSubPath + ? pathname.startsWith(href.toString()) + : href.toString() === pathname, }); return ( @@ -32,4 +34,4 @@ const ActiveLocalizedLink: FC = ({ ); }; -export default ActiveLocalizedLink; +export default ActiveLink; diff --git a/components/Containers/NavItem/index.tsx b/components/Containers/NavItem/index.tsx index df4d03059b24e..2ac1685b8f266 100644 --- a/components/Containers/NavItem/index.tsx +++ b/components/Containers/NavItem/index.tsx @@ -3,7 +3,7 @@ import classNames from 'classnames'; import type { FC, PropsWithChildren } from 'react'; import { useMemo } from 'react'; -import ActiveLocalizedLink from '@/components/Common/ActiveLocalizedLink'; +import ActiveLink from '@/components/Common/ActiveLink'; import styles from './index.module.css'; @@ -22,19 +22,19 @@ const NavItem: FC> = ({ className, }) => { const showIcon = useMemo( - () => type === 'nav' && !href.toString().startsWith('/'), + () => type === 'nav' && href.toString().startsWith('http'), [href, type] ); return ( - {children} {showIcon && } - + ); }; diff --git a/components/Header.tsx b/components/Header.tsx index bdac8a701d96c..377cc7eb1d0d7 100644 --- a/components/Header.tsx +++ b/components/Header.tsx @@ -1,29 +1,25 @@ 'use client'; -import classNames from 'classnames'; import Image from 'next/image'; import { useTranslations } from 'next-intl'; import { useTheme } from 'next-themes'; import { useState } from 'react'; +import ActiveLink from '@/components/Common/ActiveLink'; import Link from '@/components/Link'; -import { useIsCurrentPathname, useSiteNavigation } from '@/hooks'; +import { useSiteNavigation } from '@/hooks'; import { usePathname } from '@/navigation.mjs'; import { BASE_PATH } from '@/next.constants.mjs'; import { availableLocales } from '@/next.locales.mjs'; const Header = () => { const { navigationItems } = useSiteNavigation(); - const { isCurrentLocaleRoute } = useIsCurrentPathname(); const [showLangPicker, setShowLangPicker] = useState(false); const { theme, setTheme } = useTheme(); const pathname = usePathname(); const t = useTranslations(); - const getLinkClassName = (href: string) => - classNames({ active: isCurrentLocaleRoute(href, href !== '/') }); - const toggleLanguage = t('components.header.buttons.toggleLanguage'); const toggleDarkMode = t('components.header.buttons.toggleDarkMode'); @@ -43,8 +39,10 @@ const Header = () => {