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

Relax warning for `next/image` parent element (#30453)

This warning was incorrectly displaying for the "background" use case demonstrated here: https://image-component.nextjs.gallery/background
parent e82958ca
...@@ -283,7 +283,11 @@ function handleLoading( ...@@ -283,7 +283,11 @@ function handleLoading(
console.warn( console.warn(
`Image with src "${src}" may not render properly as a child of a flex container. Consider wrapping the image with a div to configure the width.` `Image with src "${src}" may not render properly as a child of a flex container. Consider wrapping the image with a div to configure the width.`
) )
} else if (layout === 'fill' && parent.position !== 'relative') { } else if (
layout === 'fill' &&
parent.position !== 'relative' &&
parent.position !== 'fixed'
) {
console.warn( console.warn(
`Image with src "${src}" may not render properly with a parent using position:"${parent.position}". Consider changing the parent style to position:"relative" with a width and height.` `Image with src "${src}" may not render properly with a parent using position:"${parent.position}". Consider changing the parent style to position:"relative" with a width and height.`
) )
......
import React from 'react' import React from 'react'
import Image from 'next/image' import Image from 'next/image'
import img from '../public/test.jpg' import jpg from '../public/test.jpg'
import png from '../public/test.png'
import webp from '../public/test.webp'
const Page = () => { const Page = () => {
return ( return (
<div style={{ position: 'static' }}> <>
<Image id="img" layout="fill" src={img} /> <h1>Layout fill inside non-relative parent</h1>
</div> <div style={{ position: 'static', width: '200px', height: '200px' }}>
<Image id="static" layout="fill" priority src={jpg} />
</div>
<div style={{ position: 'fixed', width: '200px', height: '200px' }}>
<Image id="fixed" layout="fill" priority src={png} />
</div>
<div style={{ position: 'relative', width: '200px', height: '200px' }}>
<Image id="relative" layout="fill" priority src={webp} />
</div>
<footer>footer here</footer>
</>
) )
} }
......
...@@ -620,12 +620,20 @@ function runTests(mode) { ...@@ -620,12 +620,20 @@ function runTests(mode) {
appPort, appPort,
'/layout-fill-inside-nonrelative' '/layout-fill-inside-nonrelative'
) )
await browser.eval(`document.getElementById("img").scrollIntoView()`) await browser.eval(`document.querySelector("footer").scrollIntoView()`)
await check(async () => { await waitFor(1000)
return (await browser.log('browser')) const warnings = (await browser.log('browser'))
.map((log) => log.message) .map((log) => log.message)
.join('\n') .join('\n')
}, /Image with src (.*)jpg(.*) may not render properly with a parent using position:"static". Consider changing the parent style to position:"relative"/gm) expect(warnings).toMatch(
/Image with src (.*)jpg(.*) may not render properly with a parent using position:"static". Consider changing the parent style to position:"relative"/gm
)
expect(warnings).not.toMatch(
/Image with src (.*)png(.*) may not render properly/gm
)
expect(warnings).not.toMatch(
/Image with src (.*)webp(.*) may not render properly/gm
)
expect(await hasRedbox(browser)).toBe(false) expect(await hasRedbox(browser)).toBe(false)
}) })
......
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