Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
b684b110
Unverified
Commit
b684b110
authored
Nov 05, 2020
by
Steven
Committed by
GitHub
Nov 05, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add props `objectFit` and `objectPosition` to Image component (#18849)
Fixes #18794
parent
8e9e9494
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
57 additions
and
22 deletions
+57
-22
image.md
docs/api-reference/next/image.md
+12
-0
background.js
examples/image-component/pages/background.js
+2
-2
layout-fill.js
examples/image-component/pages/layout-fill.js
+3
-8
styles.module.css
examples/image-component/styles.module.css
+0
-10
image.tsx
packages/next/client/image.tsx
+10
-1
layout-fill.js
.../integration/image-component/default/pages/layout-fill.js
+7
-1
index.test.js
test/integration/image-component/default/test/index.test.js
+9
-0
valid.tsx
test/integration/image-component/typescript/pages/valid.tsx
+14
-0
No files found.
docs/api-reference/next/image.md
View file @
b684b110
...
@@ -113,6 +113,18 @@ Should only be used when the image is visible above the fold. Defaults to false.
...
@@ -113,6 +113,18 @@ Should only be used when the image is visible above the fold. Defaults to false.
In some cases, you may need more advanced usage. The
`Image`
component optionally accepts the following advanced properties.
In some cases, you may need more advanced usage. The
`Image`
component optionally accepts the following advanced properties.
### objectFit
The image fit when using
`layout="fill"`
.
[
Learn more
](
https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
)
### objectPosition
The image position when using
`layout="fill"`
.
[
Learn more
](
https://developer.mozilla.org/en-US/docs/Web/CSS/object-position
)
### loading
### loading
The loading behavior of the image. Defaults to
`lazy`
.
The loading behavior of the image. Defaults to
`lazy`
.
...
...
examples/image-component/pages/background.js
View file @
b684b110
import
Image
from
'next/image'
import
Image
from
'next/image'
import
{
bgWrap
,
bgText
,
objectFitCover
}
from
'../styles.module.css'
import
{
bgWrap
,
bgText
}
from
'../styles.module.css'
const
Background
=
()
=>
(
const
Background
=
()
=>
(
<
div
>
<
div
>
...
@@ -8,8 +8,8 @@ const Background = () => (
...
@@ -8,8 +8,8 @@ const Background = () => (
alt
=
"Mountains"
alt
=
"Mountains"
src
=
"/mountains.jpg"
src
=
"/mountains.jpg"
layout
=
"fill"
layout
=
"fill"
objectFit
=
"cover"
quality
=
{
100
}
quality
=
{
100
}
className
=
{
objectFitCover
}
/
>
/
>
<
/div
>
<
/div
>
<
p
className
=
{
bgText
}
>
<
p
className
=
{
bgText
}
>
...
...
examples/image-component/pages/layout-fill.js
View file @
b684b110
import
Image
from
'next/image'
import
Image
from
'next/image'
import
{
objectFitContain
,
objectFitCover
,
objectFitNone
,
}
from
'../styles.module.css'
const
Fill
=
()
=>
(
const
Fill
=
()
=>
(
<
div
>
<
div
>
...
@@ -13,7 +8,7 @@ const Fill = () => (
...
@@ -13,7 +8,7 @@ const Fill = () => (
alt
=
"Mountains"
alt
=
"Mountains"
src
=
"/mountains.jpg"
src
=
"/mountains.jpg"
layout
=
"fill"
layout
=
"fill"
className
=
{
objectFitCover
}
objectFit
=
"cover"
/>
/>
<
/div
>
<
/div
>
<
div
style
=
{{
position
:
'relative'
,
width
:
'300px'
,
height
:
'500px'
}}
>
<
div
style
=
{{
position
:
'relative'
,
width
:
'300px'
,
height
:
'500px'
}}
>
...
@@ -21,7 +16,7 @@ const Fill = () => (
...
@@ -21,7 +16,7 @@ const Fill = () => (
alt
=
"Mountains"
alt
=
"Mountains"
src
=
"/mountains.jpg"
src
=
"/mountains.jpg"
layout
=
"fill"
layout
=
"fill"
className
=
{
objectFitContain
}
objectFit
=
"contain"
/>
/>
<
/div
>
<
/div
>
<
div
style
=
{{
position
:
'relative'
,
width
:
'300px'
,
height
:
'500px'
}}
>
<
div
style
=
{{
position
:
'relative'
,
width
:
'300px'
,
height
:
'500px'
}}
>
...
@@ -29,7 +24,7 @@ const Fill = () => (
...
@@ -29,7 +24,7 @@ const Fill = () => (
alt
=
"Mountains"
alt
=
"Mountains"
src
=
"/mountains.jpg"
src
=
"/mountains.jpg"
layout
=
"fill"
layout
=
"fill"
className
=
{
objectFitNone
}
objectFit
=
"none"
quality
=
{
100
}
quality
=
{
100
}
/
>
/
>
<
/div
>
<
/div
>
...
...
examples/image-component/styles.module.css
View file @
b684b110
...
@@ -48,13 +48,3 @@
...
@@ -48,13 +48,3 @@
padding-top
:
40vh
;
padding-top
:
40vh
;
text-shadow
:
1px
1px
1px
#3c5c5e
;
text-shadow
:
1px
1px
1px
#3c5c5e
;
}
}
.objectFitContain
{
object-fit
:
contain
;
}
.objectFitCover
{
object-fit
:
cover
;
}
.objectFitNone
{
object-fit
:
none
;
}
packages/next/client/image.tsx
View file @
b684b110
...
@@ -30,6 +30,8 @@ type ImageData = {
...
@@ -30,6 +30,8 @@ type ImageData = {
domains
?:
string
[]
domains
?:
string
[]
}
}
type
ImgElementStyle
=
NonNullable
<
JSX
.
IntrinsicElements
[
'img'
][
'style'
]
>
type
ImageProps
=
Omit
<
type
ImageProps
=
Omit
<
JSX
.
IntrinsicElements
[
'img'
],
JSX
.
IntrinsicElements
[
'img'
],
'src'
|
'srcSet'
|
'ref'
|
'width'
|
'height'
|
'loading'
|
'style'
'src'
|
'srcSet'
|
'ref'
|
'width'
|
'height'
|
'loading'
|
'style'
...
@@ -39,6 +41,8 @@ type ImageProps = Omit<
...
@@ -39,6 +41,8 @@ type ImageProps = Omit<
priority
?:
boolean
priority
?:
boolean
loading
?:
LoadingValue
loading
?:
LoadingValue
unoptimized
?:
boolean
unoptimized
?:
boolean
objectFit
?:
ImgElementStyle
[
'objectFit'
]
objectPosition
?:
ImgElementStyle
[
'objectPosition'
]
}
&
(
}
&
(
|
{
|
{
width
?:
never
width
?:
never
...
@@ -247,6 +251,8 @@ export default function Image({
...
@@ -247,6 +251,8 @@ export default function Image({
quality
,
quality
,
width
,
width
,
height
,
height
,
objectFit
,
objectPosition
,
...
all
...
all
}:
ImageProps
)
{
}:
ImageProps
)
{
const
thisEl
=
useRef
<
HTMLImageElement
>
(
null
)
const
thisEl
=
useRef
<
HTMLImageElement
>
(
null
)
...
@@ -342,7 +348,7 @@ export default function Image({
...
@@ -342,7 +348,7 @@ export default function Image({
let
wrapperStyle
:
JSX
.
IntrinsicElements
[
'div'
][
'style'
]
|
undefined
let
wrapperStyle
:
JSX
.
IntrinsicElements
[
'div'
][
'style'
]
|
undefined
let
sizerStyle
:
JSX
.
IntrinsicElements
[
'div'
][
'style'
]
|
undefined
let
sizerStyle
:
JSX
.
IntrinsicElements
[
'div'
][
'style'
]
|
undefined
let
sizerSvg
:
string
|
undefined
let
sizerSvg
:
string
|
undefined
let
imgStyle
:
JSX
.
IntrinsicElements
[
'img'
][
'style'
]
=
{
let
imgStyle
:
ImgElementStyle
|
undefined
=
{
visibility
:
lazy
?
'hidden'
:
'visible'
,
visibility
:
lazy
?
'hidden'
:
'visible'
,
position
:
'absolute'
,
position
:
'absolute'
,
...
@@ -363,6 +369,9 @@ export default function Image({
...
@@ -363,6 +369,9 @@ export default function Image({
maxWidth
:
'100%'
,
maxWidth
:
'100%'
,
minHeight
:
'100%'
,
minHeight
:
'100%'
,
maxHeight
:
'100%'
,
maxHeight
:
'100%'
,
objectFit
,
objectPosition
,
}
}
if
(
if
(
typeof
widthInt
!==
'undefined'
&&
typeof
widthInt
!==
'undefined'
&&
...
...
test/integration/image-component/default/pages/layout-fill.js
View file @
b684b110
...
@@ -10,7 +10,13 @@ const Page = () => {
...
@@ -10,7 +10,13 @@ const Page = () => {
<
/div
>
<
/div
>
<
p
>
Layout
Fill
<
/p
>
<
p
>
Layout
Fill
<
/p
>
<
div
style
=
{{
position
:
'relative'
,
width
:
'100vw'
,
height
:
'100vh'
}}
>
<
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 center"
/>
<
/div
>
<
/div
>
<
p
>
Layout
Fill
<
/p
>
<
p
>
Layout
Fill
<
/p
>
<
/div
>
<
/div
>
...
...
test/integration/image-component/default/test/index.test.js
View file @
b684b110
...
@@ -294,6 +294,15 @@ function runTests(mode) {
...
@@ -294,6 +294,15 @@ function runTests(mode) {
})
})
expect
(
await
getComputed
(
browser
,
id
,
'width'
)).
toBe
(
smallWidth
)
expect
(
await
getComputed
(
browser
,
id
,
'width'
)).
toBe
(
smallWidth
)
expect
(
await
getComputed
(
browser
,
id
,
'height'
)).
toBe
(
smallHeight
)
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 center'
)
}
finally
{
}
finally
{
if
(
browser
)
{
if
(
browser
)
{
await
browser
.
close
()
await
browser
.
close
()
...
...
test/integration/image-component/typescript/pages/valid.tsx
View file @
b684b110
...
@@ -44,6 +44,20 @@ const Page = () => {
...
@@ -44,6 +44,20 @@ const Page = () => {
width=
{
100
}
width=
{
100
}
height=
{
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
>
<
p
id=
"stubtext"
>
This is valid usage of the Image component
</
p
>
</
div
>
</
div
>
)
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment