Unverified Commit d53e2f2f authored by Steven's avatar Steven Committed by GitHub

Update `next/future/image` to support only `width` or only `height` (#40278)

This PR allows you to resize a statically imported image using only `width` or only `height`. For example:

```jsx
import img from './img.jpg'
<Image src={img} width="200" />
```

Previously, you had to specify both or else the image aspect ratio would not be preserved.
parent 71ad0dd0
...@@ -548,6 +548,8 @@ export default function Image({ ...@@ -548,6 +548,8 @@ export default function Image({
} }
let staticSrc = '' let staticSrc = ''
let widthInt = getInt(width)
let heightInt = getInt(height)
let blurWidth: number | undefined let blurWidth: number | undefined
let blurHeight: number | undefined let blurHeight: number | undefined
if (isStaticImport(src)) { if (isStaticImport(src)) {
...@@ -560,16 +562,6 @@ export default function Image({ ...@@ -560,16 +562,6 @@ export default function Image({
)}` )}`
) )
} }
blurWidth = staticImageData.blurWidth
blurHeight = staticImageData.blurHeight
blurDataURL = blurDataURL || staticImageData.blurDataURL
staticSrc = staticImageData.src
// Ignore width and height (come from the bundler) when "fill" is used
if (!fill) {
height = height || staticImageData.height
width = width || staticImageData.width
}
if (!staticImageData.height || !staticImageData.width) { if (!staticImageData.height || !staticImageData.width) {
throw new Error( throw new Error(
`An object should only be passed to the image component src parameter if it comes from a static image import. It must include height and width. Received ${JSON.stringify( `An object should only be passed to the image component src parameter if it comes from a static image import. It must include height and width. Received ${JSON.stringify(
...@@ -577,6 +569,24 @@ export default function Image({ ...@@ -577,6 +569,24 @@ export default function Image({
)}` )}`
) )
} }
blurWidth = staticImageData.blurWidth
blurHeight = staticImageData.blurHeight
blurDataURL = blurDataURL || staticImageData.blurDataURL
staticSrc = staticImageData.src
if (!fill) {
if (!widthInt && !heightInt) {
widthInt = staticImageData.width
heightInt = staticImageData.height
} else if (widthInt && !heightInt) {
const ratio = widthInt / staticImageData.width
heightInt = Math.round(staticImageData.height * ratio)
} else if (!widthInt && heightInt) {
const ratio = heightInt / staticImageData.height
widthInt = Math.round(staticImageData.width * ratio)
}
}
} }
src = typeof src === 'string' ? src : staticSrc src = typeof src === 'string' ? src : staticSrc
...@@ -593,8 +603,7 @@ export default function Image({ ...@@ -593,8 +603,7 @@ export default function Image({
const [blurComplete, setBlurComplete] = useState(false) const [blurComplete, setBlurComplete] = useState(false)
const [showAltText, setShowAltText] = useState(false) const [showAltText, setShowAltText] = useState(false)
let widthInt = getInt(width)
let heightInt = getInt(height)
const qualityInt = getInt(quality) const qualityInt = getInt(quality)
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
......
...@@ -10,6 +10,7 @@ import testSVG from '../public/test.svg' ...@@ -10,6 +10,7 @@ import testSVG from '../public/test.svg'
import testGIF from '../public/test.gif' import testGIF from '../public/test.gif'
import testBMP from '../public/test.bmp' import testBMP from '../public/test.bmp'
import testICO from '../public/test.ico' import testICO from '../public/test.ico'
import widePNG from '../public/wide.png'
import TallImage from '../components/TallImage' import TallImage from '../components/TallImage'
...@@ -19,7 +20,14 @@ const Page = () => { ...@@ -19,7 +20,14 @@ const Page = () => {
<h1 id="page-header">Static Image</h1> <h1 id="page-header">Static Image</h1>
<Image id="basic-static" src={testImg} placeholder="blur" /> <Image id="basic-static" src={testImg} placeholder="blur" />
<TallImage /> <TallImage />
<Image id="defined-size-static" src={testPNG} height="150" width="150" /> <Image
id="defined-width-and-height"
src={testPNG}
height="150"
width="150"
/>
<Image id="defined-height-only" src={widePNG} height="350" />
<Image id="defined-width-only" src={widePNG} width="400" />
<Image id="require-static" src={require('../public/foo/test-rect.jpg')} /> <Image id="require-static" src={require('../public/foo/test-rect.jpg')} />
<Image <Image
id="basic-non-static" id="basic-non-static"
......
...@@ -69,11 +69,21 @@ const runTests = (isDev) => { ...@@ -69,11 +69,21 @@ const runTests = (isDev) => {
expect(img.attr('width')).toBe('400') expect(img.attr('width')).toBe('400')
expect(img.attr('height')).toBe('300') expect(img.attr('height')).toBe('300')
}) })
it('Should allow provided width and height to override intrinsic', async () => { it('should use width and height prop to override import', async () => {
const img = $('#defined-size-static') const img = $('#defined-width-and-height')
expect(img.attr('width')).toBe('150') expect(img.attr('width')).toBe('150')
expect(img.attr('height')).toBe('150') expect(img.attr('height')).toBe('150')
}) })
it('should use height prop to adjust both width and height', async () => {
const img = $('#defined-height-only')
expect(img.attr('width')).toBe('600')
expect(img.attr('height')).toBe('350')
})
it('should use width prop to adjust both width and height', async () => {
const img = $('#defined-width-only')
expect(img.attr('width')).toBe('400')
expect(img.attr('height')).toBe('233')
})
it('Should add a blur placeholder a statically imported jpg', async () => { it('Should add a blur placeholder a statically imported jpg', async () => {
const style = $('#basic-static').attr('style') const style = $('#basic-static').attr('style')
......
...@@ -10,6 +10,7 @@ import testSVG from '../public/test.svg' ...@@ -10,6 +10,7 @@ import testSVG from '../public/test.svg'
import testGIF from '../public/test.gif' import testGIF from '../public/test.gif'
import testBMP from '../public/test.bmp' import testBMP from '../public/test.bmp'
import testICO from '../public/test.ico' import testICO from '../public/test.ico'
import widePNG from '../public/wide.png'
import TallImage from '../components/TallImage' import TallImage from '../components/TallImage'
...@@ -19,7 +20,14 @@ const Page = () => { ...@@ -19,7 +20,14 @@ const Page = () => {
<h1 id="page-header">Static Image</h1> <h1 id="page-header">Static Image</h1>
<Image id="basic-static" src={testImg} placeholder="blur" /> <Image id="basic-static" src={testImg} placeholder="blur" />
<TallImage /> <TallImage />
<Image id="defined-size-static" src={testPNG} height="150" width="150" /> <Image
id="defined-width-and-height"
src={testPNG}
height="150"
width="150"
/>
<Image id="defined-height-only" src={widePNG} height="350" />
<Image id="defined-width-only" src={widePNG} width="400" />
<Image id="require-static" src={require('../public/foo/test-rect.jpg')} /> <Image id="require-static" src={require('../public/foo/test-rect.jpg')} />
<Image <Image
id="basic-non-static" id="basic-non-static"
......
...@@ -74,11 +74,21 @@ const runTests = (isDev) => { ...@@ -74,11 +74,21 @@ const runTests = (isDev) => {
expect(img.attr('width')).toBe('400') expect(img.attr('width')).toBe('400')
expect(img.attr('height')).toBe('300') expect(img.attr('height')).toBe('300')
}) })
it('Should allow provided width and height to override intrinsic', async () => { it('should use width and height prop to override import', async () => {
const img = $('#defined-size-static') const img = $('#defined-width-and-height')
expect(img.attr('width')).toBe('150') expect(img.attr('width')).toBe('150')
expect(img.attr('height')).toBe('150') expect(img.attr('height')).toBe('150')
}) })
it('should use height prop to adjust both width and height', async () => {
const img = $('#defined-height-only')
expect(img.attr('width')).toBe('600')
expect(img.attr('height')).toBe('350')
})
it('should use width prop to adjust both width and height', async () => {
const img = $('#defined-width-only')
expect(img.attr('width')).toBe('400')
expect(img.attr('height')).toBe('233')
})
it('Should add a blur placeholder a statically imported jpg', async () => { it('Should add a blur placeholder a statically imported jpg', async () => {
const style = $('#basic-static').attr('style') const style = $('#basic-static').attr('style')
......
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