Commit 81066a39 authored by Martin Grubinger's avatar Martin Grubinger Committed by Luis Alvarez D

Fix example active-class-anme: Add support for children of ActiveLink without a className. (#9271)

* Fix example active-class-anme: Add support for children of ActiveLink without a className.

* Update examples/active-class-name/components/ActiveLink.js
Co-Authored-By: 's avatarLuis Alvarez D. <luis@zeit.co>
parent 37ee8fc9
...@@ -6,13 +6,20 @@ import React, { Children } from 'react' ...@@ -6,13 +6,20 @@ import React, { Children } from 'react'
const ActiveLink = ({ children, activeClassName, ...props }) => { const ActiveLink = ({ children, activeClassName, ...props }) => {
const { pathname } = useRouter() const { pathname } = useRouter()
const child = Children.only(children) const child = Children.only(children)
const childClassName = child.props.className || ''
const className = const className =
pathname === props.href pathname === props.href
? `${child.props.className} ${activeClassName}` ? `${childClassName} ${activeClassName}`.trim()
: child.props.className : childClassName
return <Link {...props}>{React.cloneElement(child, { className })}</Link> return (
<Link {...props}>
{React.cloneElement(child, {
className: className || null
})}
</Link>
)
} }
ActiveLink.propTypes = { ActiveLink.propTypes = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment