Unverified Commit f2251793 authored by Eric Matthys's avatar Eric Matthys Committed by GitHub

fix(Link): Do not ignore onMouseEnter prop with absolute href (#32012)


Fixes #22733

Regardless of whether it's recommended that Link be used with external href values or not, they can be used and `onMouseEnter` being swallowed with an external href value is unexpected behavior.

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [x] Make sure the linting passes by running `yarn lint`
parent 332cd06c
......@@ -291,11 +291,12 @@ function Link(props: React.PropsWithChildren<LinkProps>) {
}
childProps.onMouseEnter = (e: React.MouseEvent) => {
if (!isLocalURL(href)) return
if (child.props && typeof child.props.onMouseEnter === 'function') {
child.props.onMouseEnter(e)
}
prefetch(router, href, as, { priority: true })
if (isLocalURL(href)) {
prefetch(router, href, as, { priority: true })
}
}
// If child is an <a> tag and doesn't have a href attribute, or if the 'passHref' property is
......
......@@ -11,6 +11,8 @@ export async function getServerSideProps({ query: { port } }) {
export default function Page({ port }) {
const router = useRouter()
const [hover, setHover] = React.useState(false)
return (
<>
<Link href="https://vercel.com/">
......@@ -61,6 +63,17 @@ export default function Page({ port }) {
<Link href="mailto:idk@idk.com">
<a id="mailto-link">mailto:idk@idk.com</a>
</Link>
<br />
<Link href="https://vercel.com/">
<a
id="absolute-link-mouse-events"
data-hover={hover}
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
>
https://vercel.com/
</a>
</Link>
</>
)
}
......@@ -118,6 +118,21 @@ describe('Client Navigation', () => {
)
})
it('should call mouse handlers with an absolute url', async () => {
const browser = await webdriver(
context.appPort,
`/absolute-url?port=${context.appPort}`
)
await browser.elementByCss('#absolute-link-mouse-events').moveTo()
expect(
await browser
.waitForElementByCss('#absolute-link-mouse-events')
.getAttribute('data-hover')
).toBe('true')
})
it('should navigate an absolute local url', async () => {
const browser = await webdriver(
context.appPort,
......
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