@molecule/app-image-gallery-angular

Feature · image-gallery · App (browser) · v1.1.3 · Apache-2.0

Main image + thumbnail grid gallery with selection + optional upload

npm install @molecule/app-image-gallery-angular

npm · Source on GitHub

How it works

@molecule/app-image-gallery-angular is a ready-made image-gallery feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { Component } from '@angular/core'
import { ImageGallery } from '@molecule/app-image-gallery-angular'

@Component({
  selector: 'app-gallery',
  standalone: true,
  imports: [ImageGallery],
  template: `<mol-image-gallery [images]="images" [maxThumbnails]="4" />`,
})
export class Gallery {
  readonly images = [
    'https://example.com/photo-1.jpg',
    'https://example.com/photo-2.jpg',
    'https://example.com/photo-3.jpg',
  ]
}

Works with: @molecule/app-angular, @molecule/app-ui, @molecule/app-ui-angular

Reference

Angular image gallery — the counterpart of @molecule/app-image-gallery-react: the same props and the same DOM for the same input.

Exports <ImageGallery> — main image + thumbnail grid with controlled-optional selection and "+N" overflow summarisation.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { Component } from '@angular/core'
import { ImageGallery } from '@molecule/app-image-gallery-angular'

@Component({
  selector: 'app-gallery',
  standalone: true,
  imports: [ImageGallery],
  template: `<mol-image-gallery [images]="images" [maxThumbnails]="4" />`,
})
export class Gallery {
  readonly images = [
    'https://example.com/photo-1.jpg',
    'https://example.com/photo-2.jpg',
    'https://example.com/photo-3.jpg',
  ]
}

Type

feature

Installation

npm install @molecule/app-image-gallery-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

ImageGalleryProps

Props accepted by the ImageGallery component.

interface ImageGalleryProps {
  /** Image URLs in display order. */
  images: string[]
  /** Controlled selected index — caller owns state. */
  selectedIndex?: number
  /**
   * Called when a thumbnail is clicked. In Angular this is the component's
   * `select` output (`(select)="onSelect($event)"`); an input cannot carry a
   * callback.
   */
  onSelect?: (index: number) => void
  /** Max thumbnails shown. Extra are summarised as "+N". */
  maxThumbnails?: number
  /**
   * Alt-text for screen readers. Applied per-image; falls back through i18n to
   * `'Image {{number}}'` (`imageGallery.alt.fallback`).
   */
  alts?: string[]
  /** Extra classes on the outer wrapper. */
  className?: string
}

Classes

ImageGallery

ImageGallery component.

Main image + thumbnail grid gallery. Controlled-optional: when selectedIndex is omitted the component tracks its own selection. Used for product images, property listings, portfolio galleries.

React's root is the outer <div> when images are present and nothing when they are not, so the rendered root stands in front of the host's place and the host itself leaves the page (placeContentBefore + setElementPresent, React's fragment root).

@Component({
  selector: 'mol-image-gallery',
  standalone: true,
  hostDirectives: [MolPlacement],
  inputs: ['images', 'selectedIndex', 'maxThumbnails', 'alts', 'className'],
  outputs: ['select'],
  template: `@if (images.length > 0) {
    <div [class]="classes()">
      <div style="aspect-ratio: 4 / 3">
        @if (mainSrc(); as src) {
          <img
            [attr.src]="src"
            [attr.alt]="mainAlt()"
            style="width: 100%; height: 100%; object-fit: cover"
          />
        } @else {
          <img [attr.alt]="mainAlt()" style="width: 100%; height: 100%; object-fit: cover" />
        }
      </div>
      @if (images.length > 1) {
        <div [class]="gridClasses()">
          @for (src of visible(); track $index; let i = $index) {
            <button
              type="button"
              (click)="selectIndex(i)"
              [attr.aria-current]="i === idx() ? 'true' : null"
              [attr.aria-label]="thumbAlt(i)"
              style="aspect-ratio: 1 / 1"
            >
              <img [attr.src]="src" alt="" style="width: 100%; height: 100%; object-fit: cover" />
            </button>
          }
          @if (overflow() > 0) {
            <div [class]="overflowClasses()" style="aspect-ratio: 1 / 1">{{ overflowText() }}</div>
          }
        </div>
      }
    </div>
  }`,
})
export class ImageGallery {
  /** Image URLs in display order. */
  images: string[] = []
  /** Controlled selected index — caller owns state. */
  selectedIndex?: number
  /** Max thumbnails shown (defaults to `4` at read time; extra are "+N"). */
  maxThumbnails?: number
  /**
   * Alt-text for screen readers, per image (falls back through i18n to
   * `'Image {{number}}'`, `imageGallery.alt.fallback`).
   */
  alts?: string[]
  /** Extra classes on the outer wrapper. */
  className?: string

  /** Fires with the clicked thumbnail's index (React's `onSelect`). */
  @Output() readonly select = new EventEmitter<number>()

  /** The component's own selection while it is uncontrolled. */
  private internalIndex = 0

  /** The i18n binding the alt fallback and overflow chip read. */
  private readonly i18n = useTranslation()

  /** The component's host element (React renders no element of its own). */
  private readonly host: ElementRef<HTMLElement> = inject(ElementRef)

  constructor() {
    // React's root is the outer `<div>` itself (nothing when empty): it renders in front
    // of the host's place and the host leaves the page.
    usePlacement(() => {
      placeContentBefore(this.host.nativeElement, this.host.nativeElement)
      setElementPresent(this.host.nativeElement, false)
    })
  }

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * The max thumbnails to show (React's default parameter: a later write of
   * `undefined` must not erase the `4` default).
   * @returns The effective max thumbnails.
   */
  maxThumbnailCount(): number {
    return this.maxThumbnails ?? 4
  }

  /**
   * The selected index: the controlled prop, else the component's own.
   * @returns The selected index.
   */
  idx(): number {
    return this.selectedIndex ?? this.internalIndex
  }

  /**
   * The thumbnails React keeps (`images.slice(0, maxThumbnails)`).
   * @returns The visible image URLs.
   */
  visible(): string[] {
    return this.images.slice(0, this.maxThumbnailCount())
  }

  /**
   * How many images the "+N" chip summarises.
   * @returns The overflow count.
   */
  overflow(): number {
    return Math.max(0, this.images.length - this.visible().length)
  }

  /**
   * The main image's source (React's `images[idx]`).
   * @returns The URL, or `null` when the index is out of range. React renders
   *   the `src` attribute away; Angular's renderer rewrites a null `src` on an
   *   `<img>` to `src=""`, so the template picks an element without `src`.
   */
  mainSrc(): string | null {
    return this.images[this.idx()] ?? null
  }

  /**
   * The main image's alt text (React's `alts?.[idx] ?? t(…)` with the
   * `imageGallery.alt.fallback` fallback).
   * @returns The alt text.
   */
  mainAlt(): string {
    return (
      this.alts?.[this.idx()] ??
      this.i18n.t(
        'imageGallery.alt.fallback',
        { number: this.idx() + 1 },
        { defaultValue: 'Image {{number}}' },
      )
    )
  }

  /**
   * One thumbnail's aria-label (React's `alts?.[i] ?? t(…)` with the
   * `imageGallery.alt.fallback` fallback).
   *
   * @param index - The thumbnail's index.
   * @returns The label text.
   */
  thumbAlt(index: number): string {
    return (
      this.alts?.[index] ??
      this.i18n.t(
        'imageGallery.alt.fallback',
        { number: index + 1 },
        { defaultValue: 'Image {{number}}' },
      )
    )
  }

  /**
   * The "+N" chip's text (React's `t('imageGallery.overflow.more', …)`).
   * @returns The chip text, e.g. `+2`.
   */
  overflowText(): string {
    return this.i18n.t(
      'imageGallery.overflow.more',
      { count: this.overflow() },
      { defaultValue: '+{{count}}' },
    )
  }

  /**
   * The wrapper class list (React's `cm.cn(cm.stack(2), className)`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(this.cm().stack(2), this.className)
  }

  /**
   * The thumbnail grid's class (React's `cm.grid({ cols, gap: 'sm' })`).
   * @returns The class string.
   */
  gridClasses(): string {
    return this.cm().grid({ cols: toGridCols(this.maxThumbnailCount()), gap: 'sm' })
  }

  /**
   * The "+N" chip's class list (React's `cm.cn(cm.flex(…), cm.textSize('sm'),
   * cm.fontWeight('semibold'))`).
   * @returns The class string.
   */
  overflowClasses(): string {
    return this.cm().cn(
      this.cm().flex({ align: 'center', justify: 'center' }),
      this.cm().textSize('sm'),
      this.cm().fontWeight('semibold'),
    )
  }

  /**
   * Selects thumbnail `index`, keeping the internal state only while the
   * gallery is uncontrolled, then emits React's `onSelect`.
   *
   * @param index - The clicked thumbnail's index.
   */
  selectIndex(index: number): void {
    if (this.selectedIndex === undefined) this.internalIndex = index
    this.select.emit(index)
  }
}

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.13.0 || ^4.0.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

  • @angular/common

  • @angular/core

  • @angular/platform-browser

  • @molecule/app-angular

  • @molecule/app-ui

  • @molecule/app-ui-angular

  • Renders nothing when images is empty — no empty-state UI.

  • maxThumbnails doubles as the thumbnail grid's column count. Any value is safe: it is snapped to the nearest column count the ClassMap grid actually supports (1-6 or 12), so a real grid-cols-* class is always emitted and extra thumbnails wrap onto additional rows instead of collapsing.

  • Default alt text falls back through i18n to "Image N" (imageGallery.alt.fallback, English defaultValue) — pass alts with per-image strings in apps that know them.

  • onSelect is the select output: (select)="onSelect($event)".

  • getClassMap() requires a bonded ClassMap (e.g. @molecule/app-ui-tailwind).

Translations

Translation strings are provided by @molecule/app-locales-image-gallery.