Replacing justified-layout with My Own Package

How my old site swapped Flickr's unmaintained justified-layout for computeLayout() from my own package, without touching the photo tree.

Article Created Updated Also in German: justified-layout durch mein eigenes Paket ersetzen

Reviewed by adrian on

In April I had released @altner/astro-justified-gallery-layout and admitted that my own site did not use it. The photo section had its own hierarchical directory tree, JSON sidecars with EXIF, IPTC and GPS, and titles in several languages. None of that fitted what galleryLoader() and <JustifiedGallery /> expect. Using the full component would have meant rebuilding half the photo stack around a new data model, just for a gallery grid.

That reasoning still held. But while looking through astro.config.mjs for something else, I noticed the site still pulled in Flickr's justified-layout: the very package mine was written to replace, unmaintained since 2021, CommonJS with a UMD browser build. It was used for one thing only, computing box positions from an array of aspect ratios.

That part was never tied to my data model. galleryLoader() and the two ready-made components are built around Astro's Content Layer, but computeLayout() does not know Content Layer exists. Numbers in, numbers out.

The three call sites

Three components ran the same pattern: compute a justified grid in the browser, position <button> or <figure> elements absolutely, skip the layout on mobile.

  • PhotosDetailPage.astro, for photo sets and albums
  • PhotosTagDetailPage.astro, for tag detail pages
  • PhotoSet.astro, for inline galleries inside blog posts

All three had the same six lines at their core.

// before
import justifiedLayout from 'justified-layout';

const result = justifiedLayout(aspects, {
  containerWidth: container.clientWidth,
  targetRowHeight: 380,
  boxSpacing: 6,
  containerPadding: 0,
});
container.style.height = `${result.containerHeight}px`;
// after
import { computeLayout } from '@altner/astro-justified-gallery-layout';

const result = computeLayout(
  aspects.map((a) => ({ w: a, h: 1 })),
  { containerWidth: container.clientWidth, targetRowHeight: 380, gap: 6 },
);
container.style.height = `${result.totalHeight}px`;

What changed in the API

  • computeLayout() takes { w, h } pairs instead of bare aspect ratios. Only the ratio matters, so { w: aspect, h: 1 } carries the same information as the old array.
  • boxSpacing became gap.
  • containerPadding is gone. Its default in justified-layout is 10, but my call sites set it to 0 explicitly, so nothing changed in practice.1
  • containerHeight on the result is now totalHeight.

Everything after that kept working unchanged: reading result.boxes[i].top/left/width/height to position each element. The shape of a box did not change.

Why bother for three call sites

The gallery looked exactly the same afterwards. This was a dependency swap, no feature. Out went a package without a release since January 2021; in came one with zero runtime dependencies, ESM only, with bundled TypeScript types. It also meant using my own code where I had reached for someone else's out of habit. A strange thing to notice in your own project, but there it is.

The bigger integration still did not happen: galleryLoader(), the i18n-aware content pipeline, a shared metadata schema between the site's JSON sidecars and the package's IPTC approach. By July there was a sharper reason than in April. The site did not run photos through Astro's Content Layer at all, on purpose. A content collection cannot hold one photo's German and English metadata in the same entry. More on that in Why the photo tree is no content collection. galleryLoader() assumes a content collection to load into, and there was none to hand it. The layout maths never depended on Content Layer, so there was no reason to wait with it.

Takeaways

  • A "not ready to migrate" decision usually covers the whole integration, not every function in the dependency. Check whether a small, self-contained piece can move on its own.
  • Pure functions make the smallest migration surface: same inputs, same output shape, no state, no assumptions about the architecture around them.
  • Using your own package on your own site finds gaps a demo repository will not. This time it found none: the API worked on the first try at all three call sites.

Based on

  • Retiring justified-layout for My Own Package: the Small Version (raw/2026/2026-07-18/retiring-justified-layout-for-my-own-package-the-small-version.md) [post]
  • justified-layout 4.1.0 on npm [npm]

More on this

Topic

  • Static Website A website made of finished HTML files that are built once and then served unchanged. No server code, no database.

Builds on

  • Astro Justified Gallery Layout My npm package @altner/astro-justified-gallery-layout: a small justified gallery for Astro with EXIF, LQIP previews, a virtual variant and a lightbox.

Uses

  • Astro Tool for building websites from content such as Markdown. Ships plain HTML by default and only loads JavaScript where it is needed.

Mentioned

Show in the graph view