Lightbox

Photos one at a time, large, over the page: swipe or arrow between them, zoom in to look closer, close to go back to where you were.

Lightbox

Needs Alpine and the package's script. Without them the openers are plain links to the full photos, which is a working page. See getting started.

Use it

The easy way is photo grid's lightbox. On its own, mark each opener with x-ds-lightbox and the lightbox's name, inside an Alpine scope (any x-data), and render the lightbox once:

<div x-data>
    @foreach ($photos as $photo)
        <a href="{{ $photo->url }}" x-ds-lightbox="listing" data-ds-lightbox-caption="{{ $photo->caption }}">
            <img src="{{ $photo->thumb_url }}" alt="{{ $photo->alt }}" width="160" height="120">
        </a>
    @endforeach
</div>

<x-ds::lightbox name="listing" :thumbnails="true" :original="true" />

x-ds-lightbox takes a name, not an expression. Its photos are every opener of that name on the page, in document order, read when it opens: an opener added later is included the next time.

Props

PropTypeDefaultWhat it does
namestringlightboxWhich openers it shows.
labelstringPhoto :n of :totalThe dialog's name for each photo, followed by ": " and its alt. Translate it here.
thumbnailsboolfalseA strip of thumbnails under the photo.
originalboolfalseA link to the full image, in a new tab.
downloadboolfalseWith original, the link downloads the file instead (same-origin images only; a browser rule).
dismissiblebooltrueA swipe down closes it, as well as Escape and the close button.

What an opener can carry:

AttributeWhat it does
hrefThe full image.
data-ds-lightbox-srcThe full image, when href goes somewhere else.
data-ds-lightbox-srcsetA srcset for the full image.
data-ds-lightbox-altThe alt, when the opener has no img inside.
data-ds-lightbox-captionA caption under the photo.

The thumbnail is the img inside the opener, shown blurred while the full photo loads.

How it behaves

  • Moving: Left and Right (in the reading direction), Home and End, the arrow buttons, a swipe sideways, or a thumbnail. The photo slides in from its side; under reduced motion it just appears.
  • Zoom: a click on the photo, a double tap, a pinch, + and -, or the Zoom button, between fit and the photo's full size. Drag or use the arrows to look around it; the edges never come inside the frame.
  • Loading: if a photo takes more than 100 ms, its thumbnail shows blurred under a loader. A photo that fails says so, with Try again. The next and previous photos are fetched ahead.
  • Closing: Escape, the close button, a press on the dark space around the photo, or a swipe down. Focus goes back to the tile of the photo you were last looking at, if it can be seen, or to the one you opened.

It is dark whatever the page's theme: photos are seen best on near-black.

Accessibility

  • A modal dialog, on the same layer as modal: focus is held in it, the page behind does not scroll, and it stacks over other overlays.
  • The dialog is named "Photo 2 of 12: Pine trees on a hillside", and the same is said politely on each move. The photo itself has its alt.
  • Every gesture has a key or a button: there is nothing only a swipe does.
KeyDoes
Left / RightThe previous / next photo; when zoomed, look left / right
Up / DownWhen zoomed, look up / down (Shift for further)
Home / EndThe first / last photo
+ / -Zoom in / back to fit
EscapeCloses
TabZoom, original, close, previous, next, the thumbnails (one stop; arrows inside)

Don't

  • Don't put the only copy of a photo's meaning in a caption. Put it in the alt, which is what the dialog is named by.
  • Don't open it on a link to a page. The lightbox shows images; an opener whose href is a page needs data-ds-lightbox-src.
  • Don't render two with one name. The first on the page is the one used.

More in specs/lightbox.md.


From the package repo: edit this page on GitHub.

Was this page helpful?