Unverified Commit b2df2cff authored by Josh Slate's avatar Josh Slate Committed by GitHub

chore(examples): Update active-class-name example (#43581)


I updated the `active-class-name` example to stop using the legacy behavior.

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [x] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 4ee933db
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import Link, { LinkProps } from 'next/link' import Link, { LinkProps } from 'next/link'
import React, { useState, useEffect, ReactElement, Children } from 'react' import React, { PropsWithChildren, useState, useEffect } from 'react'
type ActiveLinkProps = LinkProps & { type ActiveLinkProps = LinkProps & {
children: ReactElement className?: string
activeClassName: string activeClassName: string
} }
const ActiveLink = ({ const ActiveLink = ({
children, children,
activeClassName, activeClassName,
className,
...props ...props
}: ActiveLinkProps) => { }: PropsWithChildren<ActiveLinkProps>) => {
const { asPath, isReady } = useRouter() const { asPath, isReady } = useRouter()
const [computedClassName, setComputedClassName] = useState(className)
const child = Children.only(children)
const childClassName = child.props.className || ''
const [className, setClassName] = useState(childClassName)
useEffect(() => { useEffect(() => {
// Check if the router fields are updated client-side // Check if the router fields are updated client-side
...@@ -33,11 +31,11 @@ const ActiveLink = ({ ...@@ -33,11 +31,11 @@ const ActiveLink = ({
const newClassName = const newClassName =
linkPathname === activePathname linkPathname === activePathname
? `${childClassName} ${activeClassName}`.trim() ? `${className} ${activeClassName}`.trim()
: childClassName : className
if (newClassName !== className) { if (newClassName !== computedClassName) {
setClassName(newClassName) setComputedClassName(newClassName)
} }
} }
}, [ }, [
...@@ -45,17 +43,14 @@ const ActiveLink = ({ ...@@ -45,17 +43,14 @@ const ActiveLink = ({
isReady, isReady,
props.as, props.as,
props.href, props.href,
childClassName,
activeClassName, activeClassName,
setClassName,
className, className,
computedClassName,
]) ])
return ( return (
<Link {...props} legacyBehavior> <Link className={computedClassName} {...props}>
{React.cloneElement(child, { {children}
className: className || null,
})}
</Link> </Link>
) )
} }
......
...@@ -13,23 +13,28 @@ const Nav = () => ( ...@@ -13,23 +13,28 @@ const Nav = () => (
`}</style> `}</style>
<ul className="nav"> <ul className="nav">
<li> <li>
<ActiveLink activeClassName="active" href="/"> <ActiveLink activeClassName="active" className="nav-link" href="/">
<a className="nav-link">Home</a> Home
</ActiveLink> </ActiveLink>
</li> </li>
<li> <li>
<ActiveLink activeClassName="active" href="/about"> <ActiveLink activeClassName="active" className="nav-link" href="/about">
<a className="nav-link">About</a> About
</ActiveLink> </ActiveLink>
</li> </li>
<li> <li>
<ActiveLink activeClassName="active" href="/blog"> <ActiveLink activeClassName="active" className="nav-link" href="/blog">
<a className="nav-link">Blog</a> Blog
</ActiveLink> </ActiveLink>
</li> </li>
<li> <li>
<ActiveLink activeClassName="active" href="/[slug]" as="/dynamic-route"> <ActiveLink
<a className="nav-link">Dynamic Route</a> activeClassName="active"
className="nav-link"
href="/[slug]"
as="/dynamic-route"
>
Dynamic Route
</ActiveLink> </ActiveLink>
</li> </li>
</ul> </ul>
......
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