-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add recipe for using getImage() and creating custom component #5042
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
Merged
sarah11918
merged 9 commits into
withastro:main
from
jdwilkin4:docs-4389-getImage-recipe
Oct 20, 2023
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8dfbd1d
docs: Add recipe for using getImage()
jdwilkin4 6f5d257
fix: review comments
jdwilkin4 08fbdc4
Merge branch 'main' into docs-4389-getImage-recipe
jdwilkin4 da1e160
chore: updating intro for custom img recipe
jdwilkin4 4235aca
fix: remove sentence for link to recipe
jdwilkin4 67bec62
chore: applying review comments
jdwilkin4 00280a9
fix: grammar
jdwilkin4 b3047d7
tiny editing tweaksk
sarah11918 0ecd246
Merge branch 'main' into docs-4389-getImage-recipe
sarah11918 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| --- | ||
| title: Build a custom Picture Component | ||
| description: Learn how to build a custom Picture Component using the getImage function | ||
| i18nReady: true | ||
| type: recipe | ||
| --- | ||
|
|
||
| The `getImage()` function is used for generating images that will be used elsewhere instead of directly in the HTML. In this recipe, you will learn how to create your own `<Picture>` component using the `getImage()` function. | ||
|
|
||
|
|
||
| ## Prerequisites | ||
| - A project with [SSR](/en/guides/server-side-rendering/) (`output: 'server'`) enabled | ||
|
|
||
|
|
||
| ## Recipe | ||
|
|
||
| 1. Create a new Astro component and import the `getImage()` function | ||
|
|
||
| ```astro title="src/components/MyCustomPictureComponent.astro" | ||
| --- | ||
| import { getImage } from "astro:assets"; | ||
| --- | ||
|
|
||
| ``` | ||
|
|
||
| 2. Destructure the `props` from `Astro.props`. You can also use TypeScript to type the props. | ||
|
|
||
| ```astro title="src/components/MyCustomPictureComponent.astro" | ||
| --- | ||
| interface Props { | ||
| mobileImgUrl: string | ImageMetadata; | ||
| desktopImgUrl: string | ImageMetadata; | ||
| alt: string; | ||
| } | ||
|
|
||
| import type { ImageMetadata } from "astro"; | ||
| const { mobileImgUrl, desktopImgUrl, alt } = Astro.props; | ||
| import { getImage } from "astro:assets"; | ||
|
jdwilkin4 marked this conversation as resolved.
Outdated
|
||
| --- | ||
|
|
||
| ``` | ||
|
|
||
| 3. Call the `getImage()` function and pass in the properties you want to use into the options object | ||
|
|
||
| ```astro title="src/components/MyCustomPictureComponent.astro" | ||
| --- | ||
| interface Props { | ||
| mobileImgUrl: string | ImageMetadata; | ||
| desktopImgUrl: string | ImageMetadata; | ||
| alt: string; | ||
| } | ||
|
|
||
| import type { ImageMetadata } from "astro"; | ||
| const { mobileImgUrl, desktopImgUrl, alt } = Astro.props; | ||
| import { getImage } from "astro:assets"; | ||
|
|
||
| const mobileImg = await getImage({ | ||
| src: mobileImgUrl, | ||
| format: "webp", | ||
| width: 200, | ||
| }); | ||
|
|
||
| const desktopImg = await getImage({ | ||
| src: desktopImgUrl, | ||
| format: "webp", | ||
| width: 800, | ||
| }); | ||
|
jdwilkin4 marked this conversation as resolved.
Outdated
|
||
| --- | ||
|
|
||
| ``` | ||
|
|
||
| 4. Add the `source` and `img` elements inside the `picture` element | ||
|
|
||
| ```astro title="src/components/MyCustomPictureComponent.astro" | ||
| --- | ||
| interface Props { | ||
| mobileImgUrl: string | ImageMetadata; | ||
| desktopImgUrl: string | ImageMetadata; | ||
| alt: string; | ||
| } | ||
|
|
||
| import type { ImageMetadata } from "astro"; | ||
| const { mobileImgUrl, desktopImgUrl, alt } = Astro.props; | ||
| import { getImage } from "astro:assets"; | ||
|
|
||
| const mobileImg = await getImage({ | ||
| src: mobileImgUrl, | ||
| format: "webp", | ||
| width: 200, | ||
| }); | ||
|
|
||
| const desktopImg = await getImage({ | ||
| src: desktopImgUrl, | ||
| format: "webp", | ||
| width: 800, | ||
| }); | ||
| --- | ||
|
|
||
| <picture> | ||
| <source media="(max-width: 799px)" srcset={mobileImg.src} /> | ||
| <source media="(min-width: 800px)" srcset={desktopImg.src} /> | ||
| <img src={desktopImg.src} alt={alt} /> | ||
| </picture> | ||
|
|
||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.