2022-07-08 16:37:55 -05:00
|
|
|
---
|
2022-07-22 18:01:56 -05:00
|
|
|
import { getPicture } from '../dist/index.js';
|
2022-07-27 10:39:05 -05:00
|
|
|
import type { ImgHTMLAttributes, HTMLAttributes } from './index.js';
|
|
|
|
import type { ImageMetadata, OutputFormat, TransformOptions } from '../dist/index.js';
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-07-27 10:39:05 -05:00
|
|
|
interface LocalImageProps extends
|
|
|
|
Omit<HTMLAttributes, 'src' | 'width' | 'height'>,
|
|
|
|
Omit<TransformOptions, 'src'>,
|
|
|
|
Pick<astroHTML.JSX.ImgHTMLAttributes, 'loading' | 'decoding'> {
|
2022-07-08 16:37:55 -05:00
|
|
|
src: ImageMetadata | Promise<{ default: ImageMetadata }>;
|
2022-07-19 14:21:58 -05:00
|
|
|
alt?: string;
|
2022-07-08 16:37:55 -05:00
|
|
|
sizes: HTMLImageElement['sizes'];
|
|
|
|
widths: number[];
|
|
|
|
formats?: OutputFormat[];
|
|
|
|
}
|
|
|
|
|
2022-07-27 10:39:05 -05:00
|
|
|
interface RemoteImageProps extends
|
|
|
|
Omit<HTMLAttributes, 'src' | 'width' | 'height'>,
|
|
|
|
TransformOptions,
|
|
|
|
Pick<ImgHTMLAttributes, 'loading' | 'decoding'> {
|
2022-07-08 16:37:55 -05:00
|
|
|
src: string;
|
2022-07-19 14:21:58 -05:00
|
|
|
alt?: string;
|
2022-07-08 16:37:55 -05:00
|
|
|
sizes: HTMLImageElement['sizes'];
|
|
|
|
widths: number[];
|
|
|
|
aspectRatio: TransformOptions['aspectRatio'];
|
|
|
|
formats?: OutputFormat[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Props = LocalImageProps | RemoteImageProps;
|
|
|
|
|
2022-07-19 14:21:58 -05:00
|
|
|
const { src, alt, sizes, widths, aspectRatio, formats = ['avif', 'webp'], loading = 'lazy', decoding = 'async', ...attrs } = Astro.props as Props;
|
2022-07-08 16:37:55 -05:00
|
|
|
|
2022-07-22 18:01:56 -05:00
|
|
|
const { image, sources } = await getPicture({ src, widths, formats, aspectRatio });
|
2022-07-08 16:37:55 -05:00
|
|
|
---
|
|
|
|
|
|
|
|
<picture {...attrs}>
|
|
|
|
{sources.map(attrs => (
|
|
|
|
<source {...attrs} {sizes}>))}
|
2022-07-19 14:21:58 -05:00
|
|
|
<img {...image} {loading} {decoding} {alt} />
|
2022-07-08 16:37:55 -05:00
|
|
|
</picture>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
img {
|
|
|
|
content-visibility: auto;
|
|
|
|
}
|
|
|
|
</style>
|