vue-confetti is a versatile Vue component and plugin designed for generating dynamic confetti effects within web applications. It allows developers to programmatically trigger, stop, and customize confetti showers through a `$confetti` instance injected into Vue components. The library supports various particle shapes including circles, rectangles, hearts, and custom images, along with extensive customization for colors, sizes, drop rates, and wind effects. Currently at version 2.3.0, it offers compatibility with both Vue 2 and Vue 3, with Vue 3 support introduced in February 2022. While releases are not frequent, they typically address bug fixes or add minor features, responding to ecosystem changes like Vue 3 adoption. Its primary advantage lies in providing a declarative and easily configurable solution for celebratory UI effects without direct canvas API manipulation, simplifying the process for adding dynamic visual flair.
npm install vue-confettiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install and use vue-confetti in a Vue 3 application with TypeScript, showcasing basic confetti triggering, stopping, and customized effects.
For Nuxt.js, register the plugin with `mode: 'client'` in your `nuxt.config.js` or `nuxt.config.ts`. For other SSR setups, ensure the plugin is only loaded or initialized on the client side.
For Vue 2, use `import Vue from 'vue'; Vue.use(VueConfetti);`. For Vue 3, use `import { createApp } from 'vue'; const app = createApp(App); app.use(VueConfetti);`.Always call `$confetti.start()` explicitly after `$confetti.update()` if you intend to immediately begin a confetti shower with the new settings.
Ensure `vue-confetti` is loaded only on the client. In Nuxt, configure it in `nuxt.config.js` as `{ src: '~/plugins/vue-confetti.js', mode: 'client' }`.Extend Vue's `ComponentCustomProperties` interface via declaration merging. Create a `shims-vue-confetti.d.ts` file:
```typescript
import 'vue';
declare module 'vue' {
interface ComponentCustomProperties {
$confetti: {
start: (options?: any) => void;
stop: () => void;
update: (options?: any) => void;
};
}
}
```
Alternatively, cast `this` to `any` in component methods: `(this as any).$confetti.start()`.Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json` for Node.js) or use a bundler like Webpack/Vite that transpiles ESM to CJS if targeting older environments. For `vue-confetti`, stick to `import` statements as it's designed for modern module systems.