Last active
October 1, 2024 16:46
-
-
Save l4r-s/f1b39f29f7a285f7c031859091e3ddb3 to your computer and use it in GitHub Desktop.
Next.js Confetti Component - Add falling confetti animation to your Next.js app, just like the background on https://confettisaas.com. Perfect for celebratory events or adding a fun, festive touch to your website.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use client"; | |
import Confetti from "react-confetti"; | |
import { useState, useEffect } from "react"; | |
export default function ConfettiComponent() { | |
const [dimensions, setDimensions] = useState({ width: 0, height: 0 }); | |
const [isSafari, setIsSafari] = useState(false); | |
useEffect(() => { | |
const updateDimensions = () => { | |
setDimensions({ | |
width: window.innerWidth, | |
height: document.documentElement.scrollHeight, | |
}); | |
}; | |
// Only set dimensions on client side | |
if (typeof window !== "undefined") { | |
updateDimensions(); | |
window.addEventListener("resize", updateDimensions); | |
// Detect if the browser is Safari | |
const userAgent = window.navigator.userAgent; | |
const isSafariBrowser = /^((?!chrome|android).)*safari/i.test(userAgent); | |
setIsSafari(isSafariBrowser); | |
} | |
// Cleanup event listener on component unmount | |
return () => { | |
if (typeof window !== "undefined") { | |
window.removeEventListener("resize", updateDimensions); | |
} | |
}; | |
}, []); | |
return ( | |
<div style={{ position: "fixed", zIndex: 1 }}> | |
<Confetti | |
width={dimensions.width} | |
height={dimensions.height} | |
wind={isSafari ? 0.1 : 0.02} | |
gravity={isSafari ? 0.1 : 0.04} | |
numberOfPieces={30} | |
recycle={true} | |
/> | |
</div> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ConfettiComponent from "./_components/confett"; | |
const inter = Inter({ subsets: ["latin"] }); | |
export default function RootLayout({ | |
children, | |
}: Readonly<{ | |
children: React.ReactNode; | |
}>) { | |
return ( | |
<html lang="en"> | |
<body className={inter.className}> | |
<div> | |
<ConfettiComponent /> | |
<div style={{ position: "relative", zIndex: 10 }}> | |
<div className="min-h-screen"> | |
{children} | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function Home() { | |
return ( | |
<div className="container mx-auto px-4 py-8 max-w-3xl"> | |
<h1 className="text-3xl font-bold text-center text-gray-800 mb-8">Welcome to the amazing Confetti Sparkling!</h1> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment