~/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.
"use client";
import { useState } from "react";
export type ImagePlaceholderProps = {
/** Image URL. When it is missing or fails to load, the striped placeholder shows instead. */
src?: string;
/** Alt text for the image. Also the accessible name of the placeholder. */
alt: string;
/** Text shown in the placeholder's bottom-left corner. @default "image coming soon" */
label?: string;
/** CSS aspect-ratio of the box, e.g. "16 / 10". null lets your className set the size. @default "4 / 5" */
aspectRatio?: string | null;
/** Extra classes for the box (width, height, margins). */
className?: string;
};
/**
* A framed image slot. Shows the image (cover-fit) when `src` loads; otherwise a
* diagonal-striped panel with a mono label, so layouts look intentional before
* the real pictures exist. A broken URL falls back to the placeholder.
*/
export function ImagePlaceholder({
src,
alt,
label = "image coming soon",
aspectRatio = "4 / 5",
className,
}: ImagePlaceholderProps) {
const [failedSrc, setFailedSrc] = useState<string | null>(null);
const showImage = Boolean(src) && failedSrc !== src;
// Catches a load error that happened before hydration (onError would not fire for it).
const checkImg = (img: HTMLImageElement | null) => {
if (img && img.complete && img.naturalWidth === 0 && src) setFailedSrc(src);
};
return (
<div
className={[
"relative flex items-end overflow-hidden border border-mpk-line bg-linear-155 from-mpk-bg-soft to-mpk-bg to-65%",
className,
]
.filter(Boolean)
.join(" ")}
style={aspectRatio ? { aspectRatio } : undefined}
role={showImage ? undefined : "img"}
aria-label={showImage ? undefined : alt}
>
{showImage ? (
// A plain <img> keeps the component framework-agnostic; swap in next/image if you like.
// eslint-disable-next-line @next/next/no-img-element
<img
ref={checkImg}
src={src}
alt={alt}
onError={() => setFailedSrc(src ?? null)}
className="absolute inset-0 block size-full object-cover"
/>
) : (
<>
<span
aria-hidden="true"
className="absolute inset-0 bg-[repeating-linear-gradient(115deg,transparent_0_18px,color-mix(in_srgb,var(--mpk-ink)_4%,transparent)_18px_19px)]"
/>
<span aria-hidden="true" className="relative px-3.5 py-3 font-mpk-mono text-xs text-mpk-dim2">
{label}
</span>
</>
)}
</div>
);
}Installation
npx mpkcn add image-placeholder01Add mpkcn-theme.css if you haven't yet (see Installation)
02Copy the source into your components folder
"use client";
import { useState } from "react";
export type ImagePlaceholderProps = {
/** Image URL. When it is missing or fails to load, the striped placeholder shows instead. */
src?: string;
/** Alt text for the image. Also the accessible name of the placeholder. */
alt: string;
/** Text shown in the placeholder's bottom-left corner. @default "image coming soon" */
label?: string;
/** CSS aspect-ratio of the box, e.g. "16 / 10". null lets your className set the size. @default "4 / 5" */
aspectRatio?: string | null;
/** Extra classes for the box (width, height, margins). */
className?: string;
};
/**
* A framed image slot. Shows the image (cover-fit) when `src` loads; otherwise a
* diagonal-striped panel with a mono label, so layouts look intentional before
* the real pictures exist. A broken URL falls back to the placeholder.
*/
export function ImagePlaceholder({
src,
alt,
label = "image coming soon",
aspectRatio = "4 / 5",
className,
}: ImagePlaceholderProps) {
const [failedSrc, setFailedSrc] = useState<string | null>(null);
const showImage = Boolean(src) && failedSrc !== src;
// Catches a load error that happened before hydration (onError would not fire for it).
const checkImg = (img: HTMLImageElement | null) => {
if (img && img.complete && img.naturalWidth === 0 && src) setFailedSrc(src);
};
return (
<div
className={[
"relative flex items-end overflow-hidden border border-mpk-line bg-linear-155 from-mpk-bg-soft to-mpk-bg to-65%",
className,
]
.filter(Boolean)
.join(" ")}
style={aspectRatio ? { aspectRatio } : undefined}
role={showImage ? undefined : "img"}
aria-label={showImage ? undefined : alt}
>
{showImage ? (
// A plain <img> keeps the component framework-agnostic; swap in next/image if you like.
// eslint-disable-next-line @next/next/no-img-element
<img
ref={checkImg}
src={src}
alt={alt}
onError={() => setFailedSrc(src ?? null)}
className="absolute inset-0 block size-full object-cover"
/>
) : (
<>
<span
aria-hidden="true"
className="absolute inset-0 bg-[repeating-linear-gradient(115deg,transparent_0_18px,color-mix(in_srgb,var(--mpk-ink)_4%,transparent)_18px_19px)]"
/>
<span aria-hidden="true" className="relative px-3.5 py-3 font-mpk-mono text-xs text-mpk-dim2">
{label}
</span>
</>
)}
</div>
);
}03Update the import paths to match your project
Usage
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
<ImagePlaceholder alt="Demo video" aspectRatio="16 / 10" label="demo.mp4 — recording soon" />Size it yourself
aspectRatio={null} drops the built-in ratio so your classes decide the size.
<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
| Prop | Type | Default | Description |
|---|---|---|---|
| src | string | — | Image URL. When it is missing or fails to load, the striped placeholder shows instead. |
| alt | string | — | Alt text for the image. Also the accessible name of the placeholder. |
| label | string | "image coming soon" | Text shown in the placeholder's bottom-left corner. |
| aspectRatio | string | null | "4 / 5" | CSS aspect-ratio of the box, e.g. "16 / 10". null lets your className set the size. |
| className | string | — | Extra classes for the box (width, height, margins). |
Accessibility
- With an image, the
<img>carries youralttext as usual. - Without one, the box is
role="img"named by the samealt, so screen readers still know what belongs there. The stripes and the corner label arearia-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.