import React from 'react'; import type { CslpTag } from '../../types'; export interface ImageBlockProps { className?: string; /** Asset URL, or a Contentstack asset object. */ image?: string | { url?: string; title?: string } | null; /** Describes the image for screen readers. Leave blank only for a purely decorative image. */ alt_text?: string; /** Shown under the image. */ caption?: string; /** How wide the image is within the page container; Full fills it. */ size?: 'Small' | 'Medium' | 'Large' | 'Full' | string; /** Contentstack Live Preview edit tags, one per editable field. See `CslpTag`. */ $?: { image?: CslpTag; alt_text?: CslpTag; caption?: CslpTag; }; } const SIZE_MAX: Record = { Small: '30rem', Medium: '48rem', Large: '64rem', Full: undefined }; /** File fields arrive as a URL string from the catalog, as an asset object from the Delivery API. */ function resolveImage(image: ImageBlockProps['image']): string { if (!image) return ''; if (typeof image === 'string') return image.trim(); return typeof image.url === 'string' ? image.url.trim() : ''; } /** * A built-in page-builder block: one image, uncropped, with optional caption. * Not listed in the component catalog; added from the builder's Basic blocks. */ export function ImageBlock({ className = '', image, alt_text = '', caption = '', size = 'Large', $ }: ImageBlockProps) { const src = resolveImage(image); if (!src) return null; // Full fills the section edge to edge — no side padding, no rounded corners. const full = size === 'Full'; const maxWidth = full ? undefined : (SIZE_MAX[size] ?? SIZE_MAX.Large); return (
{/* eslint-disable-next-line @next/next/no-img-element */} {alt_text} {caption.trim() && (
{caption}
)}
); } export default ImageBlock;