Unverified Commit 2451f52b authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add support for passing ref to next/link with new behavior enabled (#36537)

parent 3aa8aaae
......@@ -119,12 +119,13 @@ function linkClicked(
})
}
function Link(
props: React.PropsWithChildren<
type LinkPropsReal = React.PropsWithChildren<
Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, keyof LinkProps> &
LinkProps
>
) {
>
const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(
(props, forwardedRef) => {
const {
legacyBehavior = Boolean(process.env.__NEXT_NEW_LINK_BEHAVIOR) !== true,
} = props
......@@ -315,8 +316,9 @@ function Link(
}
}
const childRef: any =
legacyBehavior && child && typeof child === 'object' && child.ref
const childRef: any = legacyBehavior
? child && typeof child === 'object' && child.ref
: forwardedRef
const [setIntersectionRef, isVisible, resetVisible] = useIntersection({
rootMargin: '200px',
......@@ -332,14 +334,14 @@ function Link(
}
setIntersectionRef(el)
if (legacyBehavior && childRef) {
if (childRef) {
if (typeof childRef === 'function') childRef(el)
else if (typeof childRef === 'object') {
childRef.current = el
}
}
},
[as, childRef, href, resetVisible, setIntersectionRef, legacyBehavior]
[as, childRef, href, resetVisible, setIntersectionRef]
)
React.useEffect(() => {
const shouldPrefetch = isVisible && p && isLocalURL(href)
......@@ -435,6 +437,7 @@ function Link(
{children}
</a>
)
}
}
)
export default Link
......@@ -3,6 +3,7 @@ import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import cheerio from 'cheerio'
import path from 'path'
import webdriver from 'next-webdriver'
const appDir = path.join(__dirname, 'typescript')
......@@ -14,6 +15,7 @@ describe('New Link Behavior', () => {
files: {
pages: new FileRef(path.join(appDir, 'pages')),
'tsconfig.json': new FileRef(path.join(appDir, 'tsconfig.json')),
'next.config.js': new FileRef(path.join(appDir, 'next.config.js')),
},
dependencies: {
typescript: '*',
......@@ -31,4 +33,10 @@ describe('New Link Behavior', () => {
expect($a.text()).toBe('Visit')
expect($a.attr('href')).toBe('/test')
})
it('should apply ref on link', async () => {
const browser = await webdriver(next.url, `/ref`)
const text = await browser.elementByCss('#anchor-text').text()
expect(text).toBe('AnchorText: About')
})
})
module.exports = {
reactStrictMode: true,
experimental: {
newNextLinkBehavior: true,
},
}
......@@ -73,6 +73,7 @@ export const ButtonLink: React.FC<ButtonLinkProps> = ({
shallow={shallow}
// Always passHref to the <a> component if it's defined.
passHref={!!href}
legacyBehavior
>
<a>{children}</a>
</NextLink>
......
import { useRef, useEffect, useState } from 'react'
import Link from 'next/link'
export default function Page() {
const ref = useRef<HTMLAnchorElement>()
const [anchorText, setAnchorText] = useState(null)
useEffect(() => {
const anchorElement = ref.current
setAnchorText(anchorElement?.textContent)
}, [])
return (
<>
{anchorText && <h1 id="anchor-text">AnchorText: {anchorText}</h1>}
<Link href="/about" ref={ref}>
About
</Link>
</>
)
}
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