Photo Stack

A pile of photos. Drag the top one aside to send it to the back, or press any photo to open it as a carousel.

Drag it sideways or press the left and right arrow keys to send it to the back. Press Enter to open it.

Drag the top photo aside, or use the arrow keys, to send it to the back. Click any photo to open it.

Six photos sit in a loose pile. Drag the top one off either side and it carries on out, then tucks in at the back while the next comes forward. Press any photo, the top one or a sliver of one behind it, and it opens at full size with its neighbours beside it as a carousel. Swipe the carousel, use its arrows, or press Escape to put the pile back.

With the keyboard, Tab to the top photo, press ← or → to send it to the back, or Enter to open it. Once a photo is open, ← and → walk the carousel and Escape closes it.

How it works

  • Every card is always mounted, and only moves. The pile and the carousel are two sets of poses for the same elements, so opening a photo is that card travelling from where it was, never a copy growing out of it.
  • The poses are pure. stack.ts turns the stage's size and one question, shut or open, into a target for every card. The component measures the stage and asks.
  • A drag and a spring write the same values. Each card's position, rotation, box and opacity are motion values written imperatively, so nothing renders per frame.
  • A thrown card gets clear before it goes behind. It leaves on a quick ease-out, and only once just its edge still overlaps the pile does it give up its place in front, so the change of stacking order is hidden.
  • Hover leans the pile out. On a mouse or pen, the cards behind fan a little wider, which is the only thing at rest that says the top photo can be moved.
  • Reduced motion replaces the springs with a short ease, and a card dragged to the back goes straight there without the throw.

Cards two places from the open photo are transparent and inert. A polite live region inside the stage, which screen readers hear but sighted users do not see, announces the open photo.

Installation

pnpm dlx shadcn@latest add @uitopia/photo-stack

Usage

import PhotoStack from "@/components/ui/photo-stack";

<PhotoStack />;

The component draws its own stage and fits the photos to it. Pass your own pile with photos, and set aspect to the photos' width over height (the default pile is 4:5). Pass className to change the wrapper, and hint to change the line under the stage, or false to remove it. A new set of photos starts a new pile; a new array with the same ids does not.

<PhotoStack
  aspect={3 / 4}
  photos={[
    { id: "a", title: "Harbour at six", src: "/photos/harbour.jpg" },
    { id: "b", title: "Market stall", src: "/photos/market.jpg" },
    { id: "c", title: "Night train", src: "/photos/train.jpg" },
  ]}
/>

API

PropTypeDefaultDescription
photosreadonly Photo[]six picturesThe pile, top first. A photo is { id, title, src }.
aspectnumber0.8Width over height of every photo.
hintstring | falsea short lineThe line under the stage. false hides it.
classNamestring-Classes for the outer wrapper.

Photos

The default pile is six public-domain works from the Metropolitan Museum of Art's Open Access collection, released under CC0. Each is cropped to a 4:5 portrait, softened, saturated and grained so they read as one set, and they are hosted on this site, so the component adds no image weight to your bundle. Pass your own photos for anything you ship.