This is a direct follow-up to Building a Travel Map.
In that post, I focused on architecture: geocoding, marker clustering, URL state, and map controls.
This post focuses on the interaction loop1 that makes the page feel connected: select a pin, reveal a location-specific masonry gallery2, and optionally open a photo in lightbox mode3.
In the current implementation, lightbox interactions are enabled on desktop layouts and disabled on smaller viewports.
Connecting map and gallery
The map uses a single selection state, selectedLocation, as the handoff between map interaction and gallery rendering.
When a marker is clicked (or activated by keyboard), the page:
- Stores the location name.
- Filters caption entries to that location.
- Renders a masonry grid below the map for that location only.
The header copy on the page now reflects this behavior explicitly:
Click a marker to open location photos, then browse the masonry grid below.
Marker click → selection state
Each marker sets the same location state:
marker.on('click', () => {
setSelectedLocation(entry.location);
if (isTouchInteraction) {
marker.openPopup();
}
});
Keyboard users get the same behavior through Enter and Space, so selection is not pointer-only.
That consistency keeps the map interaction simple: every activation path converges on setSelectedLocation(...).
Selection state → masonry data
Once selected, photos are derived from captions.json and limited to entries that have resolved image data:
const selectedLocationPhotos = useMemo(() => {
if (!selectedLocation) return [];
return captionData
.filter((entry) => entry.location.trim() === selectedLocation)
.filter((entry) => Boolean(imageLookup[entry.filename]))
.map((entry) => ({
filename: entry.filename,
caption: entry.caption || '',
src: imageLookup[entry.filename]?.src,
}));
}, [imageLookup, selectedLocation]);
This keeps rendering deterministic and avoids broken tiles when image metadata is missing.

Masonry rendering only when a pin is selected
The location gallery is intentionally conditional:
{
selectedLocation && (
<LocationGallery>
<GalleryTitle>{selectedLocation}</GalleryTitle>
<MasonryGrid breakpointCols={breakpointColumns}>
{selectedLocationPhotos.map((photo, index) => (
<MasonryItem key={photo.filename}>
<MasonryImage
src={photo.src}
alt={photo.caption || `Photo ${index + 1}`}
loading="lazy"
/>
</MasonryItem>
))}
</MasonryGrid>
</LocationGallery>
)
}
No selected pin means no grid. This prevents the gallery from competing with the map before the user chooses a location.

Mobile behavior: auto-scroll to selected gallery
On compact viewports, selecting a marker can leave the gallery below the fold.
To fix that, the page auto-scrolls to the location gallery once per page session (while respecting reduced-motion settings). That makes the map-to-gallery handoff smoother on touch devices without repeated forced scrolling.
Clearing selection
The gallery header includes a Clear action that resets selectedLocation.
That returns the page to map-first mode immediately without forcing a map reset.
Why this iteration matters
The map page now behaves more like a two-stage explorer:
- Spatial discovery on the map.
- Visual browsing in a focused masonry layout.
This keeps the map uncluttered while still making image exploration fast once a pin is chosen.
The building blocks, looking back
Stripped down, what I actually needed was:
- A single selection state (
selectedLocation) shared by map markers and gallery rendering. - A derived photo list (
selectedLocationPhotos) filtered from your caption/image metadata. - Conditional gallery rendering so the masonry grid appears only after a location is selected.
- A clear/reset action that returns the page to map-first mode.
- Optional viewport-aware polish:
- auto-scroll to the gallery on compact layouts
- desktop-only lightbox behavior if tap gestures should remain map-first on mobile
If your map supports keyboard focus, make sure marker activation (Enter/Space) updates the same selection state as pointer clicks.
What's next
The next likely polish step is persisting selectedLocation in URL state so shared links can open directly into the same map + masonry context.
Footnotes
-
An interaction loop is the repeated sequence of user action and system response; here, selecting a pin immediately updates the gallery below the map. ↩
-
A masonry gallery arranges images in columns with varied heights, minimizing gaps instead of forcing a uniform row grid. ↩
-
Lightbox mode opens an image in an enlarged overlay so users can focus on it without leaving the page. ↩
