← Components

Communication

Flip board

Split-flap characters for counters and headlines. When the value changes, each cell flips through the characters in between, staggered left to right.

Examples and variants

Departures

Press Next status to cascade.

ON TIME
Example
function FlipDepartures() {
  const [index, setIndex] = useState(0)
  return (
    <div className="flex flex-col items-center gap-stack-sm">
      <FlipBoard value={departures[index]} length={8} />
      <Button size="sm" variant="secondary" onClick={() => setIndex((i) => (i + 1) % departures.length)}>
        Next status
      </Button>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/flip-board.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/flip-board.tsxflip-board
      components/ui/flip-board.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      import { motionLevel } from "@/lib/overlay"
      import { tokenMs } from "@/lib/motion/token-ms"
      
      /*
        Flip board: split-flap characters for counters and headlines. When `value`
        changes, each cell flips through the characters between its old and new
        letter, staggered left to right, so a cascade runs across the board.
        Calm flips at most 3 times per cell; still swaps the text. Screen readers
        get the final text only.
      */
      const CHARSET = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,:-!?&/%"
      
      function steps(from: string, to: string, max: number) {
        const a = Math.max(0, CHARSET.indexOf(from))
        const b = CHARSET.indexOf(to)
        if (b === -1) return [to]
        const path: string[] = []
        for (let i = (a + 1) % CHARSET.length; ; i = (i + 1) % CHARSET.length) {
          path.push(CHARSET[i])
          if (i === b) break
        }
        // Keep the last `max` flips so long distances don't take forever.
        return path.slice(-max)
      }
      
      function FlipBoard({
        value,
        length,
        className,
      }: {
        value: string
        /** Fixed number of cells; shorter values are padded with spaces. Defaults to the value's length. */
        length?: number
        className?: string
      }) {
        const ref = React.useRef<HTMLSpanElement>(null)
        const size = Math.max(length ?? 0, value.length)
        const target = value.toUpperCase().padEnd(size, " ")
        const [cells, setCells] = React.useState(() => target.split(""))
        const shown = React.useRef(cells)
      
        React.useEffect(() => {
          const level = motionLevel(ref.current)
          const timers: number[] = []
          const max = level === "calm" ? 3 : 10
          const step = tokenMs("--flip-step", ref.current ?? undefined)
          const stagger = tokenMs("--flip-stagger", ref.current ?? undefined)
          const set = (index: number, char: string) => {
            const next = [...shown.current]
            while (next.length < target.length) next.push(" ")
            next.length = target.length
            next[index] = char
            shown.current = next
            setCells(next)
          }
          target.split("").forEach((char, index) => {
            const from = shown.current[index] ?? " "
            if (from === char) return
            const path = level === "still" ? [char] : steps(from, char, max)
            path.forEach((c, n) => {
              timers.push(window.setTimeout(() => set(index, c), level === "still" ? 0 : index * stagger + n * step))
            })
          })
          if (shown.current.length > target.length) timers.push(window.setTimeout(() => set(0, shown.current[0] ?? " "), 0))
          return () => timers.forEach((id) => window.clearTimeout(id))
        }, [target])
      
        return (
          <span ref={ref} data-slot="flip-board" className={cn("inline-flex gap-(--flip-gap)", className)}>
            <span className="sr-only" aria-live="polite">
              {value}
            </span>
            {cells.map((char, index) => (
              <span
                key={index}
                aria-hidden
                className="flip-cell relative inline-grid h-(--flip-cell-height) w-(--flip-cell-width) place-items-center overflow-hidden rounded-(--flip-corner) bg-(--flip-container) font-mono text-title text-(--flip-label) [perspective:calc(var(--flip-cell-height)*4)]"
              >
                <span key={char + index} className="flip-char">
                  {char === " " ? " " : char}
                </span>
                <span className="pointer-events-none absolute inset-x-0 top-1/2 h-(--stroke-width-thin) bg-(--flip-seam)" />
              </span>
            ))}
          </span>
        )
      }
      
      export { FlipBoard }
      

Imports

What the examples on this page import:

Imports
import { Button } from "@/components/ui/button"
import { CardStack } from "@/components/ui/card-stack"
import { Confetti } from "@/components/ui/confetti"
import { Dice } from "@/components/ui/dice"
import { FlipBoard } from "@/components/ui/flip-board"
import { Metaballs } from "@/components/ui/metaballs"
import { tokenMs } from "@/lib/motion/token-ms"

Anatomy

  1. 1Cell · --flip-cell-width, --flip-cell-height, --flip-corner, --flip-container, --flip-label.
  2. 2Seam · --flip-seam across the middle.

Props

PropTypeDefaultDescription
valuestring—Text to show; upper-cased.
lengthnumber—Fixed cell count; pads with spaces.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--flip-cell-widthvar(--space-8)32pxsame
--flip-cell-heightvar(--space-12)48pxsame
--flip-gapvar(--space-1)4pxsame
--flip-cornervar(--shape-xs)6pxsame
--flip-containervar(--surface-inverse)oklch(0.22 0.01 260)oklch(0.95 0 0)
--flip-labelvar(--on-surface-inverse)oklch(0.98 0.004 95)oklch(0.14 0 0)
--flip-seamcolor-mix(in oklab, var(--surface-inverse), var(--on-surface-inverse) 18%)color-mix(in oklab, var(--surface-inverse), var(--on-surface-inverse) 18%)same
--flip-stepvar(--duration-micro)90mssame
--flip-staggervar(--duration-stagger)40mssame

Motion

TriggerWhat happensTokens
value changesUp to 10 flips per cell, one per --flip-step, cells start --flip-stagger apart. Calm: 3 flips. Still: text swap.--flip-step, --flip-stagger

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Only the final text is announced; the cells are hidden from screen readers.

Usage

Do

  • Use for short statuses and counters.

Don’t

  • Don't use for long text or values that change every second.

Code

components/ui/flip-board.tsx
"use client"

import * as React from "react"
import { cn } from "cn"
import { motionLevel } from "@/lib/overlay"
import { tokenMs } from "@/lib/motion/token-ms"

/*
  Flip board: split-flap characters for counters and headlines. When `value`
  changes, each cell flips through the characters between its old and new
  letter, staggered left to right, so a cascade runs across the board.
  Calm flips at most 3 times per cell; still swaps the text. Screen readers
  get the final text only.
*/
const CHARSET = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,:-!?&/%"

function steps(from: string, to: string, max: number) {
  const a = Math.max(0, CHARSET.indexOf(from))
  const b = CHARSET.indexOf(to)
  if (b === -1) return [to]
  const path: string[] = []
  for (let i = (a + 1) % CHARSET.length; ; i = (i + 1) % CHARSET.length) {
    path.push(CHARSET[i])
    if (i === b) break
  }
  // Keep the last `max` flips so long distances don't take forever.
  return path.slice(-max)
}

function FlipBoard({
  value,
  length,
  className,
}: {
  value: string
  /** Fixed number of cells; shorter values are padded with spaces. Defaults to the value's length. */
  length?: number
  className?: string
}) {
  const ref = React.useRef<HTMLSpanElement>(null)
  const size = Math.max(length ?? 0, value.length)
  const target = value.toUpperCase().padEnd(size, " ")
  const [cells, setCells] = React.useState(() => target.split(""))
  const shown = React.useRef(cells)

  React.useEffect(() => {
    const level = motionLevel(ref.current)
    const timers: number[] = []
    const max = level === "calm" ? 3 : 10
    const step = tokenMs("--flip-step", ref.current ?? undefined)
    const stagger = tokenMs("--flip-stagger", ref.current ?? undefined)
    const set = (index: number, char: string) => {
      const next = [...shown.current]
      while (next.length < target.length) next.push(" ")
      next.length = target.length
      next[index] = char
      shown.current = next
      setCells(next)
    }
    target.split("").forEach((char, index) => {
      const from = shown.current[index] ?? " "
      if (from === char) return
      const path = level === "still" ? [char] : steps(from, char, max)
      path.forEach((c, n) => {
        timers.push(window.setTimeout(() => set(index, c), level === "still" ? 0 : index * stagger + n * step))
      })
    })
    if (shown.current.length > target.length) timers.push(window.setTimeout(() => set(0, shown.current[0] ?? " "), 0))
    return () => timers.forEach((id) => window.clearTimeout(id))
  }, [target])

  return (
    <span ref={ref} data-slot="flip-board" className={cn("inline-flex gap-(--flip-gap)", className)}>
      <span className="sr-only" aria-live="polite">
        {value}
      </span>
      {cells.map((char, index) => (
        <span
          key={index}
          aria-hidden
          className="flip-cell relative inline-grid h-(--flip-cell-height) w-(--flip-cell-width) place-items-center overflow-hidden rounded-(--flip-corner) bg-(--flip-container) font-mono text-title text-(--flip-label) [perspective:calc(var(--flip-cell-height)*4)]"
        >
          <span key={char + index} className="flip-char">
            {char === " " ? " " : char}
          </span>
          <span className="pointer-events-none absolute inset-x-0 top-1/2 h-(--stroke-width-thin) bg-(--flip-seam)" />
        </span>
      ))}
    </span>
  )
}

export { FlipBoard }