Photo grid

Photos as tiles: an album, an attachment set, a listing's pictures. Equal squares, a masonry of columns, or justified rows of one height, with "+N" on the last tile when there are more than fit, and a lightbox to see them large.

Photo grid

No JavaScript needed for the grid: each tile is a link to its full photo. lightbox needs Alpine and the package's script; without them the tiles stay links. See getting started.

Use it

<x-ds::photo-grid
    label="Trip to Skye"
    :max="6"
    :lightbox="true"
    :photos="$photos->map(fn ($p) => [
        'src' => $p->url,              // the full image
        'thumb' => $p->thumb_url,      // what the tile shows
        'alt' => $p->alt,
        'width' => $p->width,
        'height' => $p->height,
        'caption' => $p->caption,      // shown in the lightbox
    ])"
/>

Props

PropTypeDefaultWhat it does
photosarray[]The photos, in order. Arrays or objects; keys below.
layoutgrid masonry justifiedgridEqual tiles, CSS columns with each photo at its own shape, or rows of one height.
columns1–63The most columns, reached as the grid's own container widens (grid and masonry).
gapsm md lgsmThe space between tiles.
ratiosquare 4:3 3:2 16:9 3:4squareThe tiles' shape, with layout="grid".
maxint—Show at most this many tiles; the last says "+N" over it.
moreHrefstring—Where "+N" goes (the full album) instead of to its own photo. Without lightbox only: with one, "+N" opens it and the rest are a swipe away.
roundedbooltrueRounded tiles.
labelstringPhotosNames the list.
lightboxbool or stringfalsetrue renders a lightbox for these photos, with thumbnails. A name wires the tiles to a lightbox of that name you render yourself.

Per photo:

KeyWhat it does
srcThe full image: what the tile links to and the lightbox shows. Required.
altWhat the photo shows. Required unless decorative.
thumbThe smaller image the tile shows. Defaults to src.
width heightThe full image's size, so the page does not shift as tiles load, and justified rows know each photo's shape.
captionShown under the photo in the lightbox.
hrefWhere the tile goes instead of the image, such as the photo's own page.
srcset sizesPassed to the tile's image.
decorativeThe photo adds nothing the text does not say: alt="", and the tile is named by its caption or position.

A missing alt

With APP_DEBUG on, a photo with no alt that is not decorative throws, naming the photo, so the gap is found while building. In production it logs a warning and names the tile "Photo 3 of 8" rather than break the page.

One lightbox for several grids

<x-ds::photo-grid :photos="$before" lightbox="job-42" label="Before" />
<x-ds::photo-grid :photos="$after" lightbox="job-42" label="After" />
<x-ds::lightbox name="job-42" :thumbnails="true" :original="true" />

The lightbox shows every opener of its name on the page in order, so the reader goes from the last "before" to the first "after".

Accessibility

  • A list named by label; each tile is a link named by its photo's alt.
  • "+N" is drawn over the last tile and said as part of its name, ", and 4 more". The tiles past max are in the page, hidden, so the lightbox can show them, and out of the tab order.
  • The focus ring is drawn outside the tile, and the hover zoom on a tile is motion-safe.

Don't

  • Don't leave out alt to make the warning go away with decorative: a photo that is the content needs words.
  • Don't send the full image as the tile. Give thumb, and srcset where you have sizes, or the grid downloads every original.
  • Don't use it for icons or logos. It is for photos, cropped to fill.

More in specs/photo-grid.md.


From the package repo: edit this page on GitHub.

Was this page helpful?