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

Update warning when parent styles break `next/image` (#28517)

- Follow up to #28221 
- Fixes #27644 
parent ed6ce1ae
......@@ -269,15 +269,17 @@ function handleLoading(
onLoadingComplete({ naturalWidth, naturalHeight })
}
if (process.env.NODE_ENV !== 'production') {
const parent = img.parentElement?.parentElement?.style
if (layout === 'responsive' && parent?.display === 'flex') {
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.`
)
} else if (layout === 'fill' && parent?.position !== 'relative') {
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.`
)
if (img.parentElement?.parentElement) {
const parent = getComputedStyle(img.parentElement.parentElement)
if (layout === 'responsive' && parent.display === 'flex') {
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.`
)
} else if (layout === 'fill' && parent.position !== 'relative') {
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.`
)
}
}
}
})
......
import React from 'react'
import Image from 'next/image'
import img from '../public/test.jpg'
import style from '../style.module.css'
const Page = () => {
return (
<div style={{ display: 'flex' }}>
<div className={style.displayFlex}>
<Image id="img" layout="responsive" src={img} />
</div>
)
......
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