website/src/components/Image.astro

44 lines
804 B
Plaintext
Raw Normal View History

2024-03-27 01:51:42 +01:00
---
import type { ImageMetadata } from "astro";
2024-04-03 14:27:48 +02:00
import { Picture as AstroImage } from "astro:assets";
2024-03-27 01:51:42 +01:00
interface Props {
src: ImageMetadata;
alt: string;
2024-04-03 14:27:48 +02:00
class?: string;
2024-03-28 18:30:52 +01:00
caption?: string;
2024-03-27 01:51:42 +01:00
maxWidth?: number;
}
2024-03-28 18:30:52 +01:00
const { src: image, alt, maxWidth } = Astro.props;
2024-03-27 01:51:42 +01:00
2024-04-03 14:27:48 +02:00
console.log({ image, alt, maxWidth });
2024-03-27 01:51:42 +01:00
const sizes = [
{
width: 240,
media: "(max-width: 360px)",
},
{
width: 540,
media: "(max-width: 720px)",
},
{
width: 720,
media: "(max-width: 1600px)",
},
{
width: image.width,
},
].filter((size) => !maxWidth || size.width <= maxWidth);
---
<AstroImage
src={image}
alt={alt}
2024-04-03 14:27:48 +02:00
class={Astro.props.class}
2024-03-27 01:51:42 +01:00
widths={sizes.map((size) => size.width)}
sizes={sizes
.map((size) => `${size.media || "100vw"} ${size.width}px`)
.join(", ")}
/>