summaryrefslogtreecommitdiff
path: root/packages/integrations/image/components/Picture.astro
blob: e86ccc65d4b46a60867862c32a546b1257c68428 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
import { getPicture } from '../dist/index.js';
import { warnForMissingAlt } from './index.js';
import type { ImgHTMLAttributes, HTMLAttributes } from './index.js';
import type { ImageMetadata, OutputFormat, TransformOptions } from '../dist/index.js';

interface LocalImageProps
	extends Omit<HTMLAttributes, 'src' | 'width' | 'height'>,
		Omit<TransformOptions, 'src'>,
		Pick<astroHTML.JSX.ImgHTMLAttributes, 'loading' | 'decoding'> {
	src: ImageMetadata | Promise<{ default: ImageMetadata }>;
	/** Defines an alternative text description of the image. Set to an empty string (alt="") if the image is not a key part of the content (it's decoration or a tracking pixel). */
	alt: string;
	sizes: HTMLImageElement['sizes'];
	widths: number[];
	formats?: OutputFormat[];
}

interface RemoteImageProps
	extends Omit<HTMLAttributes, 'src' | 'width' | 'height'>,
		TransformOptions,
		Pick<ImgHTMLAttributes, 'loading' | 'decoding'> {
	src: string;
	/** Defines an alternative text description of the image. Set to an empty string (alt="") if the image is not a key part of the content (it's decoration or a tracking pixel). */
	alt: string;
	sizes: HTMLImageElement['sizes'];
	widths: number[];
	aspectRatio: TransformOptions['aspectRatio'];
	formats?: OutputFormat[];
	background: TransformOptions['background'];
}

export type Props = LocalImageProps | RemoteImageProps;

const {
	src,
	alt,
	sizes,
	widths,
	aspectRatio,
	fit,
	background,
	position,
	formats = ['avif', 'webp'],
	loading = 'lazy',
	decoding = 'async',
	...attrs
} = Astro.props as Props;

if (alt === undefined || alt === null) {
	warnForMissingAlt();
}

const { image, sources } = await getPicture({
	src,
	widths,
	formats,
	aspectRatio,
	fit,
	background,
	position,
});

delete image.width;
delete image.height;
---

<picture {...attrs}>
	{sources.map((attrs) => <source {...attrs} {sizes} />)}
	<img {...image} {loading} {decoding} {alt} />
</picture>

<style>
	img {
		content-visibility: auto;
	}
</style>