1"use client"
2
3import 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"
19
20import { Button } from "@/components/ui/button"
21
22type Api = {
23 fire: (options?: ConfettiOptions) => void
24}
25
26type Props = React.ComponentPropsWithRef<"canvas"> & {
27 options?: ConfettiOptions
28 globalOptions?: ConfettiGlobalOptions
29 manualstart?: boolean
30 children?: ReactNode
31}
32
33export type ConfettiRef = Api | null
34
35const ConfettiContext = createContext<Api>({} as Api)
36
37// Define component first
38const ConfettiComponent = forwardRef<ConfettiRef, Props>((props, ref) => {
39 const {
40 options,
41 globalOptions = { resize: true, useWorker: true },
42 manualstart = false,
43 children,
44 ...rest
45 } = props
46 const instanceRef = useRef<ConfettiInstance | null>(null)
47
48 const canvasRef = useCallback(
49 (node: HTMLCanvasElement) => {
50 if (node !== null) {
51 if (instanceRef.current) return
52 instanceRef.current = confetti.create(node, {
53 ...globalOptions,
54 resize: true,
55 })
56 } else {
57 if (instanceRef.current) {
58 instanceRef.current.reset()
59 instanceRef.current = null
60 }
61 }
62 },
63 [globalOptions]
64 )
65
66 const fire = useCallback(
67 async (opts = {}) => {
68 try {
69 await instanceRef.current?.({ ...options, ...opts })
70 } catch (error) {
71 console.error("Confetti error:", error)
72 }
73 },
74 [options]
75 )
76
77 const api = useMemo(
78 () => ({
79 fire,
80 }),
81 [fire]
82 )
83
84 useImperativeHandle(ref, () => api, [api])
85
86 useEffect(() => {
87 if (!manualstart) {
88 ;(async () => {
89 try {
90 await fire()
91 } catch (error) {
92 console.error("Confetti effect error:", error)
93 }
94 })()
95 }
96 }, [manualstart, fire])
97
98 return (
99 <ConfettiContext.Provider value={api}>
100 <canvas ref={canvasRef} {...rest} />
101 {children}
102 </ConfettiContext.Provider>
103 )
104})
105
106// Set display name immediately
107ConfettiComponent.displayName = "Confetti"
108
109// Export as Confetti
110export const Confetti = ConfettiComponent
111
112interface ConfettiButtonProps extends React.ComponentProps<"button"> {
113 options?: ConfettiOptions &
114 ConfettiGlobalOptions & { canvas?: HTMLCanvasElement }
115}
116
117const ConfettiButtonComponent = ({
118 options,
119 children,
120 ...props
121}: ConfettiButtonProps) => {
122 const handleClick = async (event: React.MouseEvent<HTMLButtonElement>) => {
123 try {
124 const rect = event.currentTarget.getBoundingClientRect()
125 const x = rect.left + rect.width / 2
126 const y = rect.top + rect.height / 2
127 await confetti({
128 ...options,
129 origin: {
130 x: x / window.innerWidth,
131 y: y / window.innerHeight,
132 },
133 })
134 } catch (error) {
135 console.error("Confetti button error:", error)
136 }
137 }
138
139 return (
140 <Button onClick={handleClick} {...props}>
141 {children}
142 </Button>
143 )
144}
145
146ConfettiButtonComponent.displayName = "ConfettiButton"
147
148export const ConfettiButton = ConfettiButtonComponent
149