Skip to main content

useConfetti

The useConfetti hook provides a simple way to trigger confetti animations in your React application. It creates a canvas-based particle system with customizable particle count, origin position, and animation duration.

Installation

Import

Usage

Returns

(options?: ConfettiOptions) => void
A function that triggers the confetti animation when called. Accepts an optional configuration object.

ConfettiOptions

number
default:50
The total number of particles to generate. The particles are split into 75% confetti pieces and 25% sequins.
string
default:"center"
The origin point for the confetti animation. Can be one of:
  • "center" - Center of the screen
  • "top" - Top center
  • "bottom" - Bottom center
  • "left" - Left center
  • "right" - Right center
  • "top-left" - Top left corner
  • "top-right" - Top right corner
  • "bottom-left" - Bottom left corner
  • "bottom-right" - Bottom right corner
number
default:3000
The duration in milliseconds before the canvas is cleaned up (if no particles remain).

Type Definition

Examples

Success Message

Multiple Origins

Form Submission

Achievement Unlock

Custom Button Component

Common Patterns

Sequential Bursts

Create a sequence of confetti bursts:

Conditional Confetti

Trigger confetti based on conditions:

Notes

  • The hook automatically creates a full-screen canvas overlay when triggered
  • The canvas is positioned with position: fixed and z-index: 9999
  • The canvas has pointer-events: none so it doesn’t interfere with user interactions
  • Particles are automatically cleaned up when they fall off the screen
  • The canvas is removed from the DOM when all particles have finished animating
  • Multiple confetti bursts can be active simultaneously
  • The hook automatically handles window resizing
  • All resources are properly cleaned up when the component unmounts