You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/astro/src/assets/types.ts
+44-10Lines changed: 44 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -157,26 +157,60 @@ type ImageSharedProps<T> = T & {
157
157
* ```
158
158
*/
159
159
quality?: ImageQuality;
160
-
161
-
layout?: ImageLayout;
162
-
163
-
fit?: ImageFit;
164
-
165
-
position?: string;
166
160
}&(
167
161
|{
168
162
/**
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`.
171
166
*
172
167
* - `responsive` - The image will scale to fit the container, maintaining its aspect ratio, but will not exceed the specified dimensions.
173
168
* - `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.
* 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.
* 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
+
*/
179
212
priority?: boolean;
213
+
180
214
/**
181
215
* 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.
* 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.
1747
1747
*
1748
-
* ```js
1748
+
* ```js title=astro.config.mjs
1749
1749
* {
1750
-
* experimental: {
1751
-
* responsiveImages: {
1752
-
* layout: 'responsive',
1750
+
* experimental: {
1751
+
* responsiveImages: true,
1753
1752
* },
1754
1753
* }
1755
1754
* ```
1756
1755
*
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:
* 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
+
* ```
1758
1825
*
1759
-
* ```astro
1826
+
* ```astro title=MyComponent.astro
1760
1827
* ---
1761
1828
* import { Image } from 'astro:assets';
1762
1829
* import myImage from '../assets/my_image.png';
1763
1830
* ---
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" />
1765
1837
* ```
1766
1838
*
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:
0 commit comments