# unfold

A headline unfolds out of the page like paper: strips, a zig-zag band, letter cards or folded squares open in 3D.

The text block is cut into pieces of paper hinged to each other. Before it opens you see the folded packet: off-white paper backs with a faint fiber, the letters showing through a little. As it comes into view the pieces swing open one after another in perspective, standing out of the page on the way: the side turned to the light brightens, folds darken toward the crease, and the raised paper casts a soft shadow on the page. Strips roll down line by line like a letter out of an envelope, an accordion stretches open sideways, each letter of cards stands up like a little card on its bottom edge, and origami opens squares folded corner to center. Once everything lies flat the heading is plain text again.

Module `text`, category Text. Import `@dioramajs/text/unfold`. [Source](https://github.com/MasterKurosava/diorama/blob/main/packages/text/src/unfold.ts)

A letterpress studio's headline that unrolls out of a folded paper packet line by line, each strip swinging down in perspective with its shadow on the page.

```html title="index.html"
<div class="fd-hero">
  <p class="fd-kicker">Mailbox Press · Porto</p>
  <h2 class="fd-headline" data-dio="unfold">Letters from the coast, folded by hand</h2>
  <p class="fd-line">Cards and notes printed on cotton paper, posted the same week.</p>
</div>
```

## Usage

Put it on: a heading or short text block in a large font. Not img, video, canvas, svg or form fields.

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

```html
<h1 class="hero" data-dio="unfold">Letters from the coast, folded by hand</h1>
```

In page transitions: Leaving a page folds the paper back up, entering one unfolds it; at rest it leaves as text.

## Params

| Name | Attribute | Type | Default | Description |
| --- | --- | --- | --- | --- |
| `style` (rebuilds) | `data-dio-style` | `'strips' \| 'accordion' \| 'cards' \| 'origami'` | `'strips'` | strips rolls down; accordion stretches; cards stands letters up; origami opens squares. |
| `trigger` (rebuilds) | `data-dio-trigger` | `'view' \| 'scroll' \| 'hover'` | `'view'` | view opens it once in view; scroll scrubs with the page; hover folds a little under the pointer. |
| `bands` (rebuilds) | `data-dio-bands` | `number \| 'auto'` 1 to 24 | `'auto'` | Strips, pleats or rows of squares; auto cuts one per line, or a pleat per two ems. |
| `angle` | `data-dio-angle` | `number` 30 to 180° | `180` | How far each piece is folded before it opens; 180 lies folded flat. |
| `duration` | `data-dio-duration` | `number` 0.3 to 2.5 s | `0.9` | Time one piece takes to open. |
| `stagger` | `data-dio-stagger` | `number` 0 to 1 s | `0.3` | Time between neighboring pieces, shortened so all start within 1.4 durations. |
| `paper` | `data-dio-paper` | `color` | `'#f8f5ee'` | Color of the backs of the pieces, the paper seen while folded. |
| `shade` | `data-dio-shade` | `number` 0 to 1 | `0.7` | Strength of the shading on the faces and at the folds; 0 keeps flat colors. |
| `delay` | `data-dio-delay` | `number` 0 to 2 s | `0` | Wait between the trigger and the first fold. |
| `threshold` | `data-dio-threshold` | `number` 0 to 1 | `0.2` | Share of the heading in view that opens it with the view trigger, or starts the scrub. |

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

### Variants

- `style`: `strips`, `accordion`, `cards`, `origami`
  - `strips` rolls down.
  - `accordion` stretches.
  - `cards` stands letters up.
  - `origami` opens squares.
- `trigger`: `view`, `scroll`, `hover`
  - `view` opens it once in view.
  - `scroll` scrubs with the page.
  - `hover` folds a little under the pointer.

## Inputs and frames

Reads [`height`](/docs/api/inputs#input-height), [`presence`](/docs/api/inputs#presence), [`progress`](/docs/api/inputs#progress), [`size`](/docs/api/inputs#size), [`y`](/docs/api/inputs#input-y). Draws only while something on it moves, then stops. While the paper opens, and with the scroll trigger while scrolling through it; with the hover trigger while the pointer moves over it and the folds settle; none at rest. See [how a frame is decided](/docs/concepts/frames).

## Reduced motion and touch

With reduced motion it is off and the element shows as plain HTML. Off: the heading stays flat text.

Runs on every device. With trigger hover a tap folds the pieces under the finger for a moment.

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

start({ effects: { unfold: { reducedMotion: 'static' } } })
```

## Cost and quality

Loads 13.1 KB the first time a page uses it. GPU: Three instanced draws of a quad per piece (lying flat, raised, and their shadows) while the paper moves; nothing at rest. Memory: A canvas of the text at twice its CSS size with mip levels and a 256 px tile of paper fiber.

- low: The text is rastered at 1 device pixel per CSS pixel instead of 2.

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

## Recipes

### Letter cards for an event title

`style="cards"` gives every letter its own little card, hinged at the bottom of its line. Before it opens each card lies face down below its line, showing the paper back with the letter faintly through it; then the cards flip up one after another, standing out of the page halfway and casting a shadow, and land as the title. `paper` sets the color of the backs: a warm cream reads as card stock.

A pop-up book exhibition called Paper Week, whose coral letters flip up on cream cards one after another.

```html title="index.html"
<div class="fd-show">
  <p class="fd-show-date">12 to 20 April · Old Tram Depot</p>
  <h2 class="fd-show-title" data-dio="unfold" data-dio-style="cards" data-dio-paper="#fff6dc">Paper Week</h2>
  <p class="fd-show-line">Pop-up books, paper engineering and a folding bar for kids.</p>
</div>
```

```ts title="script.ts"
import { onScreen, running } from '../../replay.ts'
import { play, settle } from '../_play.ts'

// Plays the heading again after a slider rests, and once if it was on screen at load.
export default function mount(example: HTMLElement): () => void {
  const heading = example.querySelector('[data-dio="unfold"]')
  if (!heading) return () => {}
  let timer = 0
  const onChange = () => {
    clearTimeout(timer)
    // A switch or `bands` rebuilds the effect: wait until it runs again.
    timer = window.setTimeout(
      () =>
        void running(heading, 'unfold').then(
          (ok) => ok && play(heading),
        ),
      300,
    )
  }
  example.addEventListener('doc-example:change', onChange)
  if (onScreen(heading))
    void running(heading, 'unfold').then((ok) => ok && play(heading))
  return () => {
    clearTimeout(timer)
    settle(heading)
    example.removeEventListener('doc-example:change', onChange)
  }
}
```

### An accordion sale banner

`style="accordion"` cuts the text into upright panels folded like a paper fan. Closed, the panels stand in a tight zig-zag with the letters squeezed between the folds; opening, they stretch apart sideways, the panels facing the light brighten and the others darken, until the banner lies flat. A heading with `text-align: center` opens from its middle, otherwise from the side it is aligned to. `bands` sets the number of panels.

A summer sale banner in black capitals on a yellow band that stretches open like an accordion.

```html title="index.html"
<div class="fd-sale">
  <h2 class="fd-sale-title" data-dio="unfold" data-dio-style="accordion" data-dio-paper="#fffdf4">Summer sale</h2>
  <p>Up to 40% off linen, until Sunday night.</p>
</div>
```

```ts title="script.ts"
import { onScreen, running } from '../../replay.ts'
import { play, settle } from '../_play.ts'

// Plays the heading again after a slider rests, and once if it was on screen at load.
export default function mount(example: HTMLElement): () => void {
  const heading = example.querySelector('[data-dio="unfold"]')
  if (!heading) return () => {}
  let timer = 0
  const onChange = () => {
    clearTimeout(timer)
    // A switch or `bands` rebuilds the effect: wait until it runs again.
    timer = window.setTimeout(
      () =>
        void running(heading, 'unfold').then(
          (ok) => ok && play(heading),
        ),
      300,
    )
  }
  example.addEventListener('doc-example:change', onChange)
  if (onScreen(heading))
    void running(heading, 'unfold').then((ok) => ok && play(heading))
  return () => {
    clearTimeout(timer)
    settle(heading)
    example.removeEventListener('doc-example:change', onChange)
  }
}
```

### Menu labels that fold under the pointer

With `data-dio-trigger="hover"` the text stays flat and real until the pointer moves over it; then the pieces nearest the pointer fold up a little toward the viewer and lie back down when it moves on. With cards that is a wave of letters following the pointer. On a phone a tap folds the letters under the finger for a moment.

```html
<nav>
  <a href="/shop"><span data-dio="unfold" data-dio-style="cards" data-dio-trigger="hover">Shop</span></a>
  <a href="/journal"><span data-dio="unfold" data-dio-style="cards" data-dio-trigger="hover">Journal</span></a>
</nav>
```

### Origami squares scrubbed by the scroll

`data-dio-trigger="scroll"` ties the opening to the scroll position: it starts when `threshold` of the heading is in view and is flat when the heading reaches the middle of the viewport, and it folds back up when you scroll back. With `style="origami"` each line becomes a row of squares as tall as the line, each folded corner to center.

```html
<h2 data-dio="unfold" data-dio-style="origami" data-dio-trigger="scroll">Studio Kami</h2>
```

## Notes

The paper is real geometry: each piece is a flat panel turning on its hinge in 3D, and the pieces that hang from it turn with it, seen with the perspective of the heading’s own box. The light comes from the top left. Faces turned toward it brighten and faces turned away darken, the inside of every fold darkens toward its crease, and the raised paper casts a soft shadow on the page that grows wider the higher the paper is. `shade` scales all of that shading; at 0 the paper keeps flat colors.

The fronts of the pieces take the color of the page behind the heading, so when they lie flat they disappear into it and only the letters remain. When the heading sits on a picture or a gradient instead, the fronts are clear and turn to paper only while they are lifted. Put a background on a wrapper rather than on the heading itself: while the paper is drawn the heading’s own background is hidden with its text.

The heading stays real text in the DOM: screen readers read it, search engines index it, and find in page works. A heading on the first screen is hidden until the folded paper is drawn and then opens; one further down waits folded until `threshold` of it is in view. Frames run only while the paper moves; once it lies flat the DOM text is back and nothing is drawn. Under reduced motion the effect is off and the heading stays flat text.

Leave some room around the heading: paper standing out of the page and its shadow reach past the heading’s box. The canvas cannot draw `background-clip: text`, `text-shadow`, `-webkit-text-stroke` or vertical writing modes the way the browser does: an element with any of them keeps its text as it is, and a warning in development names the reason. `style`, `mode` and `bands` rebuild the effect when they change; the other params change live.

## Related

- [assemble](/docs/effects/assemble.md): Thousands of lit particles fly out of the depth around the page and gather into a heading.
- [blinds](/docs/effects/blinds.md): A heading printed across real 3D slats that turn over in a wave, lit and casting shadows, to show it.
- [counter](/docs/effects/counter.md): Stats count up to their value on real 3D mechanics: odometer drums, slot machine reels or flip cards.
- [dust](/docs/effects/dust.md): A heading crumbles into fine sand that lifts off the page and drifts away in depth as it scrolls out, or on a click.
- [emboss](/docs/effects/emboss.md): Letters pressed into or raised out of paper, leather, brushed metal, stone or plaster, lit by a light that follows the pointer.
- [extrude](/docs/effects/extrude.md): A headline becomes a solid 3D block: beveled letters with lit side walls, seen in perspective, that turn toward the pointer.
- [fire](/docs/effects/fire.md): The letters burn: flames rise from their tops, sparks drift up in depth and the firelight flickers on the page.
- [hologram](/docs/effects/hologram.md): A heading becomes a 3D hologram: projected from a glowing emitter, built of slices in depth, crossed by scan lines.
- [Reduced motion](/docs/guides/reduced-motion.md)
- [Performance and quality](/docs/guides/performance.md)
