Skip to content

Commit e2f16f9

Browse files
authored
Update next/image docs and examples (#41434)
This PR updates the docs for the following code change: - #41399 There are a few updates here: - [x] Update docs - [x] Update links to docs inside component - [x] Update examples - [x] Fix corner cases in codemod
1 parent e61820e commit e2f16f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1070
-455
lines changed

docs/advanced-features/codemods.md

+91-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,97 @@ Dangerously migrates from `next/legacy/image` to the new `next/image` by adding
6464
- Removes `objectPosition` prop and adds `style`
6565
- Removes `lazyBoundary` prop
6666
- Removes `lazyRoot` prop
67-
- TODO: handle `loader`
67+
- TODO: does not migrate the `loader` config. If you need it, you must manually add a `loader` prop.
68+
69+
#### Before: intrinsic
70+
71+
```jsx
72+
import Image from 'next/image'
73+
import img from '../img.png'
74+
75+
function Page() {
76+
return <Image src={img} />
77+
}
78+
```
79+
80+
#### After: intrinsic
81+
82+
```jsx
83+
import Image from 'next/image'
84+
import img from '../img.png'
85+
86+
const css = { maxWidth: '100%', height: 'auto' }
87+
function Page() {
88+
return <Image src={img} style={css} />
89+
}
90+
```
91+
92+
#### Before: responsive
93+
94+
```jsx
95+
import Image from 'next/image'
96+
import img from '../img.png'
97+
98+
function Page() {
99+
return <Image src={img} layout="responsive" />
100+
}
101+
```
102+
103+
#### After: responsive
104+
105+
```jsx
106+
import Image from 'next/image'
107+
import img from '../img.png'
108+
109+
const css = { width: '100%', height: 'auto' }
110+
function Page() {
111+
return <Image src={img} sizes="100vw" style={css} />
112+
}
113+
```
114+
115+
#### Before: fill
116+
117+
```jsx
118+
import Image from 'next/image'
119+
import img from '../img.png'
120+
121+
function Page() {
122+
return <Image src={img} layout="fill" />
123+
}
124+
```
125+
126+
#### After: fill
127+
128+
```jsx
129+
import Image from 'next/image'
130+
import img from '../img.png'
131+
132+
function Page() {
133+
return <Image src={img} sizes="100vw" fill />
134+
}
135+
```
136+
137+
#### Before: fixed
138+
139+
```jsx
140+
import Image from 'next/image'
141+
import img from '../img.png'
142+
143+
function Page() {
144+
return <Image src={img} layout="fixed" />
145+
}
146+
```
147+
148+
#### After: fixed
149+
150+
```jsx
151+
import Image from 'next/image'
152+
import img from '../img.png'
153+
154+
function Page() {
155+
return <Image src={img} />
156+
}
157+
```
68158

69159
## Next.js 11
70160

docs/api-reference/next/image.md

+80-178
Large diffs are not rendered by default.

docs/api-reference/next/future/image.md docs/api-reference/next/legacy/image.md

+170-171
Large diffs are not rendered by default.

docs/basic-features/image-optimization.md

+11-23
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ To add an image to your application, import the [`next/image`](/docs/api-referen
2828
import Image from 'next/image'
2929
```
3030

31-
Alternatively, you can import [`next/future/image`](/docs/api-reference/next/future/image.md) if you need a component much closer to the native `<img>` element:
32-
33-
```jsx
34-
import Image from 'next/future/image'
35-
```
36-
3731
Now, you can define the `src` for your image (either local or remote).
3832

3933
### Local Images
@@ -151,15 +145,15 @@ Because `next/image` is designed to guarantee good performance results, it canno
151145

152146
1. Automatically, using a [static import](#local-images)
153147
2. Explicitly, by including a [`width`](/docs/api-reference/next/image.md#width) and [`height`](/docs/api-reference/next/image.md#height) property
154-
3. Implicitly, by using [`layout="fill"`](/docs/api-reference/next/image.md#layout) which causes the image to expand to fill its parent element.
148+
3. Implicitly, by using [fill](/docs/api-reference/next/image.md#fill) which causes the image to expand to fill its parent element.
155149

156150
> ### What if I don't know the size of my images?
157151
>
158152
> If you are accessing images from a source without knowledge of the images' sizes, there are several things you can do:
159153
>
160-
> **Use `layout='fill'`**
154+
> **Use `fill``**
161155
>
162-
> The `fill` layout mode allows your image to be sized by its parent element. Consider using CSS to give the image's parent element space on the page, then using the [`objectFit property`](/docs/api-reference/next/image.md#objectfit) with `fill`, `contain`, or `cover`, along with the [`objectPosition property`](/docs/api-reference/next/image.md#objectposition) to define how the image should occupy that space.
156+
> The [`fill`](/docs/api-reference/next/image#fill) prop allows your image to be sized by its parent element. Consider using CSS to give the image's parent element space on the page along [`sizes`](/docs/api-reference/next/image#sizes) prop to match any media query break points. You can also use [`object-fit`](https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit) with `fill`, `contain`, or `cover`, and [`object-position`](https://developer.mozilla.org/en-US/docs/Web/CSS/object-position) to define how the image should occupy that space.
163157
>
164158
> **Normalize your images**
165159
>
@@ -175,27 +169,21 @@ If none of the suggested methods works for sizing your images, the `next/image`
175169

176170
> Note: Many of the styling issues listed below can be solved with [`next/future/image`](/docs/api-reference/next/future/image.md)
177171
178-
Styling the Image component is not that different from styling a normal `<img>` element, but there are a few guidelines to keep in mind:
179-
180-
**Pick the correct layout mode**
181-
182-
The image component has several different [layout modes](/docs/api-reference/next/image.md#layout) that define how it is sized on the page. If the styling of your image isn't turning out the way you want, consider experimenting with other layout modes.
183-
184-
**Target the image with className, not based on DOM structure**
172+
Styling the Image component is similar to styling a normal `<img>` element, but there are a few guidelines to keep in mind:
185173

186-
For most layout modes, the Image component will have a DOM structure of one `<img>` tag wrapped by exactly one `<span>`. For some modes, it may also have a sibling `<span>` for spacing. These additional `<span>` elements are critical to allow the component to prevent layout shifts.
174+
**Use `className` or `style`, not `styled-jsx`**
187175

188-
The recommended way to style the inner `<img>` is to set the `className` prop on the Image component to the value of an imported [CSS Module](/docs/basic-features/built-in-css-support.md#adding-component-level-css). The value of `className` will be automatically applied to the underlying `<img>` element.
176+
In most cases, we recommend using the `className` prop. This can be an imported [CSS Module](/docs/basic-features/built-in-css-support.md#adding-component-level-css), a [global stylesheet](/docs/basic-features/built-in-css-support#adding-a-global-stylesheet), etc.
189177

190-
Alternatively, you can import a [global stylesheet](/docs/basic-features/built-in-css-support#adding-a-global-stylesheet) and manually set the `className` prop to the same name used in the global stylesheet.
178+
You can also use the `style` prop to assign inline styles.
191179

192-
You cannot use [styled-jsx](/docs/basic-features/built-in-css-support.md#css-in-js) because it's scoped to the current component.
180+
You cannot use [styled-jsx](/docs/basic-features/built-in-css-support.md#css-in-js) because it's scoped to the current component (unless you mark the style as `global`).
193181

194-
**When using `layout='fill'`, the parent element must have `position: relative`**
182+
**When using `fill`, the parent element must have `position: relative`**
195183

196184
This is necessary for the proper rendering of the image element in that layout mode.
197185

198-
**When using `layout='responsive'`, the parent element must have `display: block`**
186+
**When using `fill`, the parent element must have `display: block`**
199187

200188
This is the default for `<div>` elements but should be specified otherwise.
201189

@@ -205,7 +193,7 @@ This is the default for `<div>` elements but should be specified otherwise.
205193

206194
### Styling Examples
207195

208-
For examples of the Image component used with the various fill modes, see the [Image component example app](https://image-component.nextjs.gallery/).
196+
For examples of the Image component used with the various styles, see the [Image component example app](https://image-component.nextjs.gallery).
209197

210198
## Configuration
211199

docs/manifest.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,14 @@
427427
}
428428
},
429429
{
430-
"title": "next/future/image",
431-
"path": "/docs/api-reference/next/future/image.md"
430+
"path": "/docs/api-reference/next/future/image",
431+
"redirect": {
432+
"destination": "/docs/api-reference/next/image"
433+
}
434+
},
435+
{
436+
"title": "next/legacy/image",
437+
"path": "/docs/api-reference/next/legacy/image.md"
432438
},
433439
{
434440
"title": "Edge Runtime",
+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
/** @type {import('next').NextConfig} */
22
module.exports = {
33
images: {
4-
domains: ['assets.vercel.com'],
54
formats: ['image/avif', 'image/webp'],
5+
remotePatterns: [
6+
{
7+
protocol: 'https',
8+
hostname: 'assets.vercel.com',
9+
port: '',
10+
pathname: '/image/upload/**',
11+
},
12+
],
613
},
714
}

examples/image-component/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"start": "next start"
77
},
88
"dependencies": {
9-
"next": "latest",
9+
"next": "canary",
1010
"react": "^18.2.0",
1111
"react-dom": "^18.2.0"
1212
},

examples/image-component/pages/background.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import Image from 'next/image'
22
import ViewSource from '../components/view-source'
33
import styles from '../styles.module.css'
4+
import mountains from '../public/mountains.jpg'
45

56
const BackgroundPage = () => (
67
<div>
78
<ViewSource pathname="pages/background.tsx" />
89
<div className={styles.bgWrap}>
910
<Image
1011
alt="Mountains"
11-
src="/mountains.jpg"
12-
layout="fill"
13-
objectFit="cover"
12+
src={mountains}
13+
placeholder="blur"
1414
quality={100}
15+
fill
16+
sizes="100vw"
17+
style={{
18+
objectFit: 'cover',
19+
}}
1520
/>
1621
</div>
1722
<p className={styles.bgText}>

examples/image-component/pages/color.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const Color = () => (
2727
blurDataURL={rgbDataURL(237, 181, 6)}
2828
width={750}
2929
height={1000}
30+
style={{
31+
maxWidth: '100%',
32+
height: 'auto',
33+
}}
3034
/>
3135
<Image
3236
alt="Cat"
@@ -35,6 +39,10 @@ const Color = () => (
3539
blurDataURL={rgbDataURL(2, 129, 210)}
3640
width={750}
3741
height={1000}
42+
style={{
43+
maxWidth: '100%',
44+
height: 'auto',
45+
}}
3846
/>
3947
</div>
4048
)
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Image from 'next/image'
2+
import ViewSource from '../components/view-source'
3+
import mountains from '../public/mountains.jpg'
4+
5+
const Fill = () => (
6+
<div>
7+
<ViewSource pathname="pages/layout-fill.tsx" />
8+
<h1>Image Component With Layout Fill</h1>
9+
<div style={{ position: 'relative', width: '300px', height: '500px' }}>
10+
<Image
11+
alt="Mountains"
12+
src={mountains}
13+
fill
14+
sizes="100vw"
15+
style={{
16+
objectFit: 'cover',
17+
}}
18+
/>
19+
</div>
20+
<div style={{ position: 'relative', width: '300px', height: '500px' }}>
21+
<Image
22+
alt="Mountains"
23+
src={mountains}
24+
fill
25+
sizes="100vw"
26+
style={{
27+
objectFit: 'contain',
28+
}}
29+
/>
30+
</div>
31+
<div style={{ position: 'relative', width: '300px', height: '500px' }}>
32+
<Image
33+
alt="Mountains"
34+
src={mountains}
35+
quality={100}
36+
fill
37+
sizes="100vw"
38+
style={{
39+
objectFit: 'none',
40+
}}
41+
/>
42+
</div>
43+
</div>
44+
)
45+
46+
export default Fill

0 commit comments

Comments
 (0)