Communication
Confetti
A short burst for real milestones. Wrap the control that earned it; each time `fire` changes, token-coloured pieces burst from its centre, fall with gravity and spin, and fade within 1.5s.
Examples and variants
First project
Fires when the button reaches success.
Confetti only for firsts
function ConfettiMilestone() {
const [fired, setFired] = useState(0)
const [status, setStatus] = useState<"idle" | "loading" | "success">("idle")
return (
<div className="flex flex-col items-center gap-2">
<Confetti fire={fired}>
<Button
status={status}
onClick={() => {
if (status !== "idle") return setStatus("idle")
setStatus("loading")
window.setTimeout(() => {
setStatus("success")
setFired((n) => n + 1)
}, tokenMs("--duration-fill"))
}}
>
{status === "success" ? "Project created" : "Create first project"}
</Button>
</Confetti>
<p className="text-sm text-muted-foreground">{status === "success" ? "Press again to reset" : "Confetti only for firsts"}</p>
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/confetti.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install cnCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/confetti.tsxconfetti
components/ui/confetti.tsx"use client" import * as React from "react" import { cn } from "cn" import { motionLevel } from "@/lib/overlay" /* Confetti: a short burst for real milestones (first project, plan approved). Wrap the thing that earned it; each time `fire` changes, token-coloured pieces burst from its centre, fall with gravity and spin, and fade within 1.5s. Max 60 pieces. Calm fires 12 and finishes sooner; still (and reduced motion) gives the wrapped element a success pop instead. */ const colours = ["--confetti-1", "--confetti-2", "--confetti-3", "--confetti-4", "--confetti-5"] type Piece = { x: number; y: number; vx: number; vy: number; spin: number; angle: number; w: number; h: number; colour: string } function burst(origin: DOMRect, count: number, life: number) { const canvas = document.createElement("canvas") const dpr = window.devicePixelRatio || 1 canvas.width = window.innerWidth * dpr canvas.height = window.innerHeight * dpr canvas.setAttribute("aria-hidden", "true") canvas.style.cssText = "position:fixed;inset:0;width:100vw;height:100vh;pointer-events:none;z-index:2147483000" document.body.appendChild(canvas) const ctx = canvas.getContext("2d") if (!ctx) return canvas.remove() ctx.scale(dpr, dpr) const style = getComputedStyle(document.documentElement) const palette = colours.map((name) => style.getPropertyValue(name).trim() || "currentColor") const cx = origin.left + origin.width / 2 const cy = origin.top + origin.height / 2 const pieces: Piece[] = Array.from({ length: count }, (_, i) => { const angle = -Math.PI / 2 + (Math.random() - 0.5) * Math.PI * 1.1 const speed = 7 + Math.random() * 7 return { x: cx, y: cy, vx: Math.cos(angle) * speed, vy: Math.sin(angle) * speed, spin: (Math.random() - 0.5) * 0.4, angle: Math.random() * Math.PI, w: 6 + Math.random() * 4, h: 3 + Math.random() * 3, colour: palette[i % palette.length], } }) const start = performance.now() const frame = (now: number) => { const t = (now - start) / life ctx.clearRect(0, 0, window.innerWidth, window.innerHeight) if (t >= 1) return canvas.remove() ctx.globalAlpha = t < 0.6 ? 1 : 1 - (t - 0.6) / 0.4 for (const p of pieces) { p.vy += 0.35 p.vx *= 0.985 p.x += p.vx p.y += p.vy p.angle += p.spin ctx.save() ctx.translate(p.x, p.y) ctx.rotate(p.angle) ctx.scale(1, Math.cos(p.angle * 2)) ctx.fillStyle = p.colour ctx.fillRect(-p.w / 2, -p.h / 2, p.w, p.h) ctx.restore() } requestAnimationFrame(frame) } requestAnimationFrame(frame) } function Confetti({ fire, pieces = 60, children, className, }: { /** Change this (a counter works) to fire a burst. The first value doesn't fire. */ fire: number /** Liquid piece count; capped at 60. */ pieces?: number children: React.ReactNode className?: string }) { const ref = React.useRef<HTMLSpanElement>(null) const first = React.useRef(fire) React.useEffect(() => { const node = ref.current if (!node || fire === first.current) return first.current = fire const level = motionLevel(node) if (level === "still") { node.removeAttribute("data-pop") void node.offsetWidth node.setAttribute("data-pop", "") return } burst(node.getBoundingClientRect(), level === "calm" ? 12 : Math.min(60, pieces), level === "calm" ? 900 : 1500) }, [fire, pieces]) return ( <span ref={ref} data-slot="confetti" className={cn("confetti-origin inline-flex", className)}> {children} </span> ) } export { Confetti }
Imports
What the examples on this page import:
import { Button } from "@/components/ui/button"
import { CardStack } from "@/components/ui/card-stack"
import { Confetti } from "@/components/ui/confetti"
import { Dice } from "@/components/ui/dice"
import { FlipBoard } from "@/components/ui/flip-board"
import { Metaballs } from "@/components/ui/metaballs"
import { tokenMs } from "@/lib/motion/token-ms"Anatomy
- 1Origin · The wrapped element; pieces start at its centre.
- 2Pieces · --confetti-1 to --confetti-5, drawn on a temporary canvas.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
fire | number | — | Change it (a counter) to fire. The first value doesn't fire. |
pieces | number | 60 | Liquid piece count, capped at 60. |
children | ReactNode | — | The origin. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--confetti-1 | var(--color-brand) | oklch(0.18 0.008 260) | oklch(0.98 0 0) |
--confetti-2 | var(--color-info) | oklch(0.6 0.17 248) | oklch(0.72 0.14 245) |
--confetti-3 | var(--color-success) | oklch(0.52 0.14 150) | oklch(0.72 0.17 150) |
--confetti-4 | var(--color-warning) | oklch(0.72 0.14 75) | oklch(0.78 0.15 75) |
--confetti-5 | var(--color-danger) | oklch(0.55 0.2 25) | oklch(0.68 0.18 22) |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| fire changes | Burst, gravity, spin, fade in 1.5s. Calm: 12 pieces, 0.9s. Still: the origin pops instead. | --spring-bouncy |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Decorative: the canvas is hidden from screen readers and ignores the pointer.
- Pair it with a real status (button text, toast); confetti is never the only feedback.
Usage
Do
- Use for firsts and big wins: first project, plan approved.
Don’t
- Don't fire on routine saves or every success.
Code
"use client"
import * as React from "react"
import { cn } from "cn"
import { motionLevel } from "@/lib/overlay"
/*
Confetti: a short burst for real milestones (first project, plan approved).
Wrap the thing that earned it; each time `fire` changes, token-coloured
pieces burst from its centre, fall with gravity and spin, and fade within
1.5s. Max 60 pieces. Calm fires 12 and finishes sooner; still (and reduced
motion) gives the wrapped element a success pop instead.
*/
const colours = ["--confetti-1", "--confetti-2", "--confetti-3", "--confetti-4", "--confetti-5"]
type Piece = { x: number; y: number; vx: number; vy: number; spin: number; angle: number; w: number; h: number; colour: string }
function burst(origin: DOMRect, count: number, life: number) {
const canvas = document.createElement("canvas")
const dpr = window.devicePixelRatio || 1
canvas.width = window.innerWidth * dpr
canvas.height = window.innerHeight * dpr
canvas.setAttribute("aria-hidden", "true")
canvas.style.cssText = "position:fixed;inset:0;width:100vw;height:100vh;pointer-events:none;z-index:2147483000"
document.body.appendChild(canvas)
const ctx = canvas.getContext("2d")
if (!ctx) return canvas.remove()
ctx.scale(dpr, dpr)
const style = getComputedStyle(document.documentElement)
const palette = colours.map((name) => style.getPropertyValue(name).trim() || "currentColor")
const cx = origin.left + origin.width / 2
const cy = origin.top + origin.height / 2
const pieces: Piece[] = Array.from({ length: count }, (_, i) => {
const angle = -Math.PI / 2 + (Math.random() - 0.5) * Math.PI * 1.1
const speed = 7 + Math.random() * 7
return {
x: cx,
y: cy,
vx: Math.cos(angle) * speed,
vy: Math.sin(angle) * speed,
spin: (Math.random() - 0.5) * 0.4,
angle: Math.random() * Math.PI,
w: 6 + Math.random() * 4,
h: 3 + Math.random() * 3,
colour: palette[i % palette.length],
}
})
const start = performance.now()
const frame = (now: number) => {
const t = (now - start) / life
ctx.clearRect(0, 0, window.innerWidth, window.innerHeight)
if (t >= 1) return canvas.remove()
ctx.globalAlpha = t < 0.6 ? 1 : 1 - (t - 0.6) / 0.4
for (const p of pieces) {
p.vy += 0.35
p.vx *= 0.985
p.x += p.vx
p.y += p.vy
p.angle += p.spin
ctx.save()
ctx.translate(p.x, p.y)
ctx.rotate(p.angle)
ctx.scale(1, Math.cos(p.angle * 2))
ctx.fillStyle = p.colour
ctx.fillRect(-p.w / 2, -p.h / 2, p.w, p.h)
ctx.restore()
}
requestAnimationFrame(frame)
}
requestAnimationFrame(frame)
}
function Confetti({
fire,
pieces = 60,
children,
className,
}: {
/** Change this (a counter works) to fire a burst. The first value doesn't fire. */
fire: number
/** Liquid piece count; capped at 60. */
pieces?: number
children: React.ReactNode
className?: string
}) {
const ref = React.useRef<HTMLSpanElement>(null)
const first = React.useRef(fire)
React.useEffect(() => {
const node = ref.current
if (!node || fire === first.current) return
first.current = fire
const level = motionLevel(node)
if (level === "still") {
node.removeAttribute("data-pop")
void node.offsetWidth
node.setAttribute("data-pop", "")
return
}
burst(node.getBoundingClientRect(), level === "calm" ? 12 : Math.min(60, pieces), level === "calm" ? 900 : 1500)
}, [fire, pieces])
return (
<span ref={ref} data-slot="confetti" className={cn("confetti-origin inline-flex", className)}>
{children}
</span>
)
}
export { Confetti }