Skip to content

Commit a6b283a

Browse files
authored
docs: add docs for responsive images (#12429)
* docs: add responsive images flag docs * docs: adds jsdoc for image components * chore: updates from review * Fix jsdoc * Changes from review
1 parent d28ba03 commit a6b283a

File tree

3 files changed

+139
-19
lines changed

3 files changed

+139
-19
lines changed

packages/astro/src/assets/types.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,26 +157,60 @@ type ImageSharedProps<T> = T & {
157157
* ```
158158
*/
159159
quality?: ImageQuality;
160-
161-
layout?: ImageLayout;
162-
163-
fit?: ImageFit;
164-
165-
position?: string;
166160
} & (
167161
| {
168162
/**
169-
* The layout type for responsive images. Overrides any default set in the Astro config.
170-
* Requires the `experimental.responsiveImages` flag to be enabled.
163+
* The layout type for responsive images. Requires the `experimental.responsiveImages` flag to be enabled in the Astro config.
164+
*
165+
* Allowed values are `responsive`, `fixed`, `full-width` or `none`. Defaults to value of `image.experimentalLayout`.
171166
*
172167
* - `responsive` - The image will scale to fit the container, maintaining its aspect ratio, but will not exceed the specified dimensions.
173168
* - `fixed` - The image will maintain its original dimensions.
174-
* - `full-width` - The image will scale to fit the container, maintaining its aspect ratio.
169+
* - `full-width` - The image will scale to fit the container, maintaining its aspect ratio, even if that means the image will exceed its original dimensions.
170+
*
171+
* **Example**:
172+
* ```astro
173+
* <Image src={...} layout="responsive" alt="..." />
174+
* ```
175175
*/
176+
176177
layout?: ImageLayout;
177-
fit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down' | (string & {});
178+
179+
/**
180+
* Defines how the image should be cropped if the aspect ratio is changed. Requires the `experimental.responsiveImages` flag to be enabled in the Astro config.
181+
*
182+
* Default is `cover`. Allowed values are `fill`, `contain`, `cover`, `none` or `scale-down`. These behave like the equivalent CSS `object-fit` values. Other values may be passed if supported by the image service.
183+
*
184+
* **Example**:
185+
* ```astro
186+
* <Image src={...} fit="contain" alt="..." />
187+
* ```
188+
*/
189+
190+
fit?: ImageFit;
191+
192+
/**
193+
* Defines the position of the image when cropping. Requires the `experimental.responsiveImages` flag to be enabled in the Astro config.
194+
*
195+
* The value is a string that specifies the position of the image, which matches the CSS `object-position` property. Other values may be passed if supported by the image service.
196+
*
197+
* **Example**:
198+
* ```astro
199+
* <Image src={...} position="center top" alt="..." />
200+
* ```
201+
*/
202+
178203
position?: string;
204+
/**
205+
* If true, the image will be loaded with a higher priority. This can be useful for images that are visible above the fold. There should usually be only one image with `priority` set to `true` per page.
206+
* All other images will be lazy-loaded according to when they are in the viewport.
207+
* **Example**:
208+
* ```astro
209+
* <Image src={...} priority alt="..." />
210+
* ```
211+
*/
179212
priority?: boolean;
213+
180214
/**
181215
* A list of widths to generate images for. The value of this property will be used to assign the `srcset` property on the final `img` element.
182216
*

packages/astro/src/assets/utils/imageAttributes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function applyResponsiveAttributes<
2727
layout,
2828
image,
2929
props,
30-
additionalAttributes
30+
additionalAttributes,
3131
}: {
3232
layout: Exclude<ImageLayout, 'none'>;
3333
image: GetImageResult;

packages/astro/src/types/public/config.ts

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,27 +1743,113 @@ export interface ViteUserConfig extends OriginalViteUserConfig {
17431743
* @version 5.0.0
17441744
* @description
17451745
*
1746-
* Enables and configures automatic responsive image options for images in your project. Set to `true` (for no default option passed to your images) or an object with default responsive image configuration options.
1746+
* Enables automatic responsive images in your project.
17471747
*
1748-
* ```js
1748+
* ```js title=astro.config.mjs
17491749
* {
1750-
* experimental: {
1751-
* responsiveImages: {
1752-
* layout: 'responsive',
1750+
* experimental: {
1751+
* responsiveImages: true,
17531752
* },
17541753
* }
17551754
* ```
17561755
*
1757-
* Then, you can add a `layout` option to any `<Image />` component when needed to override your default configuration: `responsive`, `fixed`, `full-width`, or `none`. This attribute is required to transform your images if `responsiveImages.layout` is not configured. Images with a layout value of `undefined` or `none` will not be transformed.
1756+
* When enabled, you can pass a `layout` props to any `<Image />` or `<Picture />` component to create a responsive image. When a layout is set, images have automatically generated `srcset` and `sizes` attributes based on the image's dimensions and the layout type. Images with `responsive` and `full-width` layouts will have styles applied to ensure they resize according to their container.
1757+
*
1758+
* ```astro title=MyComponent.astro
1759+
* ---
1760+
* import { Image, Picture } from 'astro:assets';
1761+
* import myImage from '../assets/my_image.png';
1762+
* ---
1763+
* <Image src={myImage} alt="A description of my image." layout='responsive' width={800} height={600} />
1764+
* <Picture src={myImage} alt="A description of my image." layout='full-width' formats={['avif', 'webp', 'jpeg']} />
1765+
* ```
1766+
* This `<Image />` component will generate the following HTML output:
1767+
* ```html title=Output
1768+
*
1769+
* <img
1770+
* src="/_astro/my_image.hash3.webp"
1771+
* srcset="/_astro/my_image.hash1.webp 640w,
1772+
* /_astro/my_image.hash2.webp 750w,
1773+
* /_astro/my_image.hash3.webp 800w,
1774+
* /_astro/my_image.hash4.webp 828w,
1775+
* /_astro/my_image.hash5.webp 1080w,
1776+
* /_astro/my_image.hash6.webp 1280w,
1777+
* /_astro/my_image.hash7.webp 1600w"
1778+
* alt="A description of my image"
1779+
* sizes="(min-width: 800px) 800px, 100vw"
1780+
* loading="lazy"
1781+
* decoding="async"
1782+
* fetchpriority="auto"
1783+
* width="800"
1784+
* height="600"
1785+
* style="--w: 800; --h: 600; --fit: cover; --pos: center;"
1786+
* data-astro-image="responsive"
1787+
* >
1788+
* ```
1789+
*
1790+
* The following styles are applied to ensure the images resize correctly:
1791+
*
1792+
* ```css title="Responsive Image Styles"
1793+
* [data-astro-image] {
1794+
* width: 100%;
1795+
* height: auto;
1796+
* object-fit: var(--fit);
1797+
* object-position: var(--pos);
1798+
* aspect-ratio: var(--w) / var(--h)
1799+
* }
1800+
*
1801+
* [data-astro-image=responsive] {
1802+
* max-width: calc(var(--w) * 1px);
1803+
* max-height: calc(var(--h) * 1px)
1804+
* }
1805+
*
1806+
* [data-astro-image=fixed] {
1807+
* width: calc(var(--w) * 1px);
1808+
* height: calc(var(--h) * 1px)
1809+
* }
1810+
* ```
1811+
* You can enable responsive images for all `<Image />` and `<Picture />` components by setting `image.experimentalLayout` with a default value. This can be overridden by the `layout` prop on each component.
1812+
*
1813+
* **Example:**
1814+
* ```js title=astro.config.mjs
1815+
* {
1816+
* image: {
1817+
* // Used for all `<Image />` and `<Picture />` components unless overridden
1818+
* experimentalLayout: 'responsive',
1819+
* },
1820+
* experimental: {
1821+
* responsiveImages: true,
1822+
* },
1823+
* }
1824+
* ```
17581825
*
1759-
* ```astro
1826+
* ```astro title=MyComponent.astro
17601827
* ---
17611828
* import { Image } from 'astro:assets';
17621829
* import myImage from '../assets/my_image.png';
17631830
* ---
1764-
* <Image src={myImage} alt="A description of my image." layout='fixed' />
1831+
*
1832+
* <Image src={myImage} alt="This will use responsive layout" width={800} height={600} />
1833+
*
1834+
* <Image src={myImage} alt="This will use full-width layout" layout="full-width" />
1835+
*
1836+
* <Image src={myImage} alt="This will disable responsive images" layout="none" />
17651837
* ```
17661838
*
1839+
* #### Responsive image properties
1840+
*
1841+
* These are additional properties available to the `<Image />` and `<Picture />` components when responsive images are enabled:
1842+
*
1843+
* - `layout`: The layout type for the image. Can be `responsive`, `fixed`, `full-width` or `none`. Defaults to value of `image.experimentalLayout`.
1844+
* - `fit`: Defines how the image should be cropped if the aspect ratio is changed. Values match those of CSS `object-fit`. Defaults to `cover`, or the value of `image.experimentalObjectFit` if set.
1845+
* - `position`: Defines the position of the image crop if the aspect ratio is changed. Values match those of CSS `object-position`. Defaults to `center`, or the value of `image.experimentalObjectPosition` if set.
1846+
* - `priority`: If set, eagerly loads the image. Otherwise images will be lazy-loaded. Use this for your largest above-the-fold image. Defaults to `false`.
1847+
*
1848+
* The following `<Image />` component properties should not be used with responsive images as these are automatically generated:
1849+
*
1850+
* - `densities`
1851+
* - `widths`
1852+
* - `sizes`
17671853
*/
17681854

17691855
responsiveImages?: boolean;

0 commit comments

Comments
 (0)