Skip to content

Commit 5705553

Browse files
i18n(ja): recipe/build custom img component (#12419)
Co-authored-by: Yan <61414485+yanthomasdev@users.noreply.github.com>
1 parent 2316e02 commit 5705553

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
---
2+
title: カスタム画像コンポーネントを作成する
3+
description: getImage関数を使って、メディアクエリ対応のカスタム画像コンポーネントを作成する方法を学びます。
4+
i18nReady: true
5+
type: recipe
6+
---
7+
import { Steps } from '@astrojs/starlight/components';
8+
9+
Astroには画像の表示と最適化に使える組み込みコンポーネントが2つあります。`<Picture>`コンポーネントはレスポンシブ画像を表示し、異なる形式やサイズに対応できます。`<Image>`コンポーネントは画像を最適化し、形式や画質などのプロパティを渡せます。
10+
11+
`<Picture>``<Image>`が現時点でサポートしていないオプションが必要な場合は、`getImage()`関数を使ってカスタムコンポーネントを作成できます。
12+
13+
このレシピでは、[`getImage()`関数](/ja/guides/images/#getimageで画像を生成する)を使い、メディアクエリに基づいて異なるソース画像を表示する独自の画像コンポーネントを作成します。
14+
15+
## レシピ
16+
17+
<Steps>
18+
1. 新しいAstroコンポーネントを作成し、`getImage()`関数をインポートします。
19+
20+
21+
```astro title="src/components/MyCustomImageComponent.astro"
22+
---
23+
import { getImage } from "astro:assets";
24+
---
25+
```
26+
27+
2. カスタム画像用の新しいコンポーネントを作成します。`MyCustomImageComponent.astro``Astro.props`から3つの`props`を受け取ります。`mobileImgUrl``desktopImgUrl`はビューポートに応じた画像生成に使い、`alt`は代替テキストに使います。これらのpropsはコンポーネントをレンダリングする箇所で渡します。次のインポートを追加し、使用するpropsを定義します。TypeScriptで型付けもできます。
28+
29+
30+
```astro title="src/components/MyCustomImageComponent.astro" ins={3, 11}
31+
---
32+
import type { ImageMetadata } from "astro";
33+
import { getImage } from "astro:assets";
34+
35+
interface Props {
36+
mobileImgUrl: string | ImageMetadata;
37+
desktopImgUrl: string | ImageMetadata;
38+
alt: string;
39+
}
40+
41+
const { mobileImgUrl, desktopImgUrl, alt } = Astro.props;
42+
---
43+
```
44+
45+
3. `getImage()`関数に必要なプロパティを渡して、レスポンシブ画像をそれぞれ定義します。
46+
47+
48+
```astro title="src/components/MyCustomImageComponent.astro" ins={13-18, 20-25}
49+
---
50+
import type { ImageMetadata } from "astro";
51+
import { getImage } from "astro:assets";
52+
53+
interface Props {
54+
mobileImgUrl: string | ImageMetadata;
55+
desktopImgUrl: string | ImageMetadata;
56+
alt: string;
57+
}
58+
59+
const { mobileImgUrl, desktopImgUrl, alt } = Astro.props;
60+
61+
const mobileImg = await getImage({
62+
src: mobileImgUrl,
63+
format: "webp",
64+
width: 200,
65+
height: 200,
66+
});
67+
68+
const desktopImg = await getImage({
69+
src: desktopImgUrl,
70+
format: "webp",
71+
width: 800,
72+
height: 200,
73+
});
74+
---
75+
```
76+
77+
4. 望むメディアクエリに基づいて異なる画像を`srcset`に出し分ける`<picture>`要素を作成します。
78+
79+
80+
```astro title="src/components/MyCustomImageComponent.astro" ins={28-32}
81+
---
82+
import type { ImageMetadata } from "astro";
83+
import { getImage } from "astro:assets";
84+
85+
interface Props {
86+
mobileImgUrl: string | ImageMetadata;
87+
desktopImgUrl: string | ImageMetadata;
88+
alt: string;
89+
}
90+
91+
const { mobileImgUrl, desktopImgUrl, alt } = Astro.props;
92+
93+
const mobileImg = await getImage({
94+
src: mobileImgUrl,
95+
format: "webp",
96+
width: 200,
97+
height: 200,
98+
});
99+
100+
const desktopImg = await getImage({
101+
src: desktopImgUrl,
102+
format: "webp",
103+
width: 800,
104+
height: 200,
105+
});
106+
---
107+
108+
<picture>
109+
<source media="(max-width: 799px)" srcset={mobileImg.src} />
110+
<source media="(min-width: 800px)" srcset={desktopImg.src} />
111+
<img src={desktopImg.src} alt={alt} />
112+
</picture>
113+
```
114+
115+
5. 任意の`.astro`ファイルで`<MyCustomImageComponent />`をインポートして使用します。2つのビューポート幅に対応するpropsを渡します。
116+
117+
118+
```astro title="src/pages/index.astro"
119+
---
120+
import MyCustomImageComponent from "../components/MyCustomImageComponent.astro";
121+
import mobileImage from "../images/mobile-profile-image.jpg";
122+
import desktopImage from "../images/desktop-profile-image.jpg";
123+
---
124+
125+
<MyCustomImageComponent
126+
mobileImgUrl={mobileImage}
127+
desktopImgUrl={desktopImage}
128+
alt="ユーザープロフィール画像"
129+
/>
130+
```
131+
</Steps>

0 commit comments

Comments
 (0)