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