Image Gallery

Free

Grid, masonry, strip and product-viewer layouts with a lightbox viewer, keyboard navigation and selectable mode.

</>sv-image-gallery galleryimageslightbox

Complexity tier: Interactive The primitive bar, plus: its async states are declared — none is an answer — it has a keyboard model and restores focus, and any destructive action it offers can be undone.

import { SvImageGalleryComponent } from '@styloviz/display/image-gallery';

Demo & usage

Basic usage

Usage example — open the gallery for the full interactive demo.

An interactive demo for this component lives in the gallery.

Open live demo

More examples

Selectable grid with captions

example.htmlAngular
<sv-image-gallery
  [images]="images"
  layout="grid"
  [columns]="4"
  [captions]="true"
  [selectable]="true"
  [(selected)]="selectedImageIds" />

A selection that survives pagination

`selected` is a `model`, so the HOST owns the set — which is what makes it persist across a page turn or a route change, and is the only place it can persist. The component deliberately never prunes ids for images it cannot see: a gallery that "tidied up" the selection to match the images currently bound would silently delete the user's choices on every page turn, which is the persistence itself, destroyed by the tidying. What comes with keeping them is the duty to show them — select twelve on page one, turn to page two, and none of them is ticked while "Delete selected" still acts on all twelve. The strip says so, and "Clear selection" clears the hidden part too.

example.htmlAngular
<sv-image-gallery
  [images]="page()"
  [selectable]="true"
  [(selected)]="chosen" />

// chosen lives in the HOST, so it survives the page change:
readonly chosen = signal<Set<string>>(new Set());

When to use

  • Displaying a collection of images users browse and open full-screen.
  • You want grid/masonry/strip layouts plus a keyboard-friendly lightbox.
  • For a single rotating banner, use the Carousel.

Async states

  • loading
  • empty
  • error
  • retry

Item identity & owned state

Restorable state
  • selected

Bind these to put a saved view back — the open sections, the hidden columns, the selected rows — and read their Change events to store what the user does.

Inputs

NameTypeDefaultDescription
loadingbooleanfalseTrue while the images are being fetched.
errorSvDataError | string | nullnullLoad failure. Use `toSvDataError()` from `@styloviz/core` to normalize an `HttpErrorResponse`.
emptyMessagestring'No images yet'Copy for the no-data state.
filteredbooleanfalseTrue when the emptiness is caused by a filter rather than an empty set.
imagesreadonly GalleryImage[][]Images to display (src, alt, optional caption).
layoutGalleryLayout'grid'Layout: grid, masonry or strip.
ariaLabelstring''What this gallery is, e.g. `'Product photos'` — names the list of images. Two galleries on one product page (the shots and the customer uploads) both announce as an unnamed list of N items without this. Left unset, no name is invented: `<ul role="list">` already gives a listener the count and the boundaries.
columnsGalleryColumns3Number of columns (2–5).
lightboxbooleantrueOpen images in a full-screen lightbox on click.
captionsbooleantrueShow captions under images.
selectablebooleanfalseEnable multi-select mode.
zoombooleantrueProduct-viewer: enable hover-zoom on the main image.
zoomLevelnumber2Product-viewer: magnification factor for the hover-zoom.
viewerSizeGalleryViewerSize'compact'Width of the `viewer` layout. `compact` caps it at 28rem.
viewerAspectGalleryViewerAspect'square'Frame of the `viewer` layout's main image.
selectedSet<string> (two-way)new Set()Which images are selected (two-way). Bind it to preselect a saved set or clear the selection after acting on it; `(selectedChange)` emits on every change.
activeIndexnumber0Optional externally-controlled active image (viewer layout). Clamps to range; resets on image change.

Outputs

EventPayload typeDescription
retryvoidThe host should re-run the failed load.
imageClickGalleryImageEmitted with the clicked image.
selectedChangeSet<string>Emits the new `selected` whenever it changes — the output half of `[(selected)]`.

Wire it to your API

This component is headless about transport: it renders what you give it and reports what the user asks for. Nothing below requires @styloviz/data — drive it from whatever you already use.

Template

Bound to the names this component actually exposes.

usage.htmlAngular
<sv-image-gallery
  [images]="images()"
  [loading]="loading()"
  [error]="error()"
  [filtered]="filtered()"
  (retry)="load()"
/>

Host class

Drive the three async inputs from your own fetch.

host.component.tsTypeScript
import { signal } from '@angular/core';
import { toSvDataError, type SvDataError } from '@styloviz/core';
import type { GalleryImage } from '@styloviz/display/image-gallery';

readonly images = signal<GalleryImage[]>([]);
readonly loading = signal(false);
readonly error = signal<SvDataError | null>(null);
readonly filtered = signal<boolean>(false);

async load(): Promise<void> {
  this.loading.set(true);
  this.error.set(null);
  try {
    const res = await fetch('/api/images');
    if (!res.ok) throw res;
    this.images.set(await res.json());
  } catch (e) {
    this.error.set(toSvDataError(e));
  } finally {
    this.loading.set(false);
  }
}
  • This component does not emit a query, so it renders what you give it. Call `load()` on your own trigger and bind `images`, `loading` and `error`.
  • Set `filtered` when a search or filter is active. It is the difference between "you have no orders" and "no orders match this filter" — telling a customer with 4,000 orders the first one reads as a bug.
  • Normalize failures with `toSvDataError()` from `@styloviz/core`. It derives `retryable` from the status, so a 404 does not get a "Try again" button that can only fail again.

Full walkthrough: Connecting to a backend.

Customization & theming

  • Grid, masonry, strip or product-viewer layouts (2–5 columns).
  • Lightbox with keyboard navigation.
  • Optional selectable mode.
  • Product-viewer layout: a large main image with hover-zoom (zoomLevel) and a thumbnail strip to switch images.

Best practices

  • Do: Give each image a `srcset` (and, if your layout differs from the gallery's, a `sizes`). Without one every thumbnail downloads the full-size `src`; with one the gallery reports a `sizes` derived from its layout and `columns`, so a four-column grid fetches quarter-width candidates, and the lightbox asks for at most its 900px frame.
  • Tip: Volume guide: thumbnails load lazily, so the first paint costs only what is on screen, but every image is still a DOM node and a request once scrolled to. Up to ~60 images on one page is comfortable with `srcset`; past ~100, page the album (or load more on demand) rather than passing the whole set — and without `srcset` budget for the full file size of every image the reader scrolls past.
  • Do: Provide descriptive `alt` text on every image for accessibility and SEO.
  • Do: Serve appropriately sized thumbnails; load full-res only in the lightbox.
  • Tip: Enable `selectable` for bulk operations like delete or add-to-album.
  • Do: Hold `selected` in the host — a signal, a store, a route param. It is the only place a selection can survive a page turn, and the component will not prune it behind your back.
  • Don't: Don't clear the selection yourself on every `images` change. That is the tidying the component avoids on purpose, and it deletes exactly the choices the user made on the previous page.

Accessibility

  • The lightbox's previous/next buttons sit beside the frame on a wide screen and over the picture's edges on a phone — their offset is the free space beside the frame less an 8px margin, so they are never pushed off-screen below ~480px.
  • Focus after a mutation: closing the lightbox returns focus to the control that opened it — or, if the reader paged to another image, to THAT image's thumbnail (the viewer layout also makes it the active image). If neither survives, focus goes into the gallery, never `<body>`.
  • Every layout is a `<ul role="list">`, so a listener is told the images are one set of a known size — "list, 12 items" — which was the only fact the grid conveyed visually and nothing carried. A table of alt texts would be the alt texts again in a worse container, so there is no companion summary here.
  • The `role="list"` is not redundant: Tailwind's preflight strips `list-style`, and that alone is enough for Safari/VoiceOver to stop treating the `<ul>` as a list.
  • Name the gallery with `ariaLabel` when a page has more than one.
  • The lightbox is a modal `dialog` with a focus trap; focus returns to the originating thumbnail on close, and ←/→/Esc navigate.
  • Built with safe DOM APIs (no HTML injection), so image-supplied alt/title/author cannot inject markup.
  • View and select controls are labelled buttons reachable by keyboard in every layout; images require descriptive alt text.
  • A selection that outlives the page it was made on is disclosed in a `role="status"` strip — "12 selected — 9 not on this page" — because the component deliberately never prunes ids for images it cannot see, and a reader who cannot tell would believe the selection is only what is ticked in front of them.

Feedback on Image Gallery

Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.

Loads giscus.app and GitHub — nothing is fetched until you click.