Skip to content
Primitives

Confetti

Celebration burst for success moments (checkout, onboarding done). Self-contained Motion particles — no canvas, no extra dependency. Honors reduced motion.

Motionparticle launch + gravity arc; fires nothing under reduced motion

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { Confetti } from "kineto/confetti";

Preview

Fire from any event via the ref — here, a button click.

tsx
"use client";

import * as React from "react";
import { AnimatePresence, motion } from "motion/react";

import { cn } from "@/lib/cn";
import { useReducedMotion } from "@/lib/use-reduced-motion";

/* Celebration burst for success moments (checkout, onboarding done). Self-contained DOM particles
 * via Motion — no canvas, no extra dependency. Honors reduced motion (fires nothing). Fire it
 * imperatively through the ref handle from ANY event (a successful mutation, a socket message, a
 * click), or set `fireOnMount` on a success screen. */

const DEFAULT_COLORS = [
  "hsl(var(--brand))",
  "hsl(var(--success))",
  "hsl(var(--warning))",
  "hsl(var(--primary))",
];

export interface FireOptions {
  /** Burst origin in viewport pixels. Defaults to screen center. */
  origin?: { x: number; y: number };
}

export interface ConfettiHandle {
  fire: (opts?: FireOptions) => void;
}

export interface ConfettiProps extends React.HTMLAttributes<HTMLDivElement> {
  particleCount?: number;
  colors?: string[];
  /** Cone width of the launch, in degrees. */
  spread?: number;
  /** Seconds each particle lives. */
  duration?: number;
  /** Fire once when mounted (e.g. on a success screen). */
  fireOnMount?: boolean;
}

interface Particle {
  id: number;
  x: number;
  y: number;
  vx: number;
  vy: number;
  color: string;
  rotate: number;
  size: number;
}

const Confetti = React.forwardRef<ConfettiHandle, ConfettiProps>(
  (
    {
      className,
      particleCount = 80,
      colors = DEFAULT_COLORS,
      spread = 70,
      duration = 2.2,
      fireOnMount = false,
      ...props
    },
    ref
  ) => {
    const reduced = useReducedMotion();
    const [bursts, setBursts] = React.useState<{ id: number; particles: Particle[] }[]>([]);
    const counter = React.useRef(0);

    const fire = React.useCallback(
      (opts?: FireOptions) => {
        if (reduced || typeof window === "undefined") return;
        const origin = opts?.origin ?? { x: window.innerWidth / 2, y: window.innerHeight / 2 };
        const id = counter.current++;
        const particles: Particle[] = Array.from({ length: particleCount }, (_, i) => {
          // Launch in an upward cone, then gravity takes over (see the y keyframes below).
          const angle = ((-90 + (Math.random() - 0.5) * spread) * Math.PI) / 180;
          const velocity = 220 + Math.random() * 380;
          return {
            id: i,
            x: origin.x,
            y: origin.y,
            vx: Math.cos(angle) * velocity,
            vy: Math.sin(angle) * velocity,
            color: colors[i % colors.length]!,
            rotate: Math.random() * 360,
            size: 6 + Math.random() * 6,
          };
        });
        setBursts((b) => [...b, { id, particles }]);
        window.setTimeout(
          () => setBursts((b) => b.filter((x) => x.id !== id)),
          duration * 1000 + 200
        );
      },
      [reduced, particleCount, colors, spread, duration]
    );

    React.useImperativeHandle(ref, () => ({ fire }), [fire]);

    React.useEffect(() => {
      if (fireOnMount) fire();
      // Fire exactly once on mount.
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);

    return (
      <div
        aria-hidden="true"
        className={cn("pointer-events-none fixed inset-0 z-[100] overflow-hidden", className)}
        {...props}
      >
        <AnimatePresence>
          {bursts.flatMap((burst) =>
            burst.particles.map((p) => (
              <motion.span
                key={`${burst.id}-${p.id}`}
                initial={{ x: p.x, y: p.y, opacity: 1, rotate: 0 }}
                animate={{
                  x: [p.x, p.x + p.vx * 0.6, p.x + p.vx],
                  y: [p.y, p.y + p.vy, p.y + p.vy + 720],
                  rotate: p.rotate + 540,
                  opacity: [1, 1, 0],
                }}
                transition={{ duration, ease: [0.2, 0.6, 0.35, 1], times: [0, 0.35, 1] }}
                style={{
                  position: "absolute",
                  width: p.size,
                  height: p.size * 0.6,
                  borderRadius: 2,
                  backgroundColor: p.color,
                }}
              />
            ))
          )}
        </AnimatePresence>
      </div>
    );
  }
);
Confetti.displayName = "Confetti";

export { Confetti };

src/components/ui/confetti.tsx

Props

PropTypeDefaultDescription
fire()(opts?: { origin? }) => void—Imperative ref handle — call from any event to launch a burst.
particleCountnumber80Particles per burst.
colorsstring[]—Particle colors (defaults to brand/success/warning/ink).
spreadnumber70Cone width of the launch, in degrees.
durationnumber2.2Seconds each particle lives.
fireOnMountbooleanfalseFire once when mounted (e.g. a success screen).