Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples/active-class-name #34205

Merged
merged 4 commits into from Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 34 additions & 8 deletions examples/active-class-name/components/ActiveLink.js
@@ -1,20 +1,46 @@
import { useState, useEffect } from 'react'
import { useRouter } from 'next/router'
import PropTypes from 'prop-types'
import Link from 'next/link'
import React, { Children } from 'react'

const ActiveLink = ({ children, activeClassName, ...props }) => {
const { asPath } = useRouter()
const { asPath, isReady } = useRouter()

const child = Children.only(children)
const childClassName = child.props.className || ''
const [className, setClassName] = useState(childClassName)

useEffect(() => {
// Check if the router fields are updated client-side
if (isReady) {
// Dynamic route will be matched via props.as
// Static route will be matched via props.href
const linkPathname = new URL(props.as || props.href, location.href)
.pathname

// Using URL().pathname to get rid of query and hash
const activePathname = new URL(asPath, location.href).pathname

const newClassName =
linkPathname === activePathname
? `${childClassName} ${activeClassName}`.trim()
: childClassName

// pages/index.js will be matched via props.href
// pages/about.js will be matched via props.href
// pages/[slug].js will be matched via props.as
const className =
asPath === props.href || asPath === props.as
? `${childClassName} ${activeClassName}`.trim()
: childClassName
if (newClassName !== className) {
setClassName(newClassName)
}
}
}, [
asPath,
isReady,
props.as,
props.href,
childClassName,
activeClassName,
setClassName,
className,
])

xhoantran marked this conversation as resolved.
Show resolved Hide resolved
return (
<Link {...props}>
Expand Down
5 changes: 5 additions & 0 deletions examples/active-class-name/components/Nav.js
Expand Up @@ -22,6 +22,11 @@ const Nav = () => (
<a className="nav-link">About</a>
</ActiveLink>
</li>
<li>
<ActiveLink activeClassName="active" href="/blog">
<a className="nav-link">Blog</a>
</ActiveLink>
</li>
<li>
<ActiveLink activeClassName="active" href="/[slug]" as="/dynamic-route">
<a className="nav-link">Dynamic Route</a>
Expand Down
10 changes: 10 additions & 0 deletions examples/active-class-name/next.config.js
@@ -0,0 +1,10 @@
module.exports = {
async rewrites() {
return [
{
source: '/blog',
destination: '/news',
},
]
},
}
10 changes: 10 additions & 0 deletions examples/active-class-name/pages/news.js
@@ -0,0 +1,10 @@
import Nav from '../components/Nav'

const News = () => (
<>
<Nav />
<p>Hello, I'm the news page</p>
</>
)

export default News