skip to content
mpkcn

~/components/image-placeholder

Image Placeholder

A framed image slot that shows a striped placeholder with a mono label until the image exists or when it fails to load.

no src
Mountains at dusk, flat illustration
src loaded
Broken image
broken src

Installation

terminal
npx mpkcn add image-placeholder
deps:none beyond react· tested with react@19.3.0

Usage

tsx
import { ImagePlaceholder } from "@/components/mpkcn/image-placeholder";

export function ProjectCard() {
  return <ImagePlaceholder src="/images/dashboard.png" alt="Dashboard, dark theme" className="w-full" />;
}

While src is empty, or if the file fails to load, the box shows faint diagonal stripes and a mono label in the corner. Once the image loads it fills the frame (object-fit: cover). So you can lay out a page before the screenshots exist, then drop the files in.

Examples

Wide, with a custom label

Size it yourself

aspectRatio={null} drops the built-in ratio so your classes decide the size.

tsx
<ImagePlaceholder alt="Avatar" aspectRatio={null} className="size-24" label="" />

With next/image

The component uses a plain <img> so it works in any React app. In Next.js you can swap that tag for next/image with fill in your copy of the file.

Props

PropTypeDefaultDescription
srcstring—Image URL. When it is missing or fails to load, the striped placeholder shows instead.
altstring—Alt text for the image. Also the accessible name of the placeholder.
labelstring"image coming soon"Text shown in the placeholder's bottom-left corner.
aspectRatiostring | null"4 / 5"CSS aspect-ratio of the box, e.g. "16 / 10". null lets your className set the size.
classNamestring—Extra classes for the box (width, height, margins).

Accessibility

  • With an image, the <img> carries your alt text as usual.
  • Without one, the box is role="img" named by the same alt, so screen readers still know what belongs there. The stripes and the corner label are aria-hidden.
  • A load error that happens before hydration is caught too, so a broken URL never shows the browser's broken-image icon for long.