Install react-confetti-explosion
masterInstall the package using pnpm:
$ pnpm add react-confetti-explosionrepository·master·Indexed 42 days ago
https://github.com/herrethan/react-confetti-explosionA lightweight CSS-animation based confetti exploder for React version 3.0.3. It provides a ConfettiExplosion component that creates confetti effects without requiring a canvas element, utilizing a portal to append to the document body by default.
Install the package using pnpm:
$ pnpm add react-confetti-explosionImport ConfettiExplosion and render it conditionally to trigger an explosion. Note that the component is appended to the document body via a Portal by default, so you must manage its mounting and unmounting (e.g., via state) to control when the explosion occurs and to ensure it is cleaned up.
import ConfettiExplosion from 'react-confetti-explosion';
function App() {
const [isExploding, setIsExploding] = React.useState(false);
return <>{isExploding && <ConfettiExplosion />}</>;
}By default, ConfettiExplosion uses ReactDOM.createPortal() to append itself to the document body. This ensures it layers above page content, but you must remember to unmount the component when the explosion is finished.
height is 120vh to ensure particles land past the bottom of the viewport.vh or %), the explosion appearance may vary between mobile and desktop screens.height is set too small, particles may visibly land on the floor instead of disappearing off-screen.The library uses estimated CSS animations rather than a full physics engine to remain lightweight. Some combinations of props may not look perfectly realistic.
Use the following prop configurations to achieve different explosion scales:
Large explosion
force: 0.8duration: 3000particleCount: 250width: 1600Medium explosion
force: 0.6duration: 2500particleCount: 80width: 1000Small explosion
force: 0.4duration: 2200particleCount: 30width: 400The ConfettiExplosion component accepts several optional props to customize the appearance and behavior of the explosion. For optimal performance, it is generally recommended to keep particleCount under 400.
| Name | Type | Default | Description |
| ------------- | `number` | 100 | Total number of particles used. Generally try to stay under 400 for optimal performance. |
| particleSize | `number` | 12 | Size of particles in pixels. This means width for squares, diameter for circles. Note there is also a bit of randomness added to these. |
| duration | `number` | 2200 | Duration of explosion in ms. This is the time it takes particles to travel from explosion point to the floor, as defined by `height`. |
| onComplete | `function` | `undefined` | Function that is called at end of `duration` |
| zIndex | `number` | `undefined` | zIndex that will be applied to the explosion, in case higher Portal stacking order is required. |
| colors | `string[]` | ['#FFC700','#FF0000','#2E3191','#41BBC7'] | An array of any css-readable colors, which are evenly distributed across the number of total particles. |
| force | `number` | 0.5 | Between 0-1, roughly the vertical force at which particles initially explode. |
| height | `number` or `string` | '120vh' | Pixel distance the particles will vertically spread from initial explosion point. |
| width | `number` | 1000 | Pixel distance the particles will horizontally spread from initial explosion point. |
| portal | `boolean` | `true` | Optionally opt out of Portal rendering. |The ConfettiExplosion component is the primary entry point for triggering confetti effects. It can be imported as a named export or as the default export from react-confetti-explosion.
import ConfettiExplosion from 'react-confetti-explosion';
// Or as a named export
// import { ConfettiExplosion } from 'react-confetti-explosion';
function MyComponent() {
return <ConfettiExplosion />;
}The ConfettiExplosion component accepts several props to customize the appearance, behavior, and lifecycle of the animation.
| Prop | Type | Default | Description |
|---|---|---|---|
particleCount | number | 100 | Number of confetti particles to render. |
duration | number | 2200 | Duration of the animation in milliseconds. |
colors | string[] | ['#FFC700', '#FF0000', '#2E3191', '#41BBC7'] | Array of colors to use for the particles. |
particleSize | number | 12 | Size of each particle in pixels. |
force | number | 0.5 | Explosion force (0-1). Higher values make particles shoot higher. |
height | number | string | '120vh' | Height of the explosion area (e.g., pixels or CSS units). |
width | number | 1000 | Width of the explosion area in pixels. |
portal | boolean | true | Whether to render the confetti in a portal attached to document.body. |
zIndex | number | undefined | The z-index of the confetti container. |
onComplete | () => void | undefined | Callback function called when the animation completes. |
ConfettiProps to the ConfettiExplosion component.