# stack

The images of a container become a pile of cards; a swipe, a click or a key throws the top one off.

The top card covers the box, framed like its image, and the next cards lie under it with a soft shadow: scattered at small angles, fanned about a point under the box, or stepped back and smaller. The top card follows a drag and tilts with it; let go far or fast enough, it flies off the way it was thrown while the pile moves up a place, otherwise it springs back. A click flicks it away from the side that was clicked, and with tabindex on the container the arrows bring the next or the previous card. The images stay in the page in order with their alt text; without WebGL the container is the layout you wrote.

Module `media`, category Galleries and viewers. Import `@dioramajs/media/stack`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/media/src/stack.ts)

Six places on the coast as a pile of photos; drag the top one away, click it or use the arrow keys, and the next comes forward.

```html title="index.html"
<section class="trips" data-dio="stack" tabindex="0" aria-label="Six places on the coast. Click, swipe or use the arrow keys for the next one." style="touch-action: none">
  <img src="/media/coast/headland-800.avif" alt="The headland: a grassy point dropping into the sea under a pale sky" width="800" height="534" />
  <img src="/media/coast/boulders-600.avif" alt="The boulder beach: rounded stones at the edge of the water" width="600" height="750" />
  <img src="/media/coast/islets-600.avif" alt="The islets: dark rocks in a blue bay, a flat-topped mountain in the haze" width="600" height="600" />
  <img src="/media/coast/surf-800.avif" alt="The point break: surf over brown rocks at the foot of the cliffs" width="800" height="534" />
  <img src="/media/coast/slope-800.avif" alt="The cliff path: a green slope falling to the sea" width="800" height="534" />
  <img src="/media/coast/shore-800.avif" alt="The white shore: pale rocks and low bushes above a grey sea" width="800" height="534" />
</section>
```

## Usage

Put it on: a container of images the size of one card.

`start()` registers `stack`, so the attribute alone puts it on an element.

```html
<section class="picks" data-dio="stack" tabindex="0" aria-label="Picks of the week. Use the arrow keys or swipe to see the next." style="touch-action: pan-y"><img src="/picks/1.avif" alt="Linen shirt" /><img src="/picks/2.avif" alt="Canvas tote" /><img src="/picks/3.avif" alt="Field watch" /></section>
```

In page transitions: Fades with the page transition.

- Event `dio:index`, detail `{ index: number }`: A card is on top, after a throw or a change of index; index is its place among the images, from 0. Bubbles.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `index` | `data-dio-index` | `number` 0 to 10 | `0` | Card on top from 0; a fraction holds the top card that far in flight. |
| `style` (rebuilds) | `data-dio-style` | `'pile' \| 'fan' \| 'cascade'` | `'pile'` | pile scatters the cards; fan opens them to both sides; cascade steps each one back. |
| `rotate` | `data-dio-rotate` | `number` 0 to 30° | `6` | Largest angle of a card under the top one; pile and fan only. |
| `offset` | `data-dio-offset` | `number` 0 to 60 px | `12` | Scatter of the pile, step of the cascade, depth of the fan, in px. |
| `duration` | `data-dio-duration` | `number` 0.2 to 2 s | `0.6` | Length of a throw by click or key. |
| `shadow` | `data-dio-shadow` | `number` 0 to 1 | `0.35` | Darkness of the shadow under each card, 0 for none. |
| `loop` | `data-dio-loop` | `boolean` | `true` | A thrown card goes under the pile; off, the last card stays on top. |
| `swipe` | `data-dio-swipe` | `boolean` | `true` | Dragging or swiping the top card throws it. |
| `click` | `data-dio-click` | `boolean` | `true` | A click on the pile throws the top card. |
| `items` (rebuilds) | `data-dio-items` | `string` | `'img'` | Selector of the images inside the container. |
| `seed` | `data-dio-seed` | `number` 1 to 100 | `1` | Arrangement of the scattered pile. |

Changing a param marked rebuilds re-runs the effect setup.

### Variants

- `style`: `pile`, `fan`, `cascade`
  - `pile` scatters the cards.
  - `fan` opens them to both sides.
  - `cascade` steps each one back.

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`opacity`](/docs/api/inputs#opacity), [`reducedMotion`](/docs/api/inputs#input-reducedMotion), [`size`](/docs/api/inputs#size), [`width`](/docs/api/inputs#input-width). Draws only while something on it moves, then stops. Only while a card is in flight or held; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it shows a still frame: motion inputs are zero and time stands still. Cards change at once: no flight and no card following the finger, while a swipe, a click or a key still brings the next one.

Runs on every device. A tap throws the top card. Dragging it needs touch-action: pan-y or none on the container, so the page keeps its own gestures otherwise.

```ts
import { start } from '@dioramajs/diorama'

start({ effects: { stack: { reducedMotion: 'off' } } })
```

## Cost and quality

Loads 5.1 KB the first time a page uses it. GPU: Up to five card quads with a picture and a shadow each, one texture read per pixel. Memory: Up to six pictures at a time: the top card, the pile under it and the card thrown last.

See [the quality plugin](/docs/effects/quality).

## Recipes

### A fan of projects that stops at the last one

The `fan` style opens the cards under the top one to both sides, like a hand of cards, for a short list such as featured work or this week’s picks. With `loop` set to false the last card stays on top: a drag only nudges it and a click does nothing.

Four project covers fanned out; each click throws the top one off until the last.

```html title="index.html"
<section class="covers" data-dio="stack" data-dio-style="fan" data-dio-loop="false" data-dio-rotate="7" tabindex="0" aria-label="Four projects. Click or use the arrow keys for the next one." style="touch-action: pan-y">
  <img src="/media/work/cyanotype.webp" alt="Cyanotype: prints of seaweed in deep blue" width="480" height="384" />
  <img src="/media/work/atlas-of-ice.webp" alt="Atlas of Ice: maps of the polar shelves" width="480" height="384" />
  <img src="/media/work/hares-fur.webp" alt="Hare’s Fur: glazed tea bowls" width="480" height="384" />
  <img src="/media/work/polar-night.webp" alt="Polar Night: photographs of the winter dark" width="480" height="384" />
</section>
```

### Name the card on top

Nothing in the page tells a screen reader that another card came to the top. Put a caption with `aria-live="polite"` next to the pile and fill it from `dio:index`, whose `index` is the place of the card on top, from 0. The example at the top of this page names the place this way.

```html
<section
  class="trips"
  data-dio="stack"
  tabindex="0"
  aria-label="Places on the coast. Click, swipe or use the arrow keys for the next one."
  style="touch-action: none"
>
  <img src="/media/coast/headland-800.avif" />
  <img src="/media/coast/islets-600.avif" />
</section>
<p class="place" aria-live="polite">The headland</p>
```

```ts
const trips = document.querySelector('.trips')
const place = document.querySelector('.place')
const cards = [...trips.querySelectorAll('img')]

trips.addEventListener('dio:index', (e) => {
  place.textContent = cards[e.detail.index].alt.split(':')[0]
})
```

### Buttons for the next and the previous card

A pile reads as something to swipe, but a button says it plainly and works for everyone. Set `index` on the instance: one more throws the top card off, one less brings the last thrown card back onto the pile.

```ts
const trips = document.querySelector('.trips')

document.querySelector('.next').addEventListener('click', () => {
  const stack = app.effects.get(trips, 'stack')
  stack?.set({ index: Math.round(stack.params.index) + 1 })
})
```

## Notes

The container is the size of one card: the top card covers its box, and the cards under it, their shadows and a thrown card reach past it. Leave some room around the box, about `offset` times three plus the corners a turned card shows, and keep captions and buttons outside it. A thrown card is cut by an ancestor with `overflow: hidden`, as a CSS transform would be.

Each card takes the picture of its image cut to the box, at the image’s own `object-position`; `object-fit: contain` on an image keeps it whole on a plain card. The corners follow the container’s `border-radius`.

The images stay in the page in their order, each with its alt text, so a screen reader reaches all of them, and without WebGL the container is the strip you styled. With `tabindex="0"` on the container the pile takes focus and changes with the arrow keys, PageUp, PageDown, Home and End; give it an `aria-label` that says what is in it and how to move on.

A drag with a mouse always carries the card. On touch screens it needs `touch-action` on the container: `none` lets the card go any way, `pan-y` keeps the page scrolling up and down while sideways drags carry the card.

`style` and `items` rebuild the effect when they change. Set them in the markup rather than switching them while the pile is on screen.

## Related

- [carousel](/docs/effects/carousel.md): Pictures on a 3D ring that turns with a drag, the keys, the page scroll or by itself, the front one enlarged.
- [coverflow](/docs/effects/coverflow.md): A row of covers as the classic cover flow: the middle one faces you, its neighbours turn away in perspective.
- [flipbook](/docs/effects/flipbook.md): The page images of a container become a book whose pages bend as they turn.
- [gallery](/docs/effects/gallery.md): A container of images shows one at a time and changes them with a displacement, a slide, a crossfade or a zoom.
- [hover-preview](/docs/effects/hover-preview.md): Over a list of projects, the picture of the item under the pointer follows the pointer, stands beside the item or holds a fixed place.
- [lightbox](/docs/effects/lightbox.md): A click opens an image full screen: its copy flies out of the grid, pages and zooms.
- [polaroids](/docs/effects/polaroids.md): Photos scattered like prints on a table: drag one to the top, click it to hold it up large.
- [ambient](/docs/effects/ambient.md): A soft glow in the colors of the frame around a video or an image, like ambient mode.
- [Reduced motion](/docs/guides/reduced-motion.md)
