|
| 1 | +import { motion } from "framer-motion"; |
| 2 | +import { useEffect, useState } from "react"; |
| 3 | + |
| 4 | +declare global { |
| 5 | + namespace JSX { |
| 6 | + interface IntrinsicElements { |
| 7 | + "spline-viewer": React.DetailedHTMLProps< |
| 8 | + React.HTMLAttributes<HTMLElement> & { |
| 9 | + url?: string; |
| 10 | + "loading-anim-type"?: string; |
| 11 | + }, |
| 12 | + HTMLElement |
| 13 | + >; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + interface Window { |
| 18 | + __splineLoader?: Promise<void>; |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export function TriggerRotatingLogo() { |
| 23 | + const [isSplineReady, setIsSplineReady] = useState(false); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + // Already registered from a previous render |
| 27 | + if (customElements.get("spline-viewer")) { |
| 28 | + setIsSplineReady(true); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // Another mount already started loading - share the same promise |
| 33 | + if (window.__splineLoader) { |
| 34 | + window.__splineLoader.then(() => setIsSplineReady(true)).catch(() => setIsSplineReady(false)); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // First mount: create script and shared loader promise |
| 39 | + const script = document.createElement("script"); |
| 40 | + script.type = "module"; |
| 41 | + // Version pinned; SRI hash omitted as unpkg doesn't guarantee hash stability across deploys |
| 42 | + script.src = "https://unpkg.com/@splinetool/[email protected]/build/spline-viewer.js"; |
| 43 | + |
| 44 | + window.__splineLoader = new Promise<void>((resolve, reject) => { |
| 45 | + script.onload = () => resolve(); |
| 46 | + script.onerror = () => reject(); |
| 47 | + }); |
| 48 | + |
| 49 | + window.__splineLoader.then(() => setIsSplineReady(true)).catch(() => setIsSplineReady(false)); |
| 50 | + |
| 51 | + document.head.appendChild(script); |
| 52 | + |
| 53 | + // Intentionally no cleanup: once the custom element is registered globally, |
| 54 | + // removing the script would break re-mounts while providing no benefit |
| 55 | + }, []); |
| 56 | + |
| 57 | + if (!isSplineReady) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + return ( |
| 62 | + <motion.div |
| 63 | + className="pointer-events-none absolute inset-0 overflow-hidden" |
| 64 | + initial={{ opacity: 0 }} |
| 65 | + animate={{ opacity: 1 }} |
| 66 | + transition={{ delay: 0.5, duration: 2, ease: "easeOut" }} |
| 67 | + > |
| 68 | + <spline-viewer |
| 69 | + loading-anim-type="spinner-small-light" |
| 70 | + url="https://prod.spline.design/wRly8TZN-e0Twb8W/scene.splinecode" |
| 71 | + style={{ width: "100%", height: "100%" }} |
| 72 | + /> |
| 73 | + </motion.div> |
| 74 | + ); |
| 75 | +} |
0 commit comments