Skip to Content
Puzzle
UI ComponentsData display

Image

Responsive images with loading states, fallbacks, and framework image support.

The Image component provides a consistent way to display images with responsive sizing, loading placeholders, and fallback content. It renders a native HTML <img> by default and can also use an image component configured for your framework.

Import

import { Image } from "@puzzle-ui/ui";

Basic usage

Provide a source and alternative text. Set the intrinsic dimensions to reserve space while the image loads.

390 × 450

The width and height props describe the image's intrinsic dimensions in pixels. They can be inferred when src is a static image import with src, width, and height fields.

Fitting and positioning

Use fit to control how the image fills its box, and position to align the image content within that box. These correspond to CSS object-fit and object-position; the defaults are "cover" and "center".

390 × 360

The radius prop sets the border radius on the image container. It accepts the presets "none", "sm", "md", "lg", "xl", and "full", as well as a CSS border-radius value.

Aspect ratio and fill

Set aspectRatio to make the image fill a full-width container with a chosen ratio.

<Image
  src="/images/landscape.webp"
  alt="Mountain landscape"
  aspectRatio="16 / 9"
/>

Use fill to position the image across its parent container. The parent must have a defined size; set aspectRatio on the Image or give the parent an explicit height.

<div style={{ width: "100%", height: 240 }}>
  <Image
    src="/images/landscape.webp"
    alt="Mountain landscape"
    fill
    fit="cover"
  />
</div>

Loading placeholders

Use loadingPlaceholder to display custom content over the image while it loads.

<Image
  src="/images/landscape.webp"
  alt="Mountain landscape"
  loadingPlaceholder={<span>Loading image...</span>}
/>

Alternatively, set placeholder="blur" and provide a small preview through blurDataURL. A static image import can provide this value automatically.

<Image
  src="/images/landscape.webp"
  alt="Mountain landscape"
  placeholder="blur"
  blurDataURL="data:image/jpeg;base64,..."
/>

For native <img> elements, the component displays the blur preview behind the image while it loads. When a framework image component is configured, the placeholder and blurDataURL props are forwarded to it. A custom loadingPlaceholder, when provided, takes precedence over the native blur preview.

Fallbacks

Use fallbackSrc to try another image if the primary source fails. Use fallback to render custom content when no usable source is available or all image sources fail.

390 × 200

If neither a usable src nor a fallback is provided, the component renders nothing.

Responsive sources

Pass sources to render <source> elements inside a native <picture>. This supports art direction and selecting different formats or files for different media conditions.

<Image
  src="/images/landscape.webp"
  alt="Mountain landscape"
  sources={[
    {
      srcSet: "/images/landscape-mobile.webp",
      media: "(max-width: 640px)",
    },
    {
      srcSet: "/images/landscape.avif",
      type: "image/avif",
    },
  ]}
/>

The standard image props srcSet and sizes are also supported.

Framework image components

By default, Image renders a native <img>. You can provide a framework-specific image component with component, or configure one through Theme's components.image. The component prop takes precedence over the theme configuration.

<Image
  component={FrameworkImage}
  src="/images/landscape.webp"
  alt="Mountain landscape"
  quality={85}
  preload
/>

Framework-specific options such as quality, unoptimized, and componentProps are forwarded to a custom image component. preload uses React DOM image preloading for a native <img> and is forwarded to a configured image component.

API

Props

PropTypeDefaultDescription
srcImageSource | null—Image URL or static image data. Can be omitted when fallbackSrc or fallback is provided.
altstringRequiredAlternative text for the image. Use an empty string for decorative images.
widthnumber—Intrinsic image width in pixels; inferred from static image data when available.
heightnumber—Intrinsic image height in pixels; inferred from static image data when available.
fitImageFit"cover"How the image is sized within its box.
positionCSSProperties["objectPosition"]"center"Image alignment within its box.
aspectRatioCSSProperties["aspectRatio"]—Aspect ratio for a full-width image container.
fillbooleanfalsePositions the image to fill its container. The container needs a defined size.
radiusImageRadius—Border radius of the image container.
fallbackSrcImageSource | null—Alternate image source used if the primary source fails.
fallbackReactNode—Content shown if there is no usable image source or the image fails.
loadingPlaceholderReactNode—Custom content shown over the image while it loads.
placeholderImagePlaceholder"empty"Loading placeholder strategy: "empty" or "blur".
blurDataURLstring—Small preview image used by the blur placeholder.
sourcesImageSourceSet[]—Responsive sources rendered inside a <picture> element.
componentElementType—Framework-specific image component for this instance.
componentPropsRecord<string, unknown>—Additional props forwarded to the configured image component.
qualitynumber—Image optimization quality forwarded to a custom image component.
preloadbooleanfalsePreloads a native image or is forwarded to a custom image component.
unoptimizedboolean—Disables optimization in a compatible custom image component.
classNamestring—Additional class name applied to the image element.
styleCSSProperties—Styles applied to the image element.
containerClassNamestring—Additional class name applied to the outer container.
containerStyleCSSProperties—Styles applied to the outer container.

Native image props

Image supports the standard React <img> props, including loading, fetchPriority, decoding, crossOrigin, referrerPolicy, onLoad, and onError, as well as aria-* and data-* attributes. Framework-specific props should be passed through componentProps.

TypeScript types

The component exports ImageProps, ImageSource, ImageSourceData, ImageSourceSet, ImageFit, ImageRadius, and ImagePlaceholder.

ImageSource accepts either a URL string or static image data:

type ImageSource = string | ImageSourceData;

type ImageSourceData = {
  src: string;
  width: number;
  height: number;
  blurDataURL?: string;
  blurWidth?: number;
  blurHeight?: number;
};

Accessibility

  • Provide descriptive alt text for images that convey information.
  • Use alt="" when an image is decorative so assistive technology can ignore it.
  • If fallback communicates image content, the component exposes it as an image with the provided alt text.
  • Native image attributes and event handlers are forwarded to the rendered image element.
;