Split-flap display in React: a departure board that flips in 3D

Build a split-flap departure board in React: tiles that scramble through letters, a falling 3D flap on each change, staggered rows and a server-safe random stand-in.

6 min read

The real component. Click the picture to open it live and try it before you read how it was built.

Freight lives on departure boards, so our freight forwarding page does too. Every tile is a real split-flap: it scrambles through letters and lands.

A tile is two faces and a falling flap

Each tile prints its current letter underneath. When the letter changes, a lower flap carrying the new letter falls from flat to upright on its top hinge, in 3D and darkened while it falls. One flap falls in less than a frame of the scramble, so every letter is seen landing.

src/registry/landing-pages/transit/flap-tile.tsx

"use client";

import { motion } from "motion/react";
import { cn } from "@/lib/utils";

// One flap falls in less than a frame of the scramble, so every letter is seen landing.
const FALL_S = 0.09;

interface FlapTileProps {
  char: string;
  // False for reduced motion: the letter changes without a flap falling.
  isAnimated: boolean;
  className?: string;
}

// One tile of the board. Each time its letter changes, a lower flap carrying the new letter falls from flat
// to upright on its top hinge, in 3D and darkened while it falls, over the letter already printed underneath.
export function FlapTile({ char, isAnimated, className }: FlapTileProps) {
  return (
    <span
      className={cn(
        "relative inline-grid place-items-center rounded-sm bg-zinc-900 text-yellow-300 perspective-near after:absolute after:inset-x-0 after:top-1/2 after:z-10 after:h-px after:bg-black/60",
        className,
      )}
    >
      {char}
      {isAnimated && (
        <motion.span
          key={char}
          initial={{ rotateX: 90, filter: "brightness(0.4)" }}
          animate={{ rotateX: 0, filter: "brightness(1)" }}
          transition={{ duration: FALL_S, ease: "easeIn" }}
          className="absolute inset-x-0 bottom-0 h-1/2 origin-top overflow-hidden rounded-b-sm bg-inherit backface-hidden"
        >
          <span className="absolute inset-x-0 -top-full bottom-0 grid place-items-center">{char}</span>
        </motion.span>
      )}
    </span>
  );
}
One flap tile

The scramble

A word flips through stand-in letters before landing, left to right, each tile a few frames after the one before. The stand-ins come from a seeded function of the tile and frame, so the server and the browser render the same letters and nothing flickers on hydration.

src/registry/landing-pages/transit/split-flap.tsx

"use client";

import { useEffect, useState } from "react";
import { cn } from "@/lib/utils";
import { usePrefersReducedMotion } from "@/hooks/use-prefers-reduced-motion";
import { FlapTile } from "./flap-tile";

const CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const FRAME_MS = 55;
// Flips before the first letter lands, and how many more each next letter takes.
const FIRST_SETTLE = 6;
const SETTLE_STEP = 2;

// A stable stand-in letter for a tile on a given frame, the same on the server and in the browser.
function scrambled(index: number, frame: number): string {
  const value = Math.sin(index * 91.7 + frame * 13.3) * 4375.85;
  return CHARSET[Math.floor((value - Math.floor(value)) * CHARSET.length)];
}

interface FlapRowProps {
  text: string;
  tileClassName?: string;
  // Frames to wait before flipping starts, to stagger several rows.
  delayFrames: number;
}

// The tiles of one text: each flips through letters, then lands on its own, left to right.
function FlapRow({ text, tileClassName, delayFrames }: FlapRowProps) {
  const shouldReduceMotion = usePrefersReducedMotion();
  const [frame, setFrame] = useState(-delayFrames);
  const lastFrame = FIRST_SETTLE + text.length * SETTLE_STEP;
  const isDone = shouldReduceMotion || frame >= lastFrame;

  useEffect(() => {
    if (isDone) return;
    const timer = window.setInterval(() => setFrame((current) => current + 1), FRAME_MS);
    return () => window.clearInterval(timer);
  }, [isDone]);

  return [...text].map((char, index) => {
    if (char === " ") return <span key={index} className="w-2 sm:w-3" />;
    const hasLanded = isDone || frame >= FIRST_SETTLE + index * SETTLE_STEP;
    return (
      <FlapTile
        key={index}
        char={hasLanded ? char : scrambled(index, frame)}
        isAnimated={!shouldReduceMotion}
        className={tileClassName}
      />
    );
  });
}

interface SplitFlapProps {
  text: string;
  // Sizes and colours each tile: its padding, text size and faces.
  tileClassName?: string;
  delayFrames?: number;
  // Changing it flips the same text again, as on hover.
  flipKey?: number;
  className?: string;
}

// Text on a split-flap departure board. Whenever the text changes, its tiles flip through letters and land
// on the new one; for reduced motion the new text just appears. Screen readers get the text itself.
export function SplitFlap({ text, tileClassName, delayFrames = 0, flipKey = 0, className }: SplitFlapProps) {
  return (
    <span className={cn("inline-flex flex-wrap gap-0.5 font-martian uppercase", className)}>
      <span className="sr-only">{text}</span>
      <span aria-hidden className="contents">
        <FlapRow key={`${text}-${flipKey}`} text={text} tileClassName={tileClassName} delayFrames={delayFrames} />
      </span>
    </span>
  );
}
A word of tiles, staggered

Reduced motion

With reduced motion the letter simply changes, with no falling flap and no scramble.

More articles