Unverified Commit c4b6bb85 authored by Jan Kaifer's avatar Jan Kaifer Committed by GitHub

Add `ref` forwarding for `next/image` (#43193)

Add ref forwarding for next/image with integration test


- fixes https://github.com/vercel/next.js/discussions/42885
- fixes https://github.com/vercel/next.js/issues/18398 (this one was closed with `onLoadingComplete`)

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added
Co-authored-by: 's avatarSteven <229881+styfle@users.noreply.github.com>
parent 0f195f02
...@@ -16,6 +16,7 @@ description: Enable Image Optimization with the built-in Image component. ...@@ -16,6 +16,7 @@ description: Enable Image Optimization with the built-in Image component.
| Version | Changes | | Version | Changes |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `v13.0.6` | `ref` prop added. |
| `v13.0.0` | `<span>` wrapper removed. `layout`, `objectFit`, `objectPosition`, `lazyBoundary`, `lazyRoot` props removed. `alt` is required. `onLoadingComplete` receives reference to `img` element. Built-in loader config removed. | | `v13.0.0` | `<span>` wrapper removed. `layout`, `objectFit`, `objectPosition`, `lazyBoundary`, `lazyRoot` props removed. `alt` is required. `onLoadingComplete` receives reference to `img` element. Built-in loader config removed. |
| `v12.3.0` | `remotePatterns` and `unoptimized` configuration is stable. | | `v12.3.0` | `remotePatterns` and `unoptimized` configuration is stable. |
| `v12.2.0` | Experimental `remotePatterns` and experimental `unoptimized` configuration added. `layout="raw"` removed. | | `v12.2.0` | Experimental `remotePatterns` and experimental `unoptimized` configuration added. `layout="raw"` removed. |
...@@ -284,7 +285,6 @@ Other properties on the `<Image />` component will be passed to the underlying ...@@ -284,7 +285,6 @@ Other properties on the `<Image />` component will be passed to the underlying
`img` element with the exception of the following: `img` element with the exception of the following:
- `srcSet`. Use [Device Sizes](#device-sizes) instead. - `srcSet`. Use [Device Sizes](#device-sizes) instead.
- `ref`. Use [`onLoadingComplete`](#onloadingcomplete) instead.
- `decoding`. It is always `"async"`. - `decoding`. It is always `"async"`.
## Configuration Options ## Configuration Options
......
This diff is collapsed.
import React, { useState } from 'react'
import Image from 'next/image'
import { motion } from 'framer-motion'
import testPng from '../images/test.png'
const CustomImage = React.forwardRef((props, ref) => (
<Image
ref={ref}
id="img"
src={testPng}
width={300}
height={300}
alt="test img"
{...props}
/>
))
const MotionImage = motion(CustomImage)
export default function Page() {
const [clicked, setClicked] = useState(false)
return (
<MotionImage
onClick={() => setClicked(true)}
initial={{ opacity: 1 }}
animate={{ opacity: clicked ? 0 : 1 }}
transition={{ duration: 0.5 }}
/>
)
}
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { waitFor } from 'next-test-utils'
import path from 'path'
import webdriver from 'next-webdriver'
describe('next-image-forward-ref', () => {
let next: NextInstance
const appDir = path.join(__dirname, 'app')
beforeAll(async () => {
next = await createNext({
files: new FileRef(appDir),
dependencies: {
'framer-motion': '7.6.9',
},
})
})
afterAll(() => next.destroy())
it('allows framer-motion to animate opacity', async () => {
const browser = await webdriver(next.url, '/framer-motion')
expect(
Number(await browser.elementById('img').getComputedCss('opacity'))
).toBeCloseTo(1)
browser.elementById('img').click()
await waitFor(1000)
expect(
Number(await browser.elementById('img').getComputedCss('opacity'))
).toBeCloseTo(0)
})
})
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