Unverified Commit 301c029e authored by Steven's avatar Steven Committed by GitHub

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

For many users refactoring from `<img>` to `<Image>`, we can often support their properties as-is.
The exception was width/height.

This PR allows the `<Image>` component to accept strings for `width` and `height` just like `<img>`.

Related to #18122 
parent 0dff7def
...@@ -29,8 +29,8 @@ type ImageProps = Omit< ...@@ -29,8 +29,8 @@ type ImageProps = Omit<
loading?: LoadingValue loading?: LoadingValue
unoptimized?: boolean unoptimized?: boolean
} & ( } & (
| { width: number; height: number; unsized?: false } | { width: number | string; height: number | string; unsized?: false }
| { width?: number; height?: number; unsized: true } | { width?: number | string; height?: number | string; unsized: true }
) )
const imageData: ImageData = process.env.__NEXT_IMAGE_OPTS as any const imageData: ImageData = process.env.__NEXT_IMAGE_OPTS as any
...@@ -229,16 +229,18 @@ export default function Image({ ...@@ -229,16 +229,18 @@ export default function Image({
className = className ? className + ' __lazy' : '__lazy' 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 divStyle: React.CSSProperties | undefined
let imgStyle: React.CSSProperties | undefined let imgStyle: React.CSSProperties | undefined
let wrapperStyle: React.CSSProperties | undefined let wrapperStyle: React.CSSProperties | undefined
if (typeof height === 'number' && typeof width === 'number' && !unsized) { if (
// <Image src="i.png" width=100 height=100 /> typeof height !== 'undefined' &&
const quotient = height / width typeof width !== 'undefined' &&
!unsized
) {
// <Image src="i.png" width={100} height={100} />
// <Image src="i.png" width="100" height="100" />
const quotient =
parseInt(height as string, 10) / parseInt(width as string, 10)
const ratio = isNaN(quotient) ? 1 : quotient * 100 const ratio = isNaN(quotient) ? 1 : quotient * 100
wrapperStyle = { wrapperStyle = {
maxWidth: '100%', maxWidth: '100%',
...@@ -278,6 +280,10 @@ export default function Image({ ...@@ -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 ( return (
<div style={wrapperStyle}> <div style={wrapperStyle}>
<div style={divStyle}> <div style={divStyle}>
......
...@@ -5,7 +5,7 @@ const Page = () => { ...@@ -5,7 +5,7 @@ const Page = () => {
return ( return (
<div> <div>
<p>Hello World</p> <p>Hello World</p>
<Image id="basic-image" src="/test.jpg" width={400} height={400}></Image> <Image id="basic-image" src="/test.jpg" width="400" height="400"></Image>
<Image id="unsized-image" src="/test.png" unsized></Image> <Image id="unsized-image" src="/test.png" unsized></Image>
<p id="stubtext">This is the index page</p> <p id="stubtext">This is the index page</p>
</div> </div>
......
...@@ -29,6 +29,7 @@ function runTests() { ...@@ -29,6 +29,7 @@ function runTests() {
const result = await browser.eval( const result = await browser.eval(
`document.getElementById('basic-image').naturalWidth` `document.getElementById('basic-image').naturalWidth`
) )
if (result === 0) { if (result === 0) {
throw new Error('Incorrectly loaded image') throw new Error('Incorrectly loaded image')
} }
...@@ -36,10 +37,15 @@ function runTests() { ...@@ -36,10 +37,15 @@ function runTests() {
return 'result-correct' return 'result-correct'
}, /result-correct/) }, /result-correct/)
await browser.eval(
'document.getElementById("unsized-image").scrollIntoView()'
)
await check(async () => { await check(async () => {
const result = await browser.eval( const result = await browser.eval(
`document.getElementById('unsized-image').naturalWidth` `document.getElementById('unsized-image').naturalWidth`
) )
if (result === 0) { if (result === 0) {
throw new Error('Incorrectly loaded image') throw new Error('Incorrectly loaded image')
} }
......
...@@ -6,11 +6,17 @@ const Page = () => { ...@@ -6,11 +6,17 @@ const Page = () => {
<div> <div>
<p>Hello World</p> <p>Hello World</p>
<Image <Image
id="with-and-height" id="width-and-height-num"
src="https://via.placeholder.com/500" src="https://via.placeholder.com/500"
width={500} width={500}
height={500} height={500}
></Image> ></Image>
<Image
id="width-and-height-str"
src="https://via.placeholder.com/500"
width="500"
height="500"
></Image>
<Image <Image
id="unsized-image" id="unsized-image"
src="https://via.placeholder.com/100" src="https://via.placeholder.com/100"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment