-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathindex.tsx
43 lines (36 loc) · 1.18 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use client';
import classNames from 'classnames';
import type { ComponentProps, FC } from 'react';
import Link from '@/components/Link';
import { usePathname } from '@/navigation';
import { VERSION_SUPPORT_SHORTCUT } from '@/next.constants.mjs';
type ActiveLocalizedLinkProps = ComponentProps<typeof Link> & {
activeClassName?: string;
allowSubPath?: boolean;
};
const ActiveLink: FC<ActiveLocalizedLinkProps> = ({
children,
activeClassName = 'active',
allowSubPath = false,
className,
href = '',
...props
}) => {
const pathname = usePathname();
const finalClassName = classNames(className, {
[activeClassName]: allowSubPath
? // When using allowSubPath we want only to check if
// the current pathname starts with the utmost upper level
// of an href (e.g. /docs/...)
pathname.startsWith(`/${href.toString().split('/')[1]}`) &&
// but not when this link is for the deep link shortcut to previous releases
href.toString() !== VERSION_SUPPORT_SHORTCUT
: href.toString() === pathname,
});
return (
<Link className={finalClassName} href={href} {...props}>
{children}
</Link>
);
};
export default ActiveLink;