r/reactjs • NextJS App Router • 29d ago

Show /r/reactjs How I made a lightbox open out of a cropped thumbnail (FLIP + clip-path), now an MIT library

https://liftbox.digital

I wanted a React lightbox where the photo grows out of the exact thumbnail you clicked and shrinks back into it on close.

Easy, until your grid uses object-fit: cover. The thumbnail only shows the middle of the photo, so if you grow the full image from that box, the first frame visibly jumps.

Here's the 4-step fix that worked:

1. Measure the end state first. Lay the image out at its final size, then measure it and the thumbnail with getBoundingClientRect().

2. Scale it onto the thumbnail. Use the smallest uniform scale that still covers the thumb: const scale = Math.max(thumb.width / img.width, thumb.height / img.height);

3. Crop it like the thumbnail. clip-path: inset(...) hides everything the thumbnail hides. The catch: the inset is in the image's own pixels, before the scale, so the visible area is thumb size / scale.

4. Animate both at once. transform + clip-path in a single element.animate(). To close, play the same keyframes in reverse.

That's the happy path. The edge cases ate most of the time:

  • Thumbnail scrolled off screen: fall back to a zoom + fade
  • Rotated photo: swap the axes before computing the crop
  • Closing while zoomed in: start from where it's actually drawn
  • prefers-reduced-motion**:** skip the flight entirely
  • Focus: send it back to the thumbnail on close

I packaged it as liftbox: MIT, zero dependencies, ~13 kB, with grids, zoom, video and deep links. There's also a comparison with YARL, PhotoSwipe, lightGallery and others that tells you when to pick them instead.

Code: https://github.com/esdrassantos06/liftbox (the flight lives in src/Lightbox.tsx and src/geometry.ts)

Docs + demos: https://liftbox.digital

Two asks:

  1. Open the demo on your phone and tell me where it breaks.
  2. Ask me anything about the math. Happy to go deeper on any step.

Disclosure: English isn't my first language, so I had help polishing this post.

1 Upvotes

10 comments sorted by

2

u/DVR_Design 29d ago

One edge case to check is a thumbnail with rounded corners. Since the inset() values are in the image's own pixels before the scale, the radius has to be divided by the same scale too, or the corners look too round at the start and snap at the end. You can animate it in the same keyframes with clip-path: inset(t r b l round Npx). Also set fill: 'both' on the element.animate() call so you don't get a one-frame flash between the animation finishing and the final styles applying.

0

u/Suitable-Visual7567 NextJS App Router 28d ago

Good catch, you found a real gap. The inset has no round right now, so a thumbnail with rounded corners (the built-in grid uses 4px by default) starts the flight with square corners. You barely notice it at 4px, but with a 16px radius you’d see it right away. youe math checks out too,the radius needs the same "/ scale" as the inset values, and then it animates to 0 in the same keyframes. I’ll add it in the next patch.

On fill: the opening ends on keyframes that match the element’s resting styles (identity transform, inset(0) ). Nothing changes when it finishes, so there’s no frame that could flash. Closing already uses fill: 'forwards' to hold the thumbnail pose until unmount, and backwards only matters with a delay, which there isn’t. If you did see a flash in the demo, tell me which browser and I’ll look into it.

0

u/Suitable-Visual7567 NextJS App Router 28d ago

Shipped your suggestion in 1.3.3: the flight now reads the thumbnail’s border-radius and starts the clip with "round r/scale", easing to square corners as it grows. I gave you credit in the changelog with a link back to this thread. Thanks for spotting it.

1

u/nameupp 28d ago

the math for handling object-fit: cover via clip-path: inset() is super clean, especially syncing it with transform in a single element.animate() call. one edge case that always bites FLIP transitions on photo grids is custom object-position; if a grid uses object-position: top or center top for portrait headshots so faces don't get cropped, symmetrical insets (img - thumb / scale) / 2 will cause the first frame to jump because the thumbnail isn't centered. reading getComputedStyle(thumb).objectPosition to bias the top/bottom delta makes it bulletproof for any gallery layout. also watch out for body scrollbar lock shifting the grid coordinates between open and close if scrollbar-gutter: stable isn't set imo

-1

u/Suitable-Visual7567 NextJS App Router 28d ago

You're right about object-position. the lib ignores it, so top-aligned thumbs jump. Fixing it in both the inset and the translate, since the inset alone won't line it up. The scroll lock already pads the body by the scrollbar width, so the grid doesn't move.

1

u/[deleted] 29d ago

[removed] — view removed comment

1

u/Suitable-Visual7567 NextJS App Router 28d ago

Transform and clip-path share the same keyframes in one element.animate() call, so they can’t drift apart: one easing, one duration. Good tip on will-change, I’m not using it yet and I’ll test it on a cheap Android.

MIT was on purpose. lightGallery is GPL 3, so closed-source projects need a paid license, and I wanted a lightbox you can drop into any client project without worrying about that

1

u/Winodows_Gandy 28d ago

yeah that mid-animation wobble when the easing curves dont match is super noticeable once you know to look for it.