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

Relax warning for next/image parent element #30453

Merged
merged 1 commit into from
Oct 27, 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
6 changes: 5 additions & 1 deletion packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,11 @@ function handleLoading(
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') {
} else if (
layout === 'fill' &&
parent.position !== 'relative' &&
parent.position !== 'fixed'
) {
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,12 +1,24 @@
import React from 'react'
import Image from 'next/image'
import img from '../public/test.jpg'
import jpg from '../public/test.jpg'
import png from '../public/test.png'
import webp from '../public/test.webp'

const Page = () => {
return (
<div style={{ position: 'static' }}>
<Image id="img" layout="fill" src={img} />
</div>
<>
<h1>Layout fill inside non-relative parent</h1>
<div style={{ position: 'static', width: '200px', height: '200px' }}>
<Image id="static" layout="fill" priority src={jpg} />
</div>
<div style={{ position: 'fixed', width: '200px', height: '200px' }}>
<Image id="fixed" layout="fill" priority src={png} />
</div>
<div style={{ position: 'relative', width: '200px', height: '200px' }}>
<Image id="relative" layout="fill" priority src={webp} />
</div>
<footer>footer here</footer>
</>
)
}

Expand Down
20 changes: 14 additions & 6 deletions test/integration/image-component/default/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,12 +620,20 @@ function runTests(mode) {
appPort,
'/layout-fill-inside-nonrelative'
)
await browser.eval(`document.getElementById("img").scrollIntoView()`)
await check(async () => {
return (await browser.log('browser'))
.map((log) => log.message)
.join('\n')
}, /Image with src (.*)jpg(.*) may not render properly with a parent using position:"static". Consider changing the parent style to position:"relative"/gm)
await browser.eval(`document.querySelector("footer").scrollIntoView()`)
await waitFor(1000)
const warnings = (await browser.log('browser'))
.map((log) => log.message)
.join('\n')
expect(warnings).toMatch(
/Image with src (.*)jpg(.*) may not render properly with a parent using position:"static". Consider changing the parent style to position:"relative"/gm
)
expect(warnings).not.toMatch(
/Image with src (.*)png(.*) may not render properly/gm
)
expect(warnings).not.toMatch(
/Image with src (.*)webp(.*) may not render properly/gm
)
expect(await hasRedbox(browser)).toBe(false)
})

Expand Down