2021-06-20 14:37:51 +00:00
|
|
|
import {useRouter} from 'next/router'
|
2021-06-01 22:30:40 +00:00
|
|
|
import Link from 'next/link'
|
2021-06-20 14:37:51 +00:00
|
|
|
import React, {Children} from 'react'
|
2021-06-01 22:30:40 +00:00
|
|
|
|
|
|
|
const ActiveLink = ({ children, activeClassName, ...props }) => {
|
|
|
|
const { asPath } = useRouter()
|
|
|
|
const child = Children.only(children)
|
|
|
|
const childClassName = child.props.className || ''
|
|
|
|
|
|
|
|
const className =
|
|
|
|
asPath === props.href || asPath === props.as
|
|
|
|
? `${childClassName} ${activeClassName}`.trim()
|
|
|
|
: childClassName
|
|
|
|
|
|
|
|
return <Link {...props}>{React.cloneElement(child, { className })}</Link>;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActiveLink.defaultProps = {
|
|
|
|
activeClassName: 'active'
|
|
|
|
} as Partial<Props>
|
|
|
|
|
|
|
|
export default ActiveLink
|