Unverified Commit b05cdb1f authored by Steven's avatar Steven Committed by GitHub

Add naturalWidth/naturalHeight to `onLoadingComplete()` callback (#27695)

Resolves #27213 
parent b3c959b2
......@@ -202,6 +202,11 @@ The image position when using `layout="fill"`.
A callback function that is invoked once the image is completely loaded and the [placeholder](#placeholder) has been removed.
The `onLoadingComplete` function accepts one parameter, 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)
### loading
> **Attention**: This property is only meant for advanced usage. Switching an
......
......@@ -50,6 +50,11 @@ type LayoutValue = typeof VALID_LAYOUT_VALUES[number]
type PlaceholderValue = 'blur' | 'empty'
type OnLoadingComplete = (result: {
naturalWidth: number
naturalHeight: number
}) => void
type ImgElementStyle = NonNullable<JSX.IntrinsicElements['img']['style']>
interface StaticImageData {
......@@ -103,7 +108,7 @@ export type ImageProps = Omit<
unoptimized?: boolean
objectFit?: ImgElementStyle['objectFit']
objectPosition?: ImgElementStyle['objectPosition']
onLoadingComplete?: () => void
onLoadingComplete?: OnLoadingComplete
}
const {
......@@ -249,7 +254,7 @@ function handleLoading(
img: HTMLImageElement | null,
src: string,
placeholder: PlaceholderValue,
onLoadingComplete?: () => void
onLoadingComplete?: OnLoadingComplete
) {
if (!img) {
return
......@@ -265,7 +270,10 @@ function handleLoading(
}
loadedImageURLs.add(src)
if (onLoadingComplete) {
onLoadingComplete()
const { naturalWidth, naturalHeight } = img
// Pass back read-only primitive values but not the
// underlying DOM element because it could be misused.
onLoadingComplete({ naturalWidth, naturalHeight })
}
})
}
......
......@@ -2,7 +2,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000"
width="400" height="400" viewBox="0 0 400 400"
preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
......
......@@ -4,27 +4,53 @@ import Image from 'next/image'
const Page = () => (
<div>
<h1>On Loading Complete Test</h1>
<ImageWithMessage id="1" src="/test.jpg" />
<ImageWithMessage
id="1"
src="/test.jpg"
layout="intrinsic"
width="128"
height="128"
/>
<ImageWithMessage
id="2"
src={require('../public/test.png')}
placeholder="blur"
layout="fixed"
/>
<ImageWithMessage
id="3"
src="/test.svg"
layout="responsive"
width="1200"
height="1200"
/>
<div style={{ position: 'relative', width: '64px', height: '64px' }}>
<ImageWithMessage
id="4"
src="/test.ico"
layout="fill"
objectFit="contain"
/>
</div>
<div id="footer" />
</div>
)
function ImageWithMessage({ id, src }) {
function ImageWithMessage({ id, ...props }) {
const [msg, setMsg] = useState('[LOADING]')
return (
<>
<Image
id={`img${id}`}
src={src}
width="400"
height="400"
onLoadingComplete={() => setMsg(`loaded img${id}`)}
onLoadingComplete={({ naturalWidth, naturalHeight }) =>
setMsg(
`loaded img${id} with dimensions ${naturalWidth}x${naturalHeight}`
)
}
{...props}
/>
<p id={`msg${id}`}>{msg}</p>
<hr />
</>
)
}
......
......@@ -2,7 +2,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000"
width="400" height="400" viewBox="0 0 400 400"
preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
......
......@@ -188,22 +188,39 @@ function runTests(mode) {
try {
browser = await webdriver(appPort, '/on-loading-complete')
await browser.eval(`document.getElementById("footer").scrollIntoView()`)
await check(
() => browser.eval(`document.getElementById("img1").src`),
/test(.*)jpg/
)
await check(
() => browser.eval(`document.getElementById("img2").src`),
/test(.*).png/
)
await check(
() => browser.eval(`document.getElementById("img3").src`),
/test(.*)svg/
)
await check(
() => browser.eval(`document.getElementById("img4").src`),
/test(.*)ico/
)
await check(
() => browser.eval(`document.getElementById("msg1").textContent`),
'loaded img1'
'loaded img1 with dimensions 128x128'
)
await check(
() => browser.eval(`document.getElementById("msg2").textContent`),
'loaded img2'
'loaded img2 with dimensions 400x400'
)
await check(
() => browser.eval(`document.getElementById("msg3").textContent`),
'loaded img3 with dimensions 266x266'
)
await check(
() => browser.eval(`document.getElementById("msg4").textContent`),
'loaded img4 with dimensions 21x21'
)
} finally {
if (browser) {
......
......@@ -2,7 +2,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
width="400.000000pt" height="400.000000pt" viewBox="0 0 400.000000 400.000000"
width="400" height="400" viewBox="0 0 400 400"
preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,400.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
......
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