Skip to main content

Image

Renders a picture at a fixed shape with a built-in fallback when loading fails.

import { Image } from "@/fragments/ui";Source

Preview

<Image
  src="https://images.unsplash.com/photo-1555066931-4365d14bab8c?w=400&h=300&fit=crop"
  alt="Code on a screen"
  width={300}
/>

Installation

npx @usefragments/cli add image

Writes src/fragments/ui/components/Image plus what it imports, the globals stylesheet, and .fragments/registry-lock.json, then prints the packages to add. No account, no key.

Your own registry: publish from Fragments Cloud and add --registry org/name. Connect a workspace.

Examples

Aspect Ratios

Lock the frame so a grid stays even whatever the file is.

<div style={{ display: 'flex', gap: '16px', flexWrap: 'wrap' }}>
  <Image
    src="https://images.unsplash.com/photo-1555066931-4365d14bab8c?w=200&h=200&fit=crop"
    alt="Square image"
    aspectRatio="1:1"
    width={100}

Rounded Corners

Rounding softens the frame; match it to the surface it sits on.

<div style={{ display: 'flex', gap: '16px', alignItems: 'center' }}>
  <Image
    src="https://images.unsplash.com/photo-1555066931-4365d14bab8c?w=100&h=100&fit=crop"
    alt="No rounding"
    rounded="none"
    width={80}

With Fallback

A dead URL shows this instead of a broken-image glyph.

<Image
  src="https://invalid-url.example/image.jpg"
  alt="Image that will fail"
  width={200}
  height={150}
  rounded="md"

API

Component props
Prop
Type
Default
Description
srcRequiredstringNot setImage source URL
altRequiredstringNot setAlt text for accessibility (required)
aspectRatioenum1:14:316:921:9autoautoAspect ratio of the image container
objectFitenumcovercontainfillnonecoverHow the image fits within its container
widthunionNot setWidth of the image container
heightunionNot setHeight of the image container
roundedenumnonesmmdlgfullnoneBorder radius
fallbacknodeNot setContent to show while loading or on error
classNamestringNot setAdditional class name
styleobjectNot setInline styles
imgPropsobjectNot setAdditional props for the underlying img element (except src/alt/size/style/events)
onImageLoadfunctionNot setCalled when the underlying img element loads
onImageErrorfunctionNot setCalled when the underlying img element fails to load

Accessibility

  • alt is required — describe what the picture shows
  • Decoration takes alt=""
  • Words baked into an image must be repeated in alt

Guidance

Use when

  • Product shots, thumbnails, and hero art
  • Uploads you do not control and cannot trust to load

Avoid when

  • A person (use Avatar)
  • A symbol (use Icon)
  • Pure decoration behind content (use a CSS background)
  • CardchildCommon pattern to use Image at top of product cards
  • AvataralternativeUse Avatar for user profile pictures

Next steps