Confetti
magicui/confettiFreeComponent
Confetti animations are best used to delight your users when something special happens
Live preview
live · rendered by the registry
Rendered by magicui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://magicui.design/r/confetti.jsonSource
1"use client"23import type { ReactNode } from "react"4import React, {5 createContext,6 forwardRef,7 useCallback,8 useEffect,9 useImperativeHandle,10 useMemo,11 useRef,12} from "react"13import type {14 GlobalOptions as ConfettiGlobalOptions,15 CreateTypes as ConfettiInstance,16 Options as ConfettiOptions,17} from "canvas-confetti"18import confetti from "canvas-confetti"1920import { Button } from "@/components/ui/button"2122export type ConfettiRef = {23 fire: (options?: ConfettiOptions) => Promise<void> | void24}2526type Props = React.ComponentPropsWithRef<"canvas"> & {27 options?: ConfettiOptions28 globalOptions?: ConfettiGlobalOptions29 manualstart?: boolean30 children?: ReactNode31}3233const ConfettiContext = createContext<ConfettiRef | null>(null)3435const ConfettiComponent = forwardRef<ConfettiRef, Props>((props, ref) => {36 const {37 options,38 globalOptions = { resize: true, useWorker: true },39 manualstart = false,40 children,41 className,42 ...rest43 } = props4445 const canvasNodeRef = useRef<HTMLCanvasElement | null>(null)46 const instanceRef = useRef<ConfettiInstance | null>(null)47 const optionsRef = useRef(options)48 const globalOptionsRef = useRef(globalOptions)4950 useEffect(() => {51 optionsRef.current = options52 }, [options])5354 useEffect(() => {55 globalOptionsRef.current = globalOptions56 }, [globalOptions])5758 useEffect(() => {59 if (canvasNodeRef.current && !instanceRef.current) {60 instanceRef.current = confetti.create(canvasNodeRef.current, {61 resize: true,62 useWorker: true,63 ...globalOptionsRef.current,64 })65 }6667 return () => {68 instanceRef.current?.reset()69 instanceRef.current = null70 }71 }, [])7273 const fire = useCallback(async (opts: ConfettiOptions = {}) => {74 try {75 await instanceRef.current?.({76 ...optionsRef.current,77 ...opts,78 })79 } catch (error) {80 console.error("Confetti error:", error)81 }82 }, [])8384 const api = useMemo<ConfettiRef>(() => ({ fire }), [fire])8586 useImperativeHandle(ref, () => api, [api])8788 useEffect(() => {89 if (!manualstart) {90 void fire()91 }92 }, [manualstart, fire])9394 return (95 <ConfettiContext.Provider value={api}>96 <canvas ref={canvasNodeRef} className={className} {...rest} />97 {children}98 </ConfettiContext.Provider>99 )100})101102ConfettiComponent.displayName = "Confetti"103104export const Confetti = ConfettiComponent105106// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from magicui
All 247 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Androidandroid | magicui | Components | Free | no deps | |
| Animated Beamanimated-beam | magicui | Components | Free | 1 dep | |
| Animated Circular Progress Baranimated-circular-progress-bar | magicui | Components | Free | no deps | |
| Animated Gradient Textanimated-gradient-text | magicui | Components | Free | no deps | |
| Animated Grid Patternanimated-grid-pattern | magicui | Components | Free | 1 dep | |
| Animated Listanimated-list | magicui | Components | Free | 1 dep | |
| Animated Shiny Textanimated-shiny-text | magicui | Components | Free | no deps | |
| Theme Toggleranimated-theme-toggler | magicui | Components | Free | 1 dep |
