-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathindex.tsx
More file actions
34 lines (29 loc) · 961 Bytes
/
index.tsx
File metadata and controls
34 lines (29 loc) · 961 Bytes
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
import classNames from 'classnames';
import type { ComponentProps, FC } from 'react';
import type { LinkLike } from '#ui/types';
export type ActiveLocalizedLinkProps = ComponentProps<LinkLike> & {
activeClassName?: string;
allowSubPath?: boolean;
pathname?: string;
as?: LinkLike;
};
const BaseActiveLink: FC<ActiveLocalizedLinkProps> = ({
activeClassName = 'active',
allowSubPath = false,
className,
href = '',
pathname = '/',
as: Component = 'a',
...props
}) => {
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]}`)
: href.toString() === pathname,
});
return <Component className={finalClassName} href={href} {...props} />;
};
export default BaseActiveLink;