Veil UI
Components

Image with Fallback

An image that swaps to a placeholder when loading fails.

Installation

$ npx veil-ui@latest add image-with-fallback

Usage

import { ImageWithFallback } from "@/components/ui/image-with-fallback";

<ImageWithFallback src="/photo.jpg" alt="Team offsite" />;

Examples

Basic

A mountain valley

Custom

Team offsite

API reference

The package exports image-with-fallback along with its composable parts. It forwards standard attributes to its root element; see the examples above for the component-specific props.

Accessibility

  • Media players use native <video> and <audio> controls with accessible labels.
  • Images require meaningful alt text; clickable thumbnails are buttons named Play <title>.
  • Decorative play overlays, durations and icons are hidden from assistive technology.

Vue and plain HTML

This component also ships for Vue (Reka UI) and plain HTML. Install it with npx veil-ui add image-with-fallback --framework vue or switch the framework in components.json.

<script setup lang="ts">
import { ImageOff } from "lucide-vue-next";
import { ref } from "vue";

import { cn } from "@/lib/utils";

defineOptions({ inheritAttrs: false });

const props = defineProps<{
  src?: string;
  alt?: string;
  wrapperClassName?: string;
  class?: string;
}>();

const failed = ref(false);
</script>

<template>
  <div
    v-if="failed"
    data-slot="image-fallback"
    :class="
      cn(
        'flex min-h-24 flex-col items-center justify-center gap-2 rounded-lg border border-dashed bg-muted/40 p-4 text-center text-sm text-muted-foreground',
        props.wrapperClassName,
      )
    "
  >
    <slot name="fallback">
      <ImageOff class="size-5" aria-hidden="true" />
      <span>{{ props.alt || "Image unavailable" }}</span>
    </slot>
  </div>
  <img
    v-else
    data-slot="image"
    :src="props.src"
    :alt="props.alt"
    :class="cn('max-w-full rounded-lg object-cover', props.class)"
    v-bind="$attrs"
    @error="failed = true"
  />
</template>

On this page