refactor: allow linking the image

This commit is contained in:
Stefan Imhoff
2026-01-24 16:13:10 +01:00
committed by Stefan Imhoff
parent c5c1b15013
commit da1c0dd6c8

View File

@@ -2,6 +2,7 @@
import { Picture } from 'astro:assets'; import { Picture } from 'astro:assets';
import type { ImageMetadata } from 'astro'; import type { ImageMetadata } from 'astro';
import TextLink from './TextLink.astro'; import TextLink from './TextLink.astro';
import Link from './Link.astro';
interface Props { interface Props {
alt?: string; alt?: string;
@@ -9,6 +10,7 @@ interface Props {
class?: string; class?: string;
decoding?: 'async' | 'sync' | 'auto'; decoding?: 'async' | 'sync' | 'auto';
height?: string | number; height?: string | number;
href?: string;
loading?: 'lazy' | 'eager'; loading?: 'lazy' | 'eager';
noSpacing?: boolean; noSpacing?: boolean;
quality?: number; quality?: number;
@@ -25,6 +27,7 @@ const {
class: className, class: className,
decoding = 'async', decoding = 'async',
height, height,
href,
loading = 'lazy', loading = 'lazy',
noSpacing, noSpacing,
quality, quality,
@@ -60,6 +63,10 @@ if (src.startsWith('/images')) {
// Define sizes attribute for responsive images // Define sizes attribute for responsive images
const sizes = '(max-width: 440px) 100vw, (max-width: 768px) 90vw, (max-width: 1024px) 85vw, 1100px'; const sizes = '(max-width: 440px) 100vw, (max-width: 768px) 90vw, (max-width: 1024px) 85vw, 1100px';
// Determine the tag and specific attributes
const ImageWrapper = href ? Link : 'div';
const wrapperProps = href ? { href } : {};
--- ---
<figure <figure
@@ -74,7 +81,10 @@ const sizes = '(max-width: 440px) 100vw, (max-width: 768px) 90vw, (max-width: 10
]} ]}
{...props} {...props}
> >
<div class="figure-content flex flex-wrap gap-6 md:flex-nowrap [&_div]:flex-grow"> <ImageWrapper
class="figure-content flex flex-wrap gap-6 md:flex-nowrap [&_div]:flex-grow"
{...wrapperProps}
>
{ {
localImage ? ( localImage ? (
<Picture <Picture
@@ -100,7 +110,7 @@ const sizes = '(max-width: 440px) 100vw, (max-width: 768px) 90vw, (max-width: 10
/> />
) )
} }
</div> </ImageWrapper>
{ {
(caption || source) && ( (caption || source) && (
<figcaption class="text-center text-2 mbs-2 [text-wrap:balance]"> <figcaption class="text-center text-2 mbs-2 [text-wrap:balance]">