Skip to content

Commit cfd3354

Browse files
committed
Update warning when parent styles break next/image
1 parent ed6ce1a commit cfd3354

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

packages/next/client/image.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,17 @@ function handleLoading(
269269
onLoadingComplete({ naturalWidth, naturalHeight })
270270
}
271271
if (process.env.NODE_ENV !== 'production') {
272-
const parent = img.parentElement?.parentElement?.style
273-
if (layout === 'responsive' && parent?.display === 'flex') {
274-
console.warn(
275-
`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.`
276-
)
277-
} else if (layout === 'fill' && parent?.position !== 'relative') {
278-
console.warn(
279-
`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.`
280-
)
272+
if (img.parentElement?.parentElement) {
273+
const parent = getComputedStyle(img.parentElement.parentElement)
274+
if (layout === 'responsive' && parent.display === 'flex') {
275+
console.warn(
276+
`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.`
277+
)
278+
} else if (layout === 'fill' && parent.position !== 'relative') {
279+
console.warn(
280+
`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.`
281+
)
282+
}
281283
}
282284
}
283285
})

0 commit comments

Comments
 (0)