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 props objectFit and objectPosition to Image component #18849

Merged
merged 5 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions examples/image-component/pages/background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Image from 'next/image'
import { bgWrap, bgText, objectFitCover } from '../styles.module.css'
import { bgWrap, bgText } from '../styles.module.css'

const Background = () => (
<div>
Expand All @@ -8,8 +8,8 @@ const Background = () => (
alt="Mountains"
src="/mountains.jpg"
layout="fill"
objectFit="cover"
quality={100}
className={objectFitCover}
/>
</div>
<p className={bgText}>
Expand Down
11 changes: 3 additions & 8 deletions examples/image-component/pages/layout-fill.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import Image from 'next/image'
import {
objectFitContain,
objectFitCover,
objectFitNone,
} from '../styles.module.css'

const Fill = () => (
<div>
Expand All @@ -13,23 +8,23 @@ const Fill = () => (
alt="Mountains"
src="/mountains.jpg"
layout="fill"
className={objectFitCover}
objectFit="cover"
/>
</div>
<div style={{ position: 'relative', width: '300px', height: '500px' }}>
<Image
alt="Mountains"
src="/mountains.jpg"
layout="fill"
className={objectFitContain}
objectFit="contain"
/>
</div>
<div style={{ position: 'relative', width: '300px', height: '500px' }}>
<Image
alt="Mountains"
src="/mountains.jpg"
layout="fill"
className={objectFitNone}
objectFit="none"
quality={100}
/>
</div>
Expand Down
10 changes: 0 additions & 10 deletions examples/image-component/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,3 @@
padding-top: 40vh;
text-shadow: 1px 1px 1px #3c5c5e;
}

.objectFitContain {
object-fit: contain;
}
.objectFitCover {
object-fit: cover;
}
.objectFitNone {
object-fit: none;
}
11 changes: 10 additions & 1 deletion packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type ImageData = {
domains?: string[]
}

type ImgElementStyle = NonNullable<JSX.IntrinsicElements['img']['style']>

type ImageProps = Omit<
JSX.IntrinsicElements['img'],
'src' | 'srcSet' | 'ref' | 'width' | 'height' | 'loading' | 'style'
Expand All @@ -39,6 +41,8 @@ type ImageProps = Omit<
priority?: boolean
loading?: LoadingValue
unoptimized?: boolean
objectFit?: ImgElementStyle['objectFit']
objectPosition?: ImgElementStyle['objectPosition']
} & (
| {
width?: never
Expand Down Expand Up @@ -247,6 +251,8 @@ export default function Image({
quality,
width,
height,
objectFit,
objectPosition,
...all
}: ImageProps) {
const thisEl = useRef<HTMLImageElement>(null)
Expand Down Expand Up @@ -342,7 +348,7 @@ export default function Image({
let wrapperStyle: JSX.IntrinsicElements['div']['style'] | undefined
let sizerStyle: JSX.IntrinsicElements['div']['style'] | undefined
let sizerSvg: string | undefined
let imgStyle: JSX.IntrinsicElements['img']['style'] = {
let imgStyle: ImgElementStyle | undefined = {
visibility: lazy ? 'hidden' : 'visible',

position: 'absolute',
Expand All @@ -363,6 +369,9 @@ export default function Image({
maxWidth: '100%',
minHeight: '100%',
maxHeight: '100%',

objectFit,
objectPosition,
}
if (
typeof widthInt !== 'undefined' &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ const Page = () => {
</div>
<p>Layout Fill</p>
<div style={{ position: 'relative', width: '100vw', height: '100vh' }}>
<Image id="fill2" src="/wide.png" layout="fill" />
<Image
id="fill2"
src="/wide.png"
layout="fill"
objectFit="cover"
objectPosition="left"
/>
</div>
<p>Layout Fill</p>
</div>
Expand Down
9 changes: 9 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ function runTests(mode) {
})
expect(await getComputed(browser, id, 'width')).toBe(smallWidth)
expect(await getComputed(browser, id, 'height')).toBe(smallHeight)

const objectFit = await browser.eval(
`document.getElementById("${id}").style.objectFit`
)
const objectPosition = await browser.eval(
`document.getElementById("${id}").style.objectPosition`
)
expect(objectFit).toBe('cover')
expect(objectPosition).toBe('left')
} finally {
if (browser) {
await browser.close()
Expand Down
14 changes: 14 additions & 0 deletions test/integration/image-component/typescript/pages/valid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ const Page = () => {
width={100}
height={100}
/>
<Image
id="object-fit-cover-position-right"
src="https://via.placeholder.com/500"
layout="fill"
objectFit="cover"
objectPosition="right"
/>
<Image
id="object-fit-scale-down-position-50px"
src="https://via.placeholder.com/500"
layout="fill"
objectFit="scale-down"
objectPosition="50px"
/>
<p id="stubtext">This is valid usage of the Image component</p>
</div>
)
Expand Down