|
| 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