{
  "$schema": "https://www.cooki-ui.design/schema/registry-item.json",
  "name": "3d-image-pageflip",
  "type": "registry:ui",
  "title": "3D Image Pageflip",
  "description": "Realistic 3D magazine and book page-turn flip interaction with dynamic shadow rendering, spine shifting, and leaf navigation.",
  "category": "media",
  "dependencies": [
    "lucide-react",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/media/3d-image-pageflip/3d-image-pageflip.tsx",
      "type": "registry:ui",
      "target": "components/ui/3d-image-pageflip.tsx",
      "content": "\"use client\";\n\nimport React, { useState, useEffect, useCallback, CSSProperties, forwardRef, useImperativeHandle } from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport { ChevronLeft, ChevronRight, RotateCcw } from \"lucide-react\";\n\nexport interface PageFlipLeaf {\n    id?: string | number;\n    frontImage: string;\n    backImage: string;\n    frontTitle?: string;\n    backTitle?: string;\n    frontSubtitle?: string;\n    backSubtitle?: string;\n    frontBadge?: string;\n    backBadge?: string;\n}\n\nexport interface ThreeDImagePageflipProps {\n    /** Array of page leaves, each containing front and back images & metadata */\n    pages?: PageFlipLeaf[];\n    /** Default turned page count (0 = closed book on cover) */\n    defaultTurnedIndex?: number;\n    /** Controlled turned page count */\n    turnedIndex?: number;\n    /** Callback fired when page flip changes */\n    onPageChange?: (turnedCount: number, totalLeaves: number) => void;\n    /** Width of a single page in pixels (default: 230) */\n    pageWidth?: number;\n    /** Height of a single page in pixels (default: 330) */\n    pageHeight?: number;\n    /** 3D perspective depth in pixels (default: 1300) */\n    perspective?: number;\n    /** Maximum hover peek angle in degrees (default: 14) */\n    peekAngle?: number;\n    /** Total turn angle in degrees (default: 180) */\n    turnAngle?: number;\n    /** Transition flip animation duration in seconds (default: 0.65) */\n    duration?: number;\n    /** Easing curve for flip animation (default: \"cubic-bezier(0.4, 0, 0.2, 1)\") */\n    easing?: string;\n    /** Shadow intensity factor (0.0 to 1.0, default: 0.45) */\n    shadowIntensity?: number;\n    /** Dynamically shift spine horizontally when book is open to center the 2-page spread (default: true) */\n    spineShift?: boolean;\n    /** Border radius for pages (default: \"10px\") */\n    radius?: string | number;\n    /** Enable page numbering tags (default: true) */\n    showPageNumbers?: boolean;\n    /** Enable outer book leather spine binding (default: true) */\n    showSpineBinding?: boolean;\n    /** Accent glow color for active elements (default: \"#00F5FF\") */\n    accentColor?: string;\n    /** Enable automatic page flipping (default: false) */\n    autoplay?: boolean;\n    /** Autoplay interval in milliseconds (default: 3500) */\n    autoplayInterval?: number;\n    /** Pause autoplay on hover (default: true) */\n    pauseOnHover?: boolean;\n    /** Enable interactive click on pages to flip (default: true) */\n    interactive?: boolean;\n    /** Enable navigation buttons (default: true) */\n    showControls?: boolean;\n    /** Optional container class name */\n    className?: string;\n    /** Optional container inline style */\n    style?: CSSProperties;\n}\n\nexport interface ThreeDImagePageflipHandle {\n    next: () => void;\n    prev: () => void;\n    reset: () => void;\n    goTo: (index: number) => void;\n    getTurnedCount: () => number;\n    getTotalLeaves: () => number;\n}\n\nconst DEFAULT_PAGES: PageFlipLeaf[] = [\n    {\n        id: 1,\n        frontImage: \"https://images.unsplash.com/photo-1600585154340-be6161a56a0c?auto=format&fit=crop&w=800&q=80\",\n        backImage: \"https://images.unsplash.com/photo-1512917774080-9991f1c4c750?auto=format&fit=crop&w=800&q=80\",\n        frontTitle: \"Villa Solarium\",\n        frontSubtitle: \"Horizon Pool & Architecture\",\n        frontBadge: \"Cover\",\n        backTitle: \"Minimal Horizon\",\n        backSubtitle: \"Geometric Water Pavilion\",\n        backBadge: \"Plate 01\",\n    },\n    {\n        id: 2,\n        frontImage: \"https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80\",\n        backImage: \"https://images.unsplash.com/photo-1486406146926-c627a92ad1ab?auto=format&fit=crop&w=800&q=80\",\n        frontTitle: \"Apex Structure\",\n        frontSubtitle: \"Parametric Glass Facade\",\n        frontBadge: \"Plate 02\",\n        backTitle: \"Glass Skyline\",\n        backSubtitle: \"Monolith Metropolitan Tower\",\n        backBadge: \"Plate 03\",\n    },\n    {\n        id: 3,\n        frontImage: \"https://images.unsplash.com/photo-1506744038136-46273834b3fb?auto=format&fit=crop&w=800&q=80\",\n        backImage: \"https://images.unsplash.com/photo-1511818966892-d7d671e672a2?auto=format&fit=crop&w=800&q=80\",\n        frontTitle: \"Emerald Cascade\",\n        frontSubtitle: \"Alpine Mist & Forest Ridge\",\n        frontBadge: \"Plate 04\",\n        backTitle: \"Nordic Pavilion\",\n        backSubtitle: \"Natural Timber Canopy\",\n        backBadge: \"Plate 05\",\n    },\n    {\n        id: 4,\n        frontImage: \"https://images.unsplash.com/photo-1518780664697-55e3ad937233?auto=format&fit=crop&w=800&q=80\",\n        backImage: \"https://images.unsplash.com/photo-1507652313519-d4e9174996dd?auto=format&fit=crop&w=800&q=80\",\n        frontTitle: \"Warm Solarium\",\n        frontSubtitle: \"Sunlight & Interior Loft\",\n        frontBadge: \"Plate 06\",\n        backTitle: \"Zen Courtyard\",\n        backSubtitle: \"Brutalist Stone Water Feature\",\n        backBadge: \"Plate 07\",\n    },\n    {\n        id: 5,\n        frontImage: \"https://images.unsplash.com/photo-1486325212027-8081e485255e?auto=format&fit=crop&w=800&q=80\",\n        backImage: \"https://images.unsplash.com/photo-1479839672679-a46483c0e7c8?auto=format&fit=crop&w=800&q=80\",\n        frontTitle: \"Cyber Metropolis\",\n        frontSubtitle: \"Urban Geometric Skyline\",\n        frontBadge: \"Plate 08\",\n        backTitle: \"Monolith Curve\",\n        backSubtitle: \"Brutalist Concrete Finish\",\n        backBadge: \"Endplate\",\n    },\n];\n\nexport const ThreeDImagePageflip = forwardRef<ThreeDImagePageflipHandle, ThreeDImagePageflipProps>(({\n    pages = DEFAULT_PAGES,\n    defaultTurnedIndex = 0,\n    turnedIndex: controlledTurnedIndex,\n    onPageChange,\n    pageWidth = 230,\n    pageHeight = 330,\n    perspective = 1300,\n    peekAngle = 14,\n    turnAngle = 180,\n    duration = 0.65,\n    easing = \"cubic-bezier(0.4, 0, 0.2, 1)\",\n    shadowIntensity = 0.45,\n    spineShift = true,\n    radius = \"10px\",\n    showPageNumbers = true,\n    showSpineBinding = true,\n    accentColor = \"#00F5FF\",\n    autoplay = false,\n    autoplayInterval = 3500,\n    pauseOnHover = true,\n    interactive = true,\n    showControls = true,\n    className,\n    style,\n}, ref) => {\n    const [internalTurned, setInternalTurned] = useState<number>(defaultTurnedIndex);\n    const [isHovered, setIsHovered] = useState<boolean>(false);\n    const [peekingIndex, setPeekingIndex] = useState<number | null>(null);\n\n    const totalLeaves = pages.length;\n    const currentTurned = controlledTurnedIndex !== undefined ? controlledTurnedIndex : internalTurned;\n    const isOpen = currentTurned > 0 && currentTurned < totalLeaves;\n\n    const parsedRadius = typeof radius === \"number\" ? `${radius}px` : radius;\n\n    const setTurned = useCallback((newCount: number) => {\n        const clamped = Math.max(0, Math.min(newCount, totalLeaves));\n        if (controlledTurnedIndex === undefined) {\n            setInternalTurned(clamped);\n        }\n        if (onPageChange) {\n            onPageChange(clamped, totalLeaves);\n        }\n    }, [controlledTurnedIndex, totalLeaves, onPageChange]);\n\n    const flipNext = useCallback(() => {\n        if (currentTurned < totalLeaves) {\n            setTurned(currentTurned + 1);\n        }\n    }, [currentTurned, totalLeaves, setTurned]);\n\n    const flipPrev = useCallback(() => {\n        if (currentTurned > 0) {\n            setTurned(currentTurned - 1);\n        }\n    }, [currentTurned, setTurned]);\n\n    const resetBook = useCallback(() => {\n        setTurned(0);\n    }, [setTurned]);\n\n    useImperativeHandle(ref, () => ({\n        next: flipNext,\n        prev: flipPrev,\n        reset: resetBook,\n        goTo: (idx) => setTurned(idx),\n        getTurnedCount: () => currentTurned,\n        getTotalLeaves: () => totalLeaves,\n    }));\n\n    // Autoplay Timer\n    useEffect(() => {\n        if (!autoplay || (pauseOnHover && isHovered) || totalLeaves <= 1) return;\n        const timer = setInterval(() => {\n            setInternalTurned((prev) => (prev >= totalLeaves ? 0 : prev + 1));\n        }, autoplayInterval);\n        return () => clearInterval(timer);\n    }, [autoplay, autoplayInterval, pauseOnHover, isHovered, totalLeaves]);\n\n    // Keyboard Navigation\n    useEffect(() => {\n        const handleKeyDown = (e: KeyboardEvent) => {\n            if (e.key === \"ArrowRight\") flipNext();\n            if (e.key === \"ArrowLeft\") flipPrev();\n        };\n        window.addEventListener(\"keydown\", handleKeyDown);\n        return () => window.removeEventListener(\"keydown\", handleKeyDown);\n    }, [flipNext, flipPrev]);\n\n    const handleLeafClick = (index: number) => {\n        if (!interactive) return;\n        if (index === currentTurned) {\n            // Click unturned top page -> flip forward\n            flipNext();\n        } else if (index === currentTurned - 1) {\n            // Click turned top left page -> flip backward\n            flipPrev();\n        }\n    };\n\n    return (\n        <div\n            className={cn(\"w-full flex flex-col items-center justify-center select-none py-6\", className)}\n            style={style}\n            onMouseEnter={() => setIsHovered(true)}\n            onMouseLeave={() => {\n                setIsHovered(false);\n                setPeekingIndex(null);\n            }}\n        >\n            {/* 3D Book Viewport Stage */}\n            <div\n                className=\"relative flex items-center justify-center transition-all duration-500\"\n                style={{\n                    perspective: `${perspective}px`,\n                    width: `${pageWidth * 2 + 40}px`,\n                    height: `${pageHeight + 40}px`,\n                }}\n            >\n                {/* 3D Book Container */}\n                <div\n                    className=\"relative transition-transform\"\n                    style={{\n                        width: `${pageWidth}px`,\n                        height: `${pageHeight}px`,\n                        transformStyle: \"preserve-3d\",\n                        transition: `transform ${duration}s ${easing}`,\n                        transform: spineShift && isOpen ? `translateX(${pageWidth / 2}px)` : \"translateX(0)\",\n                    }}\n                >\n                    {/* Spine Shadow & Binding Crease */}\n                    {showSpineBinding && (\n                        <div\n                            className=\"absolute top-0 bottom-0 left-[-4px] w-[8px] rounded-l-sm bg-gradient-to-r from-black/80 via-zinc-800 to-black/40 shadow-2xl z-30 pointer-events-none\"\n                            style={{\n                                opacity: isOpen ? 0.95 : 0.6,\n                                transition: `opacity ${duration}s ease`,\n                            }}\n                        />\n                    )}\n\n                    {/* Ground Ambience Drop Shadow underneath the book */}\n                    <div\n                        className=\"absolute -bottom-6 left-[-15%] w-[130%] h-8 bg-black/40 rounded-full blur-xl pointer-events-none transition-all duration-500\"\n                        style={{\n                            opacity: isOpen ? 0.7 : 0.4,\n                            transform: isOpen ? \"scale(1.15)\" : \"scale(0.85)\",\n                        }}\n                    />\n\n                    {/* Book Leaves Stacking Loop */}\n                    {pages.map((leaf, index) => {\n                        const isTurned = index < currentTurned;\n                        const isCanPeek = index === currentTurned;\n                        const isPeeking = peekingIndex === index;\n\n                        // Calculate Z-Index: turned leaves stack forward on left, unturned leaves stack backward on right\n                        const zIndex = isTurned ? index + 1 : totalLeaves - index;\n\n                        // Rotation Angle\n                        let leafRotation = isTurned ? -turnAngle : 0;\n                        if (!isTurned && isPeeking) {\n                            leafRotation = -peekAngle;\n                        }\n\n                        return (\n                            <div\n                                key={leaf.id ?? index}\n                                onClick={() => handleLeafClick(index)}\n                                onMouseEnter={() => {\n                                    if (isCanPeek) setPeekingIndex(index);\n                                }}\n                                onMouseLeave={() => {\n                                    if (peekingIndex === index) setPeekingIndex(null);\n                                }}\n                                className={cn(\n                                    \"absolute inset-0 origin-left cursor-pointer\",\n                                    interactive ? \"cursor-pointer\" : \"pointer-events-none\"\n                                )}\n                                style={{\n                                    transformStyle: \"preserve-3d\",\n                                    transition: `transform ${duration}s ${easing}`,\n                                    transform: `rotateY(${leafRotation}deg)`,\n                                    zIndex,\n                                    borderRadius: parsedRadius,\n                                }}\n                            >\n                                {/* FRONT FACE (Visible when page is on the right) */}\n                                <div\n                                    className=\"absolute inset-0 w-full h-full bg-zinc-950 overflow-hidden\"\n                                    style={{\n                                        backfaceVisibility: \"hidden\",\n                                        WebkitBackfaceVisibility: \"hidden\",\n                                        borderRadius: parsedRadius,\n                                        boxShadow: `0 12px 28px rgba(0, 0, 0, ${shadowIntensity})`,\n                                    }}\n                                >\n                                    <img\n                                        src={leaf.frontImage}\n                                        alt={leaf.frontTitle ?? `Page ${index * 2 + 1}`}\n                                        className=\"w-full h-full object-cover pointer-events-none select-none\"\n                                        loading=\"lazy\"\n                                    />\n\n                                    {/* Spine crease shadow overlay for 3D depth */}\n                                    <div\n                                        className=\"absolute inset-0 pointer-events-none transition-opacity duration-300\"\n                                        style={{\n                                            background: \"linear-gradient(to right, rgba(0,0,0,0.55) 0%, rgba(0,0,0,0.12) 14%, transparent 35%)\",\n                                            opacity: isTurned ? 0 : 1,\n                                        }}\n                                    />\n\n                                    {/* Bottom Vignette & Metadata */}\n                                    <div className=\"absolute inset-x-0 bottom-0 p-3.5 bg-gradient-to-t from-black/85 via-black/35 to-transparent text-white pointer-events-none\">\n                                        <div className=\"flex items-center justify-between gap-2 mb-1\">\n                                            {leaf.frontBadge && (\n                                                <span\n                                                    className=\"px-2 py-0.5 rounded-full text-[9px] font-bold uppercase tracking-wider backdrop-blur-md border border-white/20\"\n                                                    style={{\n                                                        color: accentColor,\n                                                        backgroundColor: `${accentColor}20`,\n                                                    }}\n                                                >\n                                                    {leaf.frontBadge}\n                                                </span>\n                                            )}\n                                            {showPageNumbers && (\n                                                <span className=\"text-[10px] font-mono text-zinc-300/80\">\n                                                    {index * 2 + 1}\n                                                </span>\n                                            )}\n                                        </div>\n                                        {leaf.frontTitle && (\n                                            <h4 className=\"text-xs sm:text-sm font-bold tracking-tight text-white drop-shadow-sm line-clamp-1\">\n                                                {leaf.frontTitle}\n                                            </h4>\n                                        )}\n                                        {leaf.frontSubtitle && (\n                                            <p className=\"text-[10px] text-zinc-300/70 font-medium line-clamp-1\">\n                                                {leaf.frontSubtitle}\n                                            </p>\n                                        )}\n                                    </div>\n                                </div>\n\n                                {/* BACK FACE (Visible when page is turned to the left) */}\n                                <div\n                                    className=\"absolute inset-0 w-full h-full bg-zinc-950 overflow-hidden\"\n                                    style={{\n                                        backfaceVisibility: \"hidden\",\n                                        WebkitBackfaceVisibility: \"hidden\",\n                                        transform: \"rotateY(180deg)\",\n                                        borderRadius: parsedRadius,\n                                        boxShadow: `0 12px 28px rgba(0, 0, 0, ${shadowIntensity})`,\n                                    }}\n                                >\n                                    <img\n                                        src={leaf.backImage}\n                                        alt={leaf.backTitle ?? `Page ${index * 2 + 2}`}\n                                        className=\"w-full h-full object-cover pointer-events-none select-none\"\n                                        loading=\"lazy\"\n                                    />\n\n                                    {/* Spine crease shadow overlay for turned back-face */}\n                                    <div\n                                        className=\"absolute inset-0 pointer-events-none transition-opacity duration-300\"\n                                        style={{\n                                            background: \"linear-gradient(to left, rgba(0,0,0,0.55) 0%, rgba(0,0,0,0.12) 14%, transparent 35%)\",\n                                            opacity: isTurned ? 1 : 0,\n                                        }}\n                                    />\n\n                                    {/* Bottom Vignette & Metadata */}\n                                    <div className=\"absolute inset-x-0 bottom-0 p-3.5 bg-gradient-to-t from-black/85 via-black/35 to-transparent text-white pointer-events-none\">\n                                        <div className=\"flex items-center justify-between gap-2 mb-1\">\n                                            {leaf.backBadge && (\n                                                <span\n                                                    className=\"px-2 py-0.5 rounded-full text-[9px] font-bold uppercase tracking-wider backdrop-blur-md border border-white/20\"\n                                                    style={{\n                                                        color: accentColor,\n                                                        backgroundColor: `${accentColor}20`,\n                                                    }}\n                                                >\n                                                    {leaf.backBadge}\n                                                </span>\n                                            )}\n                                            {showPageNumbers && (\n                                                <span className=\"text-[10px] font-mono text-zinc-300/80\">\n                                                    {index * 2 + 2}\n                                                </span>\n                                            )}\n                                        </div>\n                                        {leaf.backTitle && (\n                                            <h4 className=\"text-xs sm:text-sm font-bold tracking-tight text-white drop-shadow-sm line-clamp-1\">\n                                                {leaf.backTitle}\n                                            </h4>\n                                        )}\n                                        {leaf.backSubtitle && (\n                                            <p className=\"text-[10px] text-zinc-300/70 font-medium line-clamp-1\">\n                                                {leaf.backSubtitle}\n                                            </p>\n                                        )}\n                                    </div>\n                                </div>\n                            </div>\n                        );\n                    })}\n                </div>\n            </div>\n\n            {/* Book Controls & Page Progress Toolbar */}\n            {showControls && (\n                <div className=\"flex items-center justify-center gap-3 mt-4 select-none\">\n                    <button\n                        onClick={flipPrev}\n                        disabled={currentTurned === 0}\n                        aria-label=\"Previous Page\"\n                        className=\"px-3.5 py-1.5 rounded-xl flex items-center gap-1 text-xs font-semibold bg-card border border-border text-foreground hover:bg-muted hover:scale-105 active:scale-95 disabled:opacity-30 disabled:pointer-events-none transition-all shadow-xs cursor-pointer\"\n                    >\n                        <ChevronLeft className=\"w-3.5 h-3.5\" />\n                        <span>Prev</span>\n                    </button>\n\n                    <button\n                        onClick={resetBook}\n                        disabled={currentTurned === 0}\n                        aria-label=\"Reset Book\"\n                        className=\"px-3 py-1.5 rounded-xl flex items-center gap-1 text-xs font-semibold bg-card border border-border text-foreground hover:bg-muted hover:scale-105 active:scale-95 disabled:opacity-30 disabled:pointer-events-none transition-all shadow-xs cursor-pointer\"\n                    >\n                        <RotateCcw className=\"w-3 h-3\" />\n                        <span>Reset</span>\n                    </button>\n\n                    <div className=\"px-3 py-1.5 rounded-xl bg-card border border-border text-xs font-mono text-muted-foreground shadow-xs\">\n                        <span className=\"font-bold text-foreground\">{currentTurned}</span>\n                        <span className=\"opacity-50\"> / </span>\n                        <span>{totalLeaves} leaves</span>\n                    </div>\n\n                    <button\n                        onClick={flipNext}\n                        disabled={currentTurned === totalLeaves}\n                        aria-label=\"Next Page\"\n                        className=\"px-3.5 py-1.5 rounded-xl flex items-center gap-1 text-xs font-semibold bg-card border border-border text-foreground hover:bg-muted hover:scale-105 active:scale-95 disabled:opacity-30 disabled:pointer-events-none transition-all shadow-xs cursor-pointer\"\n                    >\n                        <span>Next</span>\n                        <ChevronRight className=\"w-3.5 h-3.5\" />\n                    </button>\n                </div>\n            )}\n        </div>\n    );\n});\n\nThreeDImagePageflip.displayName = \"ThreeDImagePageflip\";\n\nexport default ThreeDImagePageflip;\n"
    }
  ]
}