Cómo funciona
A position: relative wrapper with an absolute, pointer-transparent overlay, so it works inside scaled or clipped containers. fire() creates temporary nodes, each following projectile motion with air drag, sampled into 19 keyframes and animated with the Web Animations API (transform and opacity only). Pieces remove themselves and React never re-renders.
Úsalo para
- Moments worth celebrating: check-in done, task finished, reward claimed, level up.
- The end of a key flow such as a successful payment or booking, together with a success message.
- Learning apps: finishing a course, earning a certificate, hitting a streak.
Busca otra opción cuando
- Ordinary saved or copied confirmations: use a toast or the button's own state.
- Actions repeated often on one page, such as likes or add-to-cart.
- Failures and warnings: never celebrate a negative result.
- A full-screen, continuous confetti rain: this is a one-shot burst, not a background.
Uso
import { useRef } from 'react'
import { ConfettiBurst } from './ConfettiBurst.jsx'
export function CheckInButton({ onCheckIn }) {
const confetti = useRef(null)
return (
// Wrap the button: pieces burst upward from its centre without affecting layout
<ConfettiBurst ref={confetti} count={28} spread={100}>
<button
type="button"
onClick={async () => {
await onCheckIn()
confetti.current?.fire() // celebrate after success
}}
>
Check in
</button>
</ConfettiBurst>
)
}
// Wrapping a whole card: use a block element and burst from the clicked button
// <ConfettiBurst ref={confetti} style={{ display: 'block' }}> ... </ConfettiBurst>
// confetti.current?.fire({ from: e.currentTarget, count: 40 })
// No ref: pass a success counter to fireKey and it bursts whenever it changes
// <ConfettiBurst fireKey={successCount}>...</ConfettiBurst>Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
count | number | 28 | Number of pieces per burst. |
spread | number | 100 | Spread in degrees: the start direction falls within angle ± spread / 2. 360 explodes in every direction. |
angle | number | 90 | Centre direction in degrees: 90 up, 0 right, 180 left, 270 down. |
distance | number | clamp(min(width, height) * 0.45, 60, 240) | Burst distance in px: the fastest pieces travel about this far. |
colors | string[] | chart-1 / chart-4 / chart-3 / chart-5 / chart-7 tokens | Piece colours, any CSS colour. The defaults are var() with fallbacks and follow the theme. |
duration | number | 1400 | Time in ms from burst to disappearance for one piece. |
origin | { x: number; y: number } | { x: 0.5, y: 0.5 } | Burst point inside the wrapper, 0 to 1 (x left to right, y top to bottom). |
fireKey | string | number | — | Fires a burst whenever it changes (not on first mount). Use it when you do not want a ref. |
maxParticles | number | 120 | Cap on pieces alive at once. When repeated bursts exceed it, the oldest are removed first. |
clip | boolean | false | Clips pieces to the wrapper. By default they may fly outside; turn it on when the wrapper touches the page edge and pieces could cause scrollbars. |
children | ReactNode | — | Wrapped content: a trigger button, a success card, and so on. |
ref | Ref<{ fire, clear }> | — | fire(options?) shoots a burst; options can override count, spread, angle, distance, colors, duration, origin, or set from (an element to burst from). clear() removes pieces still flying. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root element, such as className and style (use style={{ display: 'block' }} when wrapping a large block). |
Accesibilidad
- The overlay is aria-hidden with pointer-events: none, so screen readers skip it and buttons below stay clickable.
- Confetti is only visual emphasis: always show the success result as text too, in an aria-live region if needed.
- With reduced motion, no pieces are fired; only a 720ms soft glow fades in and out at the burst point.
- It never takes focus or changes layout, so the trigger button and keyboard use are unaffected.
- The number of live pieces is capped (120 by default), so repeated taps on low-end phones do not pile up.
Movimiento
A piece lasts 1400ms by default: the first quarter shoots out with exponential slowdown, then it falls at near-constant speed and fades with an ease-in over the last 32%. Pieces start 0 to 60ms apart.
Añádelo a tu proyecto
Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.
ConfettiBurst.jsxconfetti-burst.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.



