Page Flip

An interactive book component with realistic page-turning physics and drag gestures.

Page Flip

An interactive book component with realistic page-turning physics and drag gestures.

The book opens with the cover alone on the right, then shows two-page spreads. Hover a corner to peel it, press and drag to turn a page, or use the arrow keys and the previous/next buttons. Frames narrower than 520px automatically switch to one page at a time.

Installation

Install the component with the Craft-UI Blocks CLI — it drops page-flip.tsx into your project and wires up its runtime dependencies:

npx mvpblocks add page-flip
pnpm dlx mvpblocks add page-flip
yarn dlx mvpblocks add page-flip
bunx mvpblocks add page-flip

File Structure

The CLI saves the component to your project's components/ui folder:

Import the default export from @/components/ui/page-flip. The prop type is exported as PageFlipProps.

Usage

PageFlip is a client component and every prop is optional — with no props it renders a demo spring menu. Drop it into any wide card slot:

"use client";

import PageFlip from "@/components/ui/page-flip";

export function BookCard() {
  return <PageFlip />;
}

Each item in pages is a React node in reading order. The first page is the cover, shown alone on the right. Keep page content sized in container units so it scales with the book:

const pages = [
  <div key="cover" className="flex size-full items-center justify-center">
    Cover
  </div>,
  <div key="one" className="size-full p-6">First page</div>,
  <div key="two" className="size-full p-6">Second page</div>,
  <div key="three" className="size-full p-6">Third page</div>,
];

<PageFlip
  pages={pages}
  onSpreadChange={(spread) => console.log("spread", spread)}
  className="max-w-xl"
/>

onSpreadChange fires after a turn settles with the new spread index. The footer shows which pages are visible and stays in sync when the layout switches between one and two pages.

Theming

Pages are warm paper by default: #fbfaf7 for the front and #f1efe9 for the back, with dark:bg-zinc-900 and dark:bg-zinc-800 in dark mode. A rounded spine, a gradient gutter, and a subtle ring draw the fold without images.

The demo menu uses emerald accents, but your page nodes control their own colors. className lands on the outer wrapper, so width, spacing, and overflow are overridable:

<PageFlip className="max-w-2xl" />

Animations

  • Corner peek: moving the mouse within 64px of a corner lifts it up to 34px toward the pointer.
  • Page turn: a completed turn runs for 0.6s with a soft ease; a released drag that does not cross the spine settles back in 0.32s.
  • Fold shadow: the flap's drop shadow grows with turn progress, then disappears when the page lands.
  • Drag: a press becomes a drag after 4px; the corner is clamped to points the paper can physically reach.
  • Reduced motion: presses turn pages instantly and the corner peek is disabled.

How it works

twoPages and onePage decide which node belongs on the left, right, turning sheet, and back of the sheet. A ResizeObserver measures the frame and switches to single-page mode below 520px, keeping the same page in view.

foldStyles computes the crease as the line halfway between the corner's resting point and its dragged point. The page polygon is clipped on both sides of that line, and the flap is reflected across it with a CSS matrix(), which produces the paper-fold effect. clipPolygon handles corners dragged past the spine without tearing the shape.

A tap always turns the page; a drag only commits once the corner crosses the spine. Arrow keys call the same go function used by the buttons, and the footer is an aria-live region so screen readers hear the new page range.

Props

Prop

Type

Registry Bundle

Install the exact registry entry shown on the right when you want the component file and its declared runtime dependencies together. Dependency: motion (page-turn animation).

Contact

If you find any bug or issue, feel free to raise an issue.

Last updated on