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
3 changed files with 30 additions and 15 deletions

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>