Skip to content

Commit

Permalink
updated example to also support a [slug] path that matches as ins…
Browse files Browse the repository at this point in the history
…tead of `href`
  • Loading branch information
AubreyHewes committed Aug 5, 2020
1 parent 5218e76 commit b4c0cb9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/active-class-name/components/ActiveLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ const ActiveLink = ({ children, activeClassName, ...props }) => {
const child = Children.only(children)
const childClassName = child.props.className || ''

// 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.href || asPath === props.as
? `${childClassName} ${activeClassName}`.trim()
: childClassName

Expand Down
5 changes: 5 additions & 0 deletions examples/active-class-name/components/Nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const Nav = () => (
<a className="nav-link">About</a>
</ActiveLink>
</li>
<li>
<ActiveLink activeClassName="active" href="/[slug]" as="/slug">
<a className="nav-link">Slug</a>
</ActiveLink>
</li>
</ul>
</nav>
)
Expand Down
14 changes: 14 additions & 0 deletions examples/active-class-name/pages/[slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useRouter } from 'next/router'
import Nav from '../components/Nav'

const SlugPage = () => {
const { asPath } = useRouter()
return (
<>
<Nav />
<p>Hello, I'm the {asPath} page</p>
</>
)
}

export default SlugPage

0 comments on commit b4c0cb9

Please sign in to comment.