2017-10-31 07:58:00 +00:00
|
|
|
import { withRouter } from 'next/router'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import React, { Children } from 'react'
|
2017-10-30 14:24:31 +00:00
|
|
|
|
|
|
|
const ActiveLink = ({ router, children, ...props }) => {
|
2017-10-31 07:58:00 +00:00
|
|
|
const child = Children.only(children)
|
2017-10-30 14:24:31 +00:00
|
|
|
|
2018-02-12 09:10:01 +00:00
|
|
|
let className = child.props.className || null
|
2017-10-30 14:24:31 +00:00
|
|
|
if (router.pathname === props.href && props.activeClassName) {
|
2018-12-17 16:34:32 +00:00
|
|
|
className = `${className !== null ? className : ''} ${
|
|
|
|
props.activeClassName
|
|
|
|
}`.trim()
|
2017-10-30 14:24:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 07:58:00 +00:00
|
|
|
delete props.activeClassName
|
2017-10-30 14:24:31 +00:00
|
|
|
|
2017-10-31 07:58:00 +00:00
|
|
|
return <Link {...props}>{React.cloneElement(child, { className })}</Link>
|
|
|
|
}
|
2017-10-30 14:24:31 +00:00
|
|
|
|
2017-10-31 07:58:00 +00:00
|
|
|
export default withRouter(ActiveLink)
|