forked from skillrecordings/egghead-next
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.tsx
More file actions
33 lines (28 loc) · 704 Bytes
/
Link.tsx
File metadata and controls
33 lines (28 loc) · 704 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
import React, {FunctionComponent} from 'react'
import {useRouter} from 'next/router'
import NextLink from 'next/link'
import {UrlObject} from 'url'
declare type Url = string | UrlObject
type LinkProps = {
href: Url
children: React.ReactElement
activeClassName?: string
}
const Link: FunctionComponent<LinkProps> = ({
href,
children,
activeClassName,
...props
}) => {
const router = useRouter()
let className = children?.props?.className || ''
if (router.asPath.includes(`${href}`)) {
className = `${className} ${activeClassName}`
}
return (
<NextLink href={href} {...props}>
{React.cloneElement(children, {className})}
</NextLink>
)
}
export default Link