-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add confetti effect on create profile
- Loading branch information
1 parent
c8071b2
commit 5430151
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ConfettiProvider } from "@/components/Confetti" | ||
import { PropsWithChildren } from "react" | ||
|
||
const Layout = ({ children }: PropsWithChildren) => { | ||
return <ConfettiProvider>{children}</ConfettiProvider> | ||
} | ||
|
||
export default Layout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"use client" | ||
|
||
import { | ||
MutableRefObject, | ||
PropsWithChildren, | ||
createContext, | ||
useContext, | ||
useRef, | ||
} from "react" | ||
import ReactCanvasConfetti from "react-canvas-confetti/dist" | ||
import { TCanvasConfettiInstance } from "react-canvas-confetti/dist/types" | ||
|
||
const doubleConfetti = (confetti: TCanvasConfettiInstance) => { | ||
const count = 200 | ||
const defaultsPerBarrage: confetti.Options[] = [ | ||
{ | ||
origin: { x: -0.05 }, | ||
angle: 60, | ||
}, | ||
{ | ||
origin: { x: 1.05 }, | ||
angle: 120, | ||
}, | ||
] as const | ||
|
||
const fire = (particleRatio: number, opts: confetti.Options) => { | ||
confetti({ | ||
...opts, | ||
particleCount: Math.floor(count * particleRatio), | ||
}) | ||
} | ||
|
||
for (const defaults of defaultsPerBarrage) { | ||
fire(0.25, { | ||
spread: 26, | ||
startVelocity: 55, | ||
...defaults, | ||
}) | ||
fire(0.2, { | ||
spread: 60, | ||
...defaults, | ||
}) | ||
fire(0.35, { | ||
spread: 100, | ||
decay: 0.91, | ||
scalar: 0.8, | ||
...defaults, | ||
}) | ||
fire(0.1, { | ||
spread: 120, | ||
startVelocity: 25, | ||
decay: 0.92, | ||
scalar: 1.2, | ||
...defaults, | ||
}) | ||
fire(0.1, { | ||
spread: 120, | ||
startVelocity: 45, | ||
...defaults, | ||
}) | ||
} | ||
} | ||
|
||
const ConfettiContext = createContext< | ||
MutableRefObject<TCanvasConfettiInstance | undefined> | ||
>({} as MutableRefObject<TCanvasConfettiInstance | undefined>) | ||
|
||
export const useConfetti = () => { | ||
return { confetti: useContext(ConfettiContext) } | ||
} | ||
|
||
export const ConfettiProvider = ({ children }: PropsWithChildren) => { | ||
const confettiRef = useRef<TCanvasConfettiInstance>() | ||
const onInitHandler = ({ confetti }: { confetti: TCanvasConfettiInstance }) => { | ||
const confettiClosure = async (...args: Parameters<typeof confetti>) => { | ||
if (args[0] === undefined) { | ||
doubleConfetti(confetti) | ||
return null | ||
} | ||
return confetti() | ||
} | ||
confettiClosure.reset = confetti.reset | ||
confettiRef.current = confettiClosure | ||
} | ||
return ( | ||
<ConfettiContext.Provider value={confettiRef}> | ||
{children} | ||
<ReactCanvasConfetti | ||
onInit={onInitHandler} | ||
globalOptions={{ | ||
disableForReducedMotion: true, | ||
useWorker: true, | ||
resize: true, | ||
}} | ||
/> | ||
</ConfettiContext.Provider> | ||
) | ||
} |