Aller au contenu

Gallery

composant

A gallery: a list of figures in a grid that fills its container, its columns as wide as the container allows, read by a container query; each image a Picture of the image recipe, under its caption, credit, license and creation; with lightbox, each opens in a Lightbox.

Aperçu

Clair

  • The stage
    Opening night
  • The crowd
Code
<twig:Gallery
    label="The festival"
    aspect="1/1"
    :images="[
        {src: '/images/stage.jpg', alt: 'The stage', width: 800, height: 600, caption: 'Opening night'},
        {src: '/images/crowd.jpg', alt: 'The crowd', width: 800, height: 600},
    ]"
/>

Sombre

  • The stage
    Opening night
  • The crowd
Code
<twig:Gallery
    label="The festival"
    aspect="1/1"
    :images="[
        {src: '/images/stage.jpg', alt: 'The stage', width: 800, height: 600, caption: 'Opening night'},
        {src: '/images/crowd.jpg', alt: 'The crowd', width: 800, height: 600},
    ]"
/>

Essayer

Changez les propriétés ; le serveur rend l’exemple de nouveau.

  • The stage
    Opening night
  • The crowd
<twig:Gallery
    label="The festival"
    aspect="1/1"
    :images="[
        {src: '/images/stage.jpg', alt: 'The stage', width: 800, height: 600, caption: 'Opening night'},
        {src: '/images/crowd.jpg', alt: 'The crowd', width: 800, height: 600},
    ]"
/>

Variantes

lightbox

Whether each image opens in a Lightbox, which the lightbox recipe draws.

  • The stage
    Opening night
  • The crowd
  • The stage
    Opening night
  • The crowd
lightbox="false"
  • Opening night
    The stage
    Opening night
  • The crowd
  • Opening night
    The stage
    Opening night
  • The crowd
lightbox="true"

Usage

Usage

Cet exemple a besoin du contexte d’une page pour se rendre ; son code est ci-dessous.

Code
<twig:Gallery label="{{ 'gallery.festival'|trans }}" :images="photos" aspect="1/1" sizes="(min-width: 48rem) 33vw, 100vw" />

Installation

php bin/console ux:install gallery --kit=github.com/parler-haut-interagir-librement/ux-kit

Elle apporte les recettes dont elle a besoin : image, lightbox

Propriétés

PropriétéTypePar défautDescription
images — [] list<array{src:string,alt:string,width:int,height:int,srcset?:string|array<int|string,string>,sources?:list<array<string,mixed>>,position?:string,full?:array<string,mixed>,caption?:string,credit?:string,license?:string,licenseHref?:string,creation?:string,creationLabel?:string}> The images, each with the props of a Picture, and the full image of its lightbox.
aspect string|null null The ratio every image is cropped to, as for Image, so that the rows line up.
fit string|null null How every image fills its box, "cover" or "contain".
sizes string|null null The sizes of the candidates of widths, as wide as a column of the grid, "(min-width: 48rem) 33vw, 100vw".
lightbox bool false Whether each image opens in a Lightbox, which the lightbox recipe draws.
id string|null null The identifier of the gallery, from which those of its lightboxes are made: "gallery" when null.
openLabel string|null null Words screen readers read after the text alternative of each lightbox's button, such as "Enlarge".
closeLabel string 'Close' What screen readers read for the close button of each lightbox.

Examples

Each image enlarged

<twig:Gallery
    id="festival"
    label="The festival"
    :lightbox="true"
    openLabel="{{ 'image.enlarge'|trans }}"
    closeLabel="{{ 'dialog.close'|trans }}"
    :images="[
        {src: '/images/stage-480.jpg', alt: 'The stage', width: 480, height: 360, full: {src: '/images/stage.jpg', width: 1600, height: 1200}},
    ]"
/>

The identifier of each lightbox is made from id, lightbox-festival-1 and the others; give an id to each gallery of a page. full is the image the lightbox shows, the thumbnail itself when it has none.

Accessibility

The gallery is a list named by label, role="list" keeping its semantics where the browser drops them from a list without markers. The columns follow the width of the gallery, not that of the screen, so that a gallery in a narrow column stays readable. An image refused by the image recipe takes no place; without an image, nothing is drawn.

Fichiers à copier

templates/components/Gallery.html.twig
{#-
 # A gallery: a list of figures in a grid that fills its container, named by its label, each image through the Picture
 # of the image recipe, under its caption, credit, license and creation; with lightbox, each opens in a Lightbox. An
 # image the image recipe refuses takes no place; without an image, nothing is drawn.
 #}
{%- props
    ## string The name of the list, which screen readers read, such as "Photos of the festival".
    label,
    ## list<array{src:string,alt:string,width:int,height:int,srcset?:string|array<int|string,string>,sources?:list<array<string,mixed>>,position?:string,full?:array<string,mixed>,caption?:string,credit?:string,license?:string,licenseHref?:string,creation?:string,creationLabel?:string}> The images, each with the props of a Picture, and the full image of its lightbox.
    images = [],
    ## string|null The ratio every image is cropped to, as for Image, so that the rows line up.
    aspect = null,
    ## string|null How every image fills its box, "cover" or "contain".
    fit = null,
    ## string|null The sizes of the candidates of widths, as wide as a column of the grid, "(min-width: 48rem) 33vw, 100vw".
    sizes = null,
    ## bool Whether each image opens in a Lightbox, which the lightbox recipe draws.
    lightbox = false,
    ## string|null The identifier of the gallery, from which those of its lightboxes are made: "gallery" when null.
    id = null,
    ## string|null Words screen readers read after the text alternative of each lightbox's button, such as "Enlarge".
    openLabel = null,
    ## string What screen readers read for the close button of each lightbox.
    closeLabel = 'Close'
-%}
{%- set _images = images|filter(image => (image.src ?? '') is not empty and image_origin(image.src) is not same as('refused')) -%}
{%- if _images is not empty -%}
    <div {{ html_attr({id: id, class: ['isGallery', attributes.all.class ?? null]|filter(name => name is not empty)|join(' ')}) }}>
        <ul role="list" aria-label="{{ label }}">
            {%- for image in _images -%}
                <li>
                    {%- if lightbox -%}
                        <twig:Lightbox :id="(id ?? 'gallery') ~ '-' ~ loop.index" :src="image.src" :alt="image.alt" :width="image.width" :height="image.height" :srcset="image.srcset ?? null" :sizes="sizes" :sources="image.sources ?? []" :aspect="aspect" :fit="fit" :position="image.position ?? null" :full="image.full ?? null" :caption="image.caption ?? null" :credit="image.credit ?? null" :license="image.license ?? null" :licenseHref="image.licenseHref ?? null" :creation="image.creation ?? null" :creationLabel="image.creationLabel ?? null" :openLabel="openLabel" :closeLabel="closeLabel" />
                    {%- else -%}
                        <twig:Picture :src="image.src" :alt="image.alt" :width="image.width" :height="image.height" :srcset="image.srcset ?? null" :sizes="sizes" :sources="image.sources ?? []" :aspect="aspect" :fit="fit" :position="image.position ?? null" :caption="image.caption ?? null" :credit="image.credit ?? null" :license="image.license ?? null" :licenseHref="image.licenseHref ?? null" :creation="image.creation ?? null" :creationLabel="image.creationLabel ?? null" />
                    {%- endif -%}
                </li>
            {%- endfor -%}
        </ul>
    </div>
{%- endif -%}