A React component for rendering raster images (JPG/PNG/WebP/AVIF, etc.) with optional responsive sources and graceful fallback when no image is available. SVG sources are detected automatically and rendered as-is. The component integrates with the UXF image resizer utilities to generate optimized images for different breakpoints.
Add the package stylesheet to your global CSS if you haven't already:
@import url("@uxf/ui/raster-image/raster-image.css");
<img/>-like component that works with UXF's resizer service.<picture/> + <source/> with different widths/heights per breakpoint.import { RasterImage } from "@uxf/ui/raster-image";
export function CardThumb() {
return (
<RasterImage
alt="A beautiful landscape"
src="images/landscape.jpg"
width={400}
height={300}
loading="lazy"
/>
);
}
<img/> with the SVG URL (no resizer involved).Use the mode prop to control fit behavior via CSS classes:
Example:
<RasterImage alt="Product" src="p/1.jpg" width={320} height={320} mode="cover" />
To provide multiple sources for different viewports, pass widths and/or heights arrays and optionally breakpoints. (Default breakpoints are passed from UiContext.) The component will render a <picture/> with <source/> tags via ResponsiveImgSources.
import { em } from "./em";
<RasterImage
alt="Hero"
src="images/hero.jpg"
// Desired element box in CSS might be responsive; request multiple candidate widths
widths={[360, 640, 960, 1280]}
// Optional heights if you need height-based variants
// heights={[180, 320, 480, 640]}
// Optional map of named breakpoints to CSS media queries (passed through to `<source media="..."/>`)
breakpoints={{
sm: em(576),
md: em(768),
lg: em(1024)
}}
// You can still provide width/height to define the fallback `<img/>` dimensions
width={960}
height={540}
/>
Notes:
<picture/> with responsive sources plus a fallback <img/>.<img/> is rendered.If src is null/undefined or cannot be resolved, the component renders an EmptyImage placeholder. You can customize its content:
<RasterImage
alt="Missing"
src={null}
width={200}
height={150}
noImageContent={<span>No image</span>}
/>
export type RasterImageProps = {
alt: string; // Required alt text for accessibility
breakpoints?: Record<string, string>; // Named breakpoints -> media queries used by `<source/>`
className?: string; // Wrapper `<picture/>` class
heights?: number[]; // Candidate heights for responsive sources
imgClassName?: string; // `<img/>` element class
loading?: ImgHTMLAttributes<HTMLImageElement>["loading"]; // "lazy" | "eager"
mode?: "contain" | "cover" | "responsive"; // Adds CSS helper classes
noImageContent?: React.ReactNode; // Custom placeholder content when no image
options?: ImageSourcesOptions; // Resizer/source generation options (passed through)
quality?: Quality; // Preferred quality; defaults to original if not specified
role?: ImgHTMLAttributes<HTMLImageElement>["role"]; // Optional ARIA role override
src: ImageSource | null | undefined; // Image source compatible with @uxf/core/utils/resizer
widths?: number[]; // Candidate widths for responsive sources
} & (
| { width: number; height: number }
| { width?: never; height: number }
| { width: number; height?: never }
);
<img/> receives width/height computed via getImgElementWidth/Height based on your provided width/height and the intrinsic ratio when available.Wrapper and image classes you can target:
<picture/>: "uxf-raster-image"
<img/>: "uxf-raster-image__img"
You can also pass className and imgClassName to append your own classes.
If getSvgImgUrl(src) returns a URL, the component renders a plain <img src="...svg"/> inside <picture/>, bypassing the resizer.
<img/> source when possible.<source/> tags are produced by ResponsiveImgSources using widths/heights and breakpoints.