Gallery: optional caption, with the credit appended; caption the home page photo
All checks were successful
/ build (push) Successful in 1m58s
/ accessibility (push) Successful in 3m55s

This commit is contained in:
Stefan Bethke 2026-11-16 00:11:32 +01:00
commit 3176e02889
6 changed files with 30 additions and 15 deletions

View file

@ -265,7 +265,7 @@ In the GeoJSON, `coordinates` are already in `[lon, lat]` order, the colour and
## Shortcode: Image gallery
`{{< gallery >}}` shows a page's photos as square thumbnails, two per row on wide screens and one per row on narrow ones. Clicking a thumbnail opens the full image in a lightbox with its alt text and credit; with more than one image, arrows (on screen, the arrow keys, or swiping) move between them, and Escape, the close button or a click beside the image closes it.
`{{< gallery >}}` shows a page's photos as square thumbnails, two per row on wide screens and one per row on narrow ones. Clicking a thumbnail opens the full image in a lightbox with its caption (or alt text) and credit; with more than one image, arrows (on screen, the arrow keys, or swiping) move between them, and Escape, the close button or a click beside the image closes it.
```markdown
{{< gallery >}}
@ -277,8 +277,9 @@ In the GeoJSON, `coordinates` are already in `[lon, lat]` order, the colour and
| Param | Required | Content |
|---|---|---|
| `src` | yes | The image: a file in the page's folder, or a path in `themes/hackertours/assets/`. It must be a photo Hugo can resize (JPEG, PNG, WebP…), not SVG. |
| `alt` | yes | What the image shows. It is the thumbnail's alt text and the caption in the lightbox. |
| `credit` | no | Photographer and licence, shown under the thumbnail and in the lightbox. |
| `alt` | yes | What the image shows, for people who can't see it. It names the thumbnail's link, so it can't be empty, and is shown in the lightbox when there is no caption. |
| `caption` | no | Context for everyone, shown under the thumbnail and in the lightbox. Say something the alt text doesn't: screen readers read both, so the same text would be heard twice. |
| `credit` | no | Photographer and licence, appended to the caption under the thumbnail and in the lightbox. |
`{{< gallery ratio="original" >}}` keeps each image's own aspect ratio instead of cropping it to a square; a gallery with a single image then spans the full width, as on the home page. Without `ratio` (or with `ratio="square"`), the thumbnails are square.

View file

@ -27,7 +27,7 @@ Hackertours-Desk: DECT 8687 "TOUR", +49 40 573 0830 8687
[C3Nav-Direktlink](https://40c3.c3nav.de/l/hackertours-desk/) Ihr findet uns im Foyer Eingang Mitte, neben den Kassen.
{{< gallery ratio="original" >}}
{{< gallery-image src=`hackertours_banner_messe.jpg` alt=`Hackertours am Eingang Mitte der Messe und Congress Hamburg` credit=`CC0 Amzi` >}}
{{< gallery-image src=`hackertours_banner_messe.jpg` alt=`Glasfassade mit dem Schriftzug „Hamburg Messe + Congress, Eingang Mitte“ und dem Hackertours-Haltestellen-Schild` caption=`Hackertours am Eingang Mitte der Messe und Congress Hamburg` credit=`CC0 Amzi` >}}
{{< /gallery >}}
## Veranstaltungen

View file

@ -27,7 +27,7 @@ Hackertours Desk: DECT 8687 "TOUR", +49 40 573 0830 8687
[C3Nav Direct Link](https://40c3.c3nav.de/l/hackertours-desk/) You can find us at ground level of the Central Entrance foyer, next to the cash desk.
{{< gallery ratio="original" >}}
{{< gallery-image src=`hackertours_banner_messe.jpg` alt=`Hackertours at Central Entrance of Messe und Congress Hamburg` credit=`CC0 Amzi` >}}
{{< gallery-image src=`hackertours_banner_messe.jpg` alt=`Glass façade with the sign 'Hamburg Messe + Congress, Eingang Mitte' and a yellow HHA bus stop sign` caption=`Hackertours at Central Entrance of Messe und Congress Hamburg` credit=`CC0 Amzi` >}}
{{< /gallery >}}
## Events

View file

@ -65,9 +65,13 @@
}
}
.gallery__credit {
.gallery__caption {
margin-block-start: 0.25rem;
font-family: var(--ff-alt);
font-size: var(--fs-s);
}
.gallery__credit {
font-size: var(--fs-xs);
color: var(--color-text-meta);
}

View file

@ -23,7 +23,7 @@
<figure class="lightbox__figure">
<img class="lightbox__image" alt="">
<figcaption class="lightbox__caption" aria-live="polite">
<span class="lightbox__alt"></span>
<span class="lightbox__text"></span>
<span class="lightbox__credit"></span>
<span class="lightbox__counter"></span>
</figcaption>
@ -47,9 +47,12 @@
image.width = link.dataset.width;
image.height = link.dataset.height;
image.alt = link.dataset.alt;
// The alt text is shown as the caption, so screen readers get it once, from the image.
dialog.querySelector('.lightbox__alt').textContent = link.dataset.alt;
dialog.querySelector('.lightbox__alt').setAttribute('aria-hidden', 'true');
// The caption, or else the alt text. Shown alt text is hidden from screen readers, which
// already get it from the image.
const text = dialog.querySelector('.lightbox__text');
text.textContent = link.dataset.caption ?? link.dataset.alt;
if (link.dataset.caption) text.removeAttribute('aria-hidden');
else text.setAttribute('aria-hidden', 'true');
dialog.querySelector('.lightbox__credit').textContent = link.dataset.credit ?? '';
dialog.querySelector('.lightbox__counter').textContent =
links.length > 1 ? `${index + 1} / ${links.length}` : '';

View file

@ -1,14 +1,18 @@
{{- /*
One image in a gallery, see gallery.html. Params:
src the image, a file in the page's folder (page resource) or in assets/
alt what the image shows (required); also shown in the lightbox
credit photographer, licence; shown under the thumbnail and in the lightbox
alt what the image shows, for people who can't see it (required); the thumbnail link's
name, so it can't be empty. Shown in the lightbox when there is no caption.
caption context for everyone, shown under the thumbnail and in the lightbox (optional);
say something the alt text doesn't, or screen readers read the same text twice
credit photographer, licence; appended to the caption
*/ -}}
{{- if ne .Parent.Name "gallery" -}}
{{- errorf "gallery-image: use it inside {{< gallery >}}: %s" .Position -}}
{{- end -}}
{{- $src := .Get "src" -}}
{{- $alt := .Get "alt" -}}
{{- $caption := .Get "caption" -}}
{{- $credit := .Get "credit" -}}
{{- if not $alt -}}
{{- errorf "gallery-image: %q needs an alt text: %s" $src .Position -}}
@ -39,12 +43,15 @@ that for the original, but Hugo doesn't when it resizes. */ -}}
{{- end -}}
<li class="gallery__item">
<figure class="gallery__figure">
<a class="gallery__link" href="{{ $image.RelPermalink }}" data-width="{{ $oriented.Width }}" data-height="{{ $oriented.Height }}" data-alt="{{ $alt }}"{{ with $credit }} data-credit="{{ . }}"{{ end }}>
<a class="gallery__link" href="{{ $image.RelPermalink }}" data-width="{{ $oriented.Width }}" data-height="{{ $oriented.Height }}" data-alt="{{ $alt }}"{{ with $caption }} data-caption="{{ . }}"{{ end }}{{ with $credit }} data-credit="{{ . }}"{{ end }}>
<img src="{{ $large.RelPermalink }}" srcset="{{ $small.RelPermalink }} {{ $small.Width }}w, {{ $large.RelPermalink }} {{ $large.Width }}w" sizes="{{ $sizes }}" width="{{ $large.Width }}" height="{{ $large.Height }}" alt="{{ $alt }}" loading="lazy" decoding="async">
<span class="visually-hidden"> ({{ i18n "gallery_enlarge" }})</span>
</a>
{{- with $credit }}
<figcaption class="gallery__credit">{{ . }}</figcaption>
{{- if or $caption $credit }}
<figcaption class="gallery__caption">
{{- with $caption }}{{ . }}{{ end }}
{{- with $credit }}{{ if $caption }} {{ end }}<span class="gallery__credit">{{ . }}</span>{{ end -}}
</figcaption>
{{- end }}
</figure>
</li>