Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Confetti Component
*
* CSS-animated confetti particles for celebration effects.
* Uses pure CSS animations for performance - no canvas or heavy libraries.
*/
import { css } from '@styled/css'
import { useEffect, useMemo, useRef, useState } from 'react'
import type { CelebrationType } from '../Provider'
import { CELEBRATION_TIMING, CONFETTI_CONFIG } from '../utils/celebration'
interface ConfettiProps {
type: CelebrationType
origin: { x: number; y: number }
onComplete: () => void
}
interface Particle {
id: number
x: number
y: number
color: string
size: number
angle: number // Direction in degrees
distance: number // How far to travel
rotation: number // Initial rotation
rotationSpeed: number // Rotation during animation
delay: number // Stagger start
}
// Generate random particles based on config
function generateParticles(type: CelebrationType, origin: { x: number; y: number }): Particle[] {
const config = CONFETTI_CONFIG[type]
const particles: Particle[] = []
for (let i = 0; i < config.count; i++) {
// Random angle within spread, centered upward (-90 deg)
const spreadRad = (config.spread * Math.PI) / 180
const baseAngle = -Math.PI / 2 // Upward
const angle = baseAngle + (Math.random() - 0.5) * spreadRad
particles.push({
id: i,
x: origin.x,
y: origin.y,
color: config.colors[Math.floor(Math.random() * config.colors.length)],
size: 6 + Math.random() * 6, // 6-12px
angle: (angle * 180) / Math.PI,
distance: 80 + Math.random() * 120, // 80-200px
rotation: Math.random() * 360,
rotationSpeed: (Math.random() - 0.5) * 720, // -360 to +360 deg
delay: Math.random() * 50, // 0-50ms stagger
})
}
return particles
}
export function Confetti({ type, origin, onComplete }: ConfettiProps) {
const [isComplete, setIsComplete] = useState(false)
const particles = useMemo(() => generateParticles(type, origin), [type, origin])
const timing = CELEBRATION_TIMING[type]
// Store onComplete in a ref so the timer doesn't restart when the callback changes
const onCompleteRef = useRef(onComplete)
onCompleteRef.current = onComplete
// Call onComplete when animation finishes
useEffect(() => {
const timer = setTimeout(() => {
setIsComplete(true)
onCompleteRef.current()
}, timing.confettiDuration)
return () => clearTimeout(timer)
}, [timing.confettiDuration])
if (isComplete) return null
return (
<div
data-component="confetti"
className={css({
position: 'fixed',
inset: 0,
pointerEvents: 'none',
zIndex: 10000,
overflow: 'hidden',
})}
>
<style>
{`
@keyframes confettiFallback {
0% {
transform: translateY(0) rotate(0deg) scale(1);
opacity: 1;
}
40% {
transform: translateY(-80px) rotate(180deg) scale(1);
opacity: 1;
}
100% {
transform: translateY(30px) rotate(360deg) scale(0.3);
opacity: 0;
}
}
`}
</style>
{particles.map((particle) => (
<div
key={particle.id}
className={css({
position: 'absolute',
borderRadius: '2px',
})}
style={{
left: particle.x,
top: particle.y,
width: particle.size,
height: particle.size * 0.6,
backgroundColor: particle.color,
animation: `confettiFallback ${timing.confettiDuration}ms ease-out forwards`,
animationDelay: `${particle.delay}ms`,
transform: `rotate(${particle.rotation}deg)`,
}}
/>
))}
</div>
)
}
// Alternative implementation with proper burst effect
export function ConfettiBurst({ type, origin, onComplete }: ConfettiProps) {
const [isComplete, setIsComplete] = useState(false)
const particles = useMemo(() => generateParticles(type, origin), [type, origin])
const timing = CELEBRATION_TIMING[type]
// Store onComplete in a ref so the timer doesn't restart when the callback changes
// This fixes a bug where mouse movement during celebration would restart the timer
const onCompleteRef = useRef(onComplete)
onCompleteRef.current = onComplete
useEffect(() => {
const timer = setTimeout(() => {
setIsComplete(true)
onCompleteRef.current()
}, timing.confettiDuration)
return () => clearTimeout(timer)
}, [timing.confettiDuration])
if (isComplete) return null
return (
<div
data-component="confetti-burst"
className={css({
position: 'fixed',
inset: 0,
pointerEvents: 'none',
zIndex: 10000,
overflow: 'hidden',
})}
>
<style>
{`
@keyframes confettiMotion {
0% {
transform: translate(0, 0) rotate(0deg) scale(1);
opacity: 1;
}
50% {
opacity: 1;
}
100% {
transform: translate(var(--offset-x), calc(var(--offset-y) + 60px)) rotate(360deg) scale(0.3);
opacity: 0;
}
}
`}
</style>
{particles.map((particle) => {
const offsetX = Math.cos((particle.angle * Math.PI) / 180) * particle.distance
const offsetY = Math.sin((particle.angle * Math.PI) / 180) * particle.distance
return (
<div
key={particle.id}
className={css({
position: 'absolute',
borderRadius: '2px',
})}
style={
{
left: particle.x,
top: particle.y,
width: particle.size,
height: particle.size * 0.6,
backgroundColor: particle.color,
'--offset-x': `${offsetX}px`,
'--offset-y': `${offsetY}px`,
animation: `confettiMotion ${timing.confettiDuration}ms ease-out ${particle.delay}ms forwards`,
} as React.CSSProperties
}
/>
)
})}
</div>
)
}
|