Skip to content
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

docs: add docs for responsive images #12429

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions packages/astro/src/assets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,60 @@ type ImageSharedProps<T> = T & {
* ```
*/
quality?: ImageQuality;

layout?: ImageLayout;

fit?: ImageFit;

position?: string;
} & (
| {
/**
* The layout type for responsive images. Overrides any default set in the Astro config.
* Requires the `experimental.responsiveImages` flag to be enabled.
* The layout type for responsive images. Requires the `experimental.responsiveImages` flag to be enabled in the Astro config.
*
* Allowed values are `responsive`, `fixed`, `full-width` or `none`. Defaults to value of `image.experimentalLayout`.
*
* - `responsive` - The image will scale to fit the container, maintaining its aspect ratio, but will not exceed the specified dimensions.
* - `fixed` - The image will maintain its original dimensions.
* - `full-width` - The image will scale to fit the container, maintaining its aspect ratio.
* - `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.
*
* **Example**:
* ```astro
* <Image src={...} layout="responsive" alt="..." />
* ```
*/

layout?: ImageLayout;
fit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down' | (string & {});

/**
* 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.
*
* 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.
*
* **Example**:
* ```astro
* <Image src={...} fit="contain" alt="..." />
* ```
*/

fit?: ImageFit;

/**
* Defines the position of the image when cropping. Requires the `experimental.responsiveImages` flag to be enabled in the Astro config.
*
* 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.
*
* **Example**:
* ```astro
* <Image src={...} position="center top" alt="..." />
* ```
*/

position?: string;
/**
* 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.
* All other images will be lazy-loaded according to when they are in the viewport.
* **Example**:
* ```astro
* <Image src={...} priority alt="..." />
* ```
*/
priority?: boolean;

/**
* 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.
*
Expand Down
27 changes: 19 additions & 8 deletions packages/astro/src/types/public/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1743,27 +1743,38 @@ export interface ViteUserConfig extends OriginalViteUserConfig {
* @version 5.0.0
* @description
*
* 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.
* Enables automatic responsive images in your project.
*
* ```js
* {
* experimental: {
* responsiveImages: {
* layout: 'responsive',
* experimental: {
* responsiveImages: true,
* },
* }
* ```
*
* 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.
* When enabled, you can pass a `layout` props to any `<Image /> or `<Picture />` to enable automatic responsive images. You can also set a default value using `image.experimentalLayout`. 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* When enabled, you can pass a `layout` props to any `<Image /> or `<Picture />` to enable automatic responsive images. You can also set a default value using `image.experimentalLayout`. 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.
* When enabled, you can pass a `layout` prop to any `<Image /> or `<Picture />` to enable automatic responsive images on a per-image basis. Images will use the configured value of `image.experimentalLayout` by default, or the value passed to the component prop directly if provided.
*
* `srcset` and `sizes` attributes will be automatically generated based on the image's dimensions and the `layout` type. Images with `responsive` and `full-width` layouts will have appropriate styles applied to ensure they resize according to their container.

I wasn't sure whether setting image.experimentalLayout makes ALL your <Image /> components responsive by default, or (as my suggestion above is for), you still need to specify <Image layout /> but you just don't have to pass a value and the default set will be used.

So I think something like what I suggested above works for that second case. If the first case is actually true, then I think that could be made explicit more like:

Enabling this experimental flag allows you to configure image.experimentalLayout to set a default layout for all <Image /> components: responsive, fixed, full-width or none. You can override individual images by passing a layout prop with the desired value directly."

The main point of the feedback was that I wasn't entirely sure what the "default" behaviour is, so something that makes that more explicit is probably helpful!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting experimentalLayout does enable it for all Images and Pictures. That's just a default though: you can instead enable it on an image-by-image basis, and if it is set, you can override it on each image too. I'll try to word that more clearly!

*
* ```astro
* ---
* import { Image } from 'astro:assets';
* import { Image, Picture } from 'astro:assets';
* import myImage from '../assets/my_image.png';
* ---
* <Image src={myImage} alt="A description of my image." layout='fixed' />
* <Image src={myImage} alt="A description of my image." layout='responsive' width={800} height={600} />
* <Picture src={myImage} alt="A description of my image." layout='full-width' formats={['avif', 'webp', 'jpeg']} />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be helpful to also show the rendered HTML below like we do for the basic <Image /> component. What do you think?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't is because it's very verbose, with a large srcset. I could do an abbreviated one though.

* ```
*
*
* ### Responsive image properties
*
* These are additional Image properties available when responsive images are enabled:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* These are additional Image properties available when responsive images are enabled:
* These are additional properties available to the `<Image />` component when responsive images are enabled:

Just confirming, not the <Picture /> component too? Only the <Image /> component?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no, it's for both

*
* - `layout`: The layout type for the image. Can be `responsive`, `fixed`, `full-width` or `none`. Defaults to value of `image.experimentalLayout`.
* - `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.
* - `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.
* - `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`.
*
* The `densities` prop is not supported when using responsive images. It is not recommended to use the `widths` or `sizes` props as these are automatically generated based on the image's dimensions and the layout type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* The `densities` prop is not supported when using responsive images. It is not recommended to use the `widths` or `sizes` props as these are automatically generated based on the image's dimensions and the layout type.
* The following `<Image />` component properties should not be used with responsive images as these are automatically generated and cannot be independently configured:
*
* - `densities`
* - `widths`
* - `sizes`

Maybe something like this? Make it more accurate (but doesn't have to be ENTIRELY and comprehensively accurate; just needs to say these are the ones not to use). I think framing them as the list not to use vs putting this info in paragraph text is more scannable.

*
*/

responsiveImages?: boolean;
Expand Down
Loading