Veil UI
Components

Image Gallery

A thumbnail grid that opens images in a lightbox.

Installation

$ npx veil-ui@latest add image-gallery

Usage

import { ImageGallery } from "@/components/ui/image-gallery";

<ImageGallery images={[{ src: "/one.jpg", alt: "First" }, { src: "/two.jpg", alt: "Second" }]} />;

Examples

Basic

Two columns

API reference

The package exports image-gallery 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-gallery --framework vue or switch the framework in components.json.

<script setup lang="ts">
import { ref } from "vue";

import { Lightbox } from "@/components/ui/lightbox";
import { cn } from "@/lib/utils";

interface GalleryImage {
  src: string;
  alt: string;
}

const props = withDefaults(
  defineProps<{
    images: GalleryImage[];
    columns?: 2 | 3 | 4;
    class?: string;
    label?: string;
  }>(),
  { columns: 3, label: "Image gallery" },
);

const columnsClass = {
  2: "sm:grid-cols-2",
  3: "sm:grid-cols-3",
  4: "sm:grid-cols-4",
};

const open = ref(false);
const index = ref(0);

function select(imageIndex: number) {
  index.value = imageIndex;
  open.value = true;
}
</script>

<template>
  <div data-slot="image-gallery" :class="cn('w-full', props.class)">
    <ul
      :aria-label="label"
      :class="cn('grid grid-cols-2 gap-2', columnsClass[columns])"
    >
      <li v-for="(image, imageIndex) in images" :key="image.src">
        <button
          type="button"
          :aria-label="`Open ${image.alt}`"
          class="group block w-full overflow-hidden rounded-lg outline-none focus-visible:ring-2 focus-visible:ring-ring"
          @click="select(imageIndex)"
        >
          <img
            :src="image.src"
            :alt="image.alt"
            loading="lazy"
            class="aspect-square w-full object-cover transition-transform duration-300 group-hover:scale-105"
          />
        </button>
      </li>
    </ul>
    <Lightbox
      :images="images"
      :index="index"
      :open="open"
      :label="label"
      @update:open="open = $event"
      @update:index="index = $event"
    />
  </div>
</template>

On this page