page flip.md

Page Flip

A book you turn by the corner. Drag a page over the spine or tap to turn it.

Installation

npx shadcn@latest add @kokonutui/page-flip

Props

Prop Type Default Description
pages? ReactNode[] built-in Pages in reading order. The first is the cover, alone on the right.
onSpreadChange? (spread: number) => void Called with the spread index after a page settles.
className? string Extra classes for the root element.

Source

"use client";

/**
 * @author: @dorianbaffier
 * @description: Page Flip
 * @version: 1.0.0
 * @date: 2026-10-21
 * @license: MIT
 * @website: https://kokonutui.com
 * @github: https://github.com/kokonut-labs/kokonutui
 */

import { animate, useReducedMotion } from "motion/react";
import {
  type CSSProperties,
  type KeyboardEvent,
  type PointerEvent,
  type ReactNode,
  useCallback,
  useEffect,
  useLayoutEffect,
  useRef,
  useState,
} from "react";
import { cn } from "@/lib/utils";

interface Pt {
  x: number;
  y: number;
}

/** A page being turned: which way, by which corner, and where that corner is. */
interface Turn {
  dir: 1 | -1;
  /** The corner's resting point, in book pixels. */
  corner: Pt;
  /** Where the corner is now. */
  at: Pt;
}

export interface PageFlipProps {
  /** Pages in reading order. The first is the cover, alone on the right. */
  pages?: ReactNode[];
  /** Called with the spread index after a page settles. */
  onSpreadChange?: (spread: number) => void;
  className?: string;
}

//... (rest of the source code)

export default function PageFlip({
  pages = DEFAULT_PAGES,
  onSpreadChange,
  className,
}: PageFlipProps) {
  //... (rest of the component logic)
}

function Course({
  title,
  dishes,
  page,
}: {
  title: string;
  dishes: Dish[];
  page: number;
}) {
  return (
    <div className="flex size-full flex-col px-[9cqw] pt-[10cqw] pb-[7cqw]">
      <p className="text-[3.4cqw] text-zinc-500 uppercase tracking-[0.2em] dark:text-zinc-400">
        {title}
      </p>
      <div className="mt-[3cqw] h-px w-[10cqw] bg-emerald-800/40 dark:bg-emerald-300/40" />
      <ul className="mt-[6cqw] flex flex-col gap-[5.5cqw]">
        {dishes.map((dish) => (
          <li key={dish.name}>
            <p className="flex items-baseline gap-[2cqw]">
              <span className="font-medium text-[4.6cqw] leading-tight">
                {dish.name}
              </span>
              <span className="mb-[0.8cqw] flex-1 self-end border-zinc-300 border-b border-dotted dark:border-zinc-700" />
              <span className="text-[4cqw] tabular-nums">{dish.price}</span>
            </p>
            <p className="mt-[1cqw] text-[3.4cqw] text-zinc-500 leading-snug dark:text-zinc-400">
              {dish.note}
            </p>
          </li>
        ))}
      </ul>
      <p className="mt-auto text-center text-[3cqw] text-zinc-400 tabular-nums dark:text-zinc-500">
        {page}
      </p>
    </div>
  );
}