d73c04a9e5
* <Picture /> should pass all unrecognized props down to the <img> element
* chore: add changeset
* Adding test coverage for custom <img> attributes
* chore: adding a README note for passing attributes to the picture's img
* Revert "<Picture /> should pass all unrecognized props down to the <img> element"
This reverts commit ce3e33930f
.
* Picture should pass alt text to the img
41 lines
1.3 KiB
Text
41 lines
1.3 KiB
Text
---
|
|
// @ts-ignore
|
|
import loader from 'virtual:image-loader';
|
|
import { getPicture } from '../src/get-picture.js';
|
|
import type { ImageAttributes, ImageMetadata, OutputFormat, PictureAttributes, TransformOptions } from '../src/types.js';
|
|
|
|
export interface LocalImageProps extends Omit<PictureAttributes, 'src' | 'width' | 'height'>, Omit<TransformOptions, 'src'>, Pick<ImageAttributes, 'loading' | 'decoding'> {
|
|
src: ImageMetadata | Promise<{ default: ImageMetadata }>;
|
|
alt?: string;
|
|
sizes: HTMLImageElement['sizes'];
|
|
widths: number[];
|
|
formats?: OutputFormat[];
|
|
}
|
|
|
|
export interface RemoteImageProps extends Omit<PictureAttributes, 'src' | 'width' | 'height'>, TransformOptions, Pick<ImageAttributes, 'loading' | 'decoding'> {
|
|
src: string;
|
|
alt?: string;
|
|
sizes: HTMLImageElement['sizes'];
|
|
widths: number[];
|
|
aspectRatio: TransformOptions['aspectRatio'];
|
|
formats?: OutputFormat[];
|
|
}
|
|
|
|
export type Props = LocalImageProps | RemoteImageProps;
|
|
|
|
const { src, alt, sizes, widths, aspectRatio, formats = ['avif', 'webp'], loading = 'lazy', decoding = 'async', ...attrs } = Astro.props as Props;
|
|
|
|
const { image, sources } = await getPicture({ loader, src, widths, formats, aspectRatio });
|
|
---
|
|
|
|
<picture {...attrs}>
|
|
{sources.map(attrs => (
|
|
<source {...attrs} {sizes}>))}
|
|
<img {...image} {loading} {decoding} {alt} />
|
|
</picture>
|
|
|
|
<style>
|
|
img {
|
|
content-visibility: auto;
|
|
}
|
|
</style>
|