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 warning when parent styles break next/image #28517

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
)
}
}
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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>
)
Expand Down
3 changes: 3 additions & 0 deletions test/integration/image-component/default/style.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.displayFlex {
display: flex;
}