-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add warning when parent styles break
next/image
(#28221)
Fixes #27644
- Loading branch information
Showing
4 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -245,6 +245,7 @@ function defaultImageLoader(loaderProps: ImageLoaderProps) { | |
function handleLoading( | ||
img: HTMLImageElement | null, | ||
src: string, | ||
layout: LayoutValue, | ||
placeholder: PlaceholderValue, | ||
onLoadingComplete?: OnLoadingComplete | ||
) { | ||
|
@@ -267,6 +268,18 @@ function handleLoading( | |
// underlying DOM element because it could be misused. | ||
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') { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
styfle
Author
Member
|
||
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.` | ||
) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
@@ -612,7 +625,7 @@ export default function Image({ | |
className={className} | ||
ref={(img) => { | ||
setRef(img) | ||
handleLoading(img, srcString, placeholder, onLoadingComplete) | ||
handleLoading(img, srcString, layout, placeholder, onLoadingComplete) | ||
}} | ||
style={{ ...imgStyle, ...blurStyle }} | ||
/> | ||
|
13 changes: 13 additions & 0 deletions
13
test/integration/image-component/default/pages/layout-fill-inside-nonrelative.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react' | ||
import Image from 'next/image' | ||
import img from '../public/test.jpg' | ||
|
||
const Page = () => { | ||
return ( | ||
<div style={{ position: 'static' }}> | ||
<Image id="img" layout="fill" src={img} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |
13 changes: 13 additions & 0 deletions
13
test/integration/image-component/default/pages/layout-responsive-inside-flex.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react' | ||
import Image from 'next/image' | ||
import img from '../public/test.jpg' | ||
|
||
const Page = () => { | ||
return ( | ||
<div style={{ display: 'flex' }}> | ||
<Image id="img" layout="responsive" src={img} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@styfle What's wrong with parent being any non
static
position. I set parent toposition: absolute; top: 0; left: 0; right: 0; bottom: 0;
a lot a this check spams dev console. Official nextjs demo usesposition: fixed;
and I believe it's legitimate usecase. https://image-component.nextjs.gallery/backgroundI didn't find any explanation in the issue mentioned in commit message.
Thank you for your answer.