← All items

Page transition

effect · page-transition · navigation · T1 · v1.0.0 · ready

About

Wraps the page. Navigating through it raises a coloured band from the bottom pulling a dark screen over the page, while marked headings drop their letters; the host's update (a route change, new content) runs behind the screen while a logo fades in, for at least a minimum hold and until the update finishes; then the screen lifts off the top, a second band trailing, and the new page shows. Without motion the update runs at once.

Use the button on each page to navigate.

Layers

Native
No gated feature: supported everywhere.
Static
No curtain: the update runs at once.

Skin

VariableDefaultMeaning
--page-transition-band#e7000bColour of the leading and trailing bands.
--page-transition-screen#0a0a0aColour of the screen between the bands, behind the logo.
--page-transition-mark-color#fafafaText colour of the logo slot (SVG logos drawn in currentColor take it).
--page-transition-band-height25vhHeight of each band.
--page-transition-duration700msLength of the cover and of the uncover, each.
--page-transition-easingcubic-bezier(0.65, 0, 0.35, 1)Easing of the cover and of the uncover.
--page-transition-hold600msMinimum time the screen stays, while the logo fades in.
--page-transition-letter-duration400msHow long one letter takes to drop out of view.
--page-transition-letter-stagger30msDelay between consecutive letters.
--page-transition-z-index1000Stacking level of the curtain; above everything on the page.

Accessibility

Static layer: no curtain; the update runs at once.

The curtain is hidden from assistive technology; ExitLetters exposes its text once, unsplit.

Sources

  • reel · thedesignshop.tds · "Page Transitions" reel; technique only (P6).

Code

Download zip

Self-contained: copy every file, keeping the folder structure.

context.ts
// web-effects page-transition 1.0.0 · MIT-0
import { createContext, useContext } from "react";

/** Runs `update` (swap content, push a route) while the page is covered. */
export type Navigate = (update: () => void | Promise<void>) => Promise<void>;

export const NavigateContext = createContext<Navigate | null>(null);

/** The `navigate` function of the enclosing `<PageTransition>`. */
export function usePageTransition(): Navigate {
  const navigate = useContext(NavigateContext);
  if (!navigate)
    throw new Error("usePageTransition needs an enclosing <PageTransition>.");
  return navigate;
}
exit-letters.tsx
// web-effects page-transition 1.0.0 · MIT-0
import type { CSSProperties } from "react";
import { splitLetters } from "./letters";
import mechanism from "./mechanism.module.css";

/**
 * Text whose letters drop out of view when a transition starts. Place it inside a
 * `<PageTransition>` (inside a heading, for example); assistive technology reads the plain text.
 */
export function ExitLetters({ children }: { children: string }) {
  return (
    <>
      <span className={mechanism.visuallyHidden}>{children}</span>
      <span aria-hidden="true">
        {splitLetters(children).map((token, i) =>
          token.kind === "space" ? (
            token.text
          ) : (
            <span key={i} className={mechanism.word}>
              {token.letters.map(({ char, index }) => (
                <span
                  key={index}
                  className={mechanism.letter}
                  style={{ "--_page-transition-index": index } as CSSProperties}
                >
                  {char}
                </span>
              ))}
            </span>
          ),
        )}
      </span>
    </>
  );
}
letters.ts
// web-effects page-transition 1.0.0 · MIT-0
// Splits text into words and spaces for the letter exit. Words keep their letters together on
// one line; each letter carries its position in the whole text, for the stagger. Pure.

export type LetterToken =
  | { kind: "word"; letters: { char: string; index: number }[] }
  | { kind: "space"; text: string };

export function splitLetters(text: string): LetterToken[] {
  let index = 0;
  return (text.match(/\s+|\S+/g) ?? []).map((part) =>
    /^\s/.test(part)
      ? { kind: "space", text: part }
      : {
          kind: "word",
          // Code points, not UTF-16 units, so letters outside the BMP stay whole.
          letters: Array.from(part, (char) => ({ char, index: index++ })),
        },
  );
}
mechanism.module.css
/* web-effects page-transition 1.0.0 · MIT-0 */
/* Mechanism of the page transition (D2.8, D3.11), shared by both styling variants.
   The hook sets data-phase on .root: exit → hold → reveal, then removes it. .panel is a fixed
   column [band | screen | band], one viewport plus two bands tall, starting just below the
   viewport. exit: it rises until the screen covers the viewport (letters inside .root fall at the
   same time); hold: it rests while the host's update runs and the mark fades in; reveal: it rises
   off the top, the second band trailing, uncovering the new content. Translations are in % of
   the panel's own height H = viewport + 2 bands: covering is −H + band, gone is −2H + 2 bands.
   L1: CSS animations, run by every supported browser: no feature gate, no JS layer (D3.6c).
   L3 (base rules): no overlay and no animation, so the hook's waits resolve at once. */
.root {
  --_page-transition-band: var(--page-transition-band, #e7000b);
  --_page-transition-screen: var(--page-transition-screen, #0a0a0a);
  --_page-transition-mark-color: var(--page-transition-mark-color, #fafafa);
  --_page-transition-band-height: var(--page-transition-band-height, 25vh);
  --_page-transition-duration: var(--page-transition-duration, 700ms);
  --_page-transition-easing: var(
    --page-transition-easing,
    cubic-bezier(0.65, 0, 0.35, 1)
  );
  --_page-transition-hold: var(--page-transition-hold, 600ms);
  --_page-transition-letter-duration: var(
    --page-transition-letter-duration,
    400ms
  );
  --_page-transition-letter-stagger: var(
    --page-transition-letter-stagger,
    30ms
  );
  --_page-transition-z-index: var(--page-transition-z-index, 1000);
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: var(--_page-transition-z-index);
  display: none;
  overflow: hidden;
  pointer-events: none;
}

.panel {
  position: absolute;
  top: 100%;
  right: 0;
  left: 0;
  display: flex;
  flex-direction: column;
  height: calc(100% + 2 * var(--_page-transition-band-height));
}

.band {
  flex: none;
  height: var(--_page-transition-band-height);
  background: var(--_page-transition-band);
}

.screen {
  display: grid;
  flex: 1;
  place-items: center;
  background: var(--_page-transition-screen);
  color: var(--_page-transition-mark-color);
}

.mark {
  opacity: 0;
}

/* Letter exit: each word clips its letters, which drop below the line one after another. The
   padding and negative margin leave room for descenders inside the clip. */
.word {
  display: inline-block;
  overflow: hidden;
  padding-block: 0.1em;
  margin-block: -0.1em;
  vertical-align: top;
  white-space: nowrap;
}

.letter {
  display: inline-block;
}

.visuallyHidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@keyframes cover {
  from {
    translate: 0 0;
  }
  to {
    translate: 0 calc(-100% + var(--_page-transition-band-height));
  }
}

@keyframes uncover {
  from {
    translate: 0 calc(-100% + var(--_page-transition-band-height));
  }
  to {
    translate: 0 calc(-200% + 2 * var(--_page-transition-band-height));
  }
}

@keyframes mark-in {
  from {
    translate: 0 1rem;
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fall {
  to {
    translate: 0 110%;
  }
}

/* L1 native (gated on the override attribute, D3.6a). */
:root:not([data-fx-layer]) .root[data-phase] .overlay,
:root[data-fx-layer="native"] .root[data-phase] .overlay {
  display: block;
  pointer-events: auto;
}

:root:not([data-fx-layer]) .root[data-phase="exit"] .panel,
:root[data-fx-layer="native"] .root[data-phase="exit"] .panel {
  animation: cover var(--_page-transition-duration)
    var(--_page-transition-easing) forwards;
}

:root:not([data-fx-layer]) .root[data-phase="hold"] .panel,
:root[data-fx-layer="native"] .root[data-phase="hold"] .panel {
  translate: 0 calc(-100% + var(--_page-transition-band-height));
}

:root:not([data-fx-layer]) .root[data-phase="hold"] .mark,
:root[data-fx-layer="native"] .root[data-phase="hold"] .mark {
  animation: mark-in var(--_page-transition-hold) ease-out forwards;
}

:root:not([data-fx-layer]) .root[data-phase="reveal"] .panel,
:root[data-fx-layer="native"] .root[data-phase="reveal"] .panel {
  animation: uncover var(--_page-transition-duration)
    var(--_page-transition-easing) forwards;
}

:root:not([data-fx-layer]) .root[data-phase="reveal"] .mark,
:root[data-fx-layer="native"] .root[data-phase="reveal"] .mark {
  opacity: 1;
}

:root:not([data-fx-layer]) .root[data-phase="exit"] .letter,
:root[data-fx-layer="native"] .root[data-phase="exit"] .letter {
  animation: fall var(--_page-transition-letter-duration) ease-in forwards;
  animation-delay: calc(
    var(--_page-transition-index) * var(--_page-transition-letter-stagger)
  );
}

/* L3 under reduced motion (unless forced): no overlay, nothing moves. */
@media (prefers-reduced-motion: reduce) {
  :root:not([data-fx-layer]) .root[data-phase] .overlay {
    display: none;
  }

  /* Same specificity as the native rules above, so these win by order. */
  :root:not([data-fx-layer]) .root[data-phase] .panel,
  :root:not([data-fx-layer]) .root[data-phase] .mark,
  :root:not([data-fx-layer]) .root[data-phase] .letter {
    animation: none;
  }
}
page-transition.tsx
// web-effects page-transition 1.0.0 · MIT-0
"use client";

import type { ReactNode } from "react";
import { NavigateContext } from "./context";
import mechanism from "./mechanism.module.css";
import { usePhases } from "./use-phases";

// The provider and its overlay, shared by both styling variants (P8): the effect has no
// presentation of its own (skin, logo and content come from the host).

export function PageTransitionRoot({
  children,
  logo,
  className,
}: {
  children: ReactNode;
  logo?: ReactNode;
  className: string;
}) {
  const { navigate, rootRef, contentRef, panelRef, markRef } = usePhases();
  return (
    <NavigateContext value={navigate}>
      <div ref={rootRef} className={`${mechanism.root} ${className}`}>
        <div ref={contentRef}>{children}</div>
        <div aria-hidden="true" className={mechanism.overlay}>
          <div ref={panelRef} className={mechanism.panel}>
            <div className={mechanism.band} />
            <div className={mechanism.screen}>
              <div ref={markRef} className={mechanism.mark}>
                {logo}
              </div>
            </div>
            <div className={mechanism.band} />
          </div>
        </div>
      </div>
    </NavigateContext>
  );
}
tailwind/PageTransition.tsx
// web-effects page-transition 1.0.0 · MIT-0
"use client";

import type { ReactNode } from "react";
import { PageTransitionRoot } from "../page-transition";

export { usePageTransition } from "../context";
export { ExitLetters } from "../exit-letters";

/**
 * Covers the page while content changes: `usePageTransition()` inside it returns
 * `navigate(update)`, which raises a band-led curtain, runs `update` (it may return a promise)
 * behind it with `logo` showing, then uncovers the new content. Wrap text in `<ExitLetters>` to
 * make its letters fall as the curtain rises.
 */
export function PageTransition({
  children,
  logo,
  className = "",
}: {
  children: ReactNode;
  logo?: ReactNode;
  className?: string;
}) {
  return (
    <PageTransitionRoot logo={logo} className={className}>
      {children}
    </PageTransitionRoot>
  );
}
use-phases.ts
// web-effects page-transition 1.0.0 · MIT-0
import { useCallback, useRef } from "react";
import type { Navigate } from "./context";

// Behaviour shared by both styling variants (P8): the phase sequence. The phase is a DOM
// attribute, not React state, so each wait can read the animations its phase starts right away
// (getAnimations() applies pending style changes first). CSS owns all timing: on the static
// layer no animation runs and every wait resolves at once, so `update` runs immediately.

const finished = (element: Element) =>
  Promise.allSettled(element.getAnimations().map((a) => a.finished));

export function usePhases() {
  const rootRef = useRef<HTMLDivElement>(null);
  const contentRef = useRef<HTMLDivElement>(null);
  const panelRef = useRef<HTMLDivElement>(null);
  const markRef = useRef<HTMLDivElement>(null);

  const navigate: Navigate = useCallback(async (update) => {
    const [root, content, panel, mark] = [
      rootRef.current,
      contentRef.current,
      panelRef.current,
      markRef.current,
    ];
    // One transition at a time; navigations during one are ignored.
    if (!root || !content || !panel || !mark || root.dataset.phase) return;
    content.inert = true;
    try {
      root.dataset.phase = "exit";
      await finished(panel);
      root.dataset.phase = "hold";
      // The new content must be ready and the mark shown for its minimum hold.
      await Promise.all([update(), finished(mark)]);
    } finally {
      root.dataset.phase = "reveal";
      content.inert = false;
      await finished(panel);
      delete root.dataset.phase;
    }
  }, []);

  return { navigate, rootRef, contentRef, panelRef, markRef };
}