react-confetti-explosion

repository·master·Indexed 42 days ago

https://github.com/herrethan/react-confetti-explosion

A 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.

Tokens
1.7K
Snippets
4
Records
8
Agent score
4%

What's inside react-confetti-explosion

  1. Use the ConfettiExplosion component

    master

    Import 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 />}</>;
    }
  2. Troubleshoot ConfettiExplosion behavior

    master

    Portal and Layering

    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 and Viewport

    • The default height is 120vh to ensure particles land past the bottom of the viewport.
    • If you use relative strings (like vh or %), the explosion appearance may vary between mobile and desktop screens.
    • If height is set too small, particles may visibly land on the floor instead of disappearing off-screen.

    Physics Limitations

    The library uses estimated CSS animations rather than a full physics engine to remain lightweight. Some combinations of props may not look perfectly realistic.

  3. Configure explosion size and force examples

    master

    Use the following prop configurations to achieve different explosion scales:

    Large explosion

    • force: 0.8
    • duration: 3000
    • particleCount: 250
    • width: 1600

    Medium explosion

    • force: 0.6
    • duration: 2500
    • particleCount: 80
    • width: 1000

    Small explosion

    • force: 0.4
    • duration: 2200
    • particleCount: 30
    • width: 400
  4. Configure ConfettiExplosion props

    master

    The 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. |
  5. Use the ConfettiExplosion component

    master

    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 />;
    }
  6. Configure ConfettiExplosion props

    master

    The ConfettiExplosion component accepts several props to customize the appearance, behavior, and lifecycle of the animation.

    PropTypeDefaultDescription
    particleCountnumber100Number of confetti particles to render.
    durationnumber2200Duration of the animation in milliseconds.
    colorsstring[]['#FFC700', '#FF0000', '#2E3191', '#41BBC7']Array of colors to use for the particles.
    particleSizenumber12Size of each particle in pixels.
    forcenumber0.5Explosion force (0-1). Higher values make particles shoot higher.
    heightnumber | string'120vh'Height of the explosion area (e.g., pixels or CSS units).
    widthnumber1000Width of the explosion area in pixels.
    portalbooleantrueWhether to render the confetti in a portal attached to document.body.
    zIndexnumberundefinedThe z-index of the confetti container.
    onComplete() => voidundefinedCallback function called when the animation completes.