Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update onLoadingComplete for next/future/image to receive reference to <img> #40326

Merged
merged 10 commits into from Sep 8, 2022
16 changes: 7 additions & 9 deletions docs/api-reference/next/future/image.md
Expand Up @@ -7,11 +7,11 @@ description: Try the latest Image Optimization with the new `next/future/image`
<details>
<summary><b>Version History</b></summary>

| Version | Changes |
| --------- | --------------------------------------------------------------------------------------------------------------------------- |
| `v12.3.0` | `next/future/image` component stable. `remotePatterns` config stable. `unoptimized` config stable. `alt` property required. |
| `v12.2.4` | `fill` property added. |
| `v12.2.0` | Experimental `next/future/image` component introduced. |
| Version | Changes |
| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `v12.3.0` | `next/future/image` component stable. `remotePatterns` config stable. `unoptimized` config stable. `alt` property required. `onLoadingComplete` receives `<img>` |
| `v12.2.4` | `fill` property added. |
| `v12.2.0` | Experimental `next/future/image` component introduced. |

</details>

Expand All @@ -33,6 +33,7 @@ Compared to `next/image`, the new `next/future/image` component has the followin
- Removes `lazyRoot` prop since there is no native equivalent
- Removes `loader` config in favor of [`loader`](#loader) prop
- Changed `alt` prop from optional to required
- Changed `onLoadingComplete` callback to receive reference to `<img>` element

## Known Browser Bugs

Expand Down Expand Up @@ -306,10 +307,7 @@ Also keep in mind that the required `width` and `height` props can interact with

A callback function that is invoked once the image is completely loaded and the [placeholder](#placeholder) has been removed.

The callback function will be called with one argument, an object with the following properties:

- [`naturalWidth`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalWidth)
- [`naturalHeight`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalHeight)
The callback function will be called with one argument, a reference to the underlying `img` element.
styfle marked this conversation as resolved.
Show resolved Hide resolved

### onLoad

Expand Down
10 changes: 2 additions & 8 deletions packages/next/client/future/image.tsx
Expand Up @@ -47,10 +47,7 @@ type ImageLoaderPropsWithConfig = ImageLoaderProps & {

type PlaceholderValue = 'blur' | 'empty'

type OnLoadingComplete = (result: {
naturalWidth: number
naturalHeight: number
}) => void
type OnLoadingComplete = (img: HTMLImageElement) => void

type ImgElementStyle = NonNullable<JSX.IntrinsicElements['img']['style']>

Expand Down Expand Up @@ -265,10 +262,7 @@ function handleLoading(
setBlurComplete(true)
}
if (onLoadingCompleteRef?.current) {
const { naturalWidth, naturalHeight } = img
// Pass back read-only primitive values but not the
// underlying DOM element because it could be misused.
onLoadingCompleteRef.current({ naturalWidth, naturalHeight })
onLoadingCompleteRef.current(img)
}
if (process.env.NODE_ENV !== 'production') {
if (img.getAttribute('data-nimg') === 'future-fill') {
Expand Down
Expand Up @@ -105,13 +105,16 @@ function ImageWithMessage({ id, idToCount, setIdToCount, ...props }) {
<div className="wrap">
<Image
id={`img${id}`}
onLoadingComplete={({ naturalWidth, naturalHeight }) => {
onLoadingComplete={(img) => {
const { naturalWidth, naturalHeight } = img
let count = idToCount[id] || 0
count++
idToCount[id] = count
setIdToCount(idToCount)
setMsg(
`loaded ${count} img${id} with dimensions ${naturalWidth}x${naturalHeight}`
`loaded ${count} ${
img instanceof Image ? 'img' : ''
}${id} with dimensions ${naturalWidth}x${naturalHeight}`
)
}}
{...props}
Expand Down