Lightbox

A recipe: photos opened large — a Dialog holding a Carousel of them all. No component of its own.

Usage

Usage

import { A11yModule } from '@angular/cdk/a11y';import { NgOptimizedImage } from '@angular/common';import {  Component,  ElementRef,  ViewEncapsulation,  afterRenderEffect,  signal,  viewChildren,} from '@angular/core';

How it is made

Two signals are yours: whether it is open (the Dialog's [(open)]) and the photo shown (the Carousel's [(activeIndex)]). A thumbnail sets both; the Carousel starts on that photo when the Dialog opens.

  • Each photo is a slide. Previous and Next sit on it (start-2, end-2, variant secondary), in a positioned box around the viewport.
  • The caption and the counter (“3 / 6”) come from the index.
  • The thumbnails below are a row of buttons inside the Carousel, so the arrow keys still change the photo from there; each sets the photo, the current one aria-current, and an afterRenderEffect keeps it in view.
  • Swipes, drags and the keys come from the Carousel (with Embla's plugins if you want more); Escape, the backdrop and focus from the Dialog.
import {  ScDialogProvider, ScDialogPortal, ScDialog, ScDialogClose,  ScDialogHeader, ScDialogTitle,} from '@semantic-components/ui';import {  ScCarousel, ScCarouselViewport, ScCarouselTrack, ScCarouselItem,  ScCarouselPrevious, ScCarouselNext,} from '@semantic-components/ui/carousel';
<div scDialogProvider [(open)]="open">  <button (click)="show(i)">…thumbnail…</button>  <ng-template scDialogPortal>    <div scDialog>      <button scDialogClose>…</button>      <div scDialogHeader><h2 scDialogTitle>…</h2></div>      <div scCarousel [(activeIndex)]="index" aria-roledescription="…" aria-label="…">        <div class="relative">          <div scCarouselViewport cdkFocusInitial>            <div scCarouselTrack>              <div scCarouselItem …>…photo…</div>            </div>          </div>          <button scCarouselPrevious class="start-2">…</button>          <button scCarouselNext class="end-2">…</button>        </div>        <p>…caption… {{ index() + 1 }} / {{ photos.length }}</p>        <div role="group" aria-label="…">…thumbnail buttons…</div>      </div>    </div>  </ng-template></div>

Accessibility

  • The thumbnails are buttons, named by their images’ alt text; the Dialog is named by its title.
  • Focus starts on the photo (the Carousel's viewport, cdkFocusInitial), so ← and → move between the photos at once — from the thumbnails too; Escape closes it, and focus goes back to the thumbnail.
  • Each slide is named by its place (“3 of 6”).

Examples

Article

The café opens at eight. By nine the long table fills with people who came for one coffee and stay for three. Light comes in from the street side, so the best seats are by the window.

In the afternoon we walked down to the coast, where the rocks run out into the water and the wind never quite stops.

The way back went through the forest: an hour of shade, a path soft with needles, and nobody else on it.

import { A11yModule } from '@angular/cdk/a11y';import { NgOptimizedImage } from '@angular/common';import {  Component,  ElementRef,  ViewEncapsulation,  afterRenderEffect,  signal,  viewChildren,} from '@angular/core';

RTL

import { A11yModule } from '@angular/cdk/a11y';import { Dir } from '@angular/cdk/bidi';import { NgOptimizedImage } from '@angular/common';import {  Component,  ElementRef,  LOCALE_ID,  ViewEncapsulation,  afterRenderEffect,  signal,