UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
[Feat] New Component: Confetti Effect (#116)
Bankk24d631c4a1c7
diff --git a/config/docs.ts b/config/docs.tsindex 254693ae..eea17df2 100644--- a/config/docs.ts+++ b/config/docs.ts@@ -143,6 +143,12 @@ export const docsConfig: DocsConfig = {items: [],label: "New",},+ {+ title: "Confetti",+ href: `/docs/components/confetti`,+ items: [],+ label: "New",+ },],},{diff --git a/content/docs/components/confetti.mdx b/content/docs/components/confetti.mdxnew file mode 100644index 00000000..e2e0193a--- /dev/null+++ b/content/docs/components/confetti.mdx@@ -0,0 +1,84 @@+---+title: Confetti+date: 2024-05-26+description: How to add confetti effects to your project+author: Bankkroll+published: true+---++<ComponentPreview name="confetti-basic-cannon" />++<Steps>++### Installation++The confetti is built on canvas confetti.++```bash+npm install --save canvas-confetti+```++Copy and paste the following code into your project.++```text+components/magicui/confetti.tsx+```++<ComponentSource name="confetti" />++</Steps>++## Examples++### Random Direction++<ComponentPreview name="confetti-random-direction" />++### Fireworks++<ComponentPreview name="confetti-fireworks" />++### Side Cannons++<ComponentPreview name="confetti-side-cannons" />++### Stars++<ComponentPreview name="confetti-stars" />++### Custom Shapes++<ComponentPreview name="confetti-custom-shapes" />++### Emoji++<ComponentPreview name="confetti-emoji" />+++## Props++| Prop | Type | Description | Default |+| ------------------------- | ------------------------- | ------------------------------------------------ | ----------------------------------------------------------------------- |+| particleCount | Integer | The number of confetti particles to launch | 50 |+| angle | Number | The angle in degrees at which to launch confetti | 90 |+| spread | Number | The spread in degrees of the confetti | 45 |+| startVelocity | Number | The initial velocity of the confetti | 45 |+| decay | Number | The rate at which confetti slows down | 0.9 |+| gravity | Number | The gravity applied to confetti particles | 1 |+| drift | Number | The horizontal drift applied to particles | 0 |+| flat | Boolean | Whether confetti particles are flat | false |+| ticks | Number | The number of frames confetti lasts | 200 |+| origin | Object | The origin point of the confetti | `{ x: 0.5, y: 0.5 }` |+| colors | Array of Strings | Array of color strings in HEX format | ['#26ccff', '#a25afd', '#ff5e7e', '#88ff5a', '#fcff42', '#ffa62d', '#ff36ff'] |+| shapes | Array of Strings | Array of shapes for the confetti | ['square', 'circle'] |+| zIndex | Integer | The z-index of the confetti | 100 |+| disableForReducedMotion | Boolean | Disables confetti for users who prefer no motion | false |+| useWorker | Boolean | Use Web Worker for better performance | true |+| resize | Boolean | Whether to resize the canvas | true |+| canvas | HTMLCanvasElement or null | Custom canvas element to draw confetti | null |+| scalar | Number | Scaling factor for confetti size | 1 |++## Credits++- Credit to [Bankk](https://www.x.com/bankkroll_eth)+- Inspired by [canvas-confetti](https://www.npmjs.com/package/canvas-confetti)diff --git a/package.json b/package.jsonindex 4023f820..293d40cd 100644--- a/package.json+++ b/package.json@@ -64,6 +64,7 @@"@t3-oss/env-nextjs": "^0.7.1","@vercel/og": "^0.5.20","autoprefixer": "10.4.14",+ "canvas-confetti": "^1.9.3","class-variance-authority": "^0.6.0","clsx": "^2.0.0","cmdk": "^0.2.0",@@ -131,6 +132,5 @@"engines": {"node": ">=20.6.1","pnpm": ">=9"- },- "packageManager": "[email protected]"+ }}diff --git a/registry/components/example/confetti-basic-cannon.tsx b/registry/components/example/confetti-basic-cannon.tsxnew file mode 100644index 00000000..915696aa--- /dev/null+++ b/registry/components/example/confetti-basic-cannon.tsx@@ -0,0 +1,14 @@+import { Button } from "@/components/ui/button";+import { Confetti } from "@/registry/components/magicui/confetti";++export default function ConfettiBasicCannon() {+ const handleClick = () => {+ Confetti({}); // Use default settings for basic confetti+ };++ return (+ <div className="relative justify-center">+ <Button onClick={handleClick}>Trigger Basic Cannon</Button>+ </div>+ );+}diff --git a/registry/components/example/confetti-custom-shapes.tsx b/registry/components/example/confetti-custom-shapes.tsxnew file mode 100644index 00000000..369557c5--- /dev/null+++ b/registry/components/example/confetti-custom-shapes.tsx@@ -0,0 +1,64 @@+import { Button } from "@/components/ui/button";+import { Confetti } from "@/registry/components/magicui/confetti";++export default function ConfettiCustomShapes() {+ const handleClick = () => {+ const scalar = 2;+ const triangle = Confetti.shapeFromPath({+ path: "M0 10 L5 0 L10 10z",+ scalar,+ });+ const square = Confetti.shapeFromPath({+ path: "M0 0 L10 0 L10 10 L0 10 Z",+ scalar,+ });+ const coin = Confetti.shapeFromPath({+ path: "M5 0 A5 5 0 1 0 5 10 A5 5 0 1 0 5 0 Z",+ scalar,+ });+ const tree = Confetti.shapeFromPath({+ path: "M5 0 L10 10 L0 10 Z",+ scalar,+ });++ const defaults = {+ spread: 360,+ ticks: 60,+ gravity: 0,+ decay: 0.96,+ startVelocity: 20,+ shapes: [triangle, square, coin, tree],+ scalar,+ };++ const shoot = () => {+ Confetti({+ ...defaults,+ particleCount: 30,+ });++ Confetti({+ ...defaults,+ particleCount: 5,+ flat: true,+ });++ Confetti({+ ...defaults,+ particleCount: 15,+ scalar: scalar / 2,+ shapes: ["circle"],+ });+ };++ setTimeout(shoot, 0);+ setTimeout(shoot, 100);+ setTimeout(shoot, 200);+ };++ return (+ <div className="relative flex items-center justify-center">+ <Button onClick={handleClick}>Trigger Shapes</Button>+ </div>+ );+}diff --git a/registry/components/example/confetti-emoji.tsx b/registry/components/example/confetti-emoji.tsxnew file mode 100644index 00000000..36afd5f1--- /dev/null+++ b/registry/components/example/confetti-emoji.tsx@@ -0,0 +1,49 @@+import { Button } from "@/components/ui/button";+import { Confetti } from "@/registry/components/magicui/confetti";++export default function ConfettiEmoji() {+ const handleClick = () => {+ const scalar = 2;+ const unicorn = Confetti.shapeFromText({ text: "🦄", scalar });++ const defaults = {+ spread: 360,+ ticks: 60,+ gravity: 0,+ decay: 0.96,+ startVelocity: 20,+ shapes: [unicorn],+ scalar,+ };++ const shoot = () => {+ Confetti({+ ...defaults,+ particleCount: 30,+ });++ Confetti({+ ...defaults,+ particleCount: 5,+ flat: true,+ });++ Confetti({+ ...defaults,+ particleCount: 15,+ scalar: scalar / 2,+ shapes: ["circle"],+ });+ };++ setTimeout(shoot, 0);+ setTimeout(shoot, 100);+ setTimeout(shoot, 200);+ };++ return (+ <div className="relative justify-center">+ <Button onClick={handleClick}>Trigger Emoji</Button>+ </div>+ );+}diff --git a/registry/components/example/confetti-fireworks.tsx b/registry/components/example/confetti-fireworks.tsxnew file mode 100644index 00000000..99101869--- /dev/null+++ b/registry/components/example/confetti-fireworks.tsx@@ -0,0 +1,39 @@+import { Button } from "@/components/ui/button";+import { Confetti } from "@/registry/components/magicui/confetti";++export default function ConfettiFireworks() {+ const handleClick = () => {+ const duration = 5 * 1000;+ const animationEnd = Date.now() + duration;+ const defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 0 };++ const randomInRange = (min: number, max: number) =>+ Math.random() * (max - min) + min;++ const interval = window.setInterval(() => {+ const timeLeft = animationEnd - Date.now();++ if (timeLeft <= 0) {+ return clearInterval(interval);+ }++ const particleCount = 50 * (timeLeft / duration);+ Confetti({+ ...defaults,+ particleCount,+ origin: { x: randomInRange(0.1, 0.3), y: Math.random() - 0.2 },+ });+ Confetti({+ ...defaults,+ particleCount,+ origin: { x: randomInRange(0.7, 0.9), y: Math.random() - 0.2 },+ });+ }, 250);+ };++ return (+ <div className="relative justify-center">+ <Button onClick={handleClick}>Trigger Fireworks</Button>+ </div>+ );+}diff --git a/registry/components/example/confetti-random-direction.tsx b/registry/components/example/confetti-random-direction.tsxnew file mode 100644index 00000000..9bb73285--- /dev/null+++ b/registry/components/example/confetti-random-direction.tsx@@ -0,0 +1,16 @@+import { Button } from "@/components/ui/button";+import { Confetti } from "@/registry/components/magicui/confetti";++export default function ConfettiRandomDirection() {+ const handleClick = () => {+ Confetti({+ angle: Math.random() * 360,+ });+ };++ return (+ <div className="relative justify-center">+ <Button onClick={handleClick}>Trigger Random Direction</Button>+ </div>+ );+}diff --git a/registry/components/example/confetti-side-cannons.tsx b/registry/components/example/confetti-side-cannons.tsxnew file mode 100644index 00000000..332e8a37--- /dev/null+++ b/registry/components/example/confetti-side-cannons.tsx@@ -0,0 +1,40 @@+import { Button } from "@/components/ui/button";+import { Confetti } from "@/registry/components/magicui/confetti";++export default function ConfettiSideCannons() {+ const handleClick = () => {+ const end = Date.now() + 3 * 1000; // 3 seconds+ const colors = ["#a786ff", "#fd8bbc", "#eca184", "#f8deb1"];++ const frame = () => {+ if (Date.now() > end) return;++ Confetti({+ particleCount: 2,+ angle: 60,+ spread: 55,+ startVelocity: 60,+ origin: { x: 0, y: 0.5 },+ colors: colors,+ });+ Confetti({+ particleCount: 2,+ angle: 120,+ spread: 55,+ startVelocity: 60,+ origin: { x: 1, y: 0.5 },+ colors: colors,+ });++ requestAnimationFrame(frame);+ };++ frame();+ };++ return (+ <div className="relative justify-center">+ <Button onClick={handleClick}>Trigger Side Cannons</Button>+ </div>+ );+}diff --git a/registry/components/example/confetti-stars.tsx b/registry/components/example/confetti-stars.tsxnew file mode 100644index 00000000..f5bef070--- /dev/null+++ b/registry/components/example/confetti-stars.tsx@@ -0,0 +1,41 @@+import { Button } from "@/components/ui/button";+import { Confetti } from "@/registry/components/magicui/confetti";++export default function ConfettiStars() {+ const handleClick = () => {+ const defaults = {+ spread: 360,+ ticks: 50,+ gravity: 0,+ decay: 0.94,+ startVelocity: 30,+ colors: ["#FFE400", "#FFBD00", "#E89400", "#FFCA6C", "#FDFFB8"],+ };++ const shoot = () => {+ Confetti({+ ...defaults,+ particleCount: 40,+ scalar: 1.2,+ shapes: ["star"],+ });++ Confetti({+ ...defaults,+ particleCount: 10,+ scalar: 0.75,+ shapes: ["circle"],+ });+ };++ setTimeout(shoot, 0);+ setTimeout(shoot, 100);+ setTimeout(shoot, 200);+ };++ return (+ <div className="relative justify-center">+ <Button onClick={handleClick}>Trigger Stars</Button>+ </div>+ );+}diff --git a/registry/components/magicui/confetti.tsx b/registry/components/magicui/confetti.tsxnew file mode 100644index 00000000..4b06b4b6--- /dev/null+++ b/registry/components/magicui/confetti.tsx@@ -0,0 +1,52 @@+import confetti from "canvas-confetti";++interface ConfettiOptions extends confetti.Options {+ particleCount?: number;+ angle?: number;+ spread?: number;+ startVelocity?: number;+ decay?: number;+ gravity?: number;+ drift?: number;+ flat?: boolean;+ ticks?: number;+ origin?: { x: number; y: number };+ colors?: string[];+ shapes?: confetti.Shape[];+ zIndex?: number;+ disableForReducedMotion?: boolean;+ useWorker?: boolean;+ resize?: boolean;+ canvas?: HTMLCanvasElement | null;+ scalar?: number;+}++const Confetti = (options: ConfettiOptions) => {+ if (+ options.disableForReducedMotion &&+ window.matchMedia("(prefers-reduced-motion)").matches+ ) {+ return;+ }++ const confettiInstance = options.canvas+ ? confetti.create(options.canvas, {+ resize: options.resize ?? true,+ useWorker: options.useWorker ?? true,+ })+ : confetti;++ confettiInstance({+ ...options,+ });+};++Confetti.shapeFromPath = (options: { path: string; [key: string]: any }) => {+ return confetti.shapeFromPath({ ...options });+};++Confetti.shapeFromText = (options: { text: string; [key: string]: any }) => {+ return confetti.shapeFromText({ ...options });+};++export { Confetti };diff --git a/registry/index.tsx b/registry/index.tsxindex 6869d7b5..d3b27c72 100644--- a/registry/index.tsx+++ b/registry/index.tsx@@ -232,6 +232,11 @@ const ui: Registry = {type: "components:ui",files: ["registry/components/magicui/gauge-circle.tsx"],},+ confetti: {+ name: "confetti",+ type: "components:ui",+ files: ["registry/components/magicui/confetti.tsx"],+ },};const example: Registry = {@@ -807,6 +812,62 @@ const example: Registry = {() => import("@/registry/components/example/shine-border-demo-2"),),},+ "confetti-basic-cannon": {+ name: "confetti-basic-cannon",+ type: "components:example",+ files: ["registry/components/example/confetti-basic-cannon.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/confetti-basic-cannon"),+ ),+ },+ "confetti-random-direction": {+ name: "confetti-random-direction",+ type: "components:example",+ files: ["registry/components/example/confetti-random-direction.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/confetti-random-direction"),+ ),+ },+ "confetti-fireworks": {+ name: "confetti-fireworks",+ type: "components:example",+ files: ["registry/components/example/confetti-fireworks.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/confetti-fireworks"),+ ),+ },+ "confetti-stars": {+ name: "confetti-stars",+ type: "components:example",+ files: ["registry/components/example/confetti-stars.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/confetti-stars"),+ ),+ },+ "confetti-side-cannons": {+ name: "confetti-side-cannons",+ type: "components:example",+ files: ["registry/components/example/confetti-side-cannons.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/confetti-side-cannons"),+ ),+ },+ "confetti-custom-shapes": {+ name: "confetti-custom-shapes",+ type: "components:example",+ files: ["registry/components/example/confetti-custom-shapes.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/confetti-custom-shapes"),+ ),+ },+ "confetti-emoji": {+ name: "confetti-emoji",+ type: "components:example",+ files: ["registry/components/example/confetti-emoji.tsx"],+ component: React.lazy(+ () => import("@/registry/components/example/confetti-emoji"),+ ),+ },};export const registry: Registry = {...ui,