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

Add support for string width/height on Image component #18178

Merged
merged 5 commits into from Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 15 additions & 9 deletions packages/next/client/image.tsx
Expand Up @@ -29,8 +29,8 @@ type ImageProps = Omit<
loading?: LoadingValue
unoptimized?: boolean
} & (
| { width: number; height: number; unsized?: false }
| { width?: number; height?: number; unsized: true }
| { width: number | string; height: number | string; unsized?: false }
| { width?: number | string; height?: number | string; unsized: true }
)

const imageData: ImageData = process.env.__NEXT_IMAGE_OPTS as any
Expand Down Expand Up @@ -229,16 +229,18 @@ export default function Image({
className = className ? className + ' __lazy' : '__lazy'
}

// No need to add preloads on the client side--by the time the application is hydrated,
// it's too late for preloads
const shouldPreload = priority && typeof window === 'undefined'

let divStyle: React.CSSProperties | undefined
let imgStyle: React.CSSProperties | undefined
let wrapperStyle: React.CSSProperties | undefined
if (typeof height === 'number' && typeof width === 'number' && !unsized) {
// <Image src="i.png" width=100 height=100 />
const quotient = height / width
if (
typeof height !== 'undefined' &&
typeof width !== 'undefined' &&
!unsized
) {
// <Image src="i.png" width={100} height={100} />
// <Image src="i.png" width="100"" height="100" />
styfle marked this conversation as resolved.
Show resolved Hide resolved
const quotient =
parseInt(height as string, 10) / parseInt(width as string, 10)
const ratio = isNaN(quotient) ? 1 : quotient * 100
wrapperStyle = {
maxWidth: '100%',
Expand Down Expand Up @@ -278,6 +280,10 @@ export default function Image({
}
}

// No need to add preloads on the client side--by the time the application is hydrated,
// it's too late for preloads
const shouldPreload = priority && typeof window === 'undefined'

return (
<div style={wrapperStyle}>
<div style={divStyle}>
Expand Down
6 changes: 6 additions & 0 deletions test/integration/image-component/default/pages/index.js
Expand Up @@ -6,6 +6,12 @@ const Page = () => {
<div>
<p>Hello World</p>
<Image id="basic-image" src="/test.jpg" width={400} height={400}></Image>
<Image
id="width-height-string"
src="/test.jpg"
width="400"
height="400"
></Image>
<Image id="unsized-image" src="/test.png" unsized></Image>
<p id="stubtext">This is the index page</p>
</div>
Expand Down
11 changes: 11 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Expand Up @@ -36,6 +36,17 @@ function runTests() {
return 'result-correct'
}, /result-correct/)

await check(async () => {
const result = await browser.eval(
`document.getElementById('width-height-string').naturalWidth`
)
if (result === 0) {
throw new Error('Incorrectly loaded image')
}

return 'result-correct'
}, /result-correct/)

await check(async () => {
const result = await browser.eval(
`document.getElementById('unsized-image').naturalWidth`
Expand Down
Expand Up @@ -6,11 +6,17 @@ const Page = () => {
<div>
<p>Hello World</p>
<Image
id="with-and-height"
id="width-and-height-num"
src="https://via.placeholder.com/500"
width={500}
height={500}
></Image>
<Image
id="width-and-height-str"
src="https://via.placeholder.com/500"
width="500"
height="500"
></Image>
<Image
id="unsized-image"
src="https://via.placeholder.com/100"
Expand Down